extensionsViews.test.ts 27.8 KB
Newer Older
R
Ramya Achutha Rao 已提交
1 2 3 4 5 6 7 8
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import * as assert from 'assert';
import { assign } from 'vs/base/common/objects';
import { generateUuid } from 'vs/base/common/uuid';
9
import { ExtensionsListView } from 'vs/workbench/contrib/extensions/browser/extensionsViews';
R
Ramya Achutha Rao 已提交
10
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
11 12
import { IExtensionsWorkbenchService } from 'vs/workbench/contrib/extensions/common/extensions';
import { ExtensionsWorkbenchService } from 'vs/workbench/contrib/extensions/node/extensionsWorkbenchService';
R
Ramya Achutha Rao 已提交
13
import {
14
	IExtensionManagementService, IExtensionGalleryService, IExtensionEnablementService, IExtensionTipsService, ILocalExtension, IGalleryExtension, IQueryOptions,
S
Sandeep Somavarapu 已提交
15
	DidInstallExtensionEvent, DidUninstallExtensionEvent, InstallExtensionEvent, IExtensionIdentifier, IExtensionManagementServerService, EnablementState, ExtensionRecommendationReason, SortBy, IExtensionManagementServer
R
Ramya Achutha Rao 已提交
16
} from 'vs/platform/extensionManagement/common/extensionManagement';
17
import { getGalleryExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
18
import { ExtensionManagementService } from 'vs/platform/extensionManagement/node/extensionManagementService';
19
import { ExtensionTipsService } from 'vs/workbench/contrib/extensions/electron-browser/extensionTipsService';
20
import { TestExtensionEnablementService } from 'vs/workbench/services/extensionManagement/test/electron-browser/extensionEnablementService.test';
21
import { ExtensionGalleryService } from 'vs/platform/extensionManagement/common/extensionGalleryService';
R
Ramya Achutha Rao 已提交
22 23 24 25 26 27 28
import { IURLService } from 'vs/platform/url/common/url';
import { Emitter } from 'vs/base/common/event';
import { IPager } from 'vs/base/common/paging';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
29
import { TestContextService, TestWindowService, TestSharedProcessService } from 'vs/workbench/test/workbenchTestServices';
R
Ramya Achutha Rao 已提交
30 31 32 33 34 35 36
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ILogService, NullLogService } from 'vs/platform/log/common/log';
import { IWindowService } from 'vs/platform/windows/common/windows';
import { URLService } from 'vs/platform/url/common/urlService';
import { URI } from 'vs/base/common/uri';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { SinonStub } from 'sinon';
37 38
import { IExperimentService, ExperimentState, ExperimentActionType } from 'vs/workbench/contrib/experiments/common/experimentService';
import { ExperimentService } from 'vs/workbench/contrib/experiments/electron-browser/experimentService';
39
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
40
import { RemoteAgentService } from 'vs/workbench/services/remote/electron-browser/remoteAgentServiceImpl';
41
import { ExtensionIdentifier, ExtensionType, IExtensionDescription } from 'vs/platform/extensions/common/extensions';
42
import { ISharedProcessService } from 'vs/platform/ipc/electron-browser/sharedProcessService';
S
Sandeep Somavarapu 已提交
43
import { ExtensionManagementServerService } from 'vs/workbench/services/extensions/electron-browser/extensionManagementServerService';
44
import { IProductService } from 'vs/platform/product/common/product';
R
Ramya Achutha Rao 已提交
45 46 47 48 49 50 51 52 53 54 55


