Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/test/automation/src/editors.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 { Code } from './code';
7
8
export class Editors {
9
10
constructor(private code: Code) { }
11
12
async saveOpenedFile(): Promise<any> {
13
await this.code.dispatchKeybinding(process.platform === 'darwin' ? 'cmd+s' : 'ctrl+s', async () => {
14
await this.code.waitForElements('.tab.active.dirty', false, results => results.length === 0);
15
});
16
}
17
18
async selectTab(fileName: string): Promise<void> {
19
20
// Selecting a tab and making an editor have keyboard focus
21
// is critical to almost every test. As such, we try our
22
// best to retry this task in case some other component steals
23
// focus away from the editor while we attempt to get focus
24
25
let error: unknown | undefined = undefined;
26
let retries = 0;
27
while (retries < 10) {
28
await this.code.waitAndClick(`.tabs-container div.tab[data-resource-name$="${fileName}"]`);
29
30
try {
31
await this.waitForEditorFocus(fileName, 5 /* 5 retries * 100ms delay = 0.5s */);
32
return;
33
} catch (e) {
34
error = e;
35
retries++;
36
}
37
}
38
39
// We failed after 10 retries
40
throw error;
41
}
42
43
async waitForEditorFocus(fileName: string, retryCount?: number): Promise<void> {
44
await this.waitForActiveTab(fileName, undefined, retryCount);
45
await this.waitForActiveEditor(fileName, retryCount);
46
}
47
48
async waitForActiveTab(fileName: string, isDirty: boolean = false, retryCount?: number): Promise<void> {
49
await this.code.waitForElement(`.tabs-container div.tab.active${isDirty ? '.dirty' : ''}[aria-selected="true"][data-resource-name$="${fileName}"]`, undefined, retryCount);
50
}
51
52
async waitForActiveEditor(fileName: string, retryCount?: number): Promise<any> {
53
const selector = `.editor-instance .monaco-editor[data-uri$="${fileName}"] ${!this.code.editContextEnabled ? 'textarea' : '.native-edit-context'}`;
54
return this.code.waitForActiveElement(selector, retryCount);
55
}
56
57
async waitForTab(fileName: string, isDirty: boolean = false): Promise<void> {
58
await this.code.waitForElement(`.tabs-container div.tab${isDirty ? '.dirty' : ''}[data-resource-name$="${fileName}"]`);
59
}
60
61
async newUntitledFile(): Promise<void> {
62
const accept = () => this.waitForEditorFocus('Untitled-1');
63
if (process.platform === 'darwin') {
64
await this.code.dispatchKeybinding('cmd+n', accept);
65
} else {
66
await this.code.dispatchKeybinding('ctrl+n', accept);
67
}
68
}
69
}
70
71