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