Skip to content

Commit

Permalink
quick access - picker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Mar 18, 2020
1 parent a8e13f1 commit 51ec5e9
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/vs/platform/quickinput/browser/pickerQuickAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem
const picksToken = picksCts.token;
const res = this.getPicks(picker.value.substr(this.prefix.length).trim(), disposables.add(new DisposableStore()), picksToken);
if (isFastAndSlowPicksType(res)) {
picker.items = res.picks;
if (res.picks.length > 0) {
// Optimization: if there are no fast results
// we do nto simply unset all the existing items
// to reduce the flickering.
picker.items = res.picks;
}
picker.busy = true;
try {
const additionalPicks = await res.additionalPicks;
if (picksToken.isCancellationRequested) {
return;
}

if (additionalPicks.length > 0) {
if (res.picks.length === 0 || additionalPicks.length > 0) {
// Optimization: we only update the picker items if we either
// did not update them earlier, or we actually got new results
picker.items = [...res.picks, ...additionalPicks];
}
} finally {
Expand Down Expand Up @@ -187,5 +194,5 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem
* through this token.
* @returns the picks either directly, as promise or combined fast and slow results.
*/
protected abstract getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): Array<T | IQuickPickSeparator> | Promise<Array<T | IQuickPickSeparator>> | { picks: Array<T | IQuickPickSeparator>, additionalPicks: Promise<Array<T | IQuickPickSeparator>> };
protected abstract getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): Array<T | IQuickPickSeparator> | Promise<Array<T | IQuickPickSeparator>> | FastAndSlowPicksType<T>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class AnythingQuickAccessProvider extends PickerQuickAccessProvider<IAnyt
return super.provide(picker, token);
}

protected getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): FastAndSlowPicksType<IAnythingQuickPickItem | IQuickPickSeparator> {
protected getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): FastAndSlowPicksType<IAnythingQuickPickItem> {

// Find a suitable range from the pattern looking for ":", "#" or ","
let range: IRange | undefined = undefined;
Expand Down
119 changes: 119 additions & 0 deletions src/vs/workbench/test/browser/quickAccess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
import { TestServiceAccessor, workbenchInstantiationService } from 'vs/workbench/test/browser/workbenchTestServices';
import { DisposableStore, toDisposable, IDisposable } from 'vs/base/common/lifecycle';
import { timeout } from 'vs/base/common/async';
import { PickerQuickAccessProvider, FastAndSlowPicksType } from 'vs/platform/quickinput/browser/pickerQuickAccess';

suite('QuickAccess', () => {

Expand Down Expand Up @@ -192,4 +193,122 @@ suite('QuickAccess', () => {

restore();
});

let fastProviderCalled = false;
let slowProviderCalled = false;
let fastAndSlowProviderCalled = false;

let slowProviderCanceled = false;
let fastAndSlowProviderCanceled = false;

class FastTestQuickPickProvider extends PickerQuickAccessProvider<IQuickPickItem> {

constructor() {
super('fast');
}

protected getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): Array<IQuickPickItem> {
fastProviderCalled = true;

return [{ label: 'Fast Pick' }];
}
}

class SlowTestQuickPickProvider extends PickerQuickAccessProvider<IQuickPickItem> {

constructor() {
super('slow');
}

protected async getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): Promise<Array<IQuickPickItem>> {
slowProviderCalled = true;

await timeout(1);

if (token.isCancellationRequested) {
slowProviderCanceled = true;
}

return [{ label: 'Slow Pick' }];
}
}

class FastAndSlowTestQuickPickProvider extends PickerQuickAccessProvider<IQuickPickItem> {

constructor() {
super('bothFastAndSlow');
}

protected getPicks(filter: string, disposables: DisposableStore, token: CancellationToken): FastAndSlowPicksType<IQuickPickItem> {
fastAndSlowProviderCalled = true;

return {
picks: [{ label: 'Fast Pick' }],
additionalPicks: (async () => {
await timeout(1);

if (token.isCancellationRequested) {
fastAndSlowProviderCanceled = true;
}

return [{ label: 'Slow Pick' }];
})()
};
}
}

const fastProviderDescriptor = { ctor: FastTestQuickPickProvider, prefix: 'fast', helpEntries: [] };
const slowProviderDescriptor = { ctor: SlowTestQuickPickProvider, prefix: 'slow', helpEntries: [] };
const fastAndSlowProviderDescriptor = { ctor: FastAndSlowTestQuickPickProvider, prefix: 'bothFastAndSlow', helpEntries: [] };

test('quick pick access', async () => {
const registry = (Registry.as<IQuickAccessRegistry>(Extensions.Quickaccess));
const restore = (registry as QuickAccessRegistry).clear();

const disposables = new DisposableStore();

disposables.add(registry.registerQuickAccessProvider(fastProviderDescriptor));
disposables.add(registry.registerQuickAccessProvider(slowProviderDescriptor));
disposables.add(registry.registerQuickAccessProvider(fastAndSlowProviderDescriptor));

accessor.quickInputService.quickAccess.show('fast');
assert.equal(fastProviderCalled, true);
assert.equal(slowProviderCalled, false);
assert.equal(fastAndSlowProviderCalled, false);
fastProviderCalled = false;

accessor.quickInputService.quickAccess.show('slow');
await timeout(2);

assert.equal(fastProviderCalled, false);
assert.equal(slowProviderCalled, true);
assert.equal(slowProviderCanceled, false);
assert.equal(fastAndSlowProviderCalled, false);
slowProviderCalled = false;

accessor.quickInputService.quickAccess.show('bothFastAndSlow');
await timeout(2);

assert.equal(fastProviderCalled, false);
assert.equal(slowProviderCalled, false);
assert.equal(fastAndSlowProviderCalled, true);
assert.equal(fastAndSlowProviderCanceled, false);
fastAndSlowProviderCalled = false;

accessor.quickInputService.quickAccess.show('slow');
accessor.quickInputService.quickAccess.show('bothFastAndSlow');
accessor.quickInputService.quickAccess.show('fast');

assert.equal(fastProviderCalled, true);
assert.equal(slowProviderCalled, true);
assert.equal(fastAndSlowProviderCalled, true);

await timeout(2);
assert.equal(slowProviderCanceled, true);
assert.equal(fastAndSlowProviderCanceled, true);

disposables.dispose();

restore();
});
});

0 comments on commit 51ec5e9

Please sign in to comment.