Commit f5734ece authored by Solomon Kinard's avatar Solomon Kinard Committed by Commit Bot

[Extensions] Use promises in Search API test.

References: crrev.com/c/2368147 & testing_api.md#use-promises.

Bug: 1119846

Change-Id: Id7e603c415f8d2c6fee00a615b97078e486fe6f7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2382888
Commit-Queue: Solomon Kinard <solomonkinard@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805595}
parent d145fd42
...@@ -21,9 +21,10 @@ chrome.test.runTests([ ...@@ -21,9 +21,10 @@ chrome.test.runTests([
}); });
}, },
// Display results in current tab if no disposition is provided.
function QueryPopulatedDispositionEmpty() { function QueryPopulatedDispositionEmpty() {
chrome.tabs.create({}, (tab) => { chrome.tabs.create({}, (tab) => {
addTabListener(tab.id); waitForTabAndPass(tab.id);
chrome.search.query({search: SEARCH_WORDS}, () => {}); chrome.search.query({search: SEARCH_WORDS}, () => {});
}); });
}, },
...@@ -31,7 +32,7 @@ chrome.test.runTests([ ...@@ -31,7 +32,7 @@ chrome.test.runTests([
// Display results in current tab if said disposition is provided. // Display results in current tab if said disposition is provided.
function QueryPopulatedDispositionCurrentTab() { function QueryPopulatedDispositionCurrentTab() {
chrome.tabs.create({}, (tab) => { chrome.tabs.create({}, (tab) => {
addTabListener(tab.id); waitForTabAndPass(tab.id);
chrome.search.query({search: SEARCH_WORDS, disposition: 'CURRENT_TAB'}); chrome.search.query({search: SEARCH_WORDS, disposition: 'CURRENT_TAB'});
}); });
}, },
...@@ -40,14 +41,24 @@ chrome.test.runTests([ ...@@ -40,14 +41,24 @@ chrome.test.runTests([
function QueryPopulatedDispositionNewTab() { function QueryPopulatedDispositionNewTab() {
chrome.tabs.query({}, (initialTabs) => { chrome.tabs.query({}, (initialTabs) => {
let initialTabIds = initialTabs.map(tab => tab.id); let initialTabIds = initialTabs.map(tab => tab.id);
addAnyTabListener(); Promise
chrome.search.query( .all([
{search: SEARCH_WORDS, disposition: 'NEW_TAB'}, () => { waitForAnyTab(),
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { new Promise(resolve => {
assertEq(1, tabs.length); chrome.search.query(
// A new tab should have been created. {search: SEARCH_WORDS, disposition: 'NEW_TAB'}, () => {
assertFalse(initialTabIds.includes(tabs[0].id)); chrome.tabs.query(
}); {active: true, currentWindow: true}, (tabs) => {
assertEq(1, tabs.length);
// A new tab should have been created.
assertFalse(initialTabIds.includes(tabs[0].id));
resolve();
});
});
}),
])
.then(() => {
succeed();
}); });
}); });
}, },
...@@ -56,15 +67,24 @@ chrome.test.runTests([ ...@@ -56,15 +67,24 @@ chrome.test.runTests([
function QueryPopulatedDispositionNewWindow() { function QueryPopulatedDispositionNewWindow() {
chrome.windows.getAll({}, (initialWindows) => { chrome.windows.getAll({}, (initialWindows) => {
let initialWindowIds = initialWindows.map(window => window.id); let initialWindowIds = initialWindows.map(window => window.id);
addAnyTabListener(); Promise
chrome.search.query( .all([
{search: SEARCH_WORDS, disposition: 'NEW_WINDOW'}, () => { waitForAnyTab(),
chrome.windows.getAll({}, (windows) => { new Promise((resolve) => {
assertEq(windows.length, initialWindowIds.length + 1); chrome.search.query(
let window = {search: SEARCH_WORDS, disposition: 'NEW_WINDOW'}, () => {
windows.find(window => !initialWindowIds.includes(window.id)); chrome.windows.getAll({}, (windows) => {
assertTrue(!!window); let window = windows.find(
}); window => !initialWindowIds.includes(window.id));
assertEq(windows.length, initialWindowIds.length + 1);
assertTrue(!!window);
resolve();
});
});
}),
])
.then(() => {
succeed();
}); });
}); });
}, },
...@@ -72,7 +92,7 @@ chrome.test.runTests([ ...@@ -72,7 +92,7 @@ chrome.test.runTests([
// Display results in specified tab if said tabId is provided. // Display results in specified tab if said tabId is provided.
function QueryPopulatedTabIDValid() { function QueryPopulatedTabIDValid() {
chrome.tabs.create({}, (tab) => { chrome.tabs.create({}, (tab) => {
addTabListener(tab.id); waitForTabAndPass(tab.id);
chrome.search.query({search: SEARCH_WORDS, tabId: tab.id}); chrome.search.query({search: SEARCH_WORDS, tabId: tab.id});
}); });
}, },
...@@ -98,23 +118,29 @@ chrome.test.runTests([ ...@@ -98,23 +118,29 @@ chrome.test.runTests([
}, },
]); ]);
let addTabListener = (tabIdExpected) => { function waitForTab(tabIdExpected) {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) { return new Promise((resolve) => {
if ((tabIdExpected != -1 && tabId != tabIdExpected) || chrome.tabs.onUpdated.addListener(function listener(
changeInfo.status !== 'complete') { tabId, changeInfo, tab) {
return; // Not our tab. if ((tabIdExpected != -1 && tabId != tabIdExpected) ||
} changeInfo.status !== 'complete') {
// Note: make sure to stop listening to future events, so that this return; // Not our tab.
// doesn't affect future tests. }
chrome.tabs.onUpdated.removeListener(listener); // Note: make sure to stop listening to future events, so that this
// The tab finished loading. It should be on google (the default // doesn't affect future tests.
// search engine). chrome.tabs.onUpdated.removeListener(listener);
const hostname = new URL(tab.url).hostname; // The tab finished loading. It should be on google (the default
assertEq('www.google.com', hostname); // search engine).
succeed(); assertEq('www.google.com', new URL(tab.url).hostname);
resolve();
});
}); });
}; };
let addAnyTabListener = () => { function waitForAnyTab() {
addTabListener(-1); return waitForTab(-1);
}; };
function waitForTabAndPass(tabId) {
waitForTab(tabId).then(succeed);
}
\ No newline at end of file
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