OLD | NEW |
(Empty) | |
| 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 pass = chrome.test.callbackPass; |
| 6 var assertEq = chrome.test.assertEq; |
| 7 var assertTrue = chrome.test.assertTrue; |
| 8 |
| 9 var win, tab; |
| 10 var inIncognitoContext = chrome.extension.inIncognitoContext; |
| 11 |
| 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); |
| 25 } |
| 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; |
| 31 } |
| 32 |
| 33 // Listen to some events to make sure we don't get events from the other |
| 34 // profile. |
| 35 |
| 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."); |
| 42 } |
| 43 }); |
| 44 |
| 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."); |
| 52 } |
| 53 }); |
| 54 |
| 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); |
| 63 |
| 64 win = windows[0]; |
| 65 tab = win.tabs[0]; |
| 66 assertEq(inIncognitoContext, win.incognito); |
| 67 })); |
| 68 }, |
| 69 |
| 70 // Tests that we can update an incognito tab and get the event for it. |
| 71 function tabUpdate() { |
| 72 var newUrl = "about:blank"; |
| 73 |
| 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 }); |
| 83 |
| 84 // Update our tab. |
| 85 chrome.tabs.update(tab.id, {"url": newUrl}, pass()); |
| 86 }, |
| 87 |
| 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/files/extensions/test_file.html" |
| 92 .replace(/PORT/, config.testServer.port); |
| 93 |
| 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.extension.lastError); |
| 104 })); |
| 105 })); |
| 106 |
| 107 var done = chrome.test.listenForever(chrome.extension.onRequest, |
| 108 function(request, sender, sendResponse) { |
| 109 assertEq(inIncognitoContext, request.inIncognitoContext); |
| 110 sendResponse(); |
| 111 done(); |
| 112 }); |
| 113 }, |
| 114 |
| 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(); |
| 134 } |
| 135 }); |
| 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 })); |
| 146 }, |
| 147 |
| 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(); |
| 153 } |
| 154 ]); |
| 155 |
| 156 }); |
OLD | NEW |