Merge from vscode e558dc6ea73a75bd69d7a0b485f0e7e4194c66bf (#6864)

This commit is contained in:
Anthony Dresser
2019-08-21 20:44:59 -07:00
committed by GitHub
parent d2ae0f0154
commit 985bfae8a0
107 changed files with 2260 additions and 814 deletions

View File

@@ -7,18 +7,13 @@ import { URI } from 'vs/base/common/uri';
import { OpenerService } from 'vs/editor/browser/services/openerService';
import { TestCodeEditorService } from 'vs/editor/test/browser/editorTestServices';
import { CommandsRegistry, ICommandService, NullCommandService } from 'vs/platform/commands/common/commands';
import { deepClone } from 'vs/base/common/objects';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IProductService } from 'vs/platform/product/common/product';
import { IStorageService } from 'vs/platform/storage/common/storage';
suite('OpenerService', function () {
const editorService = new TestCodeEditorService();
let lastCommand: { id: string, args: any[] } | undefined;
let lastCommand: { id: string; args: any[] } | undefined;
const commandService = new class implements ICommandService {
const commandService = new (class implements ICommandService {
_serviceBrand: any;
onWillExecuteCommand = () => ({ dispose: () => { } });
onDidExecuteCommand = () => ({ dispose: () => { } });
@@ -26,79 +21,20 @@ suite('OpenerService', function () {
lastCommand = { id, args };
return Promise.resolve(undefined);
}
};
function getStorageService(trustedDomainsSetting: string[]) {
let _settings = deepClone(trustedDomainsSetting);
return new class implements IStorageService {
get = () => JSON.stringify(_settings);
store = (key: string, val: string) => _settings = JSON.parse(val);
// Don't care
_serviceBrand: any;
onDidChangeStorage = () => ({ dispose: () => { } });
onWillSaveState = () => ({ dispose: () => { } });
getBoolean = () => true;
getNumber = () => 0;
remove = () => { };
logStorage = () => { };
};
}
function getDialogService() {
return new class implements IDialogService {
_showInvoked = 0;
show = () => {
this._showInvoked++;
return Promise.resolve({} as any);
}
get confirmInvoked() { return this._showInvoked; }
// Don't care
_serviceBrand: any;
confirm = () => {
return Promise.resolve({} as any);
}
};
}
function getProductService(): IProductService {
return new class {
nameShort: 'VS Code';
_serviceBrand: any;
} as IProductService;
}
})();
setup(function () {
lastCommand = undefined;
});
test('delegate to editorService, scheme:///fff', function () {
const openerService = new OpenerService(
editorService,
NullCommandService,
getStorageService([]),
getDialogService(),
getProductService()
);
const openerService = new OpenerService(editorService, NullCommandService);
openerService.open(URI.parse('another:///somepath'));
assert.equal(editorService.lastInput!.options!.selection, undefined);
});
test('delegate to editorService, scheme:///fff#L123', function () {
const openerService = new OpenerService(
editorService,
NullCommandService,
getStorageService([]),
getDialogService(),
getProductService()
);
const openerService = new OpenerService(editorService, NullCommandService);
openerService.open(URI.parse('file:///somepath#L23'));
assert.equal(editorService.lastInput!.options!.selection!.startLineNumber, 23);
@@ -120,14 +56,7 @@ suite('OpenerService', function () {
});
test('delegate to editorService, scheme:///fff#123,123', function () {
const openerService = new OpenerService(
editorService,
NullCommandService,
getStorageService([]),
getDialogService(),
getProductService()
);
const openerService = new OpenerService(editorService, NullCommandService);
openerService.open(URI.parse('file:///somepath#23'));
assert.equal(editorService.lastInput!.options!.selection!.startLineNumber, 23);
@@ -145,14 +74,7 @@ suite('OpenerService', function () {
});
test('delegate to commandsService, command:someid', function () {
const openerService = new OpenerService(
editorService,
commandService,
getStorageService([]),
getDialogService(),
getProductService()
);
const openerService = new OpenerService(editorService, commandService);
const id = `aCommand${Math.random()}`;
CommandsRegistry.registerCommand(id, function () { });
@@ -173,69 +95,107 @@ suite('OpenerService', function () {
assert.equal(lastCommand!.args[1], true);
});
test('links are protected by dialog.show', function () {
const dialogService = getDialogService();
const openerService = new OpenerService(
editorService,
commandService,
getStorageService([]),
dialogService,
getProductService()
);
test('links are protected by validators', async function () {
const openerService = new OpenerService(editorService, commandService);
openerService.open(URI.parse('https://www.microsoft.com'));
assert.equal(dialogService.confirmInvoked, 1);
openerService.registerValidator({ shouldOpen: () => Promise.resolve(false) });
const httpResult = await openerService.open(URI.parse('https://www.microsoft.com'));
const httpsResult = await openerService.open(URI.parse('https://www.microsoft.com'));
assert.equal(httpResult, false);
assert.equal(httpsResult, false);
});
test('links on the whitelisted domains can be opened without dialog.show', function () {
const dialogService = getDialogService();
const openerService = new OpenerService(
editorService,
commandService,
getStorageService(['https://microsoft.com']),
dialogService,
getProductService()
);
test('links validated by validators go to openers', async function () {
const openerService = new OpenerService(editorService, commandService);
openerService.open(URI.parse('https://microsoft.com'));
openerService.open(URI.parse('https://microsoft.com/'));
openerService.open(URI.parse('https://microsoft.com/en-us/'));
openerService.open(URI.parse('https://microsoft.com/en-us/?foo=bar'));
openerService.open(URI.parse('https://microsoft.com/en-us/?foo=bar#baz'));
openerService.registerValidator({ shouldOpen: () => Promise.resolve(true) });
assert.equal(dialogService.confirmInvoked, 0);
let openCount = 0;
openerService.registerOpener({
open: (resource: URI) => {
openCount++;
return Promise.resolve(true);
}
});
await openerService.open(URI.parse('http://microsoft.com'));
assert.equal(openCount, 1);
await openerService.open(URI.parse('https://microsoft.com'));
assert.equal(openCount, 2);
});
test('variations of links are protected by dialog confirmation', function () {
const dialogService = getDialogService();
const openerService = new OpenerService(
editorService,
commandService,
getStorageService(['https://microsoft.com']),
dialogService,
getProductService()
);
test('links validated by multiple validators', async function () {
const openerService = new OpenerService(editorService, commandService);
openerService.open(URI.parse('http://microsoft.com'));
openerService.open(URI.parse('https://www.microsoft.com'));
let v1 = 0;
openerService.registerValidator({
shouldOpen: () => {
v1++;
return Promise.resolve(true);
}
});
assert.equal(dialogService.confirmInvoked, 2);
let v2 = 0;
openerService.registerValidator({
shouldOpen: () => {
v2++;
return Promise.resolve(true);
}
});
let openCount = 0;
openerService.registerOpener({
open: (resource: URI) => {
openCount++;
return Promise.resolve(true);
}
});
await openerService.open(URI.parse('http://microsoft.com'));
assert.equal(openCount, 1);
assert.equal(v1, 1);
assert.equal(v2, 1);
await openerService.open(URI.parse('https://microsoft.com'));
assert.equal(openCount, 2);
assert.equal(v1, 2);
assert.equal(v2, 2);
});
test('* removes all link protection', function () {
const dialogService = getDialogService();
const openerService = new OpenerService(
editorService,
commandService,
getStorageService(['*']),
dialogService,
getProductService()
);
test('links invalidated by first validator do not continue validating', async function () {
const openerService = new OpenerService(editorService, commandService);
openerService.open(URI.parse('https://code.visualstudio.com/'));
openerService.open(URI.parse('https://www.microsoft.com'));
openerService.open(URI.parse('https://www.github.com'));
let v1 = 0;
openerService.registerValidator({
shouldOpen: () => {
v1++;
return Promise.resolve(false);
}
});
assert.equal(dialogService.confirmInvoked, 0);
let v2 = 0;
openerService.registerValidator({
shouldOpen: () => {
v2++;
return Promise.resolve(true);
}
});
let openCount = 0;
openerService.registerOpener({
open: (resource: URI) => {
openCount++;
return Promise.resolve(true);
}
});
await openerService.open(URI.parse('http://microsoft.com'));
assert.equal(openCount, 0);
assert.equal(v1, 1);
assert.equal(v2, 0);
await openerService.open(URI.parse('https://microsoft.com'));
assert.equal(openCount, 0);
assert.equal(v1, 2);
assert.equal(v2, 0);
});
});