Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(501)

Side by Side Diff: chrome/test/data/extensions/api_test/preference/onchange_split/test.js

Issue 10559052: Split mode incognito extension can get misleading pref changed events from regular mode (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Wrote test Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Run with browser_tests --gtest_filter=ExtensionApiTest.PreferenceOnChangeSpli t
Bernhard Bauer 2012/06/26 08:44:29 I think you may need to break this line.
mitchellwrosen 2012/06/26 17:32:52 I've seen this line not broken up elsewhere (see c
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 // Context-to-string conversion for improved code readability on the C++ side.
38 function contextToString(inIncognitoContext) {
39 if (inIncognitoContext)
40 return "incognito";
41 return "regular";
42 }
43
44 chrome.test.runTests([
45 // Changing the regular settings when no incognito-specific settings are
46 // defined should fire one event in the regular window, and two in the
47 // incognito.
48 function changeDefault() {
49 var expected = [{
50 'value': false,
51 'levelOfControl': 'controlled_by_this_extension'
52 }];
53
54 if (inIncognitoContext) {
55 expected[1] = {
Bernhard Bauer 2012/06/26 08:44:29 I find expected.push() a bit nicer, as it doesn't
mitchellwrosen 2012/06/26 17:32:52 Done.
56 'value': false,
57 'incognitoSpecific': false,
58 'levelOfControl': 'controlled_by_this_extension'
59 };
60 }
61
62 listenUntil(allowCookies.onChange, expected);
63
64 sendMessage(
65 "changeDefault " + contextToString(inIncognitoContext) + " ready",
Bernhard Bauer 2012/06/26 08:44:29 You could extract this to a variable to make the c
mitchellwrosen 2012/06/26 17:32:52 Done.
66 pass(function() {
67 if (!inIncognitoContext) {
68 allowCookies.set({
69 'value': false
70 }, pass());
71 }
72 })
73 );
74 },
75
76 // Changing incognito-specific settings should only be visible to the
77 // incognito window.
78 function changeIncognitoOnly() {
79 var done;
80
81 if (!inIncognitoContext) {
82 done = listenAndFailWhen(allowCookies.onChange);
83 sendMessage("changeIncognitoOnly regular listening", pass(function() {
Bernhard Bauer 2012/06/26 08:44:29 You could directly pass in done to pass().
mitchellwrosen 2012/06/26 17:32:52 Do you mean like this? sendMessage("changeIncogni
Bernhard Bauer 2012/06/26 20:09:55 just `done` -- you don't want to call |done| immed
84 done();
85 }));
86 } else {
87 listenUntil(allowCookies.onChange, [{
88 'value': true,
89 'incognitoSpecific': true,
90 'levelOfControl': 'controlled_by_this_extension'
91 }]);
92 }
93
94 sendMessage(
95 "changeIncognitoOnly " + contextToString(inIncognitoContext) + " ready",
96 pass(function() {
97 if (inIncognitoContext) {
98 allowCookies.set({
99 'value': true,
100 'scope': 'incognito_session_only'
101 }, pass(sendMessage("changeIncognitoOnly incognito pref set",
102 pass())));
103 }
104 })
105 );
106 },
107
108 // Changing the regular settings when incognito-specific settings are
109 // defined should only be visible to the regular window.
110 function changeDefaultOnly() {
111 if (!inIncognitoContext) {
112 listenUntil(allowCookies.onChange, [{
113 'value': true,
114 'levelOfControl': 'controlled_by_this_extension'
115 }]);
116 } else {
117 var done = listenAndFailWhen(allowCookies.onChange);
118 sendMessage("changeDefaultOnly incognito listening", pass(function() {
119 done();
120 }));
121 }
122
123 sendMessage(
124 "changeDefaultOnly " + contextToString(inIncognitoContext) +
125 " ready",
Bernhard Bauer 2012/06/26 08:44:29 Nit: for string concatenation we usually align the
mitchellwrosen 2012/06/26 17:32:52 Done.
126 pass(function() {
127 if (!inIncognitoContext) {
128 allowCookies.set({
129 'value': true
130 }, pass(sendMessage("changeDefaultOnly pref set", pass())));
131 }
132 })
133 );
134 },
135
136 // Change the incognito setting back to false so that we get an event when
137 // clearing the value. Should not be visible to regular window.
138 function changeIncognitoOnlyBack() {
139 var done;
Bernhard Bauer 2012/06/26 08:44:29 You could move |done| into the if-clause.
140
141 if (!inIncognitoContext) {
142 done = listenAndFailWhen(allowCookies.onChange);
143 sendMessage("changeIncognitoOnlyBack regular listening", pass(function() {
144 done();
145 }));
146 } else {
147 listenUntil(allowCookies.onChange, [{
148 'value': false,
149 'incognitoSpecific': true,
150 'levelOfControl': 'controlled_by_this_extension'
151 }]);
152 }
153
154 sendMessage(
155 "changeIncognitoOnlyBack " + contextToString(inIncognitoContext) +
156 " ready",
157 pass(function() {
158 if (inIncognitoContext) {
159 allowCookies.set({
160 'value': false,
161 'scope': 'incognito_session_only'
162 }, pass(sendMessage("changeIncognitoOnlyBack incognito pref set",
163 pass())));
164 }
165 })
166 );
167 },
168
169 function clearIncognito() {
170 var done;
171
172 if (!inIncognitoContext) {
173 done = listenAndFailWhen(allowCookies.onChange);
174 sendMessage("clearIncognito regular listening", pass(function() {
175 done();
176 }));
177 } else {
178 listenUntil(allowCookies.onChange, [{
179 'value': true,
180 'incognitoSpecific': false,
181 'levelOfControl': 'controlled_by_this_extension'
182 }]);
183 }
184
185 sendMessage(
186 "clearIncognito " + contextToString(inIncognitoContext) + " ready",
187 pass(function() {
188 if (inIncognitoContext) {
189 allowCookies.clear({
190 'scope': 'incognito_session_only'
191 }, pass(sendMessage("clearIncognito incognito pref cleared",
192 pass())));
193 }
194 })
195 );
196 },
197
198 function clearDefault() {
199 var expected = [{
200 'value': true,
201 'levelOfControl': 'controllable_by_this_extension'
202 }];
203
204 if (inIncognitoContext) {
205 expected[1] = {
206 'value': true,
207 'incognitoSpecific': false,
208 'levelOfControl': 'controllable_by_this_extension'
209 };
210 }
211
212 listenUntil(allowCookies.onChange, expected);
213
214 sendMessage(
215 "clearDefault " + contextToString(inIncognitoContext) + " ready",
216 pass(function() {
217 if (!inIncognitoContext) {
Bernhard Bauer 2012/06/26 08:44:29 Nit: no braces for one-line statements.
mitchellwrosen 2012/06/26 17:32:52 Done.
218 allowCookies.clear({}, pass());
219 }
220 })
221 );
222 }
223 ]);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698