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

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

Issue 11753009: Simplify ExecuteJavaScript* functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update prerender_browsertest.cc. Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
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
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::ExecuteScript(render_view_host_,
85 "",
86 "function TestEnv() {" 85 "function TestEnv() {"
87 " this.sentinelName_ = 'download.prompt_for_download';" 86 " this.sentinelName_ = 'download.prompt_for_download';"
88 " this.prefs_ = [];" 87 " this.prefs_ = [];"
89 " TestEnv.instance_ = this;" 88 " TestEnv.instance_ = this;"
90 "}" 89 "}"
91 "" 90 ""
92 "TestEnv.handleEvent = function(event) {" 91 "TestEnv.handleEvent = function(event) {"
93 " var env = TestEnv.instance_;" 92 " var env = TestEnv.instance_;"
94 " var name = event.type;" 93 " var name = event.type;"
95 " env.removePrefListener_(name);" 94 " env.removePrefListener_(name);"
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 std::string* observed_json) const { 305 std::string* observed_json) const {
307 std::stringstream javascript; 306 std::stringstream 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::ExecuteScriptAndExtractString(
317 render_view_host_, "", javascript.str(), observed_json)); 316 render_view_host_, 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::stringstream 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::ExecuteScriptAndExtractString(
341 render_view_host_, "", javascript.str(), &observed_json)); 340 render_view_host_, 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::stringstream 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::ExecuteScriptAndExtractString(
363 render_view_host_, "", javascript.str(), &observed_json)); 362 render_view_host_, 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::stringstream 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::ExecuteScriptAndExtractString(
377 render_view_host_, "", javascript.str(), &observed_json)); 376 render_view_host_, 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::stringstream 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::ExecuteScriptAndExtractString(
405 render_view_host_, "", javascript.str(), &observed_json)); 404 render_view_host_, 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::ExecuteScript(
412 render_view_host_, "", "testEnv.startObserving();")); 411 render_view_host_, "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::ExecuteScriptAndExtractString(
417 render_view_host_, 416 render_view_host_,
418 "",
419 "testEnv.finishObservingAndReply();", 417 "testEnv.finishObservingAndReply();",
420 observed_json)); 418 observed_json));
421 } 419 }
422 420
423 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref) { 421 void PreferencesBrowserTest::UseDefaultTestPrefs(bool includeListPref) {
424 // Boolean pref. 422 // Boolean pref.
425 types_.push_back("Boolean"); 423 types_.push_back("Boolean");
426 pref_names_.push_back(prefs::kAlternateErrorPagesEnabled); 424 pref_names_.push_back(prefs::kAlternateErrorPagesEnabled);
427 policy_names_.push_back(policy::key::kAlternateErrorPagesEnabled); 425 policy_names_.push_back(policy::key::kAlternateErrorPagesEnabled);
428 non_default_values_.push_back(new base::FundamentalValue(false)); 426 non_default_values_.push_back(new base::FundamentalValue(false));
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
735 // Do not set the Boolean pref. It will toogle automatically. 733 // Do not set the Boolean pref. It will toogle automatically.
736 for (size_t i = 1; i < pref_names_.size(); ++i) 734 for (size_t i = 1; i < pref_names_.size(); ++i)
737 chromeos::proxy_cros_settings_parser::SetProxyPrefValue( 735 chromeos::proxy_cros_settings_parser::SetProxyPrefValue(
738 profile, pref_names_[i], non_default_values_[i]->DeepCopy()); 736 profile, pref_names_[i], non_default_values_[i]->DeepCopy());
739 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); 737 SetupJavaScriptTestEnvironment(pref_names_, &observed_json);
740 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, 738 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_,
741 "", false, false); 739 "", false, false);
742 } 740 }
743 741
744 #endif 742 #endif
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/options/options_ui_browsertest.cc ('k') | chrome/test/base/tracing_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698