- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / incognito / apis / background.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 var normalWindow, normalTab;
6 var incognitoWindow, incognitoTab;
7
8 var pass = chrome.test.callbackPass;
9 var fail = chrome.test.callbackFail;
10 var assertEq = chrome.test.assertEq;
11 var assertTrue = chrome.test.assertTrue;
12
13 chrome.test.getConfig(function(config) {
14   chrome.test.runTests([
15     function setupWindows() {
16       // The test harness should have set us up with 2 windows: 1 incognito
17       // and 1 regular. Verify that we can see both when we ask for it.
18       chrome.windows.getAll({populate: true}, pass(function(windows) {
19         assertEq(2, windows.length);
20
21         if (windows[0].incognito) {
22           incognitoWindow = windows[0];
23           normalWindow = windows[1];
24         } else {
25           normalWindow = windows[0];
26           incognitoWindow = windows[1];
27         }
28         normalTab = normalWindow.tabs[0];
29         incognitoTab = incognitoWindow.tabs[0];
30         assertTrue(!normalWindow.incognito);
31         assertTrue(incognitoWindow.incognito);
32       }));
33     },
34
35     // Tests that we can update an incognito tab and get the event for it.
36     function tabUpdate() {
37       var newUrl = "about:blank";
38
39       // Prepare the event listeners first.
40       var done = chrome.test.listenForever(chrome.tabs.onUpdated,
41         function(id, info, tab) {
42           if (id == incognitoTab.id) {
43             assertTrue(tab.incognito);
44             assertEq(newUrl, tab.url);
45             if (info.status == "complete")
46               done();
47           }
48         });
49
50       // Update our tabs.
51       chrome.tabs.update(incognitoTab.id, {"url": newUrl}, pass());
52     },
53
54     // Tests a sequence of tab API calls.
55     function tabNested() {
56       // Setup our listeners. We check that the events fire in order.
57       var eventCounter = 0;
58       chrome.test.listenOnce(chrome.tabs.onCreated, function(tab) {
59         assertEq(1, ++eventCounter);
60         assertEq(incognitoTab.windowId, tab.windowId);
61         assertTrue(tab.incognito);
62       });
63       chrome.test.listenOnce(chrome.tabs.onMoved, function(tabId) {
64         assertEq(2, ++eventCounter);
65       });
66       chrome.test.listenOnce(chrome.tabs.onRemoved, function(tabId) {
67         assertEq(3, ++eventCounter);
68       });
69
70       // Create, select, move, and close a tab in our incognito window.
71       chrome.tabs.create({windowId: incognitoTab.windowId},
72         pass(function(tab) {
73           chrome.tabs.move(tab.id, {index: 0},
74             pass(function(tab) {
75               assertEq(incognitoTab.incognito, tab.incognito);
76               chrome.tabs.remove(tab.id, pass());
77             }));
78         }));
79     },
80
81     // Tests content script injection to verify that the script can tell its
82     // in incongnito.
83     function contentScriptTestIncognito() {
84       assertTrue(!chrome.extension.inIncognitoContext);
85
86       var testUrl = "http://localhost:PORT/extensions/test_file.html"
87           .replace(/PORT/, config.testServer.port);
88
89       // Test that chrome.extension.inIncognitoTab is true for incognito tabs.
90       chrome.tabs.create({windowId: incognitoWindow.id, url: testUrl},
91         pass(function(tab) {
92           chrome.tabs.executeScript(tab.id,
93             {code: 'document.title = chrome.extension.inIncognitoContext'},
94             pass(function() {
95               assertEq(undefined, chrome.runtime.lastError);
96               chrome.tabs.get(tab.id, pass(function(tab) {
97                   assertEq("true", tab.title);
98                 }));
99             }));
100         }));
101
102       // ... and false for normal tabs.
103       chrome.tabs.create({windowId: normalWindow.id, url: testUrl},
104         pass(function(tab) {
105           chrome.tabs.executeScript(tab.id,
106             {code: 'document.title = chrome.extension.inIncognitoContext'},
107             pass(function() {
108               assertEq(undefined, chrome.runtime.lastError);
109               chrome.tabs.get(tab.id, pass(function(tab) {
110                   assertEq("false", tab.title);
111                 }));
112             }));
113         }));
114     },
115
116     // Tests that extensions can't move tabs between incognito and
117     // non-incognito windows.
118     function moveTabBetweenProfiles() {
119       var errorMsg = "Tabs can only be moved between " +
120                        "windows in the same profile.";
121
122       // Create a tab in the non-incognito window...
123       chrome.tabs.create({windowId: normalWindow.id, url: 'about:blank'},
124         pass(function(tab) {
125           // ... and then try to move it to the incognito window.
126           chrome.tabs.move(tab.id,
127             {windowId: incognitoWindow.id, index: 0}, fail(errorMsg));
128         }));
129     }
130   ]);
131 });