1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7 var assertEq = chrome.test.assertEq;
8 var assertFalse = chrome.test.assertFalse;
9 var assertTrue = chrome.test.assertTrue;
11 function pageUrl(letter) {
12 return chrome.extension.getURL(letter + ".html");
15 // Creates one window with tabs set to the urls in the array |tabUrls|.
16 // At least one url must be specified.
17 // The |callback| should look like function(windowId, tabIds) {...}.
18 function createWindow(tabUrls, winOptions, callback) {
19 winOptions["url"] = tabUrls;
20 chrome.windows.create(winOptions, function(win) {
22 assertTrue(win.id > 0);
23 assertEq(tabUrls.length, win.tabs.length);
25 for (var i = 0; i < win.tabs.length; i++)
26 newTabIds.push(win.tabs[i].id);
28 callback(win.id, newTabIds);
32 // Waits until all tabs (yes, in every window) have status "complete".
33 // This is useful to prevent test overlap when testing tab events.
34 // |callback| should look like function() {...}. Note that |callback| expects
36 function waitForAllTabs(callback) {
37 // Wait for all tabs to load.
38 function waitForTabs() {
39 chrome.windows.getAll({"populate": true}, function(windows) {
41 for (var i in windows) {
42 for (var j in windows[i].tabs) {
43 if (windows[i].tabs[j].status != "complete") {
54 window.setTimeout(waitForTabs, 30);
60 // Like chrome.tabs.query, but with the ability to filter by |tabId| as well.
61 // Returns the found tab or null
62 function queryForTab(tabId, queryInfo, callback) {
63 chrome.tabs.query(queryInfo,
65 var foundTabs = tabs.filter(function(tab) {
66 return (tab.id == tabId);
68 if (callback !== null)
69 callback(foundTabs.length ? foundTabs[0] : null);
74 // Check onUpdated for a queryable attribute such as muted or audible
75 // and then check that the tab, a query, and changeInfo are consistent
76 // with the expected value. Does similar checks for each
77 // (nonqueryable attribute, expected value) pair in nonqueryableAttribsDict
78 // except it does not check the query.
79 function onUpdatedExpect(queryableAttrib, expected, nonqueryableAttribsDict) {
80 var onUpdatedCompleted = chrome.test.listenForever(
81 chrome.tabs.onUpdated,
82 function(tabId, changeInfo, tab) {
83 if (nonqueryableAttribsDict !== null) {
84 var nonqueryableAttribs = Object.keys(nonqueryableAttribsDict);
85 nonqueryableAttribs.forEach(function(nonqueryableAttrib) {
86 if (typeof changeInfo[nonqueryableAttrib] !== "undefined") {
87 assertEq(nonqueryableAttribsDict[nonqueryableAttrib],
88 changeInfo[nonqueryableAttrib]);
89 assertEq(nonqueryableAttribsDict[nonqueryableAttrib],
90 tab[nonqueryableAttrib]);
94 if (changeInfo.hasOwnProperty(queryableAttrib)) {
95 assertEq(expected, changeInfo[queryableAttrib]);
96 assertEq(expected, tab[queryableAttrib]);
98 queryInfo[queryableAttrib] = expected;
99 queryForTab(tabId, queryInfo, pass(function(tab) {
100 assertEq(expected, tab[queryableAttrib]);
101 queryInfo[queryableAttrib] = !expected;
103 queryForTab(tabId, queryInfo, pass(function(tab) {
105 onUpdatedCompleted();