Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / incognito / split / background.js
blob8492c1e80e27e51f1225c44e079d085eae59afac
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 pass = chrome.test.callbackPass;
6 var assertEq = chrome.test.assertEq;
7 var assertTrue = chrome.test.assertTrue;
9 var win, tab;
10 var inIncognitoContext = chrome.extension.inIncognitoContext;
12 // Lifted from the bookmarks api_test.
13 function compareNode(left, right) {
14 //chrome.test.log("compareNode()");
15 //chrome.test.log(JSON.stringify(left, null, 2));
16 //chrome.test.log(JSON.stringify(right, null, 2));
17 // TODO(erikkay): do some comparison of dateAdded
18 if (left.id != right.id)
19 return "id mismatch: " + left.id + " != " + right.id;
20 if (left.title != right.title) {
21 // TODO(erikkay): This resource dependency still isn't working reliably.
22 // See bug 19866.
23 // return "title mismatch: " + left.title + " != " + right.title;
24 chrome.test.log("title mismatch: " + left.title + " != " + right.title);
26 if (left.url != right.url)
27 return "url mismatch: " + left.url + " != " + right.url;
28 if (left.index != right.index)
29 return "index mismatch: " + left.index + " != " + right.index;
30 return true;
33 // Listen to some events to make sure we don't get events from the other
34 // profile.
36 chrome.tabs.onUpdated.addListener(function(id, info, tab) {
37 if (inIncognitoContext != tab.incognito) {
38 chrome.test.notifyFail(
39 "[FAIL] Split-mode incognito test received an event for " +
40 (tab.incognito ? "an incognito" : "a normal") +
41 " tab in the wrong profile.");
43 });
45 chrome.extension.onRequest.addListener(
46 function(request, sender, sendResponse) {
47 if (inIncognitoContext != sender.tab.incognito) {
48 chrome.test.notifyFail(
49 "[FAIL] Split-mode incognito test received a message from " +
50 (sender.tab.incognito ? "an incognito" : "a normal") +
51 " tab in the wrong profile.");
53 });
55 chrome.test.getConfig(function(config) {
56 chrome.test.runTests([
57 function setupWindows() {
58 // The test harness should have set us up with 2 windows: 1 incognito
59 // and 1 regular. Since we are in split mode, we should only see the
60 // window for our profile.
61 chrome.windows.getAll({populate: true}, pass(function(windows) {
62 assertEq(1, windows.length);
64 win = windows[0];
65 tab = win.tabs[0];
66 assertEq(inIncognitoContext, win.incognito);
67 }));
70 // Tests that we can update an incognito tab and get the event for it.
71 function tabUpdate() {
72 var newUrl = "about:blank";
74 // Prepare the event listeners first.
75 var done = chrome.test.listenForever(chrome.tabs.onUpdated,
76 function(id, info, tab) {
77 assertEq(tab.id, id);
78 assertEq(inIncognitoContext, tab.incognito);
79 assertEq(newUrl, tab.url);
80 if (info.status == "complete")
81 done();
82 });
84 // Update our tab.
85 chrome.tabs.update(tab.id, {"url": newUrl}, pass());
88 // Tests content script injection to verify that the script can tell its
89 // in incongnito.
90 function contentScriptTestIncognito() {
91 var testUrl = "http://localhost:PORT/extensions/test_file.html"
92 .replace(/PORT/, config.testServer.port);
94 // Test that chrome.extension.inIncognitoContext is true for incognito
95 // tabs.
96 chrome.tabs.create({windowId: win.id, url: testUrl},
97 pass(function(tab) {
98 chrome.tabs.executeScript(tab.id,
99 {code: 'chrome.extension.sendRequest({' +
100 ' inIncognitoContext: chrome.extension.inIncognitoContext' +
101 '});'},
102 pass(function() {
103 assertEq(undefined, chrome.runtime.lastError);
104 }));
105 }));
107 var done = chrome.test.listenForever(chrome.extension.onRequest,
108 function(request, sender, sendResponse) {
109 assertEq(inIncognitoContext, request.inIncognitoContext);
110 sendResponse();
111 done();
115 // Tests that we can receive bookmarks events in both extension processes.
116 function bookmarkCreate() {
117 // Each process will create 1 bookmark, but expects to see updates from
118 // the other process.
119 var nodeNormal = {parentId:"1", title:"normal", url:"http://google.com/"};
120 var nodeIncog = {parentId:"1", title:"incog", url:"http://google.com/"};
121 var node = inIncognitoContext ? nodeIncog : nodeNormal;
122 var count = 0;
123 var done = chrome.test.listenForever(chrome.bookmarks.onCreated,
124 function(id, created) {
125 node = (created.title == nodeNormal.title) ? nodeNormal : nodeIncog;
126 node.id = created.id;
127 node.index = created.index;
128 chrome.test.assertEq(id, node.id);
129 chrome.test.assertTrue(compareNode(node, created));
130 if (++count == 2) {
131 chrome.test.log("Bookmarks created. Incognito=" +
132 inIncognitoContext);
133 done();
136 var message =
137 inIncognitoContext ? "waiting_incognito" : "waiting";
138 chrome.test.sendMessage(message, pass(function() {
139 chrome.bookmarks.create(node, pass(function(results) {
140 node.id = results.id; // since we couldn't know this going in
141 node.index = results.index;
142 chrome.test.assertTrue(compareNode(node, results),
143 "created node != source");
144 }));
145 }));
148 // Tests that we can set cookies in both processes.
149 function setDocumentCookie() {
150 document.cookie = "k=v";
151 chrome.test.assertTrue(document.cookie.indexOf("k=v") != -1);
152 chrome.test.succeed();