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,15 +41,25 @@ chrome.test.runTests([ ...@@ -40,15 +41,25 @@ 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
.all([
waitForAnyTab(),
new Promise(resolve => {
chrome.search.query( chrome.search.query(
{search: SEARCH_WORDS, disposition: 'NEW_TAB'}, () => { {search: SEARCH_WORDS, disposition: 'NEW_TAB'}, () => {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => { chrome.tabs.query(
{active: true, currentWindow: true}, (tabs) => {
assertEq(1, tabs.length); assertEq(1, tabs.length);
// A new tab should have been created. // A new tab should have been created.
assertFalse(initialTabIds.includes(tabs[0].id)); assertFalse(initialTabIds.includes(tabs[0].id));
resolve();
}); });
}); });
}),
])
.then(() => {
succeed();
});
}); });
}, },
...@@ -56,23 +67,32 @@ chrome.test.runTests([ ...@@ -56,23 +67,32 @@ 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
.all([
waitForAnyTab(),
new Promise((resolve) => {
chrome.search.query( chrome.search.query(
{search: SEARCH_WORDS, disposition: 'NEW_WINDOW'}, () => { {search: SEARCH_WORDS, disposition: 'NEW_WINDOW'}, () => {
chrome.windows.getAll({}, (windows) => { chrome.windows.getAll({}, (windows) => {
let window = windows.find(
window => !initialWindowIds.includes(window.id));
assertEq(windows.length, initialWindowIds.length + 1); assertEq(windows.length, initialWindowIds.length + 1);
let window =
windows.find(window => !initialWindowIds.includes(window.id));
assertTrue(!!window); assertTrue(!!window);
resolve();
}); });
}); });
}),
])
.then(() => {
succeed();
});
}); });
}, },
// 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,8 +118,10 @@ chrome.test.runTests([ ...@@ -98,8 +118,10 @@ chrome.test.runTests([
}, },
]); ]);
let addTabListener = (tabIdExpected) => { function waitForTab(tabIdExpected) {
chrome.tabs.onUpdated.addListener(function listener(tabId, changeInfo, tab) { return new Promise((resolve) => {
chrome.tabs.onUpdated.addListener(function listener(
tabId, changeInfo, tab) {
if ((tabIdExpected != -1 && tabId != tabIdExpected) || if ((tabIdExpected != -1 && tabId != tabIdExpected) ||
changeInfo.status !== 'complete') { changeInfo.status !== 'complete') {
return; // Not our tab. return; // Not our tab.
...@@ -109,12 +131,16 @@ let addTabListener = (tabIdExpected) => { ...@@ -109,12 +131,16 @@ let addTabListener = (tabIdExpected) => {
chrome.tabs.onUpdated.removeListener(listener); chrome.tabs.onUpdated.removeListener(listener);
// The tab finished loading. It should be on google (the default // The tab finished loading. It should be on google (the default
// search engine). // search engine).
const hostname = new URL(tab.url).hostname; assertEq('www.google.com', new URL(tab.url).hostname);
assertEq('www.google.com', hostname); resolve();
succeed(); });
}); });
}; };
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