suite('ExtensionsListView Tests', () => {

	let instantiationService: TestInstantiationService;
	let testableView: ExtensionsListView;
	let installEvent: Emitter<InstallExtensionEvent>,
		didInstallEvent: Emitter<DidInstallExtensionEvent>,
		uninstallEvent: Emitter<IExtensionIdentifier>,
		didUninstallEvent: Emitter<DidUninstallExtensionEvent>;

56 57 58 59 60
	const localEnabledTheme = aLocalExtension('first-enabled-extension', { categories: ['Themes', 'random'] });
	const localEnabledLanguage = aLocalExtension('second-enabled-extension', { categories: ['Programming languages'] });
	const localDisabledTheme = aLocalExtension('first-disabled-extension', { categories: ['themes'] });
	const localDisabledLanguage = aLocalExtension('second-disabled-extension', { categories: ['programming languages'] });
	const localRandom = aLocalExtension('random-enabled-extension', { categories: ['random'] });
61 62
	const builtInTheme = aLocalExtension('my-theme', { contributes: { themes: ['my-theme'] } }, { type: ExtensionType.System });
	const builtInBasic = aLocalExtension('my-lang', { contributes: { grammars: [{ language: 'my-language' }] } }, { type: ExtensionType.System });
R
Ramya Achutha Rao 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	const workspaceRecommendationA = aGalleryExtension('workspace-recommendation-A');
	const workspaceRecommendationB = aGalleryExtension('workspace-recommendation-B');
	const fileBasedRecommendationA = aGalleryExtension('filebased-recommendation-A');
	const fileBasedRecommendationB = aGalleryExtension('filebased-recommendation-B');
	const otherRecommendationA = aGalleryExtension('other-recommendation-A');

	suiteSetup(() => {
		installEvent = new Emitter<InstallExtensionEvent>();
		didInstallEvent = new Emitter<DidInstallExtensionEvent>();
		uninstallEvent = new Emitter<IExtensionIdentifier>();
		didUninstallEvent = new Emitter<DidUninstallExtensionEvent>();

		instantiationService = new TestInstantiationService();
		instantiationService.stub(ITelemetryService, NullTelemetryService);
		instantiationService.stub(ILogService, NullLogService);
		instantiationService.stub(IWindowService, TestWindowService);

		instantiationService.stub(IWorkspaceContextService, new TestContextService());
		instantiationService.stub(IConfigurationService, new TestConfigurationService());

		instantiationService.stub(IExtensionGalleryService, ExtensionGalleryService);
85
		instantiationService.stub(ISharedProcessService, TestSharedProcessService);
R
Ramya Achutha Rao 已提交
86 87 88 89 90 91 92
		instantiationService.stub(IExperimentService, ExperimentService);

		instantiationService.stub(IExtensionManagementService, ExtensionManagementService);
		instantiationService.stub(IExtensionManagementService, 'onInstallExtension', installEvent.event);
		instantiationService.stub(IExtensionManagementService, 'onDidInstallExtension', didInstallEvent.event);
		instantiationService.stub(IExtensionManagementService, 'onUninstallExtension', uninstallEvent.event);
		instantiationService.stub(IExtensionManagementService, 'onDidUninstallExtension', didUninstallEvent.event);
93
		instantiationService.stub(IRemoteAgentService, RemoteAgentService);
R
Ramya Achutha Rao 已提交
94

S
Sandeep Somavarapu 已提交
95 96 97
		instantiationService.stub(IExtensionManagementServerService, new class extends ExtensionManagementServerService {
			private _localExtensionManagementServer: IExtensionManagementServer = { extensionManagementService: instantiationService.get(IExtensionManagementService), label: 'local', authority: 'vscode-local' };
			constructor() {
98
				super(instantiationService.get(ISharedProcessService), instantiationService.get(IRemoteAgentService), instantiationService.get(IExtensionGalleryService), instantiationService.get(IConfigurationService), instantiationService.get(IProductService), instantiationService.get(ILogService));
99
			}
S
Sandeep Somavarapu 已提交
100 101 102
			get localExtensionManagementServer(): IExtensionManagementServer { return this._localExtensionManagementServer; }
			set localExtensionManagementServer(server: IExtensionManagementServer) { }
		}());
R
Ramya Achutha Rao 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

		instantiationService.stub(IExtensionEnablementService, new TestExtensionEnablementService(instantiationService));

		instantiationService.stub(IExtensionTipsService, ExtensionTipsService);
		instantiationService.stub(IURLService, URLService);

		instantiationService.stubPromise(IExtensionTipsService, 'getWorkspaceRecommendations', [
			{ extensionId: workspaceRecommendationA.identifier.id },
			{ extensionId: workspaceRecommendationB.identifier.id }]);
		instantiationService.stub(IExtensionTipsService, 'getFileBasedRecommendations', [
			{ extensionId: fileBasedRecommendationA.identifier.id },
			{ extensionId: fileBasedRecommendationB.identifier.id }]);
		instantiationService.stubPromise(IExtensionTipsService, 'getOtherRecommendations', [
			{ extensionId: otherRecommendationA.identifier.id }
		]);
118
		const reasons: { [key: string]: any } = {};
R
Ramya Achutha Rao 已提交
119 120 121 122 123 124 125 126 127 128 129
		reasons[workspaceRecommendationA.identifier.id] = { reasonId: ExtensionRecommendationReason.Workspace };
		reasons[workspaceRecommendationB.identifier.id] = { reasonId: ExtensionRecommendationReason.Workspace };
		reasons[fileBasedRecommendationA.identifier.id] = { reasonId: ExtensionRecommendationReason.File };
		reasons[fileBasedRecommendationB.identifier.id] = { reasonId: ExtensionRecommendationReason.File };
		reasons[otherRecommendationA.identifier.id] = { reasonId: ExtensionRecommendationReason.Executable };

		instantiationService.stub(IExtensionTipsService, 'getAllRecommendationsWithReason', reasons);

	});

	setup(async () => {
130
		instantiationService.stubPromise(IExtensionManagementService, 'getInstalled', [localEnabledTheme, localEnabledLanguage, localRandom, localDisabledTheme, localDisabledLanguage, builtInTheme, builtInBasic]);
R
Ramya Achutha Rao 已提交
131 132
		instantiationService.stubPromise(IExtensionManagementService, 'getExtensionsReport', []);
		instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage());
R
Ramya Achutha Rao 已提交
133
		instantiationService.stubPromise(IExperimentService, 'getExperimentsByType', []);
R
Ramya Achutha Rao 已提交
134 135

		instantiationService.stub(IExtensionService, {
136
			getExtensions: (): Promise<IExtensionDescription[]> => {
137
				return Promise.resolve([
138 139 140 141 142
					toExtensionDescription(localEnabledTheme),
					toExtensionDescription(localEnabledLanguage),
					toExtensionDescription(localRandom),
					toExtensionDescription(builtInTheme),
					toExtensionDescription(builtInBasic)
143
				]);
R
Ramya Achutha Rao 已提交
144 145
			}
		});
S
Sandeep Somavarapu 已提交
146 147
		await (<TestExtensionEnablementService>instantiationService.get(IExtensionEnablementService)).setEnablement([localDisabledTheme], EnablementState.Disabled);
		await (<TestExtensionEnablementService>instantiationService.get(IExtensionEnablementService)).setEnablement([localDisabledLanguage], EnablementState.Disabled);
R
Ramya Achutha Rao 已提交
148 149 150 151 152 153 154 155 156 157 158 159

		instantiationService.set(IExtensionsWorkbenchService, instantiationService.createInstance(ExtensionsWorkbenchService));
		testableView = instantiationService.createInstance(ExtensionsListView, {});
	});

	teardown(() => {
		(<ExtensionsWorkbenchService>instantiationService.get(IExtensionsWorkbenchService)).dispose();
		testableView.dispose();
	});

	test('Test query types', () => {
		assert.equal(ExtensionsListView.isBuiltInExtensionsQuery('@builtin'), true);
160 161 162 163 164 165 166 167
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@installed'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@enabled'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@disabled'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@outdated'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@installed searchText'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@enabled searchText'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@disabled searchText'), true);
		assert.equal(ExtensionsListView.isLocalExtensionsQuery('@outdated searchText'), true);
R
Ramya Achutha Rao 已提交
168 169
	});

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	test('Test empty query equates to sort by install count', () => {
		const target = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage());
		return testableView.show('').then(() => {
			assert.ok(target.calledOnce);
			const options: IQueryOptions = target.args[0][0];
			assert.equal(options.sortBy, SortBy.InstallCount);
		});
	});

	test('Test non empty query without sort doesnt use sortBy', () => {
		const target = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage());
		return testableView.show('some extension').then(() => {
			assert.ok(target.calledOnce);
			const options: IQueryOptions = target.args[0][0];
			assert.equal(options.sortBy, undefined);
		});
	});

	test('Test query with sort uses sortBy', () => {
		const target = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage());
		return testableView.show('some extension @sort:rating').then(() => {
			assert.ok(target.calledOnce);
			const options: IQueryOptions = target.args[0][0];
			assert.equal(options.sortBy, SortBy.WeightedRating);
		});
	});

S
Sandeep Somavarapu 已提交
197 198
	test('Test installed query results', async () => {
		await testableView.show('@installed').then(result => {
199 200 201 202 203 204
			assert.equal(result.length, 5, 'Unexpected number of results for @installed query');
			const actual = [result.get(0).name, result.get(1).name, result.get(2).name, result.get(3).name, result.get(4).name].sort();
			const expected = [localDisabledTheme.manifest.name, localEnabledTheme.manifest.name, localRandom.manifest.name, localDisabledLanguage.manifest.name, localEnabledLanguage.manifest.name];
			for (let i = 0; i < result.length; i++) {
				assert.equal(actual[i], expected[i], 'Unexpected extension for @installed query.');
			}
R
Ramya Achutha Rao 已提交
205 206
		});

S
Sandeep Somavarapu 已提交
207
		await testableView.show('@installed first').then(result => {
208
			assert.equal(result.length, 2, 'Unexpected number of results for @installed query');
S
Sandeep Somavarapu 已提交
209 210
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @installed query with search text.');
			assert.equal(result.get(1).name, localDisabledTheme.manifest.name, 'Unexpected extension for @installed query with search text.');
R
Ramya Achutha Rao 已提交
211 212
		});

S
Sandeep Somavarapu 已提交
213
		await testableView.show('@disabled').then(result => {
214 215 216
			assert.equal(result.length, 2, 'Unexpected number of results for @disabled query');
			assert.equal(result.get(0).name, localDisabledTheme.manifest.name, 'Unexpected extension for @disabled query.');
			assert.equal(result.get(1).name, localDisabledLanguage.manifest.name, 'Unexpected extension for @disabled query.');
R
Ramya Achutha Rao 已提交
217 218
		});

S
Sandeep Somavarapu 已提交
219
		await testableView.show('@enabled').then(result => {
220 221 222 223
			assert.equal(result.length, 3, 'Unexpected number of results for @enabled query');
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @enabled query.');
			assert.equal(result.get(1).name, localRandom.manifest.name, 'Unexpected extension for @enabled query.');
			assert.equal(result.get(2).name, localEnabledLanguage.manifest.name, 'Unexpected extension for @enabled query.');
R
Ramya Achutha Rao 已提交
224 225
		});

S
Sandeep Somavarapu 已提交
226
		await testableView.show('@builtin:themes').then(result => {
R
Ramya Achutha Rao 已提交
227 228 229 230
			assert.equal(result.length, 1, 'Unexpected number of results for @builtin:themes query');
			assert.equal(result.get(0).name, builtInTheme.manifest.name, 'Unexpected extension for @builtin:themes query.');
		});

S
Sandeep Somavarapu 已提交
231
		await testableView.show('@builtin:basics').then(result => {
R
Ramya Achutha Rao 已提交
232 233 234 235
			assert.equal(result.length, 1, 'Unexpected number of results for @builtin:basics query');
			assert.equal(result.get(0).name, builtInBasic.manifest.name, 'Unexpected extension for @builtin:basics query.');
		});

S
Sandeep Somavarapu 已提交
236
		await testableView.show('@builtin').then(result => {
R
Ramya Achutha Rao 已提交
237 238 239 240 241
			assert.equal(result.length, 2, 'Unexpected number of results for @builtin query');
			assert.equal(result.get(0).name, builtInBasic.manifest.name, 'Unexpected extension for @builtin query.');
			assert.equal(result.get(1).name, builtInTheme.manifest.name, 'Unexpected extension for @builtin query.');
		});

S
Sandeep Somavarapu 已提交
242
		await testableView.show('@builtin my-theme').then(result => {
R
Ramya Achutha Rao 已提交
243 244 245
			assert.equal(result.length, 1, 'Unexpected number of results for @builtin query');
			assert.equal(result.get(0).name, builtInTheme.manifest.name, 'Unexpected extension for @builtin query.');
		});
246 247
	});

