1 // Copyright (c) 2012 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 testWindowId1, testWindowId2;
7 function contains(arr, value) {
8 return arr.some(function(element) { return element == value; });
11 function checkEqualSets(set1, set2) {
12 if (set1.length != set2.length)
15 for (var x = 0; x < set1.length; x++) {
16 if (!set2.some(function(v) { return v == set1[x]; }))
23 chrome.test.runTests([
25 var tabs1 = ['http://e.com', 'http://a.com', 'http://a.com/b.html',
26 'http://b.com', 'http://a.com/d.html', 'http://a.com/c.html'];
27 var tabs2 = ['http://c.com/', 'http://a.com', 'http://a.com/b.html'];
28 chrome.windows.create({url: tabs1}, pass(function(win) {
29 testWindowId1 = win.id;
31 chrome.windows.create({url: tabs2}, pass(function(win) {
32 testWindowId2 = win.id;
36 function highlightCurrentWindow() {
37 // Check that omitting the windowId highlights the current window
38 chrome.windows.getCurrent(pass(function(win1) {
39 chrome.tabs.highlight({tabs: [0]}, pass(function(win2) {
40 assertEq(win1.id, win2.id);
45 function highlightA() {
46 chrome.tabs.query({windowId: testWindowId1, url: 'http://a.com/*'},
48 assertEq(4, tabs.length);
49 // Note: tabs.onHighlightChanged is deprecated.
50 chrome.test.listenOnce(chrome.tabs.onHighlightChanged,
51 function(highlightInfo) {
52 var tabIds = tabs.map(function(tab) { return tab.id; });
53 assertEq(highlightInfo.windowId, testWindowId1);
54 assertTrue(checkEqualSets(tabIds, highlightInfo.tabIds));
56 var tabIndices = tabs.map(function(tab) { return tab.index; });
57 chrome.tabs.highlight({
58 windowId: testWindowId1,
60 }, pass(function(win) {
61 // Verify the 'highlighted' property for every tab.
62 win.tabs.forEach(function(tab) {
63 assertEq(contains(tabIndices, tab.index), tab.highlighted);
69 function highlightB() {
70 chrome.tabs.query({windowId: testWindowId1, url: 'http://b.com/*'},
72 assertEq(1, tabs.length);
73 chrome.test.listenOnce(chrome.tabs.onHighlighted,
74 function(highlightInfo) {
75 var tabIds = tabs.map(function(tab) { return tab.id; });
76 assertEq(highlightInfo.windowId, testWindowId1);
77 assertTrue(checkEqualSets(tabIds, highlightInfo.tabIds));
79 var tabIndices = tabs.map(function(tab) { return tab.index; });
80 chrome.tabs.highlight({windowId: testWindowId1, tabs: tabIndices},
82 // Verify the 'highlighted' property for every tab.
83 win.tabs.forEach(function(tab) {
84 assertEq(contains(tabIndices, tab.index), tab.highlighted);
90 function highlightAWindow2() {
91 chrome.tabs.query({windowId: testWindowId2, url: 'http://a.com/*'},
93 assertEq(2, tabs.length);
94 chrome.test.listenOnce(chrome.tabs.onHighlighted,
95 function(highlightInfo) {
96 var tabIds = tabs.map(function(tab) { return tab.id; });
97 assertEq(highlightInfo.windowId, testWindowId2);
98 assertTrue(checkEqualSets(tabIds, highlightInfo.tabIds));
100 var tabIndices = tabs.map(function(tab) { return tab.index; });
101 chrome.tabs.highlight({windowId: testWindowId2, tabs: tabIndices},
103 // Verify the 'highlighted' property for every tab.
104 win.tabs.forEach(function(tab) {
105 assertEq(contains(tabIndices, tab.index), tab.highlighted);
108 // Verify that nothing has changed in window 1.
109 chrome.tabs.query({windowId: testWindowId1, highlighted: true},
110 pass(function(tabs) {
111 assertEq(1, tabs.length);
117 function removeTab() {
119 {windowId: testWindowId2, highlighted: true, active: false},
120 pass(function(tabs) {
121 var tabId = tabs[0].id;
122 chrome.test.listenOnce(chrome.tabs.onHighlighted,
123 function(highlightInfo) {
124 assertEq(1, highlightInfo.tabIds.length);
125 assertTrue(tabId != highlightInfo.tabIds[0]);
127 chrome.tabs.remove(tabId, pass(function() { assertTrue(true); }));
131 function noTabsHighlighted() {
132 chrome.tabs.highlight({windowId: testWindowId1, tabs: []},
133 fail("No highlighted tab"));
136 function indexNotFound() {
137 chrome.tabs.highlight({windowId: testWindowId1, tabs: [3333]},
138 fail("No tab at index: 3333."));