- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / tabs / basics / highlight.js
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.
4
5 var testWindowId1, testWindowId2;
6
7 function contains(arr, value) {
8   return arr.some(function(element) { return element == value; });
9 }
10
11 function checkEqualSets(set1, set2) {
12   if (set1.length != set2.length)
13     return false;
14
15   for (var x = 0; x < set1.length; x++) {
16     if (!set2.some(function(v) { return v == set1[x]; }))
17       return false;
18   }
19
20   return true;
21 }
22
23 chrome.test.runTests([
24   function setup() {
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;
30     }));
31     chrome.windows.create({url: tabs2}, pass(function(win) {
32       testWindowId2 = win.id;
33     }));
34   },
35
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);
41       }));
42     }));
43   },
44
45   function highlightA() {
46     chrome.tabs.query({windowId: testWindowId1, url: 'http://a.com/*'},
47                       pass(function(tabs) {
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));
55       });
56       var tabIndices = tabs.map(function(tab) { return tab.index; });
57       chrome.tabs.highlight({
58         windowId: testWindowId1,
59         tabs: tabIndices
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);
64         });
65       }));
66     }));
67   },
68
69   function highlightB() {
70     chrome.tabs.query({windowId: testWindowId1, url: 'http://b.com/*'},
71                       pass(function(tabs) {
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));
78       });
79       var tabIndices = tabs.map(function(tab) { return tab.index; });
80       chrome.tabs.highlight({windowId: testWindowId1, tabs: tabIndices},
81                          pass(function(win) {
82         // Verify the 'highlighted' property for every tab.
83         win.tabs.forEach(function(tab) {
84           assertEq(contains(tabIndices, tab.index), tab.highlighted);
85         });
86       }));
87     }));
88   },
89
90   function highlightAWindow2() {
91     chrome.tabs.query({windowId: testWindowId2, url: 'http://a.com/*'},
92                       pass(function(tabs) {
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));
99       });
100       var tabIndices = tabs.map(function(tab) { return tab.index; });
101       chrome.tabs.highlight({windowId: testWindowId2, tabs: tabIndices},
102                          pass(function(win) {
103         // Verify the 'highlighted' property for every tab.
104         win.tabs.forEach(function(tab) {
105           assertEq(contains(tabIndices, tab.index), tab.highlighted);
106         });
107
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);
112         }));
113       }));
114     }));
115   },
116
117   function removeTab() {
118     chrome.tabs.query(
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]);
126       });
127       chrome.tabs.remove(tabId, pass(function() { assertTrue(true); }));
128     }));
129   },
130
131   function noTabsHighlighted() {
132     chrome.tabs.highlight({windowId: testWindowId1, tabs: []},
133                        fail("No highlighted tab"));
134   },
135
136   function indexNotFound() {
137     chrome.tabs.highlight({windowId: testWindowId1, tabs: [3333]},
138                        fail("No tab at index: 3333."));
139   }
140 ]);
141