S
Sandeep Somavarapu 已提交
248 249
	test('Test installed query with category', async () => {
		await testableView.show('@installed category:themes').then(result => {
250
			assert.equal(result.length, 2, 'Unexpected number of results for @installed query with category');
S
Sandeep Somavarapu 已提交
251 252
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @installed query with category.');
			assert.equal(result.get(1).name, localDisabledTheme.manifest.name, 'Unexpected extension for @installed query with category.');
253 254
		});

S
Sandeep Somavarapu 已提交
255
		await testableView.show('@installed category:"themes"').then(result => {
256
			assert.equal(result.length, 2, 'Unexpected number of results for @installed query with quoted category');
S
Sandeep Somavarapu 已提交
257 258
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @installed query with quoted category.');
			assert.equal(result.get(1).name, localDisabledTheme.manifest.name, 'Unexpected extension for @installed query with quoted category.');
259 260
		});

S
Sandeep Somavarapu 已提交
261
		await testableView.show('@installed category:"programming languages"').then(result => {
262
			assert.equal(result.length, 2, 'Unexpected number of results for @installed query with quoted category including space');
S
Sandeep Somavarapu 已提交
263 264
			assert.equal(result.get(0).name, localEnabledLanguage.manifest.name, 'Unexpected extension for @installed query with quoted category including space.');
			assert.equal(result.get(1).name, localDisabledLanguage.manifest.name, 'Unexpected extension for @installed query with quoted category inlcuding space.');
265 266
		});

S
Sandeep Somavarapu 已提交
267
		await testableView.show('@installed category:themes category:random').then(result => {
268
			assert.equal(result.length, 3, 'Unexpected number of results for @installed query with multiple category');
S
Sandeep Somavarapu 已提交
269 270 271
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @installed query with multiple category.');
			assert.equal(result.get(1).name, localRandom.manifest.name, 'Unexpected extension for @installed query with multiple category.');
			assert.equal(result.get(2).name, localDisabledTheme.manifest.name, 'Unexpected extension for @installed query with multiple category.');
272 273
		});

S
Sandeep Somavarapu 已提交
274
		await testableView.show('@enabled category:themes').then(result => {
275 276 277 278
			assert.equal(result.length, 1, 'Unexpected number of results for @enabled query with category');
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @enabled query with category.');
		});

S
Sandeep Somavarapu 已提交
279
		await testableView.show('@enabled category:"themes"').then(result => {
280 281 282 283
			assert.equal(result.length, 1, 'Unexpected number of results for @enabled query with quoted category');
			assert.equal(result.get(0).name, localEnabledTheme.manifest.name, 'Unexpected extension for @enabled query with quoted category.');
		});

S
Sandeep Somavarapu 已提交
284
		await testableView.show('@enabled category:"programming languages"').then(result => {
285 286 287 288
			assert.equal(result.length, 1, 'Unexpected number of results for @enabled query with quoted category inlcuding space');
			assert.equal(result.get(0).name, localEnabledLanguage.manifest.name, 'Unexpected extension for @enabled query with quoted category including space.');
		});

S
Sandeep Somavarapu 已提交
289
		await testableView.show('@disabled category:themes').then(result => {
290 291 292 293
			assert.equal(result.length, 1, 'Unexpected number of results for @disabled query with category');
			assert.equal(result.get(0).name, localDisabledTheme.manifest.name, 'Unexpected extension for @disabled query with category.');
		});

S
Sandeep Somavarapu 已提交
294
		await testableView.show('@disabled category:"themes"').then(result => {
295 296 297 298
			assert.equal(result.length, 1, 'Unexpected number of results for @disabled query with quoted category');
			assert.equal(result.get(0).name, localDisabledTheme.manifest.name, 'Unexpected extension for @disabled query with quoted category.');
		});

S
Sandeep Somavarapu 已提交
299
		await testableView.show('@disabled category:"programming languages"').then(result => {
300 301 302
			assert.equal(result.length, 1, 'Unexpected number of results for @disabled query with quoted category inlcuding space');
			assert.equal(result.get(0).name, localDisabledLanguage.manifest.name, 'Unexpected extension for @disabled query with quoted category including space.');
		});
R
Ramya Achutha Rao 已提交
303 304 305 306 307 308 309 310 311 312 313 314
	});

	test('Test @recommended:workspace query', () => {
		const workspaceRecommendedExtensions = [
			workspaceRecommendationA,
			workspaceRecommendationB
		];
		const target = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...workspaceRecommendedExtensions));

		return testableView.show('@recommended:workspace').then(result => {
			assert.ok(target.calledOnce);
			const options: IQueryOptions = target.args[0][0];
315
			assert.equal(options.names!.length, workspaceRecommendedExtensions.length);
R
Ramya Achutha Rao 已提交
316 317
			assert.equal(result.length, workspaceRecommendedExtensions.length);
			for (let i = 0; i < workspaceRecommendedExtensions.length; i++) {
318
				assert.equal(options.names![i], workspaceRecommendedExtensions[i].identifier.id);
S
Sandeep Somavarapu 已提交
319
				assert.equal(result.get(i).identifier.id, workspaceRecommendedExtensions[i].identifier.id);
R
Ramya Achutha Rao 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
			}
		});
	});

	test('Test @recommended query', () => {
		const allRecommendedExtensions = [
			fileBasedRecommendationA,
			fileBasedRecommendationB,
			otherRecommendationA
		];
		const target = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...allRecommendedExtensions));

		return testableView.show('@recommended').then(result => {
			const options: IQueryOptions = target.args[0][0];

			assert.ok(target.calledOnce);
336
			assert.equal(options.names!.length, allRecommendedExtensions.length);
R
Ramya Achutha Rao 已提交
337 338
			assert.equal(result.length, allRecommendedExtensions.length);
			for (let i = 0; i < allRecommendedExtensions.length; i++) {
339
				assert.equal(options.names![i], allRecommendedExtensions[i].identifier.id);
S
Sandeep Somavarapu 已提交
340
				assert.equal(result.get(i).identifier.id, allRecommendedExtensions[i].identifier.id);
R
Ramya Achutha Rao 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
			}
		});
	});


	test('Test @recommended:all query', () => {
		const allRecommendedExtensions = [
			workspaceRecommendationA,
			workspaceRecommendationB,
			fileBasedRecommendationA,
			fileBasedRecommendationB,
			otherRecommendationA
		];
		const target = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...allRecommendedExtensions));

		return testableView.show('@recommended:all').then(result => {
			const options: IQueryOptions = target.args[0][0];

			assert.ok(target.calledOnce);
360
			assert.equal(options.names!.length, allRecommendedExtensions.length);
R
Ramya Achutha Rao 已提交
361 362
			assert.equal(result.length, allRecommendedExtensions.length);
			for (let i = 0; i < allRecommendedExtensions.length; i++) {
363
				assert.equal(options.names![i], allRecommendedExtensions[i].identifier.id);
S
Sandeep Somavarapu 已提交
364
				assert.equal(result.get(i).identifier.id, allRecommendedExtensions[i].identifier.id);
R
Ramya Achutha Rao 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
			}
		});
	});

	test('Test curated list experiment', () => {
		const curatedList = [
			workspaceRecommendationA,
			fileBasedRecommendationA
		];
		const experimentTarget = <SinonStub>instantiationService.stubPromise(IExperimentService, 'getCuratedExtensionsList', curatedList.map(e => e.identifier.id));
		const queryTarget = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...curatedList));

		return testableView.show('curated:mykey').then(result => {
			const curatedKey: string = experimentTarget.args[0][0];
			const options: IQueryOptions = queryTarget.args[0][0];

			assert.ok(experimentTarget.calledOnce);
			assert.ok(queryTarget.calledOnce);
383
			assert.equal(options.names!.length, curatedList.length);
R
Ramya Achutha Rao 已提交
384 385
			assert.equal(result.length, curatedList.length);
			for (let i = 0; i < curatedList.length; i++) {
386
				assert.equal(options.names![i], curatedList[i].identifier.id);
S
Sandeep Somavarapu 已提交
387
				assert.equal(result.get(i).identifier.id, curatedList[i].identifier.id);
R
Ramya Achutha Rao 已提交
388 389 390 391 392
			}
			assert.equal(curatedKey, 'mykey');
		});
	});

