Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/automation/src/settings.ts
3520 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Editor } from './editor';
7
import { Editors } from './editors';
8
import { Code } from './code';
9
import { QuickAccess } from './quickaccess';
10
11
const SEARCH_BOX_NATIVE_EDIT_CONTEXT = '.settings-editor .suggest-input-container .monaco-editor .native-edit-context';
12
const SEARCH_BOX_TEXTAREA = '.settings-editor .suggest-input-container .monaco-editor textarea';
13
14
export class SettingsEditor {
15
constructor(private code: Code, private editors: Editors, private editor: Editor, private quickaccess: QuickAccess) { }
16
17
/**
18
* Write a single setting key value pair.
19
*
20
* Warning: You may need to set `editor.wordWrap` to `"on"` if this is called with a really long
21
* setting.
22
*/
23
async addUserSetting(setting: string, value: string): Promise<void> {
24
await this.openUserSettingsFile();
25
26
await this.editors.selectTab('settings.json');
27
await this.code.dispatchKeybinding('right', () =>
28
this.editor.waitForEditorSelection('settings.json', (s) => this._acceptEditorSelection(this.code.editContextEnabled, s)));
29
await this.editor.waitForTypeInEditor('settings.json', `"${setting}": ${value},`);
30
await this.editors.saveOpenedFile();
31
}
32
33
/**
34
* Write several settings faster than multiple calls to {@link addUserSetting}.
35
*
36
* Warning: You will likely also need to set `editor.wordWrap` to `"on"` if `addUserSetting` is
37
* called after this in the test.
38
*/
39
async addUserSettings(settings: [key: string, value: string][]): Promise<void> {
40
await this.openUserSettingsFile();
41
42
await this.editors.selectTab('settings.json');
43
await this.code.dispatchKeybinding('right', () =>
44
this.editor.waitForEditorSelection('settings.json', (s) => this._acceptEditorSelection(this.code.editContextEnabled, s)));
45
await this.editor.waitForTypeInEditor('settings.json', settings.map(v => `"${v[0]}": ${v[1]},`).join(''));
46
await this.editors.saveOpenedFile();
47
}
48
49
async clearUserSettings(): Promise<void> {
50
await this.openUserSettingsFile();
51
await this.quickaccess.runCommand('editor.action.selectAll');
52
await this.code.dispatchKeybinding('Delete', async () => {
53
await this.editor.waitForEditorContents('settings.json', contents => contents === '');
54
});
55
await this.editor.waitForTypeInEditor('settings.json', `{`); // will auto close }
56
await this.editors.saveOpenedFile();
57
await this.quickaccess.runCommand('workbench.action.closeActiveEditor');
58
}
59
60
async openUserSettingsFile(): Promise<void> {
61
await this.quickaccess.runCommand('workbench.action.openSettingsJson');
62
await this.editor.waitForEditorFocus('settings.json', 1);
63
}
64
65
async openUserSettingsUI(): Promise<void> {
66
await this.quickaccess.runCommand('workbench.action.openSettings2');
67
await this.code.waitForActiveElement(this._editContextSelector());
68
}
69
70
async searchSettingsUI(query: string): Promise<void> {
71
await this.openUserSettingsUI();
72
73
await this.code.waitAndClick(this._editContextSelector());
74
await this.code.dispatchKeybinding(process.platform === 'darwin' ? 'cmd+a' : 'ctrl+a', async () => { });
75
await this.code.dispatchKeybinding('Delete', async () => {
76
await this.code.waitForElements('.settings-editor .settings-count-widget', false, results => !results || (results?.length === 1 && !results[0].textContent));
77
});
78
await this.code.waitForTypeInEditor(this._editContextSelector(), query);
79
await this.code.waitForElements('.settings-editor .settings-count-widget', false, results => results?.length === 1 && results[0].textContent.includes('Found'));
80
}
81
82
private _editContextSelector() {
83
return !this.code.editContextEnabled ? SEARCH_BOX_TEXTAREA : SEARCH_BOX_NATIVE_EDIT_CONTEXT;
84
}
85
86
private _acceptEditorSelection(editContextEnabled: boolean, s: { selectionStart: number; selectionEnd: number }): boolean {
87
if (!editContextEnabled) {
88
return true;
89
}
90
return s.selectionStart === 1 && s.selectionEnd === 1;
91
}
92
}
93
94