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,33 +15,33 @@ class TestEnv { ...@@ -13,33 +15,33 @@ class TestEnv {
chrome.input.ime.onBlur.addListener(() => { chrome.input.ime.onBlur.addListener(() => {
this.inputContext = null; this.inputContext = null;
}); });
chrome.input.ime.onSurroundingTextChanged.addListener(
(_, surroundingInfo) => {
this.surroundingText = surroundingInfo.text;
});
chrome.inputMethodPrivate.onCompositionBoundsChanged.addListener(
(_, boundsList) => {
this.compositionBounds = boundsList;
});
} }
getContextID() { getContextID() {
return this.inputContext.contextID; return this.inputContext.contextID;
} }
};
onSurroundingTextChanged() { function waitUntil(predicate) {
return new Promise((resolve) => { return new Promise((resolve) => {
chrome.input.ime.onSurroundingTextChanged.addListener( const timer = setInterval(() => {
function listener(_, surroundingInfo) { if (predicate()) {
chrome.input.ime.onSurroundingTextChanged.removeListener(listener); clearInterval(timer);
resolve(surroundingInfo.text); resolve();
});
});
} }
}, 100);
onCompositionBoundsChanged() {
return new Promise((resolve) => {
chrome.inputMethodPrivate.onCompositionBoundsChanged.addListener(
function listener(_, boundsList) {
chrome.inputMethodPrivate.onCompositionBoundsChanged.removeListener(
listener);
resolve(boundsList);
});
}); });
} }
};
const testEnv = new TestEnv(); const testEnv = new TestEnv();
...@@ -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