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