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 #include "base/location.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/single_thread_task_runner.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/net/prediction_options.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "components/content_settings/core/common/pref_names.h"
20 #include "components/translate/core/common/translate_pref_names.h"
21 #include "content/public/browser/notification_service.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/test_extension_registry_observer.h"
24 #include "extensions/test/extension_test_message_listener.h"
25 #include "extensions/test/result_catcher.h"
29 void ReleaseBrowserProcessModule() {
30 g_browser_process
->ReleaseModule();
35 class ExtensionPreferenceApiTest
: public ExtensionApiTest
{
37 ExtensionPreferenceApiTest() : profile_(NULL
) {}
39 void CheckPreferencesSet() {
40 PrefService
* prefs
= profile_
->GetPrefs();
41 const PrefService::Preference
* pref
= prefs
->FindPreference(
42 prefs::kBlockThirdPartyCookies
);
44 EXPECT_TRUE(pref
->IsExtensionControlled());
45 EXPECT_TRUE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
46 EXPECT_TRUE(prefs
->GetBoolean(autofill::prefs::kAutofillEnabled
));
47 EXPECT_FALSE(prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
48 EXPECT_TRUE(prefs
->GetBoolean(prefs::kEnableHyperlinkAuditing
));
49 EXPECT_TRUE(prefs
->GetBoolean(prefs::kEnableReferrers
));
50 EXPECT_TRUE(prefs
->GetBoolean(prefs::kEnableTranslate
));
51 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_DEFAULT
,
52 prefs
->GetInteger(prefs::kNetworkPredictionOptions
));
53 EXPECT_TRUE(prefs
->GetBoolean(
54 password_manager::prefs::kPasswordManagerSavingEnabled
));
55 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
56 EXPECT_TRUE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
59 void CheckPreferencesCleared() {
60 PrefService
* prefs
= profile_
->GetPrefs();
61 const PrefService::Preference
* pref
= prefs
->FindPreference(
62 prefs::kBlockThirdPartyCookies
);
64 EXPECT_FALSE(pref
->IsExtensionControlled());
65 EXPECT_FALSE(prefs
->GetBoolean(prefs::kAlternateErrorPagesEnabled
));
66 EXPECT_FALSE(prefs
->GetBoolean(autofill::prefs::kAutofillEnabled
));
67 EXPECT_TRUE(prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
68 EXPECT_FALSE(prefs
->GetBoolean(prefs::kEnableHyperlinkAuditing
));
69 EXPECT_FALSE(prefs
->GetBoolean(prefs::kEnableReferrers
));
70 EXPECT_FALSE(prefs
->GetBoolean(prefs::kEnableTranslate
));
71 EXPECT_EQ(chrome_browser_net::NETWORK_PREDICTION_NEVER
,
72 prefs
->GetInteger(prefs::kNetworkPredictionOptions
));
73 EXPECT_FALSE(prefs
->GetBoolean(
74 password_manager::prefs::kPasswordManagerSavingEnabled
));
75 EXPECT_FALSE(prefs
->GetBoolean(prefs::kSafeBrowsingEnabled
));
76 EXPECT_FALSE(prefs
->GetBoolean(prefs::kSearchSuggestEnabled
));
79 void SetUpOnMainThread() override
{
80 ExtensionApiTest::SetUpOnMainThread();
82 // The browser might get closed later (and therefore be destroyed), so we
84 profile_
= browser()->profile();
86 // Closing the last browser window also releases a module reference. Make
87 // sure it's not the last one, so the message loop doesn't quit
89 g_browser_process
->AddRefModule();
92 void TearDownOnMainThread() override
{
93 // ReleaseBrowserProcessModule() needs to be called in a message loop, so we
94 // post a task to do it, then run the message loop.
95 base::ThreadTaskRunnerHandle::Get()->PostTask(
96 FROM_HERE
, base::Bind(&ReleaseBrowserProcessModule
));
97 content::RunAllPendingInMessageLoop();
99 ExtensionApiTest::TearDownOnMainThread();
105 // http://crbug.com/177163
106 #if defined(OS_WIN) && !defined(NDEBUG)
107 #define MAYBE_Standard DISABLED_Standard
109 #define MAYBE_Standard Standard
111 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, MAYBE_Standard
) {
112 PrefService
* prefs
= profile_
->GetPrefs();
113 prefs
->SetBoolean(prefs::kAlternateErrorPagesEnabled
, false);
114 prefs
->SetBoolean(autofill::prefs::kAutofillEnabled
, false);
115 prefs
->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
116 prefs
->SetBoolean(prefs::kEnableHyperlinkAuditing
, false);
117 prefs
->SetBoolean(prefs::kEnableReferrers
, false);
118 prefs
->SetBoolean(prefs::kEnableTranslate
, false);
119 prefs
->SetInteger(prefs::kNetworkPredictionOptions
,
120 chrome_browser_net::NETWORK_PREDICTION_NEVER
);
121 prefs
->SetBoolean(password_manager::prefs::kPasswordManagerSavingEnabled
,
123 prefs
->SetBoolean(prefs::kSafeBrowsingEnabled
, false);
124 prefs
->SetBoolean(prefs::kSearchSuggestEnabled
, false);
125 #if defined(ENABLE_WEBRTC)
126 prefs
->SetBoolean(prefs::kWebRTCMultipleRoutesEnabled
, false);
129 const char kExtensionPath
[] = "preference/standard";
131 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath
, "test.html")) << message_
;
132 CheckPreferencesSet();
134 // The settings should not be reset when the extension is reloaded.
135 ReloadExtension(last_loaded_extension_id());
136 CheckPreferencesSet();
138 // Uninstalling and installing the extension (without running the test that
139 // calls the extension API) should clear the settings.
140 extensions::TestExtensionRegistryObserver
observer(
141 extensions::ExtensionRegistry::Get(profile_
), last_loaded_extension_id());
142 UninstallExtension(last_loaded_extension_id());
143 observer
.WaitForExtensionUninstalled();
144 CheckPreferencesCleared();
146 LoadExtension(test_data_dir_
.AppendASCII(kExtensionPath
));
147 CheckPreferencesCleared();
150 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, PersistentIncognito
) {
151 PrefService
* prefs
= profile_
->GetPrefs();
152 prefs
->SetBoolean(prefs::kBlockThirdPartyCookies
, false);
155 RunExtensionTestIncognito("preference/persistent_incognito")) <<
158 // Setting an incognito preference should not create an incognito profile.
159 EXPECT_FALSE(profile_
->HasOffTheRecordProfile());
161 PrefService
* otr_prefs
= profile_
->GetOffTheRecordProfile()->GetPrefs();
162 const PrefService::Preference
* pref
=
163 otr_prefs
->FindPreference(prefs::kBlockThirdPartyCookies
);
165 EXPECT_TRUE(pref
->IsExtensionControlled());
166 EXPECT_TRUE(otr_prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
168 pref
= prefs
->FindPreference(prefs::kBlockThirdPartyCookies
);
170 EXPECT_FALSE(pref
->IsExtensionControlled());
171 EXPECT_FALSE(prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
174 // Flakily times out: http://crbug.com/106144
175 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, DISABLED_IncognitoDisabled
) {
176 EXPECT_FALSE(RunExtensionTest("preference/persistent_incognito"));
179 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, SessionOnlyIncognito
) {
180 PrefService
* prefs
= profile_
->GetPrefs();
181 prefs
->SetBoolean(prefs::kBlockThirdPartyCookies
, false);
184 RunExtensionTestIncognito("preference/session_only_incognito")) <<
187 EXPECT_TRUE(profile_
->HasOffTheRecordProfile());
189 PrefService
* otr_prefs
= profile_
->GetOffTheRecordProfile()->GetPrefs();
190 const PrefService::Preference
* pref
=
191 otr_prefs
->FindPreference(prefs::kBlockThirdPartyCookies
);
193 EXPECT_TRUE(pref
->IsExtensionControlled());
194 EXPECT_FALSE(otr_prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
196 pref
= prefs
->FindPreference(prefs::kBlockThirdPartyCookies
);
198 EXPECT_FALSE(pref
->IsExtensionControlled());
199 EXPECT_FALSE(prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
202 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, Clear
) {
203 PrefService
* prefs
= profile_
->GetPrefs();
204 prefs
->SetBoolean(prefs::kBlockThirdPartyCookies
, true);
206 EXPECT_TRUE(RunExtensionTest("preference/clear")) << message_
;
208 const PrefService::Preference
* pref
= prefs
->FindPreference(
209 prefs::kBlockThirdPartyCookies
);
211 EXPECT_FALSE(pref
->IsExtensionControlled());
212 EXPECT_EQ(true, prefs
->GetBoolean(prefs::kBlockThirdPartyCookies
));
215 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, OnChange
) {
216 EXPECT_TRUE(RunExtensionTestIncognito("preference/onchange")) <<
220 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, OnChangeSplit
) {
221 extensions::ResultCatcher catcher
;
222 catcher
.RestrictToBrowserContext(profile_
);
223 extensions::ResultCatcher catcher_incognito
;
224 catcher_incognito
.RestrictToBrowserContext(
225 profile_
->GetOffTheRecordProfile());
227 // Open an incognito window.
228 OpenURLOffTheRecord(profile_
, GURL("chrome://newtab/"));
230 // changeDefault listeners.
231 ExtensionTestMessageListener
listener1("changeDefault regular ready", true);
232 ExtensionTestMessageListener
listener_incognito1(
233 "changeDefault incognito ready", true);
235 // changeIncognitoOnly listeners.
236 ExtensionTestMessageListener
listener2(
237 "changeIncognitoOnly regular ready", true);
238 ExtensionTestMessageListener
listener_incognito2(
239 "changeIncognitoOnly incognito ready", true);
240 ExtensionTestMessageListener
listener3(
241 "changeIncognitoOnly regular listening", true);
242 ExtensionTestMessageListener
listener_incognito3(
243 "changeIncognitoOnly incognito pref set", false);
245 // changeDefaultOnly listeners.
246 ExtensionTestMessageListener
listener4(
247 "changeDefaultOnly regular ready", true);
248 ExtensionTestMessageListener
listener_incognito4(
249 "changeDefaultOnly incognito ready", true);
250 ExtensionTestMessageListener
listener5(
251 "changeDefaultOnly regular pref set", false);
252 ExtensionTestMessageListener
listener_incognito5(
253 "changeDefaultOnly incognito listening", true);
255 // changeIncognitoOnlyBack listeners.
256 ExtensionTestMessageListener
listener6(
257 "changeIncognitoOnlyBack regular ready", true);
258 ExtensionTestMessageListener
listener_incognito6(
259 "changeIncognitoOnlyBack incognito ready", true);
260 ExtensionTestMessageListener
listener7(
261 "changeIncognitoOnlyBack regular listening", true);
262 ExtensionTestMessageListener
listener_incognito7(
263 "changeIncognitoOnlyBack incognito pref set", false);
265 // clearIncognito listeners.
266 ExtensionTestMessageListener
listener8(
267 "clearIncognito regular ready", true);
268 ExtensionTestMessageListener
listener_incognito8(
269 "clearIncognito incognito ready", true);
270 ExtensionTestMessageListener
listener9(
271 "clearIncognito regular listening", true);
272 ExtensionTestMessageListener
listener_incognito9(
273 "clearIncognito incognito pref cleared", false);
275 // clearDefault listeners.
276 ExtensionTestMessageListener
listener10(
277 "clearDefault regular ready", true);
278 ExtensionTestMessageListener
listener_incognito10(
279 "clearDefault incognito ready", true);
281 base::FilePath extension_data_dir
=
282 test_data_dir_
.AppendASCII("preference").AppendASCII("onchange_split");
283 ASSERT_TRUE(LoadExtensionIncognito(extension_data_dir
));
285 // Test 1 - changeDefault
286 EXPECT_TRUE(listener1
.WaitUntilSatisfied()); // Regular ready
287 EXPECT_TRUE(listener_incognito1
.WaitUntilSatisfied()); // Incognito ready
288 listener1
.Reply("ok");
289 listener_incognito1
.Reply("ok");
291 // Test 2 - changeIncognitoOnly
292 EXPECT_TRUE(listener2
.WaitUntilSatisfied()); // Regular ready
293 EXPECT_TRUE(listener_incognito2
.WaitUntilSatisfied()); // Incognito ready
294 EXPECT_TRUE(listener3
.WaitUntilSatisfied()); // Regular listening
295 listener2
.Reply("ok");
296 listener_incognito2
.Reply("ok");
297 // Incognito preference set -- notify the regular listener
298 EXPECT_TRUE(listener_incognito3
.WaitUntilSatisfied());
299 listener3
.Reply("ok");
301 // Test 3 - changeDefaultOnly
302 EXPECT_TRUE(listener4
.WaitUntilSatisfied()); // Regular ready
303 EXPECT_TRUE(listener_incognito4
.WaitUntilSatisfied()); // Incognito ready
304 EXPECT_TRUE(listener_incognito5
.WaitUntilSatisfied()); // Incognito listening
305 listener4
.Reply("ok");
306 listener_incognito4
.Reply("ok");
307 // Regular preference set - notify the incognito listener
308 EXPECT_TRUE(listener5
.WaitUntilSatisfied());
309 listener_incognito5
.Reply("ok");
311 // Test 4 - changeIncognitoOnlyBack
312 EXPECT_TRUE(listener6
.WaitUntilSatisfied()); // Regular ready
313 EXPECT_TRUE(listener_incognito6
.WaitUntilSatisfied()); // Incognito ready
314 EXPECT_TRUE(listener7
.WaitUntilSatisfied()); // Regular listening
315 listener6
.Reply("ok");
316 listener_incognito6
.Reply("ok");
317 // Incognito preference set -- notify the regular listener
318 EXPECT_TRUE(listener_incognito7
.WaitUntilSatisfied());
319 listener7
.Reply("ok");
321 // Test 5 - clearIncognito
322 EXPECT_TRUE(listener8
.WaitUntilSatisfied()); // Regular ready
323 EXPECT_TRUE(listener_incognito8
.WaitUntilSatisfied()); // Incognito ready
324 EXPECT_TRUE(listener9
.WaitUntilSatisfied()); // Regular listening
325 listener8
.Reply("ok");
326 listener_incognito8
.Reply("ok");
327 // Incognito preference cleared -- notify the regular listener
328 EXPECT_TRUE(listener_incognito9
.WaitUntilSatisfied());
329 listener9
.Reply("ok");
331 // Test 6 - clearDefault
332 EXPECT_TRUE(listener10
.WaitUntilSatisfied()); // Regular ready
333 EXPECT_TRUE(listener_incognito10
.WaitUntilSatisfied()); // Incognito ready
334 listener10
.Reply("ok");
335 listener_incognito10
.Reply("ok");
337 EXPECT_TRUE(catcher
.GetNextResult()) << catcher
.message();
338 EXPECT_TRUE(catcher_incognito
.GetNextResult()) << catcher
.message();
341 #if defined(OS_WIN) // http://crbug.com/477844
342 #define MAYBE_DataReductionProxy DISABLED_DataReductionProxy
344 #define MAYBE_DataReductionProxy DataReductionProxy
346 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest
, MAYBE_DataReductionProxy
) {
347 EXPECT_TRUE(RunExtensionTest("preference/data_reduction_proxy")) <<