| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/preferences_browsertest.h" | 5 #include "chrome/browser/ui/webui/options/preferences_browsertest.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <iostream> | 9 #include <iostream> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 MATCHER_P(EqualsValue, expected, "") { | 85 MATCHER_P(EqualsValue, expected, "") { |
| 86 return arg && arg->Equals(expected); | 86 return arg && arg->Equals(expected); |
| 87 } | 87 } |
| 88 | 88 |
| 89 PreferencesBrowserTest::PreferencesBrowserTest() { | 89 PreferencesBrowserTest::PreferencesBrowserTest() { |
| 90 } | 90 } |
| 91 | 91 |
| 92 PreferencesBrowserTest::~PreferencesBrowserTest() { | 92 PreferencesBrowserTest::~PreferencesBrowserTest() { |
| 93 } | 93 } |
| 94 | 94 |
| 95 PrefService* PreferencesBrowserTest::pref_service() { |
| 96 DCHECK(pref_change_registrar_); |
| 97 return pref_change_registrar_->prefs(); |
| 98 } |
| 99 |
| 95 // Navigates to the settings page, causing the JavaScript pref handling code to | 100 // Navigates to the settings page, causing the JavaScript pref handling code to |
| 96 // load and injects JavaScript testing code. | 101 // load and injects JavaScript testing code. |
| 97 void PreferencesBrowserTest::SetUpOnMainThread() { | 102 void PreferencesBrowserTest::SetUpOnMainThread() { |
| 98 ui_test_utils::NavigateToURL(browser(), | 103 ui_test_utils::NavigateToURL(browser(), |
| 99 GURL(chrome::kChromeUISettingsFrameURL)); | 104 GURL(chrome::kChromeUISettingsFrameURL)); |
| 100 SetUpPrefs(); | 105 SetUpPrefs(); |
| 101 } | 106 } |
| 102 | 107 |
| 108 void PreferencesBrowserTest::TearDownOnMainThread() { |
| 109 pref_change_registrar_.reset(); |
| 110 } |
| 111 |
| 103 void PreferencesBrowserTest::SetUpPrefs() { | 112 void PreferencesBrowserTest::SetUpPrefs() { |
| 104 content::WebContents* web_contents = | 113 content::WebContents* web_contents = |
| 105 browser()->tab_strip_model()->GetActiveWebContents(); | 114 browser()->tab_strip_model()->GetActiveWebContents(); |
| 106 ASSERT_TRUE(web_contents); | 115 ASSERT_TRUE(web_contents); |
| 107 render_view_host_ = web_contents->GetRenderViewHost(); | 116 render_view_host_ = web_contents->GetRenderViewHost(); |
| 108 ASSERT_TRUE(render_view_host_); | 117 ASSERT_TRUE(render_view_host_); |
| 109 pref_service_ = browser()->profile()->GetPrefs(); | 118 |
| 110 pref_change_registrar_.Init(pref_service_); | 119 DCHECK(!pref_change_registrar_); |
| 120 pref_change_registrar_.reset(new PrefChangeRegistrar); |
| 121 pref_change_registrar_->Init(browser()->profile()->GetPrefs()); |
| 122 |
| 111 ASSERT_TRUE(content::ExecuteScript(render_view_host_, | 123 ASSERT_TRUE(content::ExecuteScript(render_view_host_, |
| 112 "function TestEnv() {" | 124 "function TestEnv() {" |
| 113 " this.sentinelName_ = 'download.prompt_for_download';" | 125 " this.sentinelName_ = 'download.prompt_for_download';" |
| 114 " this.prefs_ = [];" | 126 " this.prefs_ = [];" |
| 115 " TestEnv.instance_ = this;" | 127 " TestEnv.instance_ = this;" |
| 116 "}" | 128 "}" |
| 117 "" | 129 "" |
| 118 "TestEnv.handleEvent = function(event) {" | 130 "TestEnv.handleEvent = function(event) {" |
| 119 " var env = TestEnv.instance_;" | 131 " var env = TestEnv.instance_;" |
| 120 " var name = event.type;" | 132 " var name = event.type;" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 " finishObservingAndReply: function() {" | 195 " finishObservingAndReply: function() {" |
| 184 " this.sentinelValue_ = !this.sentinelValue_;" | 196 " this.sentinelValue_ = !this.sentinelValue_;" |
| 185 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," | 197 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," |
| 186 " true);" | 198 " true);" |
| 187 " }" | 199 " }" |
| 188 "};")); | 200 "};")); |
| 189 } | 201 } |
| 190 | 202 |
| 191 // Forwards notifications received when pref values change in the backend. | 203 // Forwards notifications received when pref values change in the backend. |
| 192 void PreferencesBrowserTest::OnPreferenceChanged(const std::string& pref_name) { | 204 void PreferencesBrowserTest::OnPreferenceChanged(const std::string& pref_name) { |
| 193 OnCommit(pref_service_->FindPreference(pref_name.c_str())); | 205 OnCommit(pref_service()->FindPreference(pref_name.c_str())); |
| 194 } | 206 } |
| 195 | 207 |
| 196 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() { | 208 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() { |
| 197 // Sets up a mock policy provider for user and device policies. | 209 // Sets up a mock policy provider for user and device policies. |
| 198 EXPECT_CALL(policy_provider_, IsInitializationComplete(_)) | 210 EXPECT_CALL(policy_provider_, IsInitializationComplete(_)) |
| 199 .WillRepeatedly(Return(true)); | 211 .WillRepeatedly(Return(true)); |
| 200 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( | 212 policy::BrowserPolicyConnector::SetPolicyProviderForTesting( |
| 201 &policy_provider_); | 213 &policy_provider_); |
| 202 } | 214 } |
| 203 | 215 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 215 | 227 |
| 216 void PreferencesBrowserTest::ClearUserPolicies() { | 228 void PreferencesBrowserTest::ClearUserPolicies() { |
| 217 policy::PolicyMap empty_policy_map; | 229 policy::PolicyMap empty_policy_map; |
| 218 policy_provider_.UpdateChromePolicy(empty_policy_map); | 230 policy_provider_.UpdateChromePolicy(empty_policy_map); |
| 219 } | 231 } |
| 220 | 232 |
| 221 void PreferencesBrowserTest::SetUserValues( | 233 void PreferencesBrowserTest::SetUserValues( |
| 222 const std::vector<std::string>& names, | 234 const std::vector<std::string>& names, |
| 223 const std::vector<base::Value*>& values) { | 235 const std::vector<base::Value*>& values) { |
| 224 for (size_t i = 0; i < names.size(); ++i) { | 236 for (size_t i = 0; i < names.size(); ++i) { |
| 225 pref_service_->Set(names[i].c_str(), *values[i]); | 237 pref_service()->Set(names[i].c_str(), *values[i]); |
| 226 } | 238 } |
| 227 } | 239 } |
| 228 | 240 |
| 229 void PreferencesBrowserTest::VerifyKeyValue(const base::DictionaryValue& dict, | 241 void PreferencesBrowserTest::VerifyKeyValue(const base::DictionaryValue& dict, |
| 230 const std::string& key, | 242 const std::string& key, |
| 231 const base::Value& expected) { | 243 const base::Value& expected) { |
| 232 const base::Value* actual = NULL; | 244 const base::Value* actual = NULL; |
| 233 EXPECT_TRUE(dict.Get(key, &actual)) << "Was checking key: " << key; | 245 EXPECT_TRUE(dict.Get(key, &actual)) << "Was checking key: " << key; |
| 234 if (actual) | 246 if (actual) |
| 235 EXPECT_EQ(expected, *actual) << "Was checking key: " << key; | 247 EXPECT_EQ(expected, *actual) << "Was checking key: " << key; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 const base::DictionaryValue* observed_dict; | 300 const base::DictionaryValue* observed_dict; |
| 289 ASSERT_TRUE(observed_value_ptr.get()); | 301 ASSERT_TRUE(observed_value_ptr.get()); |
| 290 ASSERT_TRUE(observed_value_ptr->GetAsDictionary(&observed_dict)); | 302 ASSERT_TRUE(observed_value_ptr->GetAsDictionary(&observed_dict)); |
| 291 for (size_t i = 0; i < names.size(); ++i) { | 303 for (size_t i = 0; i < names.size(); ++i) { |
| 292 VerifyPref(observed_dict, names[i], values[i], controlledBy, disabled, | 304 VerifyPref(observed_dict, names[i], values[i], controlledBy, disabled, |
| 293 uncommitted); | 305 uncommitted); |
| 294 } | 306 } |
| 295 } | 307 } |
| 296 | 308 |
| 297 void PreferencesBrowserTest::ExpectNoCommit(const std::string& name) { | 309 void PreferencesBrowserTest::ExpectNoCommit(const std::string& name) { |
| 298 pref_change_registrar_.Add( | 310 pref_change_registrar_->Add( |
| 299 name.c_str(), | 311 name.c_str(), |
| 300 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged, | 312 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged, |
| 301 base::Unretained(this))); | 313 base::Unretained(this))); |
| 302 EXPECT_CALL(*this, OnCommit(Property(&PrefService::Preference::name, name))) | 314 EXPECT_CALL(*this, OnCommit(Property(&PrefService::Preference::name, name))) |
| 303 .Times(0); | 315 .Times(0); |
| 304 } | 316 } |
| 305 | 317 |
| 306 void PreferencesBrowserTest::ExpectSetCommit(const std::string& name, | 318 void PreferencesBrowserTest::ExpectSetCommit(const std::string& name, |
| 307 const base::Value* value) { | 319 const base::Value* value) { |
| 308 pref_change_registrar_.Add( | 320 pref_change_registrar_->Add( |
| 309 name.c_str(), | 321 name.c_str(), |
| 310 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged, | 322 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged, |
| 311 base::Unretained(this))); | 323 base::Unretained(this))); |
| 312 EXPECT_CALL(*this, OnCommit(AllOf( | 324 EXPECT_CALL(*this, OnCommit(AllOf( |
| 313 Property(&PrefService::Preference::name, name), | 325 Property(&PrefService::Preference::name, name), |
| 314 Property(&PrefService::Preference::IsUserControlled, true), | 326 Property(&PrefService::Preference::IsUserControlled, true), |
| 315 Property(&PrefService::Preference::GetValue, EqualsValue(value))))); | 327 Property(&PrefService::Preference::GetValue, EqualsValue(value))))); |
| 316 } | 328 } |
| 317 | 329 |
| 318 void PreferencesBrowserTest::ExpectClearCommit(const std::string& name) { | 330 void PreferencesBrowserTest::ExpectClearCommit(const std::string& name) { |
| 319 pref_change_registrar_.Add( | 331 pref_change_registrar_->Add( |
| 320 name.c_str(), | 332 name.c_str(), |
| 321 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged, | 333 base::Bind(&PreferencesBrowserTest::OnPreferenceChanged, |
| 322 base::Unretained(this))); | 334 base::Unretained(this))); |
| 323 EXPECT_CALL(*this, OnCommit(AllOf( | 335 EXPECT_CALL(*this, OnCommit(AllOf( |
| 324 Property(&PrefService::Preference::name, name), | 336 Property(&PrefService::Preference::name, name), |
| 325 Property(&PrefService::Preference::IsUserControlled, false)))); | 337 Property(&PrefService::Preference::IsUserControlled, false)))); |
| 326 } | 338 } |
| 327 | 339 |
| 328 void PreferencesBrowserTest::VerifyAndClearExpectations() { | 340 void PreferencesBrowserTest::VerifyAndClearExpectations() { |
| 329 Mock::VerifyAndClearExpectations(this); | 341 Mock::VerifyAndClearExpectations(this); |
| 330 pref_change_registrar_.RemoveAll(); | 342 pref_change_registrar_->RemoveAll(); |
| 331 } | 343 } |
| 332 | 344 |
| 333 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment( | 345 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment( |
| 334 const std::vector<std::string>& pref_names, | 346 const std::vector<std::string>& pref_names, |
| 335 std::string* observed_json) const { | 347 std::string* observed_json) const { |
| 336 std::stringstream javascript; | 348 std::stringstream javascript; |
| 337 javascript << "var testEnv = new TestEnv();"; | 349 javascript << "var testEnv = new TestEnv();"; |
| 338 for (std::vector<std::string>::const_iterator name = pref_names.begin(); | 350 for (std::vector<std::string>::const_iterator name = pref_names.begin(); |
| 339 name != pref_names.end(); ++name) { | 351 name != pref_names.end(); ++name) { |
| 340 javascript << "testEnv.addPref('" << name->c_str() << "');"; | 352 javascript << "testEnv.addPref('" << name->c_str() << "');"; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 base::ListValue* list = new base::ListValue; | 486 base::ListValue* list = new base::ListValue; |
| 475 list->AppendString("http://www.example.com"); | 487 list->AppendString("http://www.example.com"); |
| 476 list->AppendString("http://example.com"); | 488 list->AppendString("http://example.com"); |
| 477 non_default_values_.push_back(list); | 489 non_default_values_.push_back(list); |
| 478 } | 490 } |
| 479 | 491 |
| 480 // Retrieve default values. | 492 // Retrieve default values. |
| 481 for (std::vector<std::string>::const_iterator name = pref_names_.begin(); | 493 for (std::vector<std::string>::const_iterator name = pref_names_.begin(); |
| 482 name != pref_names_.end(); ++name) { | 494 name != pref_names_.end(); ++name) { |
| 483 default_values_.push_back( | 495 default_values_.push_back( |
| 484 pref_service_->GetDefaultPrefValue(name->c_str())->DeepCopy()); | 496 pref_service()->GetDefaultPrefValue(name->c_str())->DeepCopy()); |
| 485 } | 497 } |
| 486 } | 498 } |
| 487 | 499 |
| 488 // Verifies that initializing the JavaScript Preferences class fires the correct | 500 // Verifies that initializing the JavaScript Preferences class fires the correct |
| 489 // notifications in JavaScript. | 501 // notifications in JavaScript. |
| 490 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, FetchPrefs) { | 502 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, FetchPrefs) { |
| 491 UseDefaultTestPrefs(true); | 503 UseDefaultTestPrefs(true); |
| 492 std::string observed_json; | 504 std::string observed_json; |
| 493 | 505 |
| 494 // Verify notifications when default values are in effect. | 506 // Verify notifications when default values are in effect. |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 SetPref(name, type, &value, true, &observed_json); | 929 SetPref(name, type, &value, true, &observed_json); |
| 918 } | 930 } |
| 919 | 931 |
| 920 void VerifyCurrentProxyServer(const std::string& expected_server, | 932 void VerifyCurrentProxyServer(const std::string& expected_server, |
| 921 onc::ONCSource expected_source) { | 933 onc::ONCSource expected_source) { |
| 922 const chromeos::NetworkState* network = GetDefaultNetwork(); | 934 const chromeos::NetworkState* network = GetDefaultNetwork(); |
| 923 ASSERT_TRUE(network); | 935 ASSERT_TRUE(network); |
| 924 onc::ONCSource actual_source; | 936 onc::ONCSource actual_source; |
| 925 std::unique_ptr<ProxyConfigDictionary> proxy_dict = | 937 std::unique_ptr<ProxyConfigDictionary> proxy_dict = |
| 926 chromeos::proxy_config::GetProxyConfigForNetwork( | 938 chromeos::proxy_config::GetProxyConfigForNetwork( |
| 927 g_browser_process->local_state(), pref_service_, *network, | 939 g_browser_process->local_state(), pref_service(), *network, |
| 928 &actual_source); | 940 &actual_source); |
| 929 ASSERT_TRUE(proxy_dict); | 941 ASSERT_TRUE(proxy_dict); |
| 930 std::string actual_proxy_server; | 942 std::string actual_proxy_server; |
| 931 EXPECT_TRUE(proxy_dict->GetProxyServer(&actual_proxy_server)); | 943 EXPECT_TRUE(proxy_dict->GetProxyServer(&actual_proxy_server)); |
| 932 EXPECT_EQ(expected_server, actual_proxy_server); | 944 EXPECT_EQ(expected_server, actual_proxy_server); |
| 933 EXPECT_EQ(expected_source, actual_source); | 945 EXPECT_EQ(expected_source, actual_source); |
| 934 } | 946 } |
| 935 }; | 947 }; |
| 936 | 948 |
| 937 // Verifies that proxy settings are correctly pushed to JavaScript during | 949 // Verifies that proxy settings are correctly pushed to JavaScript during |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1094 base::FundamentalValue(3)); | 1106 base::FundamentalValue(3)); |
| 1095 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySocksPort, | 1107 SetProxyPref(chromeos::proxy_cros_settings_parser::kProxySocksPort, |
| 1096 base::FundamentalValue(4)); | 1108 base::FundamentalValue(4)); |
| 1097 | 1109 |
| 1098 VerifyCurrentProxyServer( | 1110 VerifyCurrentProxyServer( |
| 1099 "http=a.com:1;https=4.3.2.1:2;ftp=c.com:3;socks=socks4://d.com:4", | 1111 "http=a.com:1;https=4.3.2.1:2;ftp=c.com:3;socks=socks4://d.com:4", |
| 1100 onc::ONC_SOURCE_NONE); | 1112 onc::ONC_SOURCE_NONE); |
| 1101 } | 1113 } |
| 1102 | 1114 |
| 1103 #endif | 1115 #endif |
| OLD | NEW |