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 "chrome/browser/ui/webui/options/preferences_browsertest.h"
10 #include "base/callback.h"
11 #include "base/json/json_reader.h"
12 #include "base/json/json_writer.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/values.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h"
22 #include "chrome/test/base/ui_test_utils.h"
23 #include "components/policy/core/browser/browser_policy_connector.h"
24 #include "components/policy/core/common/external_data_fetcher.h"
25 #include "components/policy/core/common/policy_map.h"
26 #include "components/policy/core/common/policy_types.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_source.h"
29 #include "content/public/browser/render_view_host.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "policy/policy_constants.h"
33 #include "testing/gtest/include/gtest/gtest.h"
36 #if defined(OS_CHROMEOS)
37 #include "base/strings/stringprintf.h"
38 #include "chrome/browser/browser_process.h"
39 #include "chrome/browser/chromeos/net/proxy_config_handler.h"
40 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
41 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
42 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h"
43 #include "chrome/browser/chromeos/settings/cros_settings.h"
44 #include "chrome/browser/prefs/proxy_config_dictionary.h"
45 #include "chromeos/dbus/dbus_thread_manager.h"
46 #include "chromeos/dbus/shill_profile_client.h"
47 #include "chromeos/dbus/shill_service_client.h"
48 #include "chromeos/network/favorite_state.h"
49 #include "chromeos/network/network_state.h"
50 #include "chromeos/network/network_state_handler.h"
51 #include "chromeos/settings/cros_settings_names.h"
52 #include "content/public/test/test_utils.h"
53 #include "third_party/cros_system_api/dbus/service_constants.h"
58 using testing::Property
;
59 using testing::Return
;
64 // Helper for using EXPECT_EQ() with base::Value.
65 bool operator==(const base::Value
& first
, const base::Value
& second
) {
66 return first
.Equals(&second
);
69 // Helper for pretty-printing the contents of base::Value in case of failures.
70 void PrintTo(const base::Value
& value
, std::ostream
* stream
) {
72 JSONWriter::Write(&value
, &json
);
78 // Googlemock matcher for base::Value.
79 MATCHER_P(EqualsValue
, expected
, "") {
80 return arg
&& arg
->Equals(expected
);
83 PreferencesBrowserTest::PreferencesBrowserTest() {
86 PreferencesBrowserTest::~PreferencesBrowserTest() {
89 // Navigates to the settings page, causing the JavaScript pref handling code to
90 // load and injects JavaScript testing code.
91 void PreferencesBrowserTest::SetUpOnMainThread() {
92 ui_test_utils::NavigateToURL(browser(),
93 GURL(chrome::kChromeUISettingsFrameURL
));
97 void PreferencesBrowserTest::SetUpPrefs() {
98 content::WebContents
* web_contents
=
99 browser()->tab_strip_model()->GetActiveWebContents();
100 ASSERT_TRUE(web_contents
);
101 render_view_host_
= web_contents
->GetRenderViewHost();
102 ASSERT_TRUE(render_view_host_
);
103 pref_service_
= browser()->profile()->GetPrefs();
104 pref_change_registrar_
.Init(pref_service_
);
105 ASSERT_TRUE(content::ExecuteScript(render_view_host_
,
106 "function TestEnv() {"
107 " this.sentinelName_ = 'download.prompt_for_download';"
109 " TestEnv.instance_ = this;"
112 "TestEnv.handleEvent = function(event) {"
113 " var env = TestEnv.instance_;"
114 " var name = event.type;"
115 " env.removePrefListener_(name);"
116 " if (name == TestEnv.sentinelName_)"
117 " env.sentinelValue_ = event.value.value;"
119 " env.reply_[name] = event.value;"
120 " if (env.fetching_ && !--env.fetching_ ||"
121 " !env.fetching_ && name == env.sentinelName_) {"
122 " env.removePrefListeners_();"
123 " window.domAutomationController.send(JSON.stringify(env.reply_));"
124 " delete env.reply_;"
128 "TestEnv.prototype = {"
129 " addPrefListener_: function(name) {"
130 " Preferences.getInstance().addEventListener(name,"
131 " TestEnv.handleEvent);"
134 " addPrefListeners_: function() {"
135 " for (var i in this.prefs_)"
136 " this.addPrefListener_(this.prefs_[i]);"
139 " removePrefListener_: function(name) {"
140 " Preferences.getInstance().removeEventListener(name,"
141 " TestEnv.handleEvent);"
144 " removePrefListeners_: function() {"
145 " for (var i in this.prefs_)"
146 " this.removePrefListener_(this.prefs_[i]);"
150 " addPref: function(name) {"
151 " this.prefs_.push(name);"
154 " setupAndReply: function() {"
156 " Preferences.instance_ = new Preferences();"
157 " this.addPref(this.sentinelName_);"
158 " this.fetching_ = this.prefs_.length;"
159 " this.addPrefListeners_();"
160 " Preferences.getInstance().initialize();"
163 " runAndReply: function(test) {"
165 " this.addPrefListeners_();"
167 " this.sentinelValue_ = !this.sentinelValue_;"
168 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_,"
172 " startObserving: function() {"
174 " this.addPrefListeners_();"
177 " finishObservingAndReply: function() {"
178 " this.sentinelValue_ = !this.sentinelValue_;"
179 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_,"
185 // Forwards notifications received when pref values change in the backend.
186 void PreferencesBrowserTest::OnPreferenceChanged(const std::string
& pref_name
) {
187 OnCommit(pref_service_
->FindPreference(pref_name
.c_str()));
190 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() {
191 // Sets up a mock policy provider for user and device policies.
192 EXPECT_CALL(policy_provider_
, IsInitializationComplete(_
))
193 .WillRepeatedly(Return(true));
194 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
198 void PreferencesBrowserTest::SetUserPolicies(
199 const std::vector
<std::string
>& names
,
200 const std::vector
<base::Value
*>& values
,
201 policy::PolicyLevel level
) {
202 policy::PolicyMap map
;
203 for (size_t i
= 0; i
< names
.size(); ++i
) {
204 map
.Set(names
[i
], level
, policy::POLICY_SCOPE_USER
,
205 values
[i
]->DeepCopy(), NULL
);
207 policy_provider_
.UpdateChromePolicy(map
);
210 void PreferencesBrowserTest::ClearUserPolicies() {
211 policy::PolicyMap empty_policy_map
;
212 policy_provider_
.UpdateChromePolicy(empty_policy_map
);
215 void PreferencesBrowserTest::SetUserValues(
216 const std::vector
<std::string
>& names
,
217 const std::vector
<base::Value
*>& values
) {
218 for (size_t i
= 0; i
< names
.size(); ++i
) {
219 pref_service_
->Set(names
[i
].c_str(), *values
[i
]);
223 void PreferencesBrowserTest::VerifyKeyValue(const base::DictionaryValue
& dict
,
224 const std::string
& key
,
225 const base::Value
& expected
) {
226 const base::Value
* actual
= NULL
;
227 EXPECT_TRUE(dict
.Get(key
, &actual
)) << "Was checking key: " << key
;
229 EXPECT_EQ(expected
, *actual
) << "Was checking key: " << key
;
232 void PreferencesBrowserTest::VerifyPref(const base::DictionaryValue
* prefs
,
233 const std::string
& name
,
234 const base::Value
* value
,
235 const std::string
& controlledBy
,
238 const base::Value
* pref
= NULL
;
239 const base::DictionaryValue
* dict
= NULL
;
240 ASSERT_TRUE(prefs
->GetWithoutPathExpansion(name
, &pref
));
241 ASSERT_TRUE(pref
->GetAsDictionary(&dict
));
242 VerifyKeyValue(*dict
, "value", *value
);
243 if (!controlledBy
.empty())
244 VerifyKeyValue(*dict
, "controlledBy", base::StringValue(controlledBy
));
246 EXPECT_FALSE(dict
->HasKey("controlledBy"));
249 VerifyKeyValue(*dict
, "disabled", base::FundamentalValue(true));
250 else if (dict
->HasKey("disabled"))
251 VerifyKeyValue(*dict
, "disabled", base::FundamentalValue(false));
254 VerifyKeyValue(*dict
, "uncommitted", base::FundamentalValue(true));
255 else if (dict
->HasKey("uncommitted"))
256 VerifyKeyValue(*dict
, "uncommitted", base::FundamentalValue(false));
259 void PreferencesBrowserTest::VerifyObservedPref(const std::string
& json
,
260 const std::string
& name
,
261 const base::Value
* value
,
262 const std::string
& controlledBy
,
265 scoped_ptr
<base::Value
> observed_value_ptr(base::JSONReader::Read(json
));
266 const base::DictionaryValue
* observed_dict
;
267 ASSERT_TRUE(observed_value_ptr
.get());
268 ASSERT_TRUE(observed_value_ptr
->GetAsDictionary(&observed_dict
));
269 VerifyPref(observed_dict
, name
, value
, controlledBy
, disabled
, uncommitted
);
272 void PreferencesBrowserTest::VerifyObservedPrefs(
273 const std::string
& json
,
274 const std::vector
<std::string
>& names
,
275 const std::vector
<base::Value
*>& values
,
276 const std::string
& controlledBy
,
279 scoped_ptr
<base::Value
> observed_value_ptr(base::JSONReader::Read(json
));
280 const base::DictionaryValue
* observed_dict
;
281 ASSERT_TRUE(observed_value_ptr
.get());
282 ASSERT_TRUE(observed_value_ptr
->GetAsDictionary(&observed_dict
));
283 for (size_t i
= 0; i
< names
.size(); ++i
) {
284 VerifyPref(observed_dict
, names
[i
], values
[i
], controlledBy
, disabled
,
289 void PreferencesBrowserTest::ExpectNoCommit(const std::string
& name
) {
290 pref_change_registrar_
.Add(
292 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged
,
293 base::Unretained(this)));
294 EXPECT_CALL(*this, OnCommit(Property(&PrefService::Preference::name
, name
)))
298 void PreferencesBrowserTest::ExpectSetCommit(const std::string
& name
,
299 const base::Value
* value
) {
300 pref_change_registrar_
.Add(
302 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged
,
303 base::Unretained(this)));
304 EXPECT_CALL(*this, OnCommit(AllOf(
305 Property(&PrefService::Preference::name
, name
),
306 Property(&PrefService::Preference::IsUserControlled
, true),
307 Property(&PrefService::Preference::GetValue
, EqualsValue(value
)))));
310 void PreferencesBrowserTest::ExpectClearCommit(const std::string
& name
) {
311 pref_change_registrar_
.Add(
313 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged
,
314 base::Unretained(this)));
315 EXPECT_CALL(*this, OnCommit(AllOf(
316 Property(&PrefService::Preference::name
, name
),
317 Property(&PrefService::Preference::IsUserControlled
, false))));
320 void PreferencesBrowserTest::VerifyAndClearExpectations() {
321 Mock::VerifyAndClearExpectations(this);
322 pref_change_registrar_
.RemoveAll();
325 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment(
326 const std::vector
<std::string
>& pref_names
,
327 std::string
* observed_json
) const {
328 std::stringstream javascript
;
329 javascript
<< "var testEnv = new TestEnv();";
330 for (std::vector
<std::string
>::const_iterator name
= pref_names
.begin();
331 name
!= pref_names
.end(); ++name
) {
332 javascript
<< "testEnv.addPref('" << name
->c_str() << "');";
334 javascript
<< "testEnv.setupAndReply();";
335 std::string temp_observed_json
;
337 observed_json
= &temp_observed_json
;
338 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
339 render_view_host_
, javascript
.str(), observed_json
));
342 void PreferencesBrowserTest::SetPref(const std::string
& name
,
343 const std::string
& type
,
344 const base::Value
* value
,
346 std::string
* observed_json
) {
347 scoped_ptr
<base::Value
> commit_ptr(new base::FundamentalValue(commit
));
348 std::stringstream javascript
;
349 javascript
<< "testEnv.runAndReply(function() {"
350 << " Preferences.set" << type
<< "Pref("
351 << " '" << name
<< "',"
352 << " " << *value
<< ","
353 << " " << *commit_ptr
<< ");"
355 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
356 render_view_host_
, javascript
.str(), observed_json
));
359 void PreferencesBrowserTest::VerifySetPref(const std::string
& name
,
360 const std::string
& type
,
361 const base::Value
* value
,
364 ExpectSetCommit(name
, value
);
366 ExpectNoCommit(name
);
367 std::string observed_json
;
368 SetPref(name
, type
, value
, commit
, &observed_json
);
369 VerifyObservedPref(observed_json
, name
, value
, std::string(), false, !commit
);
370 VerifyAndClearExpectations();
373 void PreferencesBrowserTest::VerifyClearPref(const std::string
& name
,
374 const base::Value
* value
,
377 ExpectClearCommit(name
);
379 ExpectNoCommit(name
);
380 scoped_ptr
<base::Value
> commit_ptr(new base::FundamentalValue(commit
));
381 std::string commit_json
;
382 base::JSONWriter::Write(commit_ptr
.get(), &commit_json
);
383 std::stringstream javascript
;
384 javascript
<< "testEnv.runAndReply(function() {"
385 << " Preferences.clearPref("
386 << " '" << name
.c_str() << "',"
387 << " " << commit_json
.c_str() << ");});";
388 std::string observed_json
;
389 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
390 render_view_host_
, javascript
.str(), &observed_json
));
391 VerifyObservedPref(observed_json
, name
, value
, "recommended", false, !commit
);
392 VerifyAndClearExpectations();
395 void PreferencesBrowserTest::VerifyCommit(const std::string
& name
,
396 const base::Value
* value
,
397 const std::string
& controlledBy
) {
398 std::stringstream javascript
;
399 javascript
<< "testEnv.runAndReply(function() {"
400 << " Preferences.getInstance().commitPref("
401 << " '" << name
.c_str() << "');});";
402 std::string observed_json
;
403 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
404 render_view_host_
, javascript
.str(), &observed_json
));
405 VerifyObservedPref(observed_json
, name
, value
, controlledBy
, false, false);
408 void PreferencesBrowserTest::VerifySetCommit(const std::string
& name
,
409 const base::Value
* value
) {
410 ExpectSetCommit(name
, value
);
411 VerifyCommit(name
, value
, std::string());
412 VerifyAndClearExpectations();
415 void PreferencesBrowserTest::VerifyClearCommit(const std::string
& name
,
416 const base::Value
* value
) {
417 ExpectClearCommit(name
);
418 VerifyCommit(name
, value
, "recommended");
419 VerifyAndClearExpectations();
422 void PreferencesBrowserTest::VerifyRollback(const std::string
& name
,
423 const base::Value
* value
,
424 const std::string
& controlledBy
) {
425 ExpectNoCommit(name
);
426 std::stringstream javascript
;
427 javascript
<< "testEnv.runAndReply(function() {"
428 << " Preferences.getInstance().rollbackPref("
429 << " '" << name
.c_str() << "');});";
430 std::string observed_json
;
431 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
432 render_view_host_
, javascript
.str(), &observed_json
));
433 VerifyObservedPref(observed_json
, name
, value
, controlledBy
, false, true);
434 VerifyAndClearExpectations();
437 void PreferencesBrowserTest::StartObserving() {
438 ASSERT_TRUE(content::ExecuteScript(
439 render_view_host_
, "testEnv.startObserving();"));
442 void PreferencesBrowserTest::FinishObserving(std::string
* observed_json
) {
443 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
445 "testEnv.finishObservingAndReply();",
449 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref
) {
451 types_
.push_back("Boolean");
452 pref_names_
.push_back(prefs::kAlternateErrorPagesEnabled
);
453 policy_names_
.push_back(policy::key::kAlternateErrorPagesEnabled
);
454 non_default_values_
.push_back(new base::FundamentalValue(false));
457 types_
.push_back("Integer");
458 pref_names_
.push_back(prefs::kRestoreOnStartup
);
459 policy_names_
.push_back(policy::key::kRestoreOnStartup
);
460 non_default_values_
.push_back(new base::FundamentalValue(4));
463 if (includeListPref
) {
464 types_
.push_back("List");
465 pref_names_
.push_back(prefs::kURLsToRestoreOnStartup
);
466 policy_names_
.push_back(policy::key::kRestoreOnStartupURLs
);
467 base::ListValue
* list
= new base::ListValue
;
468 list
->Append(new base::StringValue("http://www.example.com"));
469 list
->Append(new base::StringValue("http://example.com"));
470 non_default_values_
.push_back(list
);
473 // Retrieve default values.
474 for (std::vector
<std::string
>::const_iterator name
= pref_names_
.begin();
475 name
!= pref_names_
.end(); ++name
) {
476 default_values_
.push_back(
477 pref_service_
->GetDefaultPrefValue(name
->c_str())->DeepCopy());
481 // Verifies that initializing the JavaScript Preferences class fires the correct
482 // notifications in JavaScript.
483 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, FetchPrefs
) {
484 UseDefaultTestPrefs(true);
485 std::string observed_json
;
487 // Verify notifications when default values are in effect.
488 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
490 observed_json
, pref_names_
, default_values_
.get(),
491 std::string(), false, false);
493 // Verify notifications when recommended values are in effect.
494 SetUserPolicies(policy_names_
, non_default_values_
.get(),
495 policy::POLICY_LEVEL_RECOMMENDED
);
496 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
497 VerifyObservedPrefs(observed_json
, pref_names_
, non_default_values_
.get(),
498 "recommended", false, false);
500 // Verify notifications when mandatory values are in effect.
501 SetUserPolicies(policy_names_
, non_default_values_
.get(),
502 policy::POLICY_LEVEL_MANDATORY
);
503 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
504 VerifyObservedPrefs(observed_json
, pref_names_
, non_default_values_
.get(),
505 "policy", true, false);
507 // Verify notifications when user-modified values are in effect.
509 SetUserValues(pref_names_
, non_default_values_
.get());
510 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
511 VerifyObservedPrefs(observed_json
,
513 non_default_values_
.get(),
519 // Verifies that setting a user-modified pref value through the JavaScript
520 // Preferences class fires the correct notification in JavaScript and causes the
521 // change to be committed to the C++ backend.
522 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, SetPrefs
) {
523 UseDefaultTestPrefs(false);
525 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
526 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
527 VerifySetPref(pref_names_
[i
], types_
[i
], non_default_values_
[i
], true);
531 // Verifies that clearing a user-modified pref value through the JavaScript
532 // Preferences class fires the correct notification in JavaScript and causes the
533 // change to be committed to the C++ backend.
534 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, ClearPrefs
) {
535 UseDefaultTestPrefs(false);
537 SetUserPolicies(policy_names_
, default_values_
.get(),
538 policy::POLICY_LEVEL_RECOMMENDED
);
539 SetUserValues(pref_names_
, non_default_values_
.get());
540 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
541 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
542 VerifyClearPref(pref_names_
[i
], default_values_
[i
], true);
546 // Verifies that when the user-modified value of a dialog pref is set and the
547 // change then committed through the JavaScript Preferences class, the correct
548 // notifications fire and a commit to the C++ backend occurs in the latter step
550 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, DialogPrefsSetCommit
) {
551 UseDefaultTestPrefs(false);
553 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
554 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
555 VerifySetPref(pref_names_
[i
], types_
[i
], non_default_values_
[i
], false);
556 VerifySetCommit(pref_names_
[i
], non_default_values_
[i
]);
560 // Verifies that when the user-modified value of a dialog pref is set and the
561 // change then rolled back through the JavaScript Preferences class, the correct
562 // notifications fire and no commit to the C++ backend occurs.
563 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, DialogPrefsSetRollback
) {
564 UseDefaultTestPrefs(false);
566 // Verify behavior when default values are in effect.
567 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
568 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
569 VerifySetPref(pref_names_
[i
], types_
[i
], non_default_values_
[i
], false);
570 VerifyRollback(pref_names_
[i
], default_values_
[i
], std::string());
573 // Verify behavior when recommended values are in effect.
574 SetUserPolicies(policy_names_
, default_values_
.get(),
575 policy::POLICY_LEVEL_RECOMMENDED
);
576 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
577 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
578 VerifySetPref(pref_names_
[i
], types_
[i
], non_default_values_
[i
], false);
579 VerifyRollback(pref_names_
[i
], default_values_
[i
], "recommended");
583 // Verifies that when the user-modified value of a dialog pref is cleared and
584 // the change then committed through the JavaScript Preferences class, the
585 // correct notifications fire and a commit to the C++ backend occurs in the
587 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, DialogPrefsClearCommit
) {
588 UseDefaultTestPrefs(false);
590 SetUserPolicies(policy_names_
, default_values_
.get(),
591 policy::POLICY_LEVEL_RECOMMENDED
);
592 SetUserValues(pref_names_
, non_default_values_
.get());
593 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
594 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
595 VerifyClearPref(pref_names_
[i
], default_values_
[i
], false);
596 VerifyClearCommit(pref_names_
[i
], default_values_
[i
]);
600 // Verifies that when the user-modified value of a dialog pref is cleared and
601 // the change then rolled back through the JavaScript Preferences class, the
602 // correct notifications fire and no commit to the C++ backend occurs.
603 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, DialogPrefsClearRollback
) {
604 UseDefaultTestPrefs(false);
606 SetUserPolicies(policy_names_
, default_values_
.get(),
607 policy::POLICY_LEVEL_RECOMMENDED
);
608 SetUserValues(pref_names_
, non_default_values_
.get());
609 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
610 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
611 VerifyClearPref(pref_names_
[i
], default_values_
[i
], false);
612 VerifyRollback(pref_names_
[i
], non_default_values_
[i
], std::string());
616 // Verifies that when preference values change in the C++ backend, the correct
617 // notifications fire in JavaScript.
618 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, NotificationsOnBackendChanges
) {
619 UseDefaultTestPrefs(false);
620 std::string observed_json
;
622 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
624 // Verify notifications when recommended values come into effect.
626 SetUserPolicies(policy_names_
, non_default_values_
.get(),
627 policy::POLICY_LEVEL_RECOMMENDED
);
628 FinishObserving(&observed_json
);
629 VerifyObservedPrefs(observed_json
, pref_names_
, non_default_values_
.get(),
630 "recommended", false, false);
632 // Verify notifications when mandatory values come into effect.
634 SetUserPolicies(policy_names_
, non_default_values_
.get(),
635 policy::POLICY_LEVEL_MANDATORY
);
636 FinishObserving(&observed_json
);
637 VerifyObservedPrefs(observed_json
, pref_names_
, non_default_values_
.get(),
638 "policy", true, false);
640 // Verify notifications when default values come into effect.
643 FinishObserving(&observed_json
);
645 observed_json
, pref_names_
, default_values_
.get(),
646 std::string(), false, false);
648 // Verify notifications when user-modified values come into effect.
650 SetUserValues(pref_names_
, non_default_values_
.get());
651 FinishObserving(&observed_json
);
652 VerifyObservedPrefs(observed_json
,
654 non_default_values_
.get(),
660 #if defined(OS_CHROMEOS)
662 // Verifies that initializing the JavaScript Preferences class fires the correct
663 // notifications in JavaScript for pref values handled by the
664 // CoreChromeOSOptionsHandler class.
665 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
, ChromeOSDeviceFetchPrefs
) {
666 std::string observed_json
;
669 pref_names_
.push_back(chromeos::kAccountsPrefAllowGuest
);
670 default_values_
.push_back(new base::FundamentalValue(true));
673 pref_names_
.push_back(chromeos::kReleaseChannel
);
674 default_values_
.push_back(new base::StringValue(""));
677 pref_names_
.push_back(chromeos::kAccountsPrefUsers
);
678 default_values_
.push_back(new base::ListValue
);
680 // Verify notifications when default values are in effect.
681 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
682 VerifyObservedPrefs(observed_json
, pref_names_
, default_values_
.get(),
683 "owner", true, false);
686 // Verifies that initializing the JavaScript Preferences class fires the correct
687 // notifications in JavaScript for non-privileged pref values handled by the
688 // CoreChromeOSOptionsHandler class.
689 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest
,
690 ChromeOSDeviceFetchNonPrivilegedPrefs
) {
691 ScopedVector
<base::Value
> decorated_non_default_values
;
692 std::string observed_json
;
694 // Non-privileged string pref.
695 pref_names_
.push_back(chromeos::kSystemTimezone
);
696 default_values_
.push_back(new base::StringValue("America/Los_Angeles"));
697 non_default_values_
.push_back(new base::StringValue("America/New_York"));
698 decorated_non_default_values
.push_back(
699 non_default_values_
.back()->DeepCopy());
701 // Verify notifications when default values are in effect.
702 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
703 VerifyObservedPrefs(observed_json
, pref_names_
, default_values_
.get(),
704 std::string(), false, false);
706 chromeos::CrosSettings
* cros_settings
= chromeos::CrosSettings::Get();
707 cros_settings
->Set(pref_names_
[0], *non_default_values_
[0]);
709 // Verify notifications when non-default values are in effect.
710 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
711 VerifyObservedPrefs(observed_json
, pref_names_
,
712 decorated_non_default_values
.get(),
713 std::string(), false, false);
716 class ManagedPreferencesBrowserTest
: public PreferencesBrowserTest
{
718 // PreferencesBrowserTest implementation:
719 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
{
720 // Set up fake install attributes.
721 scoped_ptr
<policy::StubEnterpriseInstallAttributes
> attributes(
722 new policy::StubEnterpriseInstallAttributes());
723 attributes
->SetDomain("example.com");
724 attributes
->SetRegistrationUser("user@example.com");
725 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
726 attributes
.release());
728 PreferencesBrowserTest::SetUpInProcessBrowserTestFixture();
732 // Verifies that initializing the JavaScript Preferences class fires the correct
733 // notifications in JavaScript for pref values handled by the
734 // CoreChromeOSOptionsHandler class for a managed device.
735 IN_PROC_BROWSER_TEST_F(ManagedPreferencesBrowserTest
,
736 ChromeOSDeviceFetchPrefs
) {
737 ScopedVector
<base::Value
> decorated_non_default_values
;
738 std::string observed_json
;
741 pref_names_
.push_back(chromeos::kAccountsPrefAllowGuest
);
742 non_default_values_
.push_back(new base::FundamentalValue(false));
743 decorated_non_default_values
.push_back(
744 non_default_values_
.back()->DeepCopy());
747 pref_names_
.push_back(chromeos::kReleaseChannel
);
748 non_default_values_
.push_back(new base::StringValue("stable-channel"));
749 decorated_non_default_values
.push_back(
750 non_default_values_
.back()->DeepCopy());
753 pref_names_
.push_back(chromeos::kAccountsPrefUsers
);
754 base::ListValue
* list
= new base::ListValue
;
755 list
->Append(new base::StringValue("me@google.com"));
756 list
->Append(new base::StringValue("you@google.com"));
757 non_default_values_
.push_back(list
);
758 list
= new base::ListValue
;
759 base::DictionaryValue
* dict
= new base::DictionaryValue
;
760 dict
->SetString("username", "me@google.com");
761 dict
->SetString("name", "me@google.com");
762 dict
->SetString("email", "");
763 dict
->SetBoolean("owner", false);
765 dict
= new base::DictionaryValue
;
766 dict
->SetString("username", "you@google.com");
767 dict
->SetString("name", "you@google.com");
768 dict
->SetString("email", "");
769 dict
->SetBoolean("owner", false);
771 decorated_non_default_values
.push_back(list
);
773 chromeos::CrosSettings
* cros_settings
= chromeos::CrosSettings::Get();
774 for (size_t i
= 0; i
< pref_names_
.size(); ++i
) {
775 cros_settings
->Set(pref_names_
[i
], *non_default_values_
[i
]);
778 // Verify notifications when mandatory values are in effect.
779 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
780 VerifyObservedPrefs(observed_json
, pref_names_
,
781 decorated_non_default_values
.get(),
782 "policy", true, false);
785 // Verifies that initializing the JavaScript Preferences class fires the correct
786 // notifications in JavaScript for non-privileged pref values handled by the
787 // CoreChromeOSOptionsHandler class for a managed device.
788 IN_PROC_BROWSER_TEST_F(ManagedPreferencesBrowserTest
,
789 ChromeOSDeviceFetchNonPrivilegedPrefs
) {
790 ScopedVector
<base::Value
> decorated_non_default_values
;
791 std::string observed_json
;
793 // Non-privileged string pref.
794 pref_names_
.push_back(chromeos::kSystemTimezone
);
795 non_default_values_
.push_back(new base::StringValue("America/New_York"));
796 decorated_non_default_values
.push_back(
797 non_default_values_
.back()->DeepCopy());
799 // Verify notifications when mandatory values are in effect.
800 chromeos::CrosSettings
* cros_settings
= chromeos::CrosSettings::Get();
801 cros_settings
->Set(pref_names_
[0], *non_default_values_
[0]);
803 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
804 VerifyObservedPrefs(observed_json
, pref_names_
,
805 decorated_non_default_values
.get(),
806 std::string(), false, false);
811 const char* kUserProfilePath
= "user_profile";
815 class ProxyPreferencesBrowserTest
: public PreferencesBrowserTest
{
817 virtual void SetUpOnMainThread() OVERRIDE
{
818 SetupNetworkEnvironment();
819 content::RunAllPendingInMessageLoop();
821 scoped_ptr
<base::DictionaryValue
> proxy_config_dict(
822 ProxyConfigDictionary::CreateFixedServers("127.0.0.1:8080",
823 "*.google.com, 1.2.3.4:22"));
825 ProxyConfigDictionary
proxy_config(proxy_config_dict
.get());
827 const chromeos::FavoriteState
* network
= GetDefaultFavoriteNetwork();
828 ASSERT_TRUE(network
);
829 chromeos::proxy_config::SetProxyConfigForFavoriteNetwork(proxy_config
,
832 std::string url
= base::StringPrintf("%s?network=%s",
833 chrome::kChromeUIProxySettingsURL
,
834 network
->path().c_str());
836 ui_test_utils::NavigateToURL(browser(), GURL(url
));
841 void SetupNetworkEnvironment() {
842 chromeos::ShillProfileClient::TestInterface
* profile_test
=
843 chromeos::DBusThreadManager::Get()->GetShillProfileClient()
844 ->GetTestInterface();
845 chromeos::ShillServiceClient::TestInterface
* service_test
=
846 chromeos::DBusThreadManager::Get()->GetShillServiceClient()
847 ->GetTestInterface();
849 profile_test
->AddProfile(kUserProfilePath
, "user");
851 service_test
->ClearServices();
852 service_test
->AddService("stub_ethernet",
854 shill::kTypeEthernet
,
856 true, // add to visible
857 true); // add to watchlist
858 service_test
->SetServiceProperty("stub_ethernet",
859 shill::kGuidProperty
,
860 base::StringValue("stub_ethernet"));
861 service_test
->SetServiceProperty("stub_ethernet",
862 shill::kProfileProperty
,
863 base::StringValue(kUserProfilePath
));
864 profile_test
->AddService(kUserProfilePath
, "stub_wifi2");
867 void SetONCPolicy(const char* policy_name
, policy::PolicyScope scope
) {
868 std::string onc_policy
=
869 "{ \"NetworkConfigurations\": ["
870 " { \"GUID\": \"stub_ethernet\","
871 " \"Type\": \"Ethernet\","
872 " \"Name\": \"My Ethernet\","
874 " \"Authentication\": \"None\" },"
875 " \"ProxySettings\": {"
876 " \"PAC\": \"http://domain.com/x\","
877 " \"Type\": \"PAC\" }"
880 " \"Type\": \"UnencryptedConfiguration\""
883 policy::PolicyMap map
;
885 policy::POLICY_LEVEL_MANDATORY
,
887 new base::StringValue(onc_policy
),
889 policy_provider_
.UpdateChromePolicy(map
);
891 content::RunAllPendingInMessageLoop();
894 const chromeos::FavoriteState
* GetDefaultFavoriteNetwork() {
895 chromeos::NetworkStateHandler
* handler
=
896 chromeos::NetworkHandler::Get()->network_state_handler();
897 return handler
->DefaultFavoriteNetwork();
900 void SetProxyPref(const std::string
& name
, const base::Value
& value
) {
902 switch (value
.GetType()) {
903 case base::Value::TYPE_BOOLEAN
:
906 case base::Value::TYPE_INTEGER
:
909 case base::Value::TYPE_STRING
:
916 std::string observed_json
;
917 SetPref(name
, type
, &value
, true, &observed_json
);
920 void VerifyCurrentProxyServer(const std::string
& expected_server
,
921 onc::ONCSource expected_source
) {
922 const chromeos::FavoriteState
* network
= GetDefaultFavoriteNetwork();
923 ASSERT_TRUE(network
);
924 onc::ONCSource actual_source
;
925 scoped_ptr
<ProxyConfigDictionary
> proxy_dict
=
926 chromeos::proxy_config::GetProxyConfigForFavoriteNetwork(
928 g_browser_process
->local_state(),
931 ASSERT_TRUE(proxy_dict
);
932 std::string actual_proxy_server
;
933 EXPECT_TRUE(proxy_dict
->GetProxyServer(&actual_proxy_server
));
934 EXPECT_EQ(expected_server
, actual_proxy_server
);
935 EXPECT_EQ(expected_source
, actual_source
);
939 // Verifies that proxy settings are correctly pushed to JavaScript during
940 // initialization of the proxy settings page.
941 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest
, ChromeOSInitializeProxy
) {
943 pref_names_
.push_back(chromeos::kProxySingle
);
944 non_default_values_
.push_back(new base::FundamentalValue(true));
947 pref_names_
.push_back(chromeos::kProxySingleHttpPort
);
948 non_default_values_
.push_back(new base::FundamentalValue(8080));
951 pref_names_
.push_back(chromeos::kProxySingleHttp
);
952 non_default_values_
.push_back(new base::StringValue("127.0.0.1"));
955 pref_names_
.push_back(chromeos::kProxyIgnoreList
);
956 base::ListValue
* list
= new base::ListValue();
957 list
->Append(new base::StringValue("*.google.com"));
958 list
->Append(new base::StringValue("1.2.3.4:22"));
959 non_default_values_
.push_back(list
);
961 // Verify that no policy is presented to the UI. This must be verified on the
962 // kProxyType and the kUseSharedProxies prefs.
963 pref_names_
.push_back(chromeos::kProxyType
);
964 non_default_values_
.push_back(new base::FundamentalValue(2));
966 pref_names_
.push_back(prefs::kUseSharedProxies
);
967 non_default_values_
.push_back(new base::FundamentalValue(false));
969 std::string observed_json
;
970 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
972 observed_json
, pref_names_
, non_default_values_
.get(), "", false, false);
975 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest
, ONCPolicy
) {
976 SetONCPolicy(policy::key::kOpenNetworkConfiguration
,
977 policy::POLICY_SCOPE_USER
);
979 // Verify that per-network policy is presented to the UI. This must be
980 // verified on the kProxyType.
981 pref_names_
.push_back(chromeos::kProxyType
);
982 non_default_values_
.push_back(new base::FundamentalValue(3));
984 std::string observed_json
;
985 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
987 observed_json
, pref_names_
, non_default_values_
.get(),
988 "policy", true, false);
990 // Verify that 'use-shared-proxies' is not affected by per-network policy.
992 non_default_values_
.clear();
993 pref_names_
.push_back(prefs::kUseSharedProxies
);
994 non_default_values_
.push_back(new base::FundamentalValue(false));
996 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
998 observed_json
, pref_names_
, non_default_values_
.get(), "", false, false);
1001 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest
, DeviceONCPolicy
) {
1002 SetONCPolicy(policy::key::kDeviceOpenNetworkConfiguration
,
1003 policy::POLICY_SCOPE_MACHINE
);
1005 // Verify that the policy is presented to the UI. This verification must be
1006 // done on the kProxyType pref.
1007 pref_names_
.push_back(chromeos::kProxyType
);
1008 non_default_values_
.push_back(new base::FundamentalValue(3));
1010 std::string observed_json
;
1011 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
1012 VerifyObservedPrefs(
1013 observed_json
, pref_names_
, non_default_values_
.get(),
1014 "policy", true, false);
1016 // Verify that 'use-shared-proxies' is not affected by per-network policy.
1017 pref_names_
.clear();
1018 non_default_values_
.clear();
1019 pref_names_
.push_back(prefs::kUseSharedProxies
);
1020 non_default_values_
.push_back(new base::FundamentalValue(false));
1022 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
1023 VerifyObservedPrefs(
1024 observed_json
, pref_names_
, non_default_values_
.get(), "", false, false);
1027 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest
, UserProxyPolicy
) {
1028 policy_names_
.push_back(policy::key::kProxyMode
);
1029 default_values_
.push_back(
1030 new base::StringValue(ProxyPrefs::kAutoDetectProxyModeName
));
1032 policy_names_
, default_values_
.get(), policy::POLICY_LEVEL_MANDATORY
);
1033 content::RunAllPendingInMessageLoop();
1035 // Verify that the policy is presented to the UI. This verification must be
1036 // done on the kProxyType pref.
1037 pref_names_
.push_back(chromeos::kProxyType
);
1038 non_default_values_
.push_back(new base::FundamentalValue(3));
1040 // Verify that 'use-shared-proxies' is controlled by the policy.
1041 pref_names_
.push_back(prefs::kUseSharedProxies
);
1042 non_default_values_
.push_back(new base::FundamentalValue(false));
1044 std::string observed_json
;
1045 SetupJavaScriptTestEnvironment(pref_names_
, &observed_json
);
1046 VerifyObservedPrefs(
1047 observed_json
, pref_names_
, non_default_values_
.get(),
1048 "policy", true, false);
1051 // Verifies that modifications to the proxy settings are correctly pushed from
1052 // JavaScript to the ProxyConfig property stored in the network configuration.
1053 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest
, ChromeOSSetProxy
) {
1054 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
1056 SetProxyPref(chromeos::kProxySingleHttpPort
, base::FundamentalValue(123));
1057 SetProxyPref(chromeos::kProxySingleHttp
, base::StringValue("www.adomain.xy"));
1059 VerifyCurrentProxyServer("www.adomain.xy:123",
1060 onc::ONC_SOURCE_NONE
);
1063 // Verify that default proxy ports are used and that ports can be updated
1064 // without affecting the previously set hosts.
1065 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest
, ChromeOSProxyDefaultPorts
) {
1066 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_
, NULL
));
1068 // Set to manual, per scheme proxy.
1069 SetProxyPref(chromeos::kProxySingle
, base::FundamentalValue(false));
1071 // Set hosts but no ports.
1072 SetProxyPref(chromeos::kProxyHttpUrl
, base::StringValue("a.com"));
1073 SetProxyPref(chromeos::kProxyHttpsUrl
, base::StringValue("4.3.2.1"));
1074 SetProxyPref(chromeos::kProxyFtpUrl
, base::StringValue("c.com"));
1075 SetProxyPref(chromeos::kProxySocks
, base::StringValue("d.com"));
1077 // Verify default ports.
1078 VerifyCurrentProxyServer(
1079 "http=a.com:80;https=4.3.2.1:80;ftp=c.com:80;socks=socks4://d.com:1080",
1080 onc::ONC_SOURCE_NONE
);
1082 // Set and verify the ports.
1083 SetProxyPref(chromeos::kProxyHttpPort
, base::FundamentalValue(1));
1084 SetProxyPref(chromeos::kProxyHttpsPort
, base::FundamentalValue(2));
1085 SetProxyPref(chromeos::kProxyFtpPort
, base::FundamentalValue(3));
1086 SetProxyPref(chromeos::kProxySocksPort
, base::FundamentalValue(4));
1088 VerifyCurrentProxyServer(
1089 "http=a.com:1;https=4.3.2.1:2;ftp=c.com:3;socks=socks4://d.com:4",
1090 onc::ONC_SOURCE_NONE
);