OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var pass = chrome.test.callbackPass; | 5 var pass = chrome.test.callbackPass; |
6 var fail = chrome.test.callbackFail; | 6 var fail = chrome.test.callbackFail; |
7 var assertEq = chrome.test.assertEq; | 7 var assertEq = chrome.test.assertEq; |
8 var assertFalse = chrome.test.assertFalse; | 8 var assertFalse = chrome.test.assertFalse; |
9 var assertTrue = chrome.test.assertTrue; | 9 var assertTrue = chrome.test.assertTrue; |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 callback(win.id, newTabIds); | 28 callback(win.id, newTabIds); |
29 }); | 29 }); |
30 } | 30 } |
31 | 31 |
32 // Waits until all tabs (yes, in every window) have status "complete". | 32 // Waits until all tabs (yes, in every window) have status "complete". |
33 // This is useful to prevent test overlap when testing tab events. | 33 // This is useful to prevent test overlap when testing tab events. |
34 // |callback| should look like function() {...}. Note that |callback| expects | 34 // |callback| should look like function() {...}. Note that |callback| expects |
35 // zero arguments. | 35 // zero arguments. |
36 function waitForAllTabs(callback) { | 36 function waitForAllTabs(callback) { |
37 // Wait for all tabs to load. | 37 // Wait for all tabs to load. |
38 function waitForTabs(){ | 38 function waitForTabs() { |
39 chrome.windows.getAll({"populate": true}, function(windows) { | 39 chrome.windows.getAll({"populate": true}, function(windows) { |
40 var ready = true; | 40 var ready = true; |
41 for (var i in windows){ | 41 for (var i in windows) { |
42 for (var j in windows[i].tabs) { | 42 for (var j in windows[i].tabs) { |
43 if (windows[i].tabs[j].status != "complete") { | 43 if (windows[i].tabs[j].status != "complete") { |
44 ready = false; | 44 ready = false; |
45 break; | 45 break; |
46 } | 46 } |
47 } | 47 } |
48 if (!ready) | 48 if (!ready) |
49 break; | 49 break; |
50 } | 50 } |
51 if (ready) | 51 if (ready) |
52 callback(); | 52 callback(); |
53 else | 53 else |
54 window.setTimeout(waitForTabs, 30); | 54 window.setTimeout(waitForTabs, 30); |
55 }); | 55 }); |
56 } | 56 } |
57 waitForTabs(); | 57 waitForTabs(); |
58 } | 58 } |
| 59 |
| 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, |
| 64 pass(function(tabs) { |
| 65 var foundTabs = tabs.filter(function(tab) { |
| 66 return (tab.id == tabId); |
| 67 }); |
| 68 if (callback !== null) |
| 69 callback(foundTabs.length ? foundTabs[0] : null); |
| 70 }) |
| 71 ); |
| 72 } |
| 73 |
| 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]); |
| 91 } |
| 92 }); |
| 93 } |
| 94 if (changeInfo.hasOwnProperty(queryableAttrib)) { |
| 95 assertEq(expected, changeInfo[queryableAttrib]); |
| 96 assertEq(expected, tab[queryableAttrib]); |
| 97 var queryInfo = {}; |
| 98 queryInfo[queryableAttrib] = expected; |
| 99 queryForTab(tabId, queryInfo, pass(function(tab) { |
| 100 assertEq(expected, tab[queryableAttrib]); |
| 101 queryInfo[queryableAttrib] = !expected; |
| 102 |
| 103 queryForTab(tabId, queryInfo, pass(function(tab) { |
| 104 assertEq(null, tab); |
| 105 onUpdatedCompleted(); |
| 106 })); |
| 107 })); |
| 108 } |
| 109 } |
| 110 ); |
| 111 } |
OLD | NEW |