- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / tabs / basics / query.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 testWindowId;
6 var active_tabs = [];
7 var highlighted_tabs = [];
8 var window_tabs = [];
9 var pinned_tabs = [];
10 var active_and_window_tabs = [];
11
12 chrome.test.runTests([
13   function setup() {
14     var tabs = ['http://example.org/a.html', 'http://google.com'];
15     chrome.windows.create({url: tabs}, pass(function(window) {
16       assertEq(2, window.tabs.length);
17       testWindowId = window.id;
18       chrome.tabs.create({
19         windowId: testWindowId,
20         url: 'about:blank',
21         pinned: true
22       }, pass(function(tab) {
23         assertTrue(tab.pinned);
24         assertEq(testWindowId, tab.windowId);
25       }));
26     }));
27   },
28
29   function queryAll() {
30     chrome.tabs.query({}, pass(function(tabs) {
31       assertEq(4, tabs.length);
32       for (var x = 0; x < tabs.length; x++) {
33         if (tabs[x].highlighted)
34           highlighted_tabs.push(tabs[x]);
35         if (tabs[x].active)
36           active_tabs.push(tabs[x]);
37         if (tabs[x].windowId == testWindowId) {
38           window_tabs.push(tabs[x]);
39           if (tabs[x].active)
40             active_and_window_tabs.push(tabs[x]);
41         }
42         if (tabs[x].pinned)
43           pinned_tabs.push(tabs[x]);
44       }
45     }));
46   },
47
48   function queryHighlighted() {
49     chrome.tabs.query({highlighted:true}, pass(function(tabs) {
50       assertEq(highlighted_tabs.length, tabs.length);
51       for (var x = 0; x < tabs.length; x++)
52         assertTrue(tabs[x].highlighted);
53     }));
54     chrome.tabs.query({highlighted:false}, pass(function(tabs) {
55       assertEq(4-highlighted_tabs.length, tabs.length);
56       for (var x = 0; x < tabs.length; x++)
57         assertFalse(tabs[x].highlighted);
58     }));
59   },
60
61   function queryActive() {
62     chrome.tabs.query({active: true}, pass(function(tabs) {
63       assertEq(active_tabs.length, tabs.length);
64       for (var x = 0; x < tabs.length; x++)
65         assertTrue(tabs[x].active);
66     }));
67     chrome.tabs.query({active: false}, pass(function(tabs) {
68       assertEq(4-active_tabs.length, tabs.length);
69       for (var x = 0; x < tabs.length; x++)
70         assertFalse(tabs[x].active);
71     }));
72   },
73
74   function queryWindowID() {
75     chrome.tabs.query({windowId: testWindowId}, pass(function(tabs) {
76       assertEq(window_tabs.length, tabs.length);
77       for (var x = 0; x < tabs.length; x++)
78         assertEq(testWindowId, tabs[x].windowId);
79     }));
80   },
81
82   function queryCurrentWindow() {
83     chrome.windows.getCurrent(function(win) {
84       chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT},
85                         pass(function(tabs) {
86         // The current window should only contain this test page.
87         assertEq(1, tabs.length);
88         assertEq(win.id, tabs[0].windowId);
89         assertEq(location.href, tabs[0].url);
90         assertEq(location.href, tabs[0].title);
91       }));
92     });
93   },
94
95   function queryPinned() {
96     chrome.tabs.query({pinned: true}, pass(function(tabs) {
97       assertEq(pinned_tabs.length, tabs.length);
98       for (var x = 0; x < tabs.length; x++)
99         assertTrue(tabs[x].pinned);
100     }));
101     chrome.tabs.query({pinned: false}, pass(function(tabs) {
102       assertEq(4-pinned_tabs.length, tabs.length);
103       for (var x = 0; x < tabs.length; x++)
104         assertFalse(tabs[x].pinned);
105     }));
106   },
107
108   function queryActiveAndWindowID() {
109     chrome.tabs.query({
110       active: true,
111       windowId: testWindowId
112     }, pass(function(tabs) {
113       assertEq(active_and_window_tabs.length, tabs.length);
114       for (var x = 0; x < tabs.length; x++) {
115         assertTrue(tabs[x].active);
116         assertEq(testWindowId, tabs[x].windowId);
117       }
118     }));
119   },
120
121   function queryUrl() {
122     chrome.tabs.query({url: "http://*.example.org/*"}, pass(function(tabs) {
123       assertEq(1, tabs.length);
124       assertEq("http://example.org/a.html", tabs[0].url);
125     }));
126   },
127
128   function queryStatus() {
129     chrome.tabs.query({status: "complete"}, pass(function(tabs) {
130       for (var x = 0; x < tabs.length; x++)
131         assertEq("complete", tabs[x].status);
132     }));
133   },
134
135   function queryTitle() {
136     chrome.tabs.query({title: "*query.html"}, pass(function(tabs) {
137       assertEq(1, tabs.length);
138       assertEq(chrome.extension.getURL("query.html"), tabs[0].title);
139     }));
140   },
141
142   function queryWindowType() {
143     chrome.tabs.query({windowType: "normal"}, pass(function(tabs) {
144       assertEq(4, tabs.length);
145       for (var x = 0; x < tabs.length; x++) {
146         chrome.windows.get(tabs[x].windowId, pass(function(win) {
147           assertTrue(win.type == "normal");
148           assertEq(false, win.alwaysOnTop);
149         }));
150       }
151     }));
152     chrome.windows.create({
153       url: 'about:blank',
154       type: 'popup'
155     }, pass(function(win) {
156       chrome.windows.create({
157         url: 'about:blank',
158         type: 'popup'
159       }, pass(function(win) {
160         chrome.tabs.query({
161           windowId: win.id,
162           windowType: 'popup'
163         }, pass(function(tabs) {
164           assertEq(1, tabs.length);
165         }));
166         chrome.tabs.query({windowType: 'popup'}, pass(function(tabs) {
167           assertEq(2, tabs.length);
168         }));
169         chrome.tabs.query({
170           windowType: 'popup',
171           url: 'about:*'
172         }, pass(function(tabs) {
173           assertEq(2, tabs.length);
174         }));
175       }));
176     }));
177   },
178
179   function queryIndex() {
180     chrome.tabs.query({index: 0}, pass(function(tabs) {
181       // Each of the 4 windows should have a tab at index 0.
182       assertEq(4, tabs.length);
183       for (var i = 0; i < tabs.length; i++)
184         assertEq(0, tabs[0].index);
185     }));
186   },
187
188   function queryIncognito() {
189     chrome.windows.create(
190         {url: ['http://a.com', 'http://a.com'], incognito: true},
191         pass(function(win) {
192       assertEq(null, win);
193       chrome.tabs.query({url: 'http://a.com/'}, pass(function(tabs) {
194          assertEq(0, tabs.length);
195       }));
196     }));
197   }
198 ]);
199