| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 // Content settings API test |
| 6 // browser_tests.exe --gtest_filter=ExtensionApiTest.PreferenceOnChangeSplit |
| 7 |
| 8 var inIncognitoContext = chrome.extension.inIncognitoContext; |
| 9 var pass = chrome.test.callbackPass; |
| 10 var sendMessage = chrome.test.sendMessage; |
| 11 var allowCookies = chrome.privacy.websites.thirdPartyCookiesAllowed; |
| 12 |
| 13 // Listen until |event| has fired with all of the values in |expected|. |
| 14 function listenUntil(event, expected) { |
| 15 var done = chrome.test.listenForever(event, function(value) { |
| 16 for (var i = 0; i < expected.length; i++) { |
| 17 if (chrome.test.checkDeepEq(expected[i], value)) { |
| 18 expected.splice(i, 1); |
| 19 if (expected.length == 0) |
| 20 done(); |
| 21 return; |
| 22 } |
| 23 } |
| 24 chrome.test.fail("Unexpected event: " + JSON.stringify(value)); |
| 25 }); |
| 26 } |
| 27 |
| 28 // Fail if |event| is fired (with any values). Because listenUntil stops |
| 29 // listening when |event| has fired with all the values in |expected|, it may |
| 30 // not capture superfluous unexpected events. |
| 31 function listenAndFailWhen(event) { |
| 32 return chrome.test.listenForever(event, function(value) { |
| 33 chrome.test.fail("Unexpected event: " + JSON.stringify(value)); |
| 34 }); |
| 35 } |
| 36 |
| 37 // Constructs messages to be sent via chrome.test.sendMessage |
| 38 function constructMessage(str, caller) { |
| 39 caller = caller || arguments.callee.caller.name; |
| 40 var incognitoStr = inIncognitoContext ? " incognito " : " regular "; |
| 41 console.log(caller + incognitoStr + str); |
| 42 return caller + incognitoStr + str; |
| 43 } |
| 44 |
| 45 chrome.test.runTests([ |
| 46 // Changing the regular settings when no incognito-specific settings are |
| 47 // defined should fire one event in the regular window, and two in the |
| 48 // incognito. |
| 49 function changeDefault() { |
| 50 var expected = [{ |
| 51 'value': false, |
| 52 'levelOfControl': 'controlled_by_this_extension' |
| 53 }]; |
| 54 |
| 55 if (inIncognitoContext) { |
| 56 expected.push({ |
| 57 'value': false, |
| 58 'incognitoSpecific': false, |
| 59 'levelOfControl': 'controlled_by_this_extension' |
| 60 }); |
| 61 } |
| 62 |
| 63 listenUntil(allowCookies.onChange, expected); |
| 64 |
| 65 sendMessage(constructMessage("ready"), pass(function() { |
| 66 if (!inIncognitoContext) { |
| 67 allowCookies.set({ |
| 68 'value': false |
| 69 }, pass()); |
| 70 } |
| 71 })); |
| 72 }, |
| 73 |
| 74 // Changing incognito-specific settings should only be visible to the |
| 75 // incognito window. |
| 76 function changeIncognitoOnly() { |
| 77 if (!inIncognitoContext) { |
| 78 var done = listenAndFailWhen(allowCookies.onChange); |
| 79 sendMessage(constructMessage("listening"), done); |
| 80 } else { |
| 81 listenUntil(allowCookies.onChange, [{ |
| 82 'value': true, |
| 83 'incognitoSpecific': true, |
| 84 'levelOfControl': 'controlled_by_this_extension' |
| 85 }]); |
| 86 } |
| 87 |
| 88 sendMessage(constructMessage("ready"), pass(function() { |
| 89 if (inIncognitoContext) { |
| 90 allowCookies.set({ |
| 91 'value': true, |
| 92 'scope': 'incognito_session_only' |
| 93 }, pass(sendMessage(constructMessage("pref set", "changeIncognitoOnly"), |
| 94 pass()))); |
| 95 } |
| 96 })); |
| 97 }, |
| 98 |
| 99 // Changing the regular settings when incognito-specific settings are |
| 100 // defined should only be visible to the regular window. |
| 101 function changeDefaultOnly() { |
| 102 if (!inIncognitoContext) { |
| 103 listenUntil(allowCookies.onChange, [{ |
| 104 'value': true, |
| 105 'levelOfControl': 'controlled_by_this_extension' |
| 106 }]); |
| 107 } else { |
| 108 var done = listenAndFailWhen(allowCookies.onChange); |
| 109 sendMessage(constructMessage("listening"), done); |
| 110 } |
| 111 |
| 112 sendMessage(constructMessage("ready"), pass(function() { |
| 113 if (!inIncognitoContext) { |
| 114 allowCookies.set({ |
| 115 'value': true |
| 116 }, pass(sendMessage(constructMessage("pref set", "changeDefaultOnly"), |
| 117 pass()))); |
| 118 } |
| 119 })); |
| 120 }, |
| 121 |
| 122 // Change the incognito setting back to false so that we get an event when |
| 123 // clearing the value. Should not be visible to regular window. |
| 124 function changeIncognitoOnlyBack() { |
| 125 if (!inIncognitoContext) { |
| 126 var done = listenAndFailWhen(allowCookies.onChange); |
| 127 sendMessage(constructMessage("listening"), done); |
| 128 } else { |
| 129 listenUntil(allowCookies.onChange, [{ |
| 130 'value': false, |
| 131 'incognitoSpecific': true, |
| 132 'levelOfControl': 'controlled_by_this_extension' |
| 133 }]); |
| 134 } |
| 135 |
| 136 sendMessage(constructMessage("ready"), pass(function() { |
| 137 if (inIncognitoContext) { |
| 138 allowCookies.set({ |
| 139 'value': false, |
| 140 'scope': 'incognito_session_only' |
| 141 }, pass(sendMessage(constructMessage("pref set", |
| 142 "changeIncognitoOnlyBack"), |
| 143 pass()))); |
| 144 } |
| 145 })); |
| 146 }, |
| 147 |
| 148 function clearIncognito() { |
| 149 if (!inIncognitoContext) { |
| 150 var done = listenAndFailWhen(allowCookies.onChange); |
| 151 sendMessage(constructMessage("listening"), done); |
| 152 } else { |
| 153 listenUntil(allowCookies.onChange, [{ |
| 154 'value': true, |
| 155 'incognitoSpecific': false, |
| 156 'levelOfControl': 'controlled_by_this_extension' |
| 157 }]); |
| 158 } |
| 159 |
| 160 sendMessage(constructMessage("ready"), pass(function() { |
| 161 if (inIncognitoContext) { |
| 162 allowCookies.clear({ |
| 163 'scope': 'incognito_session_only' |
| 164 }, pass(sendMessage(constructMessage("pref cleared", "clearIncognito"), |
| 165 pass()))); |
| 166 } |
| 167 })); |
| 168 }, |
| 169 |
| 170 function clearDefault() { |
| 171 var expected = [{ |
| 172 'value': true, |
| 173 'levelOfControl': 'controllable_by_this_extension' |
| 174 }]; |
| 175 |
| 176 if (inIncognitoContext) { |
| 177 expected[1] = { |
| 178 'value': true, |
| 179 'incognitoSpecific': false, |
| 180 'levelOfControl': 'controllable_by_this_extension' |
| 181 }; |
| 182 } |
| 183 |
| 184 listenUntil(allowCookies.onChange, expected); |
| 185 |
| 186 sendMessage(constructMessage("ready"), pass(function() { |
| 187 if (!inIncognitoContext) |
| 188 allowCookies.clear({}, pass()); |
| 189 })); |
| 190 } |
| 191 ]); |
| OLD | NEW |