Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / preference / preference_apitest.cc
blob6ef8bafd0074151ed6a5d51538e64482366ab18d
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"
27 namespace {
29 void ReleaseBrowserProcessModule() {
30 g_browser_process->ReleaseModule();
33 } // namespace
35 class ExtensionPreferenceApiTest : public ExtensionApiTest {
36 protected:
37 ExtensionPreferenceApiTest() : profile_(NULL) {}
39 void CheckPreferencesSet() {
40 PrefService* prefs = profile_->GetPrefs();
41 const PrefService::Preference* pref = prefs->FindPreference(
42 prefs::kBlockThirdPartyCookies);
43 ASSERT_TRUE(pref);
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);
63 ASSERT_TRUE(pref);
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
83 // save the profile.
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
88 // unexpectedly.
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();
102 Profile* profile_;
105 // http://crbug.com/177163
106 #if defined(OS_WIN) && !defined(NDEBUG)
107 #define MAYBE_Standard DISABLED_Standard
108 #else
109 #define MAYBE_Standard Standard
110 #endif
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,
122 false);
123 prefs->SetBoolean(prefs::kSafeBrowsingEnabled, false);
124 prefs->SetBoolean(prefs::kSearchSuggestEnabled, false);
125 #if defined(ENABLE_WEBRTC)
126 prefs->SetBoolean(prefs::kWebRTCMultipleRoutesEnabled, false);
127 prefs->SetBoolean(prefs::kWebRTCNonProxiedUdpEnabled, false);
128 #endif
130 const char kExtensionPath[] = "preference/standard";
132 EXPECT_TRUE(RunExtensionSubtest(kExtensionPath, "test.html")) << message_;
133 CheckPreferencesSet();
135 // The settings should not be reset when the extension is reloaded.
136 ReloadExtension(last_loaded_extension_id());
137 CheckPreferencesSet();
139 // Uninstalling and installing the extension (without running the test that
140 // calls the extension API) should clear the settings.
141 extensions::TestExtensionRegistryObserver observer(
142 extensions::ExtensionRegistry::Get(profile_), last_loaded_extension_id());
143 UninstallExtension(last_loaded_extension_id());
144 observer.WaitForExtensionUninstalled();
145 CheckPreferencesCleared();
147 LoadExtension(test_data_dir_.AppendASCII(kExtensionPath));
148 CheckPreferencesCleared();
151 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, PersistentIncognito) {
152 PrefService* prefs = profile_->GetPrefs();
153 prefs->SetBoolean(prefs::kBlockThirdPartyCookies, false);
155 EXPECT_TRUE(
156 RunExtensionTestIncognito("preference/persistent_incognito")) <<
157 message_;
159 // Setting an incognito preference should not create an incognito profile.
160 EXPECT_FALSE(profile_->HasOffTheRecordProfile());
162 PrefService* otr_prefs = profile_->GetOffTheRecordProfile()->GetPrefs();
163 const PrefService::Preference* pref =
164 otr_prefs->FindPreference(prefs::kBlockThirdPartyCookies);
165 ASSERT_TRUE(pref);
166 EXPECT_TRUE(pref->IsExtensionControlled());
167 EXPECT_TRUE(otr_prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
169 pref = prefs->FindPreference(prefs::kBlockThirdPartyCookies);
170 ASSERT_TRUE(pref);
171 EXPECT_FALSE(pref->IsExtensionControlled());
172 EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
175 // Flakily times out: http://crbug.com/106144
176 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, DISABLED_IncognitoDisabled) {
177 EXPECT_FALSE(RunExtensionTest("preference/persistent_incognito"));
180 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, SessionOnlyIncognito) {
181 PrefService* prefs = profile_->GetPrefs();
182 prefs->SetBoolean(prefs::kBlockThirdPartyCookies, false);
184 EXPECT_TRUE(
185 RunExtensionTestIncognito("preference/session_only_incognito")) <<
186 message_;
188 EXPECT_TRUE(profile_->HasOffTheRecordProfile());
190 PrefService* otr_prefs = profile_->GetOffTheRecordProfile()->GetPrefs();
191 const PrefService::Preference* pref =
192 otr_prefs->FindPreference(prefs::kBlockThirdPartyCookies);
193 ASSERT_TRUE(pref);
194 EXPECT_TRUE(pref->IsExtensionControlled());
195 EXPECT_FALSE(otr_prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
197 pref = prefs->FindPreference(prefs::kBlockThirdPartyCookies);
198 ASSERT_TRUE(pref);
199 EXPECT_FALSE(pref->IsExtensionControlled());
200 EXPECT_FALSE(prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
203 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, Clear) {
204 PrefService* prefs = profile_->GetPrefs();
205 prefs->SetBoolean(prefs::kBlockThirdPartyCookies, true);
207 EXPECT_TRUE(RunExtensionTest("preference/clear")) << message_;
209 const PrefService::Preference* pref = prefs->FindPreference(
210 prefs::kBlockThirdPartyCookies);
211 ASSERT_TRUE(pref);
212 EXPECT_FALSE(pref->IsExtensionControlled());
213 EXPECT_EQ(true, prefs->GetBoolean(prefs::kBlockThirdPartyCookies));
216 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, OnChange) {
217 EXPECT_TRUE(RunExtensionTestIncognito("preference/onchange")) <<
218 message_;
221 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, OnChangeSplit) {
222 extensions::ResultCatcher catcher;
223 catcher.RestrictToBrowserContext(profile_);
224 extensions::ResultCatcher catcher_incognito;
225 catcher_incognito.RestrictToBrowserContext(
226 profile_->GetOffTheRecordProfile());
228 // Open an incognito window.
229 OpenURLOffTheRecord(profile_, GURL("chrome://newtab/"));
231 // changeDefault listeners.
232 ExtensionTestMessageListener listener1("changeDefault regular ready", true);
233 ExtensionTestMessageListener listener_incognito1(
234 "changeDefault incognito ready", true);
236 // changeIncognitoOnly listeners.
237 ExtensionTestMessageListener listener2(
238 "changeIncognitoOnly regular ready", true);
239 ExtensionTestMessageListener listener_incognito2(
240 "changeIncognitoOnly incognito ready", true);
241 ExtensionTestMessageListener listener3(
242 "changeIncognitoOnly regular listening", true);
243 ExtensionTestMessageListener listener_incognito3(
244 "changeIncognitoOnly incognito pref set", false);
246 // changeDefaultOnly listeners.
247 ExtensionTestMessageListener listener4(
248 "changeDefaultOnly regular ready", true);
249 ExtensionTestMessageListener listener_incognito4(
250 "changeDefaultOnly incognito ready", true);
251 ExtensionTestMessageListener listener5(
252 "changeDefaultOnly regular pref set", false);
253 ExtensionTestMessageListener listener_incognito5(
254 "changeDefaultOnly incognito listening", true);
256 // changeIncognitoOnlyBack listeners.
257 ExtensionTestMessageListener listener6(
258 "changeIncognitoOnlyBack regular ready", true);
259 ExtensionTestMessageListener listener_incognito6(
260 "changeIncognitoOnlyBack incognito ready", true);
261 ExtensionTestMessageListener listener7(
262 "changeIncognitoOnlyBack regular listening", true);
263 ExtensionTestMessageListener listener_incognito7(
264 "changeIncognitoOnlyBack incognito pref set", false);
266 // clearIncognito listeners.
267 ExtensionTestMessageListener listener8(
268 "clearIncognito regular ready", true);
269 ExtensionTestMessageListener listener_incognito8(
270 "clearIncognito incognito ready", true);
271 ExtensionTestMessageListener listener9(
272 "clearIncognito regular listening", true);
273 ExtensionTestMessageListener listener_incognito9(
274 "clearIncognito incognito pref cleared", false);
276 // clearDefault listeners.
277 ExtensionTestMessageListener listener10(
278 "clearDefault regular ready", true);
279 ExtensionTestMessageListener listener_incognito10(
280 "clearDefault incognito ready", true);
282 base::FilePath extension_data_dir =
283 test_data_dir_.AppendASCII("preference").AppendASCII("onchange_split");
284 ASSERT_TRUE(LoadExtensionIncognito(extension_data_dir));
286 // Test 1 - changeDefault
287 EXPECT_TRUE(listener1.WaitUntilSatisfied()); // Regular ready
288 EXPECT_TRUE(listener_incognito1.WaitUntilSatisfied()); // Incognito ready
289 listener1.Reply("ok");
290 listener_incognito1.Reply("ok");
292 // Test 2 - changeIncognitoOnly
293 EXPECT_TRUE(listener2.WaitUntilSatisfied()); // Regular ready
294 EXPECT_TRUE(listener_incognito2.WaitUntilSatisfied()); // Incognito ready
295 EXPECT_TRUE(listener3.WaitUntilSatisfied()); // Regular listening
296 listener2.Reply("ok");
297 listener_incognito2.Reply("ok");
298 // Incognito preference set -- notify the regular listener
299 EXPECT_TRUE(listener_incognito3.WaitUntilSatisfied());
300 listener3.Reply("ok");
302 // Test 3 - changeDefaultOnly
303 EXPECT_TRUE(listener4.WaitUntilSatisfied()); // Regular ready
304 EXPECT_TRUE(listener_incognito4.WaitUntilSatisfied()); // Incognito ready
305 EXPECT_TRUE(listener_incognito5.WaitUntilSatisfied()); // Incognito listening
306 listener4.Reply("ok");
307 listener_incognito4.Reply("ok");
308 // Regular preference set - notify the incognito listener
309 EXPECT_TRUE(listener5.WaitUntilSatisfied());
310 listener_incognito5.Reply("ok");
312 // Test 4 - changeIncognitoOnlyBack
313 EXPECT_TRUE(listener6.WaitUntilSatisfied()); // Regular ready
314 EXPECT_TRUE(listener_incognito6.WaitUntilSatisfied()); // Incognito ready
315 EXPECT_TRUE(listener7.WaitUntilSatisfied()); // Regular listening
316 listener6.Reply("ok");
317 listener_incognito6.Reply("ok");
318 // Incognito preference set -- notify the regular listener
319 EXPECT_TRUE(listener_incognito7.WaitUntilSatisfied());
320 listener7.Reply("ok");
322 // Test 5 - clearIncognito
323 EXPECT_TRUE(listener8.WaitUntilSatisfied()); // Regular ready
324 EXPECT_TRUE(listener_incognito8.WaitUntilSatisfied()); // Incognito ready
325 EXPECT_TRUE(listener9.WaitUntilSatisfied()); // Regular listening
326 listener8.Reply("ok");
327 listener_incognito8.Reply("ok");
328 // Incognito preference cleared -- notify the regular listener
329 EXPECT_TRUE(listener_incognito9.WaitUntilSatisfied());
330 listener9.Reply("ok");
332 // Test 6 - clearDefault
333 EXPECT_TRUE(listener10.WaitUntilSatisfied()); // Regular ready
334 EXPECT_TRUE(listener_incognito10.WaitUntilSatisfied()); // Incognito ready
335 listener10.Reply("ok");
336 listener_incognito10.Reply("ok");
338 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
339 EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
342 #if defined(OS_WIN) // http://crbug.com/477844
343 #define MAYBE_DataReductionProxy DISABLED_DataReductionProxy
344 #else
345 #define MAYBE_DataReductionProxy DataReductionProxy
346 #endif
347 IN_PROC_BROWSER_TEST_F(ExtensionPreferenceApiTest, MAYBE_DataReductionProxy) {
348 EXPECT_TRUE(RunExtensionTest("preference/data_reduction_proxy")) <<
349 message_;