R
Ramya Achutha Rao 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	test('Test search', () => {
		const searchText = 'search-me';
		const results = [
			fileBasedRecommendationA,
			workspaceRecommendationA,
			otherRecommendationA,
			workspaceRecommendationB
		];
		const queryTarget = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...results));
		return testableView.show('search-me').then(result => {
			const options: IQueryOptions = queryTarget.args[0][0];

			assert.ok(queryTarget.calledOnce);
			assert.equal(options.text, searchText);
			assert.equal(result.length, results.length);
			for (let i = 0; i < results.length; i++) {
S
Sandeep Somavarapu 已提交
409
				assert.equal(result.get(i).identifier.id, results[i].identifier.id);
R
Ramya Achutha Rao 已提交
410 411 412 413 414 415
			}
		});
	});

	test('Test preferred search experiment', () => {
		const searchText = 'search-me';
S
Sandeep Somavarapu 已提交
416
		const actual = [
R
Ramya Achutha Rao 已提交
417 418 419 420 421
			fileBasedRecommendationA,
			workspaceRecommendationA,
			otherRecommendationA,
			workspaceRecommendationB
		];
S
Sandeep Somavarapu 已提交
422
		const expected = [
R
Ramya Achutha Rao 已提交
423 424 425 426 427 428
			workspaceRecommendationA,
			workspaceRecommendationB,
			fileBasedRecommendationA,
			otherRecommendationA
		];

S
Sandeep Somavarapu 已提交
429
		const queryTarget = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...actual));
