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