- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / tabs / on_removed / tabs_util.js
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.
4
5 // Utility functions to help with tabs/windows testing.
6
7 // Removes current windows and creates one window with tabs set to
8 // the urls in the array |tabUrls|. At least one url must be specified.
9 // The |callback| should look like function(windowId, tabIds) {...}.
10 function setupWindow(tabUrls, callback) {
11   createWindow(tabUrls, {}, function(winId, tabIds) {
12     // Remove all other windows.
13     var removedCount = 0;
14     chrome.windows.getAll({}, function(windows) {
15       for (var i in windows) {
16         if (windows[i].id != winId) {
17           chrome.windows.remove(windows[i].id, function() {
18             removedCount++;
19             if (removedCount == windows.length - 1)
20               callback(winId, tabIds);
21           });
22         }
23       }
24       if (windows.length == 1)
25         callback(winId, tabIds);
26     });
27   });
28 }
29
30 // Creates one window with tabs set to the urls in the array |tabUrls|.
31 // At least one url must be specified.
32 // The |callback| should look like function(windowId, tabIds) {...}.
33 function createWindow(tabUrls, winOptions, callback) {
34   winOptions["url"] = tabUrls;
35   chrome.windows.create(winOptions, function(win) {
36     var newTabIds = [];
37     assertTrue(win.id > 0);
38     assertEq(tabUrls.length, win.tabs.length);
39
40     for (var i = 0; i < win.tabs.length; i++)
41       newTabIds.push(win.tabs[i].id);
42
43     callback(win.id, newTabIds);
44   });
45 }
46
47 // Waits until all tabs (yes, in every window) have status "complete".
48 // This is useful to prevent test overlap when testing tab events.
49 // |callback| should look like function() {...}.  Note that |callback| expects
50 // zero arguments.
51 function waitForAllTabs(callback) {
52   // Wait for all tabs to load.
53   function waitForTabs(){
54     chrome.windows.getAll({"populate": true}, function(windows) {
55       var ready = true;
56       for (var i in windows){
57         for (var j in windows[i].tabs) {
58           if (windows[i].tabs[j].status != "complete") {
59             ready = false;
60             break;
61           }
62         }
63         if (!ready)
64           break;
65       }
66       if (ready)
67         callback();
68       else
69         window.setTimeout(waitForTabs, 30);
70     });
71   }
72   waitForTabs();
73 }
74