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.
5 var normalWindow
, normalTab
;
6 var incognitoWindow
, incognitoTab
;
8 var pass
= chrome
.test
.callbackPass
;
9 var fail
= chrome
.test
.callbackFail
;
10 var assertEq
= chrome
.test
.assertEq
;
11 var assertTrue
= chrome
.test
.assertTrue
;
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
);
21 if (windows
[0].incognito
) {
22 incognitoWindow
= windows
[0];
23 normalWindow
= windows
[1];
25 normalWindow
= windows
[0];
26 incognitoWindow
= windows
[1];
28 normalTab
= normalWindow
.tabs
[0];
29 incognitoTab
= incognitoWindow
.tabs
[0];
30 assertTrue(!normalWindow
.incognito
);
31 assertTrue(incognitoWindow
.incognito
);
35 // Tests that we can update an incognito tab and get the event for it.
36 function tabUpdate() {
37 var newUrl
= "about:blank";
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")
51 chrome
.tabs
.update(incognitoTab
.id
, {"url": newUrl
}, pass());
54 // Tests a sequence of tab API calls.
55 function tabNested() {
56 // Setup our listeners. We check that the events fire in order.
58 chrome
.test
.listenOnce(chrome
.tabs
.onCreated
, function(tab
) {
59 assertEq(1, ++eventCounter
);
60 assertEq(incognitoTab
.windowId
, tab
.windowId
);
61 assertTrue(tab
.incognito
);
63 chrome
.test
.listenOnce(chrome
.tabs
.onMoved
, function(tabId
) {
64 assertEq(2, ++eventCounter
);
66 chrome
.test
.listenOnce(chrome
.tabs
.onRemoved
, function(tabId
) {
67 assertEq(3, ++eventCounter
);
70 // Create, select, move, and close a tab in our incognito window.
71 chrome
.tabs
.create({windowId
: incognitoTab
.windowId
},
73 chrome
.tabs
.move(tab
.id
, {index
: 0},
75 assertEq(incognitoTab
.incognito
, tab
.incognito
);
76 chrome
.tabs
.remove(tab
.id
, pass());
81 // Tests content script injection to verify that the script can tell its
83 function contentScriptTestIncognito() {
84 assertTrue(!chrome
.extension
.inIncognitoContext
);
86 var testUrl
= "http://localhost:PORT/extensions/test_file.html"
87 .replace(/PORT/, config
.testServer
.port
);
89 // Test that chrome.extension.inIncognitoTab is true for incognito tabs.
90 chrome
.tabs
.create({windowId
: incognitoWindow
.id
, url
: testUrl
},
92 chrome
.tabs
.executeScript(tab
.id
,
93 {code
: 'document.title = chrome.extension.inIncognitoContext'},
95 assertEq(undefined, chrome
.runtime
.lastError
);
96 chrome
.tabs
.get(tab
.id
, pass(function(tab
) {
97 assertEq("true", tab
.title
);
102 // ... and false for normal tabs.
103 chrome
.tabs
.create({windowId
: normalWindow
.id
, url
: testUrl
},
105 chrome
.tabs
.executeScript(tab
.id
,
106 {code
: 'document.title = chrome.extension.inIncognitoContext'},
108 assertEq(undefined, chrome
.runtime
.lastError
);
109 chrome
.tabs
.get(tab
.id
, pass(function(tab
) {
110 assertEq("false", tab
.title
);
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.";
122 // Create a tab in the non-incognito window...
123 chrome
.tabs
.create({windowId
: normalWindow
.id
, url
: 'about:blank'},
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
));