Commit fc6497eb authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

sheriff: Fix flakiness in ExtensionInputMethodApiTest.Typing.

TBR=shuchen@chromium.org

Bug: 997888
Change-Id: Ic7dfb8088ec76028c2c1daabdcd582c210c7560b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1772931Reviewed-by: default avatarDarren Shen <shend@chromium.org>
Commit-Queue: Darren Shen <shend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691004}
parent 2a488ac7
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
class TestEnv { class TestEnv {
constructor() { constructor() {
this.inputContext = null; this.inputContext = null;
this.surroundingText = '';
this.compositionBounds = [];
chrome.input.ime.onFocus.addListener((context) => { chrome.input.ime.onFocus.addListener((context) => {
this.inputContext = context; this.inputContext = context;
...@@ -13,34 +15,34 @@ class TestEnv { ...@@ -13,34 +15,34 @@ class TestEnv {
chrome.input.ime.onBlur.addListener(() => { chrome.input.ime.onBlur.addListener(() => {
this.inputContext = null; this.inputContext = null;
}); });
}
getContextID() { chrome.input.ime.onSurroundingTextChanged.addListener(
return this.inputContext.contextID; (_, surroundingInfo) => {
} this.surroundingText = surroundingInfo.text;
});
onSurroundingTextChanged() { chrome.inputMethodPrivate.onCompositionBoundsChanged.addListener(
return new Promise((resolve) => { (_, boundsList) => {
chrome.input.ime.onSurroundingTextChanged.addListener( this.compositionBounds = boundsList;
function listener(_, surroundingInfo) { });
chrome.input.ime.onSurroundingTextChanged.removeListener(listener);
resolve(surroundingInfo.text);
});
});
} }
onCompositionBoundsChanged() { getContextID() {
return new Promise((resolve) => { return this.inputContext.contextID;
chrome.inputMethodPrivate.onCompositionBoundsChanged.addListener(
function listener(_, boundsList) {
chrome.inputMethodPrivate.onCompositionBoundsChanged.removeListener(
listener);
resolve(boundsList);
});
});
} }
}; };
function waitUntil(predicate) {
return new Promise((resolve) => {
const timer = setInterval(() => {
if (predicate()) {
clearInterval(timer);
resolve();
}
}, 100);
});
}
const testEnv = new TestEnv(); const testEnv = new TestEnv();
// Wrap inputMethodPrivate in a promise-based API to simplify test code. // Wrap inputMethodPrivate in a promise-based API to simplify test code.
...@@ -85,8 +87,7 @@ chrome.test.runTests([ ...@@ -85,8 +87,7 @@ chrome.test.runTests([
text: 'hello world' text: 'hello world'
}); });
chrome.test.assertEq('hello world', await waitUntil(() => testEnv.surroundingText === 'hello world');
await testEnv.onSurroundingTextChanged());
// Cursor is at the end of the string. // Cursor is at the end of the string.
await asyncInputMethodPrivate.setCompositionRange({ await asyncInputMethodPrivate.setCompositionRange({
...@@ -100,8 +101,7 @@ chrome.test.runTests([ ...@@ -100,8 +101,7 @@ chrome.test.runTests([
}); });
// Should underline "world". // Should underline "world".
chrome.test.assertEq(5, await waitUntil(() => testEnv.compositionBounds.length === 5);
(await testEnv.onCompositionBoundsChanged()).length);
await asyncInputIme.setComposition({ await asyncInputIme.setComposition({
contextID: testEnv.getContextID(), contextID: testEnv.getContextID(),
...@@ -110,8 +110,7 @@ chrome.test.runTests([ ...@@ -110,8 +110,7 @@ chrome.test.runTests([
}); });
// Composition should change to "foo". // Composition should change to "foo".
chrome.test.assertEq(3, await waitUntil(() => testEnv.compositionBounds.length === 3);
(await testEnv.onCompositionBoundsChanged()).length);
// Should replace composition with "again". // Should replace composition with "again".
await asyncInputIme.commitText({ await asyncInputIme.commitText({
...@@ -119,8 +118,7 @@ chrome.test.runTests([ ...@@ -119,8 +118,7 @@ chrome.test.runTests([
text: 'again' text: 'again'
}); });
chrome.test.assertEq('hello again', await waitUntil(() => testEnv.surroundingText === 'hello again');
await testEnv.onSurroundingTextChanged());
// Cursor is at end of the string. // Cursor is at end of the string.
// Call setCompositionRange with no segments. // Call setCompositionRange with no segments.
...@@ -131,8 +129,7 @@ chrome.test.runTests([ ...@@ -131,8 +129,7 @@ chrome.test.runTests([
}); });
// Composition should be "again". // Composition should be "again".
chrome.test.assertEq(5, waitUntil(() => testEnv.compositionBounds.length === 5);
(await testEnv.onCompositionBoundsChanged()).length);
// Should commit "again" and set composition to "in". // Should commit "again" and set composition to "in".
await asyncInputMethodPrivate.setCompositionRange({ await asyncInputMethodPrivate.setCompositionRange({
...@@ -141,6 +138,8 @@ chrome.test.runTests([ ...@@ -141,6 +138,8 @@ chrome.test.runTests([
selectionAfter: 0 selectionAfter: 0
}); });
await waitUntil(() => testEnv.compositionBounds.length === 2);
chrome.test.succeed(); chrome.test.succeed();
} }
]); ]);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment