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

Side by Side Diff: chrome/browser/ui/webui/options/preferences_browsertest.cc

Issue 2919343005: Remove tests for deprecated Options UI (Closed)
Patch Set: and more Created 3 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 #include "chrome/browser/ui/webui/options/preferences_browsertest.h"
6
7 #include <stddef.h>
8
9 #include <iostream>
10 #include <memory>
11 #include <sstream>
12 #include <utility>
13
14 #include "base/callback.h"
15 #include "base/json/json_reader.h"
16 #include "base/json/json_writer.h"
17 #include "base/memory/ptr_util.h"
18 #include "base/values.h"
19 #include "build/build_config.h"
20 #include "chrome/browser/chrome_notification_types.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "chrome/test/base/ui_test_utils.h"
27 #include "components/policy/core/browser/browser_policy_connector.h"
28 #include "components/policy/core/common/external_data_fetcher.h"
29 #include "components/policy/core/common/policy_map.h"
30 #include "components/policy/core/common/policy_types.h"
31 #include "components/policy/policy_constants.h"
32 #include "components/prefs/pref_service.h"
33 #include "content/public/browser/notification_details.h"
34 #include "content/public/browser/notification_source.h"
35 #include "content/public/browser/render_view_host.h"
36 #include "content/public/browser/web_contents.h"
37 #include "content/public/test/browser_test_utils.h"
38 #include "testing/gtest/include/gtest/gtest.h"
39 #include "url/gurl.h"
40
41 #if defined(OS_CHROMEOS)
42 #include "base/strings/stringprintf.h"
43 #include "chrome/browser/browser_process.h"
44 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
45 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h"
46 #include "chrome/browser/chromeos/settings/cros_settings.h"
47 #include "chrome/browser/chromeos/settings/stub_install_attributes.h"
48 #include "chromeos/dbus/dbus_thread_manager.h"
49 #include "chromeos/dbus/shill_profile_client.h"
50 #include "chromeos/dbus/shill_service_client.h"
51 #include "chromeos/network/network_state.h"
52 #include "chromeos/network/network_state_handler.h"
53 #include "chromeos/network/proxy/proxy_config_handler.h"
54 #include "chromeos/settings/cros_settings_names.h"
55 #include "components/onc/onc_pref_names.h"
56 #include "components/proxy_config/proxy_config_dictionary.h"
57 #include "components/proxy_config/proxy_config_pref_names.h"
58 #include "content/public/test/test_utils.h"
59 #include "third_party/cros_system_api/dbus/service_constants.h"
60 #endif
61
62 using testing::AllOf;
63 using testing::Mock;
64 using testing::Property;
65 using testing::Return;
66 using testing::_;
67
68 namespace base {
69
70 // Helper for pretty-printing the contents of base::Value in case of failures.
71 void PrintTo(const base::Value& value, std::ostream* stream) {
72 std::string json;
73 JSONWriter::Write(value, &json);
74 *stream << json;
75 }
76
77 } // namespace base
78
79 // Googlemock matcher for base::Value.
80 MATCHER_P(EqualsValue, expected, "") {
81 return arg && arg->Equals(expected);
82 }
83
84 PreferencesBrowserTest::PreferencesBrowserTest() {
85 }
86
87 PreferencesBrowserTest::~PreferencesBrowserTest() {
88 }
89
90 PrefService* PreferencesBrowserTest::pref_service() {
91 DCHECK(pref_change_registrar_);
92 return pref_change_registrar_->prefs();
93 }
94
95 // Navigates to the settings page, causing the JavaScript pref handling code to
96 // load and injects JavaScript testing code.
97 void PreferencesBrowserTest::SetUpOnMainThread() {
98 ui_test_utils::NavigateToURL(browser(),
99 GURL(chrome::kChromeUISettingsFrameURL));
100 SetUpPrefs();
101 }
102
103 void PreferencesBrowserTest::TearDownOnMainThread() {
104 pref_change_registrar_.reset();
105 }
106
107 void PreferencesBrowserTest::SetUpPrefs() {
108 content::WebContents* web_contents =
109 browser()->tab_strip_model()->GetActiveWebContents();
110 ASSERT_TRUE(web_contents);
111 render_view_host_ = web_contents->GetRenderViewHost();
112 ASSERT_TRUE(render_view_host_);
113
114 DCHECK(!pref_change_registrar_);
115 pref_change_registrar_.reset(new PrefChangeRegistrar);
116 pref_change_registrar_->Init(browser()->profile()->GetPrefs());
117
118 ASSERT_TRUE(content::ExecuteScript(render_view_host_,
119 "function TestEnv() {"
120 " this.sentinelName_ = 'download.prompt_for_download';"
121 " this.prefs_ = [];"
122 " TestEnv.instance_ = this;"
123 "}"
124 ""
125 "TestEnv.handleEvent = function(event) {"
126 " var env = TestEnv.instance_;"
127 " var name = event.type;"
128 " env.removePrefListener_(name);"
129 " if (name == TestEnv.sentinelName_)"
130 " env.sentinelValue_ = event.value.value;"
131 " else"
132 " env.reply_[name] = event.value;"
133 " if (env.fetching_ && !--env.fetching_ ||"
134 " !env.fetching_ && name == env.sentinelName_) {"
135 " env.removePrefListeners_();"
136 " window.domAutomationController.send(JSON.stringify(env.reply_));"
137 " delete env.reply_;"
138 " }"
139 "};"
140 ""
141 "TestEnv.prototype = {"
142 " addPrefListener_: function(name) {"
143 " Preferences.getInstance().addEventListener(name,"
144 " TestEnv.handleEvent);"
145 " },"
146 ""
147 " addPrefListeners_: function() {"
148 " for (var i in this.prefs_)"
149 " this.addPrefListener_(this.prefs_[i]);"
150 " },"
151 ""
152 " removePrefListener_: function(name) {"
153 " Preferences.getInstance().removeEventListener(name,"
154 " TestEnv.handleEvent);"
155 " },"
156 ""
157 " removePrefListeners_: function() {"
158 " for (var i in this.prefs_)"
159 " this.removePrefListener_(this.prefs_[i]);"
160 " },"
161 ""
162 ""
163 " addPref: function(name) {"
164 " this.prefs_.push(name);"
165 " },"
166 ""
167 " setupAndReply: function() {"
168 " this.reply_ = {};"
169 " Preferences.instance_ = new Preferences();"
170 " this.addPref(this.sentinelName_);"
171 " this.fetching_ = this.prefs_.length;"
172 " this.addPrefListeners_();"
173 " Preferences.getInstance().initialize();"
174 " },"
175 ""
176 " runAndReply: function(test) {"
177 " this.reply_ = {};"
178 " this.addPrefListeners_();"
179 " test();"
180 " this.sentinelValue_ = !this.sentinelValue_;"
181 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_,"
182 " true);"
183 " },"
184 ""
185 " startObserving: function() {"
186 " this.reply_ = {};"
187 " this.addPrefListeners_();"
188 " },"
189 ""
190 " finishObservingAndReply: function() {"
191 " this.sentinelValue_ = !this.sentinelValue_;"
192 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_,"
193 " true);"
194 " }"
195 "};"));
196 }
197
198 // Forwards notifications received when pref values change in the backend.
199 void PreferencesBrowserTest::OnPreferenceChanged(const std::string& pref_name) {
200 OnCommit(pref_service()->FindPreference(pref_name.c_str()));
201 }
202
203 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() {
204 // Sets up a mock policy provider for user and device policies.
205 EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
206 .WillRepeatedly(Return(true));
207 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
208 &policy_provider_);
209 }
210
211 void PreferencesBrowserTest::SetUserPolicies(
212 const std::vector<std::string>& names,
213 const std::vector<std::unique_ptr<base::Value>>& values,
214 policy::PolicyLevel level) {
215 policy::PolicyMap map;
216 for (size_t i = 0; i < names.size(); ++i) {
217 map.Set(names[i], level, policy::POLICY_SCOPE_USER,
218 policy::POLICY_SOURCE_CLOUD, values[i]->CreateDeepCopy(), nullptr);
219 }
220 policy_provider_.UpdateChromePolicy(map);
221 }
222
223 void PreferencesBrowserTest::ClearUserPolicies() {
224 policy::PolicyMap empty_policy_map;
225 policy_provider_.UpdateChromePolicy(empty_policy_map);
226 }
227
228 void PreferencesBrowserTest::SetUserValues(
229 const std::vector<std::string>& names,
230 const std::vector<std::unique_ptr<base::Value>>& values) {
231 for (size_t i = 0; i < names.size(); ++i) {
232 pref_service()->Set(names[i].c_str(), *values[i]);
233 }
234 }
235
236 void PreferencesBrowserTest::VerifyKeyValue(const base::DictionaryValue& dict,
237 const std::string& key,
238 const base::Value& expected) {
239 const base::Value* actual = NULL;
240 EXPECT_TRUE(dict.Get(key, &actual)) << "Was checking key: " << key;
241 if (actual)
242 EXPECT_EQ(expected, *actual) << "Was checking key: " << key;
243 }
244
245 void PreferencesBrowserTest::VerifyPref(
246 const base::DictionaryValue* prefs,
247 const std::string& name,
248 const std::unique_ptr<base::Value>& value,
249 const std::string& controlledBy,
250 bool disabled,
251 bool uncommitted) {
252 const base::Value* pref = NULL;
253 const base::DictionaryValue* dict = NULL;
254 ASSERT_TRUE(prefs->GetWithoutPathExpansion(name, &pref));
255 ASSERT_TRUE(pref->GetAsDictionary(&dict));
256 VerifyKeyValue(*dict, "value", *value);
257 if (!controlledBy.empty())
258 VerifyKeyValue(*dict, "controlledBy", base::Value(controlledBy));
259 else
260 EXPECT_FALSE(dict->HasKey("controlledBy"));
261
262 if (disabled)
263 VerifyKeyValue(*dict, "disabled", base::Value(true));
264 else if (dict->HasKey("disabled"))
265 VerifyKeyValue(*dict, "disabled", base::Value(false));
266
267 if (uncommitted)
268 VerifyKeyValue(*dict, "uncommitted", base::Value(true));
269 else if (dict->HasKey("uncommitted"))
270 VerifyKeyValue(*dict, "uncommitted", base::Value(false));
271 }
272
273 void PreferencesBrowserTest::VerifyObservedPref(
274 const std::string& json,
275 const std::string& name,
276 const std::unique_ptr<base::Value>& value,
277 const std::string& controlledBy,
278 bool disabled,
279 bool uncommitted) {
280 std::unique_ptr<base::Value> observed_value_ptr =
281 base::JSONReader::Read(json);
282 const base::DictionaryValue* observed_dict;
283 ASSERT_TRUE(observed_value_ptr.get());
284 ASSERT_TRUE(observed_value_ptr->GetAsDictionary(&observed_dict));
285 VerifyPref(observed_dict, name, value, controlledBy, disabled, uncommitted);
286 }
287
288 void PreferencesBrowserTest::VerifyObservedPrefs(
289 const std::string& json,
290 const std::vector<std::string>& names,
291 const std::vector<std::unique_ptr<base::Value>>& values,
292 const std::string& controlledBy,
293 bool disabled,
294 bool uncommitted) {
295 std::unique_ptr<base::Value> observed_value_ptr =
296 base::JSONReader::Read(json);
297 const base::DictionaryValue* observed_dict;
298 ASSERT_TRUE(observed_value_ptr.get());
299 ASSERT_TRUE(observed_value_ptr->GetAsDictionary(&observed_dict));
300 for (size_t i = 0; i < names.size(); ++i) {
301 VerifyPref(observed_dict, names[i], values[i], controlledBy, disabled,
302 uncommitted);
303 }
304 }
305
306 void PreferencesBrowserTest::ExpectNoCommit(const std::string& name) {
307 pref_change_registrar_->Add(
308 name.c_str(),
309 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged,
310 base::Unretained(this)));
311 EXPECT_CALL(*this, OnCommit(Property(&PrefService::Preference::name, name)))
312 .Times(0);
313 }
314
315 void PreferencesBrowserTest::ExpectSetCommit(
316 const std::string& name,
317 const std::unique_ptr<base::Value>& value) {
318 pref_change_registrar_->Add(
319 name.c_str(),
320 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged,
321 base::Unretained(this)));
322 EXPECT_CALL(
323 *this,
324 OnCommit(AllOf(Property(&PrefService::Preference::name, name),
325 Property(&PrefService::Preference::IsUserControlled, true),
326 Property(&PrefService::Preference::GetValue,
327 EqualsValue(value.get())))));
328 }
329
330 void PreferencesBrowserTest::ExpectClearCommit(const std::string& name) {
331 pref_change_registrar_->Add(
332 name.c_str(),
333 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged,
334 base::Unretained(this)));
335 EXPECT_CALL(*this, OnCommit(AllOf(
336 Property(&PrefService::Preference::name, name),
337 Property(&PrefService::Preference::IsUserControlled, false))));
338 }
339
340 void PreferencesBrowserTest::VerifyAndClearExpectations() {
341 Mock::VerifyAndClearExpectations(this);
342 pref_change_registrar_->RemoveAll();
343 }
344
345 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment(
346 const std::vector<std::string>& pref_names,
347 std::string* observed_json) const {
348 std::stringstream javascript;
349 javascript << "var testEnv = new TestEnv();";
350 for (const auto& name : pref_names) {
351 javascript << "testEnv.addPref('" << name.c_str() << "');";
352 }
353 javascript << "testEnv.setupAndReply();";
354 std::string temp_observed_json;
355 if (!observed_json)
356 observed_json = &temp_observed_json;
357 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
358 render_view_host_, javascript.str(), observed_json));
359 }
360
361 void PreferencesBrowserTest::SetPref(const std::string& name,
362 const std::string& type,
363 const std::unique_ptr<base::Value>& value,
364 bool commit,
365 std::string* observed_json) {
366 std::unique_ptr<base::Value> commit_ptr(new base::Value(commit));
367 std::stringstream javascript;
368 javascript << "testEnv.runAndReply(function() {"
369 << " Preferences.set" << type << "Pref("
370 << " '" << name << "',"
371 << " " << *value << ","
372 << " " << *commit_ptr << ");"
373 << "});";
374 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
375 render_view_host_, javascript.str(), observed_json));
376 }
377
378 void PreferencesBrowserTest::VerifySetPref(
379 const std::string& name,
380 const std::string& type,
381 const std::unique_ptr<base::Value>& value,
382 bool commit) {
383 if (commit)
384 ExpectSetCommit(name, value);
385 else
386 ExpectNoCommit(name);
387 std::string observed_json;
388 SetPref(name, type, value, commit, &observed_json);
389 VerifyObservedPref(observed_json, name, value, std::string(), false, !commit);
390 VerifyAndClearExpectations();
391 }
392
393 void PreferencesBrowserTest::VerifyClearPref(
394 const std::string& name,
395 const std::unique_ptr<base::Value>& value,
396 bool commit) {
397 if (commit)
398 ExpectClearCommit(name);
399 else
400 ExpectNoCommit(name);
401 std::string commit_json;
402 base::JSONWriter::Write(base::Value(commit), &commit_json);
403 std::stringstream javascript;
404 javascript << "testEnv.runAndReply(function() {"
405 << " Preferences.clearPref("
406 << " '" << name.c_str() << "',"
407 << " " << commit_json.c_str() << ");});";
408 std::string observed_json;
409 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
410 render_view_host_, javascript.str(), &observed_json));
411 VerifyObservedPref(observed_json, name, value, "recommended", false, !commit);
412 VerifyAndClearExpectations();
413 }
414
415 void PreferencesBrowserTest::VerifyCommit(
416 const std::string& name,
417 const std::unique_ptr<base::Value>& value,
418 const std::string& controlledBy) {
419 std::stringstream javascript;
420 javascript << "testEnv.runAndReply(function() {"
421 << " Preferences.getInstance().commitPref("
422 << " '" << name.c_str() << "');});";
423 std::string observed_json;
424 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
425 render_view_host_, javascript.str(), &observed_json));
426 VerifyObservedPref(observed_json, name, value, controlledBy, false, false);
427 }
428
429 void PreferencesBrowserTest::VerifySetCommit(
430 const std::string& name,
431 const std::unique_ptr<base::Value>& value) {
432 ExpectSetCommit(name, value);
433 VerifyCommit(name, value, std::string());
434 VerifyAndClearExpectations();
435 }
436
437 void PreferencesBrowserTest::VerifyClearCommit(
438 const std::string& name,
439 const std::unique_ptr<base::Value>& value) {
440 ExpectClearCommit(name);
441 VerifyCommit(name, value, "recommended");
442 VerifyAndClearExpectations();
443 }
444
445 void PreferencesBrowserTest::VerifyRollback(
446 const std::string& name,
447 const std::unique_ptr<base::Value>& value,
448 const std::string& controlledBy) {
449 ExpectNoCommit(name);
450 std::stringstream javascript;
451 javascript << "testEnv.runAndReply(function() {"
452 << " Preferences.getInstance().rollbackPref("
453 << " '" << name.c_str() << "');});";
454 std::string observed_json;
455 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
456 render_view_host_, javascript.str(), &observed_json));
457 VerifyObservedPref(observed_json, name, value, controlledBy, false, true);
458 VerifyAndClearExpectations();
459 }
460
461 void PreferencesBrowserTest::StartObserving() {
462 ASSERT_TRUE(content::ExecuteScript(
463 render_view_host_, "testEnv.startObserving();"));
464 }
465
466 void PreferencesBrowserTest::FinishObserving(std::string* observed_json) {
467 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
468 render_view_host_,
469 "testEnv.finishObservingAndReply();",
470 observed_json));
471 }
472
473 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref) {
474 // Boolean pref.
475 types_.push_back("Boolean");
476 pref_names_.push_back(prefs::kAlternateErrorPagesEnabled);
477 policy_names_.push_back(policy::key::kAlternateErrorPagesEnabled);
478 non_default_values_.push_back(base::MakeUnique<base::Value>(false));
479
480 // Integer pref.
481 types_.push_back("Integer");
482 pref_names_.push_back(prefs::kRestoreOnStartup);
483 policy_names_.push_back(policy::key::kRestoreOnStartup);
484 non_default_values_.push_back(base::MakeUnique<base::Value>(4));
485
486 // List pref.
487 if (includeListPref) {
488 types_.push_back("List");
489 pref_names_.push_back(prefs::kURLsToRestoreOnStartup);
490 policy_names_.push_back(policy::key::kRestoreOnStartupURLs);
491 auto list = base::MakeUnique<base::ListValue>();
492 list->AppendString("http://www.example.com");
493 list->AppendString("http://example.com");
494 non_default_values_.push_back(std::move(list));
495 }
496
497 // Retrieve default values.
498 for (const auto& name : pref_names_) {
499 default_values_.push_back(
500 pref_service()->GetDefaultPrefValue(name.c_str())->CreateDeepCopy());
501 }
502 }
503
504 // Verifies that initializing the JavaScript Preferences class fires the correct
505 // notifications in JavaScript.
506 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, FetchPrefs) {
507 UseDefaultTestPrefs(true);
508 std::string observed_json;
509
510 // Verify notifications when default values are in effect.
511 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
512 VerifyObservedPrefs(observed_json, pref_names_, default_values_,
513 std::string(), false, false);
514
515 // Verify notifications when recommended values are in effect.
516 SetUserPolicies(policy_names_, non_default_values_,
517 policy::POLICY_LEVEL_RECOMMENDED);
518 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
519 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_,
520 "recommended", false, false);
521
522 // Verify notifications when mandatory values are in effect.
523 SetUserPolicies(policy_names_, non_default_values_,
524 policy::POLICY_LEVEL_MANDATORY);
525 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
526 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "policy",
527 true, false);
528
529 // Verify notifications when user-modified values are in effect.
530 ClearUserPolicies();
531 SetUserValues(pref_names_, non_default_values_);
532 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
533 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_,
534 std::string(), false, false);
535 }
536
537 // Verifies that setting a user-modified pref value through the JavaScript
538 // Preferences class fires the correct notification in JavaScript and causes the
539 // change to be committed to the C++ backend.
540 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, SetPrefs) {
541 UseDefaultTestPrefs(false);
542
543 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
544 for (size_t i = 0; i < pref_names_.size(); ++i) {
545 VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], true);
546 }
547 }
548
549 // Verifies that clearing a user-modified pref value through the JavaScript
550 // Preferences class fires the correct notification in JavaScript and causes the
551 // change to be committed to the C++ backend.
552 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ClearPrefs) {
553 UseDefaultTestPrefs(false);
554
555 SetUserPolicies(policy_names_, default_values_,
556 policy::POLICY_LEVEL_RECOMMENDED);
557 SetUserValues(pref_names_, non_default_values_);
558 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
559 for (size_t i = 0; i < pref_names_.size(); ++i) {
560 VerifyClearPref(pref_names_[i], default_values_[i], true);
561 }
562 }
563
564 // Verifies that when the user-modified value of a dialog pref is set and the
565 // change then committed through the JavaScript Preferences class, the correct
566 // notifications fire and a commit to the C++ backend occurs in the latter step
567 // only.
568 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsSetCommit) {
569 UseDefaultTestPrefs(false);
570
571 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
572 for (size_t i = 0; i < pref_names_.size(); ++i) {
573 VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], false);
574 VerifySetCommit(pref_names_[i], non_default_values_[i]);
575 }
576 }
577
578 // Verifies that when the user-modified value of a dialog pref is set and the
579 // change then rolled back through the JavaScript Preferences class, the correct
580 // notifications fire and no commit to the C++ backend occurs.
581 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsSetRollback) {
582 UseDefaultTestPrefs(false);
583
584 // Verify behavior when default values are in effect.
585 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
586 for (size_t i = 0; i < pref_names_.size(); ++i) {
587 VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], false);
588 VerifyRollback(pref_names_[i], default_values_[i], std::string());
589 }
590
591 // Verify behavior when recommended values are in effect.
592 SetUserPolicies(policy_names_, default_values_,
593 policy::POLICY_LEVEL_RECOMMENDED);
594 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
595 for (size_t i = 0; i < pref_names_.size(); ++i) {
596 VerifySetPref(pref_names_[i], types_[i], non_default_values_[i], false);
597 VerifyRollback(pref_names_[i], default_values_[i], "recommended");
598 }
599 }
600
601 // Verifies that when the user-modified value of a dialog pref is cleared and
602 // the change then committed through the JavaScript Preferences class, the
603 // correct notifications fire and a commit to the C++ backend occurs in the
604 // latter step only.
605 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsClearCommit) {
606 UseDefaultTestPrefs(false);
607
608 SetUserPolicies(policy_names_, default_values_,
609 policy::POLICY_LEVEL_RECOMMENDED);
610 SetUserValues(pref_names_, non_default_values_);
611 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
612 for (size_t i = 0; i < pref_names_.size(); ++i) {
613 VerifyClearPref(pref_names_[i], default_values_[i], false);
614 VerifyClearCommit(pref_names_[i], default_values_[i]);
615 }
616 }
617
618 // Verifies that when the user-modified value of a dialog pref is cleared and
619 // the change then rolled back through the JavaScript Preferences class, the
620 // correct notifications fire and no commit to the C++ backend occurs.
621 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, DialogPrefsClearRollback) {
622 UseDefaultTestPrefs(false);
623
624 SetUserPolicies(policy_names_, default_values_,
625 policy::POLICY_LEVEL_RECOMMENDED);
626 SetUserValues(pref_names_, non_default_values_);
627 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
628 for (size_t i = 0; i < pref_names_.size(); ++i) {
629 VerifyClearPref(pref_names_[i], default_values_[i], false);
630 VerifyRollback(pref_names_[i], non_default_values_[i], std::string());
631 }
632 }
633
634 // Verifies that when preference values change in the C++ backend, the correct
635 // notifications fire in JavaScript.
636 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, NotificationsOnBackendChanges) {
637 UseDefaultTestPrefs(false);
638 std::string observed_json;
639
640 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
641
642 // Verify notifications when recommended values come into effect.
643 StartObserving();
644 SetUserPolicies(policy_names_, non_default_values_,
645 policy::POLICY_LEVEL_RECOMMENDED);
646 FinishObserving(&observed_json);
647 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_,
648 "recommended", false, false);
649
650 // Verify notifications when mandatory values come into effect.
651 StartObserving();
652 SetUserPolicies(policy_names_, non_default_values_,
653 policy::POLICY_LEVEL_MANDATORY);
654 FinishObserving(&observed_json);
655 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "policy",
656 true, false);
657
658 // Verify notifications when default values come into effect.
659 StartObserving();
660 ClearUserPolicies();
661 FinishObserving(&observed_json);
662 VerifyObservedPrefs(observed_json, pref_names_, default_values_,
663 std::string(), false, false);
664
665 // Verify notifications when user-modified values come into effect.
666 StartObserving();
667 SetUserValues(pref_names_, non_default_values_);
668 FinishObserving(&observed_json);
669 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_,
670 std::string(), false, false);
671 }
672
673 #if defined(OS_CHROMEOS)
674
675 // Verifies that initializing the JavaScript Preferences class fires the correct
676 // notifications in JavaScript for pref values handled by the
677 // CoreChromeOSOptionsHandler class.
678 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSDeviceFetchPrefs) {
679 std::string observed_json;
680
681 // Boolean pref.
682 pref_names_.push_back(chromeos::kAccountsPrefAllowGuest);
683 default_values_.push_back(base::MakeUnique<base::Value>(true));
684
685 // String pref.
686 pref_names_.push_back(chromeos::kReleaseChannel);
687 default_values_.push_back(base::MakeUnique<base::Value>(""));
688
689 // List pref.
690 pref_names_.push_back(chromeos::kAccountsPrefUsers);
691 default_values_.push_back(base::MakeUnique<base::ListValue>());
692
693 // Verify notifications when default values are in effect.
694 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
695 VerifyObservedPrefs(observed_json, pref_names_, default_values_, "owner",
696 true, false);
697 }
698
699 // Verifies that initializing the JavaScript Preferences class fires the correct
700 // notifications in JavaScript for non-privileged pref values handled by the
701 // CoreChromeOSOptionsHandler class.
702 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest,
703 ChromeOSDeviceFetchNonPrivilegedPrefs) {
704 std::vector<std::unique_ptr<base::Value>> decorated_non_default_values;
705 std::string observed_json;
706
707 // Non-privileged string pref.
708 pref_names_.push_back(chromeos::kSystemTimezone);
709 default_values_.push_back(
710 base::MakeUnique<base::Value>("America/Los_Angeles"));
711 non_default_values_.push_back(
712 base::MakeUnique<base::Value>("America/New_York"));
713 decorated_non_default_values.push_back(
714 non_default_values_.back()->CreateDeepCopy());
715
716 // Verify notifications when default values are in effect.
717 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
718 VerifyObservedPrefs(observed_json, pref_names_, default_values_,
719 std::string(), false, false);
720
721 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
722 cros_settings->Set(pref_names_[0], *non_default_values_[0]);
723
724 // Verify notifications when non-default values are in effect.
725 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
726 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values,
727 std::string(), false, false);
728 }
729
730 class ManagedPreferencesBrowserTest : public PreferencesBrowserTest {
731 protected:
732 // PreferencesBrowserTest implementation:
733 void SetUpInProcessBrowserTestFixture() override {
734 // Set up fake install attributes.
735 std::unique_ptr<chromeos::StubInstallAttributes> attributes =
736 base::MakeUnique<chromeos::StubInstallAttributes>();
737 attributes->SetCloudManaged("example.com", "fake-id");
738 policy::BrowserPolicyConnectorChromeOS::SetInstallAttributesForTesting(
739 attributes.release());
740
741 PreferencesBrowserTest::SetUpInProcessBrowserTestFixture();
742 }
743 };
744
745 // Verifies that initializing the JavaScript Preferences class fires the correct
746 // notifications in JavaScript for pref values handled by the
747 // CoreChromeOSOptionsHandler class for a managed device.
748 IN_PROC_BROWSER_TEST_F(ManagedPreferencesBrowserTest,
749 ChromeOSDeviceFetchPrefs) {
750 std::vector<std::unique_ptr<base::Value>> decorated_non_default_values;
751 std::string observed_json;
752
753 // Boolean pref.
754 pref_names_.push_back(chromeos::kAccountsPrefAllowGuest);
755 non_default_values_.push_back(base::MakeUnique<base::Value>(false));
756 decorated_non_default_values.push_back(
757 non_default_values_.back()->CreateDeepCopy());
758
759 // String pref.
760 pref_names_.push_back(chromeos::kReleaseChannel);
761 non_default_values_.push_back(
762 base::MakeUnique<base::Value>("stable-channel"));
763 decorated_non_default_values.push_back(
764 non_default_values_.back()->CreateDeepCopy());
765
766 // List pref.
767 pref_names_.push_back(chromeos::kAccountsPrefUsers);
768 auto list = base::MakeUnique<base::ListValue>();
769 list->AppendString("me@google.com");
770 list->AppendString("you@google.com");
771 non_default_values_.push_back(std::move(list));
772 list = base::MakeUnique<base::ListValue>();
773 auto dict = base::MakeUnique<base::DictionaryValue>();
774 dict->SetString("username", "me@google.com");
775 dict->SetString("name", "me@google.com");
776 dict->SetString("email", "");
777 dict->SetBoolean("owner", false);
778 list->Append(std::move(dict));
779 dict = base::MakeUnique<base::DictionaryValue>();
780 dict->SetString("username", "you@google.com");
781 dict->SetString("name", "you@google.com");
782 dict->SetString("email", "");
783 dict->SetBoolean("owner", false);
784 list->Append(std::move(dict));
785 decorated_non_default_values.push_back(std::move(list));
786
787 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
788 for (size_t i = 0; i < pref_names_.size(); ++i) {
789 cros_settings->Set(pref_names_[i], *non_default_values_[i]);
790 }
791
792 // Verify notifications when mandatory values are in effect.
793 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
794 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values,
795 "policy", true, false);
796 }
797
798 // Verifies that initializing the JavaScript Preferences class fires the correct
799 // notifications in JavaScript for non-privileged pref values handled by the
800 // CoreChromeOSOptionsHandler class for a managed device.
801 IN_PROC_BROWSER_TEST_F(ManagedPreferencesBrowserTest,
802 ChromeOSDeviceFetchNonPrivilegedPrefs) {
803 std::vector<std::unique_ptr<base::Value>> decorated_non_default_values;
804 std::string observed_json;
805
806 // Non-privileged string pref.
807 pref_names_.push_back(chromeos::kSystemTimezone);
808 non_default_values_.push_back(
809 base::MakeUnique<base::Value>("America/New_York"));
810 decorated_non_default_values.push_back(
811 non_default_values_.back()->CreateDeepCopy());
812
813 // Verify notifications when mandatory values are in effect.
814 chromeos::CrosSettings* cros_settings = chromeos::CrosSettings::Get();
815 cros_settings->Set(pref_names_[0], *non_default_values_[0]);
816
817 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
818 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values,
819 std::string(), false, false);
820 }
821
822 namespace {
823
824 const char* kUserProfilePath = "user_profile";
825
826 } // namespace
827
828 class ProxyPreferencesBrowserTest : public PreferencesBrowserTest {
829 public:
830 void SetUpOnMainThread() override {
831 SetupNetworkEnvironment();
832 content::RunAllPendingInMessageLoop();
833
834 std::unique_ptr<base::DictionaryValue> proxy_config_dict(
835 ProxyConfigDictionary::CreateFixedServers("127.0.0.1:8080",
836 "*.google.com, 1.2.3.4:22"));
837
838 ProxyConfigDictionary proxy_config(std::move(proxy_config_dict));
839
840 const chromeos::NetworkState* network = GetDefaultNetwork();
841 ASSERT_TRUE(network);
842 chromeos::proxy_config::SetProxyConfigForNetwork(proxy_config, *network);
843
844 std::string url = base::StringPrintf("%s?network=%s",
845 chrome::kChromeUIProxySettingsURL,
846 network->guid().c_str());
847
848 ui_test_utils::NavigateToURL(browser(), GURL(url));
849 SetUpPrefs();
850 }
851
852 protected:
853 void SetupNetworkEnvironment() {
854 chromeos::ShillProfileClient::TestInterface* profile_test =
855 chromeos::DBusThreadManager::Get()->GetShillProfileClient()
856 ->GetTestInterface();
857 chromeos::ShillServiceClient::TestInterface* service_test =
858 chromeos::DBusThreadManager::Get()->GetShillServiceClient()
859 ->GetTestInterface();
860
861 profile_test->AddProfile(kUserProfilePath, "user");
862
863 service_test->ClearServices();
864 service_test->AddService("stub_ethernet",
865 "stub_ethernet_guid",
866 "eth0",
867 shill::kTypeEthernet,
868 shill::kStateOnline,
869 true /* add_to_visible */ );
870 service_test->SetServiceProperty("stub_ethernet", shill::kProfileProperty,
871 base::Value(kUserProfilePath));
872 profile_test->AddService(kUserProfilePath, "stub_wifi2");
873 }
874
875 void SetONCPolicy(const char* policy_name, policy::PolicyScope scope) {
876 std::string onc_policy =
877 "{ \"NetworkConfigurations\": ["
878 " { \"GUID\": \"stub_ethernet_guid\","
879 " \"Type\": \"Ethernet\","
880 " \"Name\": \"My Ethernet\","
881 " \"Ethernet\": {"
882 " \"Authentication\": \"None\" },"
883 " \"ProxySettings\": {"
884 " \"PAC\": \"http://domain.com/x\","
885 " \"Type\": \"PAC\" }"
886 " }"
887 " ],"
888 " \"Type\": \"UnencryptedConfiguration\""
889 "}";
890
891 policy::PolicyMap map;
892 map.Set(policy_name, policy::POLICY_LEVEL_MANDATORY, scope,
893 policy::POLICY_SOURCE_CLOUD,
894 base::MakeUnique<base::Value>(onc_policy), nullptr);
895 policy_provider_.UpdateChromePolicy(map);
896
897 content::RunAllPendingInMessageLoop();
898 }
899
900 const chromeos::NetworkState* GetDefaultNetwork() {
901 chromeos::NetworkStateHandler* handler =
902 chromeos::NetworkHandler::Get()->network_state_handler();
903 return handler->DefaultNetwork();
904 }
905
906 void SetProxyPref(const std::string& name, const base::Value& value) {
907 std::string type;
908 switch (value.GetType()) {
909 case base::Value::Type::BOOLEAN:
910 type = "Boolean";
911 break;
912 case base::Value::Type::INTEGER:
913 type = "Integer";
914 break;
915 case base::Value::Type::STRING:
916 type = "String";
917 break;
918 default:
919 ASSERT_TRUE(false);
920 }
921
922 std::string observed_json;
923 SetPref(name, type, value.CreateDeepCopy(), true, &observed_json);
924 }
925
926 void VerifyCurrentProxyServer(const std::string& expected_server,
927 onc::ONCSource expected_source) {
928 const chromeos::NetworkState* network = GetDefaultNetwork();
929 ASSERT_TRUE(network);
930 onc::ONCSource actual_source;
931 std::unique_ptr<ProxyConfigDictionary> proxy_dict =
932 chromeos::proxy_config::GetProxyConfigForNetwork(
933 g_browser_process->local_state(), pref_service(), *network,
934 &actual_source);
935 ASSERT_TRUE(proxy_dict);
936 std::string actual_proxy_server;
937 EXPECT_TRUE(proxy_dict->GetProxyServer(&actual_proxy_server));
938 EXPECT_EQ(expected_server, actual_proxy_server);
939 EXPECT_EQ(expected_source, actual_source);
940 }
941 };
942
943 // Verifies that proxy settings are correctly pushed to JavaScript during
944 // initialization of the proxy settings page.
945 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSInitializeProxy) {
946 // Boolean pref.
947 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxySingle);
948 non_default_values_.push_back(base::MakeUnique<base::Value>(true));
949
950 // Integer prefs.
951 pref_names_.push_back(
952 chromeos::proxy_cros_settings_parser::kProxySingleHttpPort);
953 non_default_values_.push_back(base::MakeUnique<base::Value>(8080));
954
955 // String pref.
956 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxySingleHttp);
957 non_default_values_.push_back(base::MakeUnique<base::Value>("127.0.0.1"));
958
959 // List pref.
960 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxyIgnoreList);
961 auto list = base::MakeUnique<base::ListValue>();
962 list->AppendString("*.google.com");
963 list->AppendString("1.2.3.4:22");
964 non_default_values_.push_back(std::move(list));
965
966 // Verify that no policy is presented to the UI. This must be verified on the
967 // kProxyType and the kUseSharedProxies prefs.
968 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxyType);
969 non_default_values_.push_back(base::MakeUnique<base::Value>(2));
970
971 pref_names_.push_back(proxy_config::prefs::kUseSharedProxies);
972 non_default_values_.push_back(base::MakeUnique<base::Value>(false));
973
974 std::string observed_json;
975 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
976 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "",
977 false, false);
978 }
979
980 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ONCPolicy) {
981 SetONCPolicy(policy::key::kOpenNetworkConfiguration,
982 policy::POLICY_SCOPE_USER);
983
984 // Verify that per-network policy is presented to the UI. This must be
985 // verified on the kProxyType.
986 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxyType);
987 non_default_values_.push_back(base::MakeUnique<base::Value>(3));
988
989 std::string observed_json;
990 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
991 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "policy",
992 true, false);
993
994 // Verify that 'use-shared-proxies' is not affected by per-network policy.
995 pref_names_.clear();
996 non_default_values_.clear();
997 pref_names_.push_back(proxy_config::prefs::kUseSharedProxies);
998 non_default_values_.push_back(base::MakeUnique<base::Value>(false));
999
1000 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
1001 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "",
1002 false, false);
1003 }
1004
1005 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, DeviceONCPolicy) {
1006 SetONCPolicy(policy::key::kDeviceOpenNetworkConfiguration,
1007 policy::POLICY_SCOPE_MACHINE);
1008
1009 // Verify that the policy is presented to the UI. This verification must be
1010 // done on the kProxyType pref.
1011 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxyType);
1012 non_default_values_.push_back(base::MakeUnique<base::Value>(3));
1013
1014 std::string observed_json;
1015 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
1016 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "policy",
1017 true, false);
1018
1019 // Verify that 'use-shared-proxies' is not affected by per-network policy.
1020 pref_names_.clear();
1021 non_default_values_.clear();
1022 pref_names_.push_back(proxy_config::prefs::kUseSharedProxies);
1023 non_default_values_.push_back(base::MakeUnique<base::Value>(false));
1024
1025 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
1026 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "",
1027 false, false);
1028 }
1029
1030 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, UserProxyPolicy) {
1031 policy_names_.push_back(policy::key::kProxyMode);
1032 default_values_.push_back(
1033 base::MakeUnique<base::Value>(ProxyPrefs::kAutoDetectProxyModeName));
1034 SetUserPolicies(policy_names_, default_values_,
1035 policy::POLICY_LEVEL_MANDATORY);
1036 content::RunAllPendingInMessageLoop();
1037
1038 // Verify that the policy is presented to the UI. This verification must be
1039 // done on the kProxyType pref.
1040 pref_names_.push_back(chromeos::proxy_cros_settings_parser::kProxyType);
1041 non_default_values_.push_back(base::MakeUnique<base::Value>(3));
1042
1043 // Verify that 'use-shared-proxies' is controlled by the policy.
1044 pref_names_.push_back(proxy_config::prefs::kUseSharedProxies);
1045 non_default_values_.push_back(base::MakeUnique<base::Value>(false));
1046
1047 std::string observed_json;
1048 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
1049 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, "policy",
1050 true, false);
1051 }
1052
1053 // Verifies that modifications to the proxy settings are correctly pushed from
1054 // JavaScript to the ProxyConfig property stored in the network configuration.
1055 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSSetProxy) {
1056 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
1057
1058 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySingleHttpPort,
1059 base::Value(123));
1060 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySingleHttp,
1061 base::Value("www.adomain.xy"));
1062
1063 VerifyCurrentProxyServer("www.adomain.xy:123",
1064 onc::ONC_SOURCE_NONE);
1065 }
1066
1067 // Verify that default proxy ports are used and that ports can be updated
1068 // without affecting the previously set hosts.
1069 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSProxyDefaultPorts) {
1070 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL));
1071
1072 // Set to manual, per scheme proxy.
1073 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySingle,
1074 base::Value(false));
1075
1076 // Set hosts but no ports.
1077 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxyHttpUrl,
1078 base::Value("a.com"));
1079 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxyHttpsUrl,
1080 base::Value("4.3.2.1"));
1081 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxyFtpUrl,
1082 base::Value("c.com"));
1083 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySocks,
1084 base::Value("d.com"));
1085
1086 // Verify default ports.
1087 VerifyCurrentProxyServer(
1088 "http=a.com:80;https=4.3.2.1:80;ftp=c.com:80;socks=socks4://d.com:1080",
1089 onc::ONC_SOURCE_NONE);
1090
1091 // Set and verify the ports.
1092 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxyHttpPort,
1093 base::Value(1));
1094 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxyHttpsPort,
1095 base::Value(2));
1096 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxyFtpPort,
1097 base::Value(3));
1098 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySocksPort,
1099 base::Value(4));
1100
1101 VerifyCurrentProxyServer(
1102 "http=a.com:1;https=4.3.2.1:2;ftp=c.com:3;socks=socks4://d.com:4",
1103 onc::ONC_SOURCE_NONE);
1104 }
1105
1106 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698