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.
5 // Content settings API test
6 // browser_tests.exe --gtest_filter=ExtensionApiTest.PreferenceOnChangeSplit
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;
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)
24 chrome.test.fail("Unexpected event: " + JSON.stringify(value));
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));
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;
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
49 function changeDefault() {
52 'levelOfControl': 'controlled_by_this_extension'
55 if (inIncognitoContext) {
58 'incognitoSpecific': false,
59 'levelOfControl': 'controlled_by_this_extension'
63 listenUntil(allowCookies.onChange, expected);
65 sendMessage(constructMessage("ready"), pass(function() {
66 if (!inIncognitoContext) {
74 // Changing incognito-specific settings should only be visible to the
76 function changeIncognitoOnly() {
77 if (!inIncognitoContext) {
78 var done = listenAndFailWhen(allowCookies.onChange);
79 sendMessage(constructMessage("listening"), done);
81 listenUntil(allowCookies.onChange, [{
83 'incognitoSpecific': true,
84 'levelOfControl': 'controlled_by_this_extension'
88 sendMessage(constructMessage("ready"), pass(function() {
89 if (inIncognitoContext) {
92 'scope': 'incognito_session_only'
93 }, pass(sendMessage(constructMessage("pref set", "changeIncognitoOnly"),
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, [{
105 'levelOfControl': 'controlled_by_this_extension'
108 var done = listenAndFailWhen(allowCookies.onChange);
109 sendMessage(constructMessage("listening"), done);
112 sendMessage(constructMessage("ready"), pass(function() {
113 if (!inIncognitoContext) {
116 }, pass(sendMessage(constructMessage("pref set", "changeDefaultOnly"),
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);
129 listenUntil(allowCookies.onChange, [{
131 'incognitoSpecific': true,
132 'levelOfControl': 'controlled_by_this_extension'
136 sendMessage(constructMessage("ready"), pass(function() {
137 if (inIncognitoContext) {
140 'scope': 'incognito_session_only'
141 }, pass(sendMessage(constructMessage("pref set",
142 "changeIncognitoOnlyBack"),
148 function clearIncognito() {
149 if (!inIncognitoContext) {
150 var done = listenAndFailWhen(allowCookies.onChange);
151 sendMessage(constructMessage("listening"), done);
153 listenUntil(allowCookies.onChange, [{
155 'incognitoSpecific': false,
156 'levelOfControl': 'controlled_by_this_extension'
160 sendMessage(constructMessage("ready"), pass(function() {
161 if (inIncognitoContext) {
163 'scope': 'incognito_session_only'
164 }, pass(sendMessage(constructMessage("pref cleared", "clearIncognito"),
170 function clearDefault() {
173 'levelOfControl': 'controllable_by_this_extension'
176 if (inIncognitoContext) {
179 'incognitoSpecific': false,
180 'levelOfControl': 'controllable_by_this_extension'
184 listenUntil(allowCookies.onChange, expected);
186 sendMessage(constructMessage("ready"), pass(function() {
187 if (!inIncognitoContext)
188 allowCookies.clear({}, pass());