| 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 <iostream> | 7 #include <iostream> |
| 8 #include <sstream> | 8 #include <sstream> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 void PreferencesBrowserTest::SetUpOnMainThread() { | 74 void PreferencesBrowserTest::SetUpOnMainThread() { |
| 75 ui_test_utils::NavigateToURL(browser(), | 75 ui_test_utils::NavigateToURL(browser(), |
| 76 GURL(chrome::kChromeUISettingsFrameURL)); | 76 GURL(chrome::kChromeUISettingsFrameURL)); |
| 77 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); | 77 content::WebContents* web_contents = chrome::GetActiveWebContents(browser()); |
| 78 ASSERT_TRUE(web_contents); | 78 ASSERT_TRUE(web_contents); |
| 79 render_view_host_ = web_contents->GetRenderViewHost(); | 79 render_view_host_ = web_contents->GetRenderViewHost(); |
| 80 ASSERT_TRUE(render_view_host_); | 80 ASSERT_TRUE(render_view_host_); |
| 81 pref_change_registrar_.Init( | 81 pref_change_registrar_.Init( |
| 82 PrefServiceBase::FromBrowserContext(browser()->profile())); | 82 PrefServiceBase::FromBrowserContext(browser()->profile())); |
| 83 pref_service_ = browser()->profile()->GetPrefs(); | 83 pref_service_ = browser()->profile()->GetPrefs(); |
| 84 ASSERT_TRUE(content::ExecuteJavaScript(render_view_host_, | 84 ASSERT_TRUE(content::ExecuteJavaScript(render_view_host_, L"", |
| 85 "", | 85 L"function TestEnv() {" |
| 86 "function TestEnv() {" | 86 L" this.sentinelName_ = 'download.prompt_for_download';" |
| 87 " this.sentinelName_ = 'download.prompt_for_download';" | 87 L" this.prefs_ = [];" |
| 88 " this.prefs_ = [];" | 88 L" TestEnv.instance_ = this;" |
| 89 " TestEnv.instance_ = this;" | 89 L"}" |
| 90 "}" | 90 L"" |
| 91 "" | 91 L"TestEnv.handleEvent = function(event) {" |
| 92 "TestEnv.handleEvent = function(event) {" | 92 L" var env = TestEnv.instance_;" |
| 93 " var env = TestEnv.instance_;" | 93 L" var name = event.type;" |
| 94 " var name = event.type;" | 94 L" env.removePrefListener_(name);" |
| 95 " env.removePrefListener_(name);" | 95 L" if (name == TestEnv.sentinelName_)" |
| 96 " if (name == TestEnv.sentinelName_)" | 96 L" env.sentinelValue_ = event.value.value;" |
| 97 " env.sentinelValue_ = event.value.value;" | 97 L" else" |
| 98 " else" | 98 L" env.reply_[name] = event.value;" |
| 99 " env.reply_[name] = event.value;" | 99 L" if (env.fetching_ && !--env.fetching_ ||" |
| 100 " if (env.fetching_ && !--env.fetching_ ||" | 100 L" !env.fetching_ && name == env.sentinelName_) {" |
| 101 " !env.fetching_ && name == env.sentinelName_) {" | 101 L" env.removePrefListeners_();" |
| 102 " env.removePrefListeners_();" | 102 L" window.domAutomationController.send(JSON.stringify(env.reply_));" |
| 103 " window.domAutomationController.send(JSON.stringify(env.reply_));" | 103 L" delete env.reply_;" |
| 104 " delete env.reply_;" | 104 L" }" |
| 105 " }" | 105 L"};" |
| 106 "};" | 106 L"" |
| 107 "" | 107 L"TestEnv.prototype = {" |
| 108 "TestEnv.prototype = {" | 108 L" addPrefListener_: function(name) {" |
| 109 " addPrefListener_: function(name) {" | 109 L" Preferences.getInstance().addEventListener(name," |
| 110 " Preferences.getInstance().addEventListener(name," | 110 L" TestEnv.handleEvent);" |
| 111 " TestEnv.handleEvent);" | 111 L" }," |
| 112 " }," | 112 L"" |
| 113 "" | 113 L" addPrefListeners_: function() {" |
| 114 " addPrefListeners_: function() {" | 114 L" for (var i in this.prefs_)" |
| 115 " for (var i in this.prefs_)" | 115 L" this.addPrefListener_(this.prefs_[i]);" |
| 116 " this.addPrefListener_(this.prefs_[i]);" | 116 L" }," |
| 117 " }," | 117 L"" |
| 118 "" | 118 L" removePrefListener_: function(name) {" |
| 119 " removePrefListener_: function(name) {" | 119 L" Preferences.getInstance().removeEventListener(name," |
| 120 " Preferences.getInstance().removeEventListener(name," | 120 L" TestEnv.handleEvent);" |
| 121 " TestEnv.handleEvent);" | 121 L" }," |
| 122 " }," | 122 L"" |
| 123 "" | 123 L" removePrefListeners_: function() {" |
| 124 " removePrefListeners_: function() {" | 124 L" for (var i in this.prefs_)" |
| 125 " for (var i in this.prefs_)" | 125 L" this.removePrefListener_(this.prefs_[i]);" |
| 126 " this.removePrefListener_(this.prefs_[i]);" | 126 L" }," |
| 127 " }," | 127 L"" |
| 128 "" | 128 L"" |
| 129 "" | 129 L" addPref: function(name) {" |
| 130 " addPref: function(name) {" | 130 L" this.prefs_.push(name);" |
| 131 " this.prefs_.push(name);" | 131 L" }," |
| 132 " }," | 132 L"" |
| 133 "" | 133 L" setupAndReply: function() {" |
| 134 " setupAndReply: function() {" | 134 L" this.reply_ = {};" |
| 135 " this.reply_ = {};" | 135 L" Preferences.instance_ = new Preferences();" |
| 136 " Preferences.instance_ = new Preferences();" | 136 L" this.addPref(this.sentinelName_);" |
| 137 " this.addPref(this.sentinelName_);" | 137 L" this.fetching_ = this.prefs_.length;" |
| 138 " this.fetching_ = this.prefs_.length;" | 138 L" this.addPrefListeners_();" |
| 139 " this.addPrefListeners_();" | 139 L" Preferences.getInstance().initialize();" |
| 140 " Preferences.getInstance().initialize();" | 140 L" }," |
| 141 " }," | 141 L"" |
| 142 "" | 142 L" runAndReply: function(test) {" |
| 143 " runAndReply: function(test) {" | 143 L" this.reply_ = {};" |
| 144 " this.reply_ = {};" | 144 L" this.addPrefListeners_();" |
| 145 " this.addPrefListeners_();" | 145 L" test();" |
| 146 " test();" | 146 L" this.sentinelValue_ = !this.sentinelValue_;" |
| 147 " this.sentinelValue_ = !this.sentinelValue_;" | 147 L" Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," |
| 148 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," | 148 L" true);" |
| 149 " true);" | 149 L" }," |
| 150 " }," | 150 L"" |
| 151 "" | 151 L" startObserving: function() {" |
| 152 " startObserving: function() {" | 152 L" this.reply_ = {};" |
| 153 " this.reply_ = {};" | 153 L" this.addPrefListeners_();" |
| 154 " this.addPrefListeners_();" | 154 L" }," |
| 155 " }," | 155 L"" |
| 156 "" | 156 L" finishObservingAndReply: function() {" |
| 157 " finishObservingAndReply: function() {" | 157 L" this.sentinelValue_ = !this.sentinelValue_;" |
| 158 " this.sentinelValue_ = !this.sentinelValue_;" | 158 L" Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," |
| 159 " Preferences.setBooleanPref(this.sentinelName_, this.sentinelValue_," | 159 L" true);" |
| 160 " true);" | 160 L" }" |
| 161 " }" | 161 L"};")); |
| 162 "};")); | |
| 163 } | 162 } |
| 164 | 163 |
| 165 // Forwards notifications received when pref values change in the backend. | 164 // Forwards notifications received when pref values change in the backend. |
| 166 void PreferencesBrowserTest::OnPreferenceChanged(const std::string& pref_name) { | 165 void PreferencesBrowserTest::OnPreferenceChanged(const std::string& pref_name) { |
| 167 OnCommit(pref_service_->FindPreference(pref_name.c_str())); | 166 OnCommit(pref_service_->FindPreference(pref_name.c_str())); |
| 168 } | 167 } |
| 169 | 168 |
| 170 // Sets up a mock user policy provider. | 169 // Sets up a mock user policy provider. |
| 171 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() { | 170 void PreferencesBrowserTest::SetUpInProcessBrowserTestFixture() { |
| 172 EXPECT_CALL(policy_provider_, IsInitializationComplete()) | 171 EXPECT_CALL(policy_provider_, IsInitializationComplete()) |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 296 } |
| 298 | 297 |
| 299 void PreferencesBrowserTest::VerifyAndClearExpectations() { | 298 void PreferencesBrowserTest::VerifyAndClearExpectations() { |
| 300 Mock::VerifyAndClearExpectations(this); | 299 Mock::VerifyAndClearExpectations(this); |
| 301 pref_change_registrar_.RemoveAll(); | 300 pref_change_registrar_.RemoveAll(); |
| 302 } | 301 } |
| 303 | 302 |
| 304 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment( | 303 void PreferencesBrowserTest::SetupJavaScriptTestEnvironment( |
| 305 const std::vector<std::string>& pref_names, | 304 const std::vector<std::string>& pref_names, |
| 306 std::string* observed_json) const { | 305 std::string* observed_json) const { |
| 307 std::stringstream javascript; | 306 std::wstringstream javascript; |
| 308 javascript << "var testEnv = new TestEnv();"; | 307 javascript << "var testEnv = new TestEnv();"; |
| 309 for (std::vector<std::string>::const_iterator name = pref_names.begin(); | 308 for (std::vector<std::string>::const_iterator name = pref_names.begin(); |
| 310 name != pref_names.end(); ++name) | 309 name != pref_names.end(); ++name) |
| 311 javascript << "testEnv.addPref('" << name->c_str() << "');"; | 310 javascript << "testEnv.addPref('" << name->c_str() << "');"; |
| 312 javascript << "testEnv.setupAndReply();"; | 311 javascript << "testEnv.setupAndReply();"; |
| 313 std::string temp_observed_json; | 312 std::string temp_observed_json; |
| 314 if (!observed_json) | 313 if (!observed_json) |
| 315 observed_json = &temp_observed_json; | 314 observed_json = &temp_observed_json; |
| 316 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 315 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 317 render_view_host_, "", javascript.str(), observed_json)); | 316 render_view_host_, L"", javascript.str(), observed_json)); |
| 318 } | 317 } |
| 319 | 318 |
| 320 void PreferencesBrowserTest::VerifySetPref(const std::string& name, | 319 void PreferencesBrowserTest::VerifySetPref(const std::string& name, |
| 321 const std::string& type, | 320 const std::string& type, |
| 322 const base::Value* value, | 321 const base::Value* value, |
| 323 bool commit) { | 322 bool commit) { |
| 324 if (commit) | 323 if (commit) |
| 325 ExpectSetCommit(name, value); | 324 ExpectSetCommit(name, value); |
| 326 else | 325 else |
| 327 ExpectNoCommit(name); | 326 ExpectNoCommit(name); |
| 328 scoped_ptr<base::Value> commit_ptr(new base::FundamentalValue(commit)); | 327 scoped_ptr<base::Value> commit_ptr(new base::FundamentalValue(commit)); |
| 329 std::string value_json; | 328 std::string value_json; |
| 330 std::string commit_json; | 329 std::string commit_json; |
| 331 base::JSONWriter::Write(value, &value_json); | 330 base::JSONWriter::Write(value, &value_json); |
| 332 base::JSONWriter::Write(commit_ptr.get(), &commit_json); | 331 base::JSONWriter::Write(commit_ptr.get(), &commit_json); |
| 333 std::stringstream javascript; | 332 std::wstringstream javascript; |
| 334 javascript << "testEnv.runAndReply(function() {" | 333 javascript << "testEnv.runAndReply(function() {" |
| 335 << " Preferences.set" << type.c_str() << "Pref(" | 334 << " Preferences.set" << type.c_str() << "Pref(" |
| 336 << " '" << name.c_str() << "'," | 335 << " '" << name.c_str() << "'," |
| 337 << " " << value_json.c_str() << "," | 336 << " " << value_json.c_str() << "," |
| 338 << " " << commit_json.c_str() << ");});"; | 337 << " " << commit_json.c_str() << ");});"; |
| 339 std::string observed_json; | 338 std::string observed_json; |
| 340 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 339 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 341 render_view_host_, "", javascript.str(), &observed_json)); | 340 render_view_host_, L"", javascript.str(), &observed_json)); |
| 342 VerifyObservedPref(observed_json, name, value, "", false, !commit); | 341 VerifyObservedPref(observed_json, name, value, "", false, !commit); |
| 343 VerifyAndClearExpectations(); | 342 VerifyAndClearExpectations(); |
| 344 } | 343 } |
| 345 | 344 |
| 346 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, | 345 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, |
| 347 const base::Value* value, | 346 const base::Value* value, |
| 348 bool commit) { | 347 bool commit) { |
| 349 if (commit) | 348 if (commit) |
| 350 ExpectClearCommit(name); | 349 ExpectClearCommit(name); |
| 351 else | 350 else |
| 352 ExpectNoCommit(name); | 351 ExpectNoCommit(name); |
| 353 scoped_ptr<base::Value> commit_ptr(new base::FundamentalValue(commit)); | 352 scoped_ptr<base::Value> commit_ptr(new base::FundamentalValue(commit)); |
| 354 std::string commit_json; | 353 std::string commit_json; |
| 355 base::JSONWriter::Write(commit_ptr.get(), &commit_json); | 354 base::JSONWriter::Write(commit_ptr.get(), &commit_json); |
| 356 std::stringstream javascript; | 355 std::wstringstream javascript; |
| 357 javascript << "testEnv.runAndReply(function() {" | 356 javascript << "testEnv.runAndReply(function() {" |
| 358 << " Preferences.clearPref(" | 357 << " Preferences.clearPref(" |
| 359 << " '" << name.c_str() << "'," | 358 << " '" << name.c_str() << "'," |
| 360 << " " << commit_json.c_str() << ");});"; | 359 << " " << commit_json.c_str() << ");});"; |
| 361 std::string observed_json; | 360 std::string observed_json; |
| 362 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 361 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 363 render_view_host_, "", javascript.str(), &observed_json)); | 362 render_view_host_, L"", javascript.str(), &observed_json)); |
| 364 VerifyObservedPref(observed_json, name, value, "recommended", false, !commit); | 363 VerifyObservedPref(observed_json, name, value, "recommended", false, !commit); |
| 365 VerifyAndClearExpectations(); | 364 VerifyAndClearExpectations(); |
| 366 } | 365 } |
| 367 | 366 |
| 368 void PreferencesBrowserTest::VerifyCommit(const std::string& name, | 367 void PreferencesBrowserTest::VerifyCommit(const std::string& name, |
| 369 const base::Value* value, | 368 const base::Value* value, |
| 370 const std::string& controlledBy) { | 369 const std::string& controlledBy) { |
| 371 std::stringstream javascript; | 370 std::wstringstream javascript; |
| 372 javascript << "testEnv.runAndReply(function() {" | 371 javascript << "testEnv.runAndReply(function() {" |
| 373 << " Preferences.getInstance().commitPref(" | 372 << " Preferences.getInstance().commitPref(" |
| 374 << " '" << name.c_str() << "');});"; | 373 << " '" << name.c_str() << "');});"; |
| 375 std::string observed_json; | 374 std::string observed_json; |
| 376 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 375 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 377 render_view_host_, "", javascript.str(), &observed_json)); | 376 render_view_host_, L"", javascript.str(), &observed_json)); |
| 378 VerifyObservedPref(observed_json, name, value, controlledBy, false, false); | 377 VerifyObservedPref(observed_json, name, value, controlledBy, false, false); |
| 379 } | 378 } |
| 380 | 379 |
| 381 void PreferencesBrowserTest::VerifySetCommit(const std::string& name, | 380 void PreferencesBrowserTest::VerifySetCommit(const std::string& name, |
| 382 const base::Value* value) { | 381 const base::Value* value) { |
| 383 ExpectSetCommit(name, value); | 382 ExpectSetCommit(name, value); |
| 384 VerifyCommit(name, value, ""); | 383 VerifyCommit(name, value, ""); |
| 385 VerifyAndClearExpectations(); | 384 VerifyAndClearExpectations(); |
| 386 } | 385 } |
| 387 | 386 |
| 388 void PreferencesBrowserTest::VerifyClearCommit(const std::string& name, | 387 void PreferencesBrowserTest::VerifyClearCommit(const std::string& name, |
| 389 const base::Value* value) { | 388 const base::Value* value) { |
| 390 ExpectClearCommit(name); | 389 ExpectClearCommit(name); |
| 391 VerifyCommit(name, value, "recommended"); | 390 VerifyCommit(name, value, "recommended"); |
| 392 VerifyAndClearExpectations(); | 391 VerifyAndClearExpectations(); |
| 393 } | 392 } |
| 394 | 393 |
| 395 void PreferencesBrowserTest::VerifyRollback(const std::string& name, | 394 void PreferencesBrowserTest::VerifyRollback(const std::string& name, |
| 396 const base::Value* value, | 395 const base::Value* value, |
| 397 const std::string& controlledBy) { | 396 const std::string& controlledBy) { |
| 398 ExpectNoCommit(name); | 397 ExpectNoCommit(name); |
| 399 std::stringstream javascript; | 398 std::wstringstream javascript; |
| 400 javascript << "testEnv.runAndReply(function() {" | 399 javascript << "testEnv.runAndReply(function() {" |
| 401 << " Preferences.getInstance().rollbackPref(" | 400 << " Preferences.getInstance().rollbackPref(" |
| 402 << " '" << name.c_str() << "');});"; | 401 << " '" << name.c_str() << "');});"; |
| 403 std::string observed_json; | 402 std::string observed_json; |
| 404 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 403 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 405 render_view_host_, "", javascript.str(), &observed_json)); | 404 render_view_host_, L"", javascript.str(), &observed_json)); |
| 406 VerifyObservedPref(observed_json, name, value, controlledBy, false, true); | 405 VerifyObservedPref(observed_json, name, value, controlledBy, false, true); |
| 407 VerifyAndClearExpectations(); | 406 VerifyAndClearExpectations(); |
| 408 } | 407 } |
| 409 | 408 |
| 410 void PreferencesBrowserTest::StartObserving() { | 409 void PreferencesBrowserTest::StartObserving() { |
| 411 ASSERT_TRUE(content::ExecuteJavaScript( | 410 ASSERT_TRUE(content::ExecuteJavaScript( |
| 412 render_view_host_, "", "testEnv.startObserving();")); | 411 render_view_host_, L"", L"testEnv.startObserving();")); |
| 413 } | 412 } |
| 414 | 413 |
| 415 void PreferencesBrowserTest::FinishObserving(std::string* observed_json) { | 414 void PreferencesBrowserTest::FinishObserving(std::string* observed_json) { |
| 416 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( | 415 ASSERT_TRUE(content::ExecuteJavaScriptAndExtractString( |
| 417 render_view_host_, | 416 render_view_host_, L"", L"testEnv.finishObservingAndReply();", |
| 418 "", | |
| 419 "testEnv.finishObservingAndReply();", | |
| 420 observed_json)); | 417 observed_json)); |
| 421 } | 418 } |
| 422 | 419 |
| 423 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref) { | 420 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref) { |
| 424 // Boolean pref. | 421 // Boolean pref. |
| 425 types_.push_back("Boolean"); | 422 types_.push_back("Boolean"); |
| 426 pref_names_.push_back(prefs::kAlternateErrorPagesEnabled); | 423 pref_names_.push_back(prefs::kAlternateErrorPagesEnabled); |
| 427 policy_names_.push_back(policy::key::kAlternateErrorPagesEnabled); | 424 policy_names_.push_back(policy::key::kAlternateErrorPagesEnabled); |
| 428 non_default_values_.push_back(new base::FundamentalValue(false)); | 425 non_default_values_.push_back(new base::FundamentalValue(false)); |
| 429 | 426 |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 // Do not set the Boolean pref. It will toogle automatically. | 732 // Do not set the Boolean pref. It will toogle automatically. |
| 736 for (size_t i = 1; i < pref_names_.size(); ++i) | 733 for (size_t i = 1; i < pref_names_.size(); ++i) |
| 737 chromeos::proxy_cros_settings_parser::SetProxyPrefValue( | 734 chromeos::proxy_cros_settings_parser::SetProxyPrefValue( |
| 738 profile, pref_names_[i], non_default_values_[i]->DeepCopy()); | 735 profile, pref_names_[i], non_default_values_[i]->DeepCopy()); |
| 739 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 736 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); |
| 740 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 737 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, |
| 741 "", false, false); | 738 "", false, false); |
| 742 } | 739 } |
| 743 | 740 |
| 744 #endif | 741 #endif |
| OLD | NEW |