R
Ramya Achutha Rao 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
		const experimentTarget = <SinonStub>instantiationService.stubPromise(IExperimentService, 'getExperimentsByType', [{
			id: 'someId',
			enabled: true,
			state: ExperimentState.Run,
			action: {
				type: ExperimentActionType.ExtensionSearchResults,
				properties: {
					searchText: 'search-me',
					preferredResults: [
						workspaceRecommendationA.identifier.id,
						'something-that-wasnt-in-first-page',
						workspaceRecommendationB.identifier.id
					]
				}
			}
		}]);

		testableView.dispose();
		testableView = instantiationService.createInstance(ExtensionsListView, {});

		return testableView.show('search-me').then(result => {
			const options: IQueryOptions = queryTarget.args[0][0];

			assert.ok(experimentTarget.calledOnce);
			assert.ok(queryTarget.calledOnce);
			assert.equal(options.text, searchText);
S
Sandeep Somavarapu 已提交
456 457
			assert.equal(result.length, expected.length);
			for (let i = 0; i < expected.length; i++) {
S
Sandeep Somavarapu 已提交
458
				assert.equal(result.get(i).identifier.id, expected[i].identifier.id);
R
Ramya Achutha Rao 已提交
459 460 461 462
			}
		});
	});

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
	test('Skip preferred search experiment when user defines sort order', () => {
		const searchText = 'search-me';
		const realResults = [
			fileBasedRecommendationA,
			workspaceRecommendationA,
			otherRecommendationA,
			workspaceRecommendationB
		];

		const queryTarget = <SinonStub>instantiationService.stubPromise(IExtensionGalleryService, 'query', aPage(...realResults));

		testableView.dispose();
		testableView = instantiationService.createInstance(ExtensionsListView, {});

		return testableView.show('search-me @sort:installs').then(result => {
			const options: IQueryOptions = queryTarget.args[0][0];

			assert.ok(queryTarget.calledOnce);
			assert.equal(options.text, searchText);
			assert.equal(result.length, realResults.length);
			for (let i = 0; i < realResults.length; i++) {
S
Sandeep Somavarapu 已提交
484
				assert.equal(result.get(i).identifier.id, realResults[i].identifier.id);
485 486 487 488
			}
		});
	});

489 490 491 492 493
	function aLocalExtension(name: string = 'someext', manifest: any = {}, properties: any = {}): ILocalExtension {
		manifest = assign({ name, publisher: 'pub', version: '1.0.0' }, manifest);
		properties = assign({
			type: ExtensionType.User,
			location: URI.file(`pub.${name}`),
494
			identifier: { id: getGalleryExtensionId(manifest.publisher, manifest.name), uuid: undefined },
495 496 497
			metadata: { id: getGalleryExtensionId(manifest.publisher, manifest.name), publisherId: manifest.publisher, publisherDisplayName: 'somename' }
		}, properties);
		return <ILocalExtension>Object.create({ manifest, ...properties });
R
Ramya Achutha Rao 已提交
498 499 500 501 502 503 504 505 506 507 508 509
	}

	function aGalleryExtension(name: string, properties: any = {}, galleryExtensionProperties: any = {}, assets: any = {}): IGalleryExtension {
		const galleryExtension = <IGalleryExtension>Object.create({});
		assign(galleryExtension, { name, publisher: 'pub', version: '1.0.0', properties: {}, assets: {} }, properties);
		assign(galleryExtension.properties, { dependencies: [] }, galleryExtensionProperties);
		assign(galleryExtension.assets, assets);
		galleryExtension.identifier = { id: getGalleryExtensionId(galleryExtension.publisher, galleryExtension.name), uuid: generateUuid() };
		return <IGalleryExtension>galleryExtension;
	}

	function aPage<T>(...objects: T[]): IPager<T> {
510
		return { firstPage: objects, total: objects.length, pageSize: objects.length, getPage: () => null! };
R
Ramya Achutha Rao 已提交
511
	}
512 513 514 515 516 517 518 519 520 521

	function toExtensionDescription(local: ILocalExtension): IExtensionDescription {
		return {
			identifier: new ExtensionIdentifier(local.identifier.id),
			isBuiltin: local.type === ExtensionType.System,
			isUnderDevelopment: false,
			extensionLocation: local.location,
			...local.manifest
		};
	}
R
Ramya Achutha Rao 已提交
522 523
});