зеркало из https://github.com/electron/electron.git
test: reliably wait for spellchecker to load (#28118)
This commit is contained in:
Родитель
6c90411e21
Коммит
5b710519cf
|
@ -138,6 +138,10 @@ void TriggerFatalErrorForTesting(v8::Isolate* isolate) {
|
||||||
v8::ExtensionConfiguration config(1, bDeps);
|
v8::ExtensionConfiguration config(1, bDeps);
|
||||||
v8::Context::New(isolate, &config);
|
v8::Context::New(isolate, &config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RunUntilIdle() {
|
||||||
|
base::RunLoop().RunUntilIdle();
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void Initialize(v8::Local<v8::Object> exports,
|
void Initialize(v8::Local<v8::Object> exports,
|
||||||
|
@ -158,6 +162,7 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||||
dict.SetMethod("getWeaklyTrackedValues", &GetWeaklyTrackedValues);
|
dict.SetMethod("getWeaklyTrackedValues", &GetWeaklyTrackedValues);
|
||||||
dict.SetMethod("clearWeaklyTrackedValues", &ClearWeaklyTrackedValues);
|
dict.SetMethod("clearWeaklyTrackedValues", &ClearWeaklyTrackedValues);
|
||||||
dict.SetMethod("weaklyTrackValue", &WeaklyTrackValue);
|
dict.SetMethod("weaklyTrackValue", &WeaklyTrackValue);
|
||||||
|
dict.SetMethod("runUntilIdle", &RunUntilIdle);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,10 +7,36 @@ import { emittedOnce } from './events-helpers';
|
||||||
import { ifit, ifdescribe, delay } from './spec-helpers';
|
import { ifit, ifdescribe, delay } from './spec-helpers';
|
||||||
|
|
||||||
const features = process._linkedBinding('electron_common_features');
|
const features = process._linkedBinding('electron_common_features');
|
||||||
|
const v8Util = process._linkedBinding('electron_common_v8_util');
|
||||||
|
|
||||||
ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
||||||
let w: BrowserWindow;
|
let w: BrowserWindow;
|
||||||
|
|
||||||
|
async function rightClick () {
|
||||||
|
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
|
||||||
|
w.webContents.sendInputEvent({
|
||||||
|
type: 'mouseDown',
|
||||||
|
button: 'right',
|
||||||
|
x: 43,
|
||||||
|
y: 42
|
||||||
|
});
|
||||||
|
return (await contextMenuPromise)[1] as Electron.ContextMenuParams;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the page is just loaded, the spellchecker might not be ready yet. Since
|
||||||
|
// there is no event to know the state of spellchecker, the only reliable way
|
||||||
|
// to detect spellchecker is to keep checking with a busy loop.
|
||||||
|
async function rightClickUntil (fn: (params: Electron.ContextMenuParams) => boolean) {
|
||||||
|
const now = Date.now();
|
||||||
|
const timeout = 10 * 1000;
|
||||||
|
let contextMenuParams = await rightClick();
|
||||||
|
while (!fn(contextMenuParams) && (Date.now() - now < timeout)) {
|
||||||
|
await delay(100);
|
||||||
|
contextMenuParams = await rightClick();
|
||||||
|
}
|
||||||
|
return contextMenuParams;
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
w = new BrowserWindow({
|
w = new BrowserWindow({
|
||||||
show: false,
|
show: false,
|
||||||
|
@ -28,25 +54,13 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
||||||
await closeWindow(w);
|
await closeWindow(w);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Context menu test can not run on Windows, and it is not reliable on ARM
|
// Context menu test can not run on Windows.
|
||||||
// CI machines.
|
const shouldRun = process.platform !== 'win32';
|
||||||
const shouldRun = process.platform !== 'win32' &&
|
|
||||||
process.arch !== 'arm' &&
|
|
||||||
process.arch !== 'arm64';
|
|
||||||
|
|
||||||
ifit(shouldRun)('should detect correctly spelled words as correct', async () => {
|
ifit(shouldRun)('should detect correctly spelled words as correct', async () => {
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautiful and lovely"');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautiful and lovely"');
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
||||||
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
|
const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.selectionText.length > 0);
|
||||||
// Wait for spellchecker to load
|
|
||||||
await delay(500);
|
|
||||||
w.webContents.sendInputEvent({
|
|
||||||
type: 'mouseDown',
|
|
||||||
button: 'right',
|
|
||||||
x: 43,
|
|
||||||
y: 42
|
|
||||||
});
|
|
||||||
const contextMenuParams: Electron.ContextMenuParams = (await contextMenuPromise)[1];
|
|
||||||
expect(contextMenuParams.misspelledWord).to.eq('');
|
expect(contextMenuParams.misspelledWord).to.eq('');
|
||||||
expect(contextMenuParams.dictionarySuggestions).to.have.lengthOf(0);
|
expect(contextMenuParams.dictionarySuggestions).to.have.lengthOf(0);
|
||||||
});
|
});
|
||||||
|
@ -54,16 +68,7 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
||||||
ifit(shouldRun)('should detect incorrectly spelled words as incorrect', async () => {
|
ifit(shouldRun)('should detect incorrectly spelled words as incorrect', async () => {
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
||||||
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
|
const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
|
||||||
// Wait for spellchecker to load
|
|
||||||
await delay(500);
|
|
||||||
w.webContents.sendInputEvent({
|
|
||||||
type: 'mouseDown',
|
|
||||||
button: 'right',
|
|
||||||
x: 43,
|
|
||||||
y: 42
|
|
||||||
});
|
|
||||||
const contextMenuParams: Electron.ContextMenuParams = (await contextMenuPromise)[1];
|
|
||||||
expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
|
expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
|
||||||
expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
|
expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
|
||||||
});
|
});
|
||||||
|
@ -74,16 +79,7 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
||||||
w.webContents.session.setSpellCheckerLanguages(['en-US']);
|
w.webContents.session.setSpellCheckerLanguages(['en-US']);
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
||||||
const contextMenuPromise = emittedOnce(w.webContents, 'context-menu');
|
const contextMenuParams = await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
|
||||||
// Wait for spellchecker to load
|
|
||||||
await delay(500);
|
|
||||||
w.webContents.sendInputEvent({
|
|
||||||
type: 'mouseDown',
|
|
||||||
button: 'right',
|
|
||||||
x: 43,
|
|
||||||
y: 42
|
|
||||||
});
|
|
||||||
const contextMenuParams: Electron.ContextMenuParams = (await contextMenuPromise)[1];
|
|
||||||
expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
|
expect(contextMenuParams.misspelledWord).to.eq('Beautifulllll');
|
||||||
expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
|
expect(contextMenuParams.dictionarySuggestions).to.have.length.of.at.least(1);
|
||||||
});
|
});
|
||||||
|
@ -91,8 +87,7 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
||||||
ifit(shouldRun)('should expose webFrame spellchecker correctly', async () => {
|
ifit(shouldRun)('should expose webFrame spellchecker correctly', async () => {
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
||||||
// Wait for spellchecker to load
|
await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
|
||||||
await delay(500);
|
|
||||||
|
|
||||||
const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript('require("electron").webFrame.' + expr);
|
const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript('require("electron").webFrame.' + expr);
|
||||||
|
|
||||||
|
@ -110,16 +105,17 @@ ifdescribe(features.isBuiltinSpellCheckerEnabled())('spellchecker', () => {
|
||||||
ifit(shouldRun)('can be dynamically changed', async () => {
|
ifit(shouldRun)('can be dynamically changed', async () => {
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").value = "Beautifulllll asd asd"');
|
||||||
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
await w.webContents.executeJavaScript('document.body.querySelector("textarea").focus()');
|
||||||
// Wait for spellchecker to load
|
await rightClickUntil((contextMenuParams) => contextMenuParams.misspelledWord.length > 0);
|
||||||
await delay(500);
|
|
||||||
|
|
||||||
const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript('require("electron").webFrame.' + expr);
|
const callWebFrameFn = (expr: string) => w.webContents.executeJavaScript('require("electron").webFrame.' + expr);
|
||||||
|
|
||||||
w.webContents.session.spellCheckerEnabled = false;
|
w.webContents.session.spellCheckerEnabled = false;
|
||||||
|
v8Util.runUntilIdle();
|
||||||
expect(w.webContents.session.spellCheckerEnabled).to.be.false();
|
expect(w.webContents.session.spellCheckerEnabled).to.be.false();
|
||||||
expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(false);
|
expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(false);
|
||||||
|
|
||||||
w.webContents.session.spellCheckerEnabled = true;
|
w.webContents.session.spellCheckerEnabled = true;
|
||||||
|
v8Util.runUntilIdle();
|
||||||
expect(w.webContents.session.spellCheckerEnabled).to.be.true();
|
expect(w.webContents.session.spellCheckerEnabled).to.be.true();
|
||||||
expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(true);
|
expect(await callWebFrameFn('isWordMisspelled("testt")')).to.equal(true);
|
||||||
});
|
});
|
||||||
|
|
|
@ -43,6 +43,7 @@ declare namespace NodeJS {
|
||||||
weaklyTrackValue(value: any): void;
|
weaklyTrackValue(value: any): void;
|
||||||
clearWeaklyTrackedValues(): void;
|
clearWeaklyTrackedValues(): void;
|
||||||
getWeaklyTrackedValues(): any[];
|
getWeaklyTrackedValues(): any[];
|
||||||
|
runUntilIdle(): void;
|
||||||
isSameOrigin(a: string, b: string): boolean;
|
isSameOrigin(a: string, b: string): boolean;
|
||||||
triggerFatalErrorForTesting(): void;
|
triggerFatalErrorForTesting(): void;
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче