Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 26 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
| 27 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 28 #include "content/public/browser/render_view_host.h" | 28 #include "content/public/browser/render_view_host.h" |
| 29 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 30 #include "content/public/test/browser_test_utils.h" | 30 #include "content/public/test/browser_test_utils.h" |
| 31 #include "googleurl/src/gurl.h" | 31 #include "googleurl/src/gurl.h" |
| 32 #include "policy/policy_constants.h" | 32 #include "policy/policy_constants.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 34 |
| 35 #if defined(OS_CHROMEOS) | 35 #if defined(OS_CHROMEOS) |
| 36 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 37 #include "chrome/browser/chromeos/cros/network_library.h" | |
| 36 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h" | 38 #include "chrome/browser/chromeos/proxy_cros_settings_parser.h" |
| 37 #include "chrome/browser/chromeos/settings/cros_settings.h" | 39 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 38 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | 40 #include "chrome/browser/chromeos/settings/cros_settings_names.h" |
| 41 #include "chrome/browser/prefs/proxy_config_dictionary.h" | |
| 42 #include "chromeos/network/onc/onc_utils.h" | |
| 39 #endif | 43 #endif |
| 40 | 44 |
| 41 using testing::AllOf; | 45 using testing::AllOf; |
| 42 using testing::Mock; | 46 using testing::Mock; |
| 43 using testing::Property; | 47 using testing::Property; |
| 44 using testing::AnyNumber; | 48 using testing::AnyNumber; |
| 45 using testing::Return; | 49 using testing::Return; |
| 46 using testing::_; | 50 using testing::_; |
| 47 | 51 |
| 48 namespace base { | 52 namespace base { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 70 } | 74 } |
| 71 | 75 |
| 72 PreferencesBrowserTest::~PreferencesBrowserTest() { | 76 PreferencesBrowserTest::~PreferencesBrowserTest() { |
| 73 } | 77 } |
| 74 | 78 |
| 75 // Navigates to the settings page, causing the JavaScript pref handling code to | 79 // Navigates to the settings page, causing the JavaScript pref handling code to |
| 76 // load and injects JavaScript testing code. | 80 // load and injects JavaScript testing code. |
| 77 void PreferencesBrowserTest::SetUpOnMainThread() { | 81 void PreferencesBrowserTest::SetUpOnMainThread() { |
| 78 ui_test_utils::NavigateToURL(browser(), | 82 ui_test_utils::NavigateToURL(browser(), |
| 79 GURL(chrome::kChromeUISettingsFrameURL)); | 83 GURL(chrome::kChromeUISettingsFrameURL)); |
| 84 SetUpPrefs(); | |
| 85 } | |
| 86 | |
| 87 void PreferencesBrowserTest::SetUpPrefs() { | |
| 80 content::WebContents* web_contents = | 88 content::WebContents* web_contents = |
| 81 browser()->tab_strip_model()->GetActiveWebContents(); | 89 browser()->tab_strip_model()->GetActiveWebContents(); |
| 82 ASSERT_TRUE(web_contents); | 90 ASSERT_TRUE(web_contents); |
| 83 render_view_host_ = web_contents->GetRenderViewHost(); | 91 render_view_host_ = web_contents->GetRenderViewHost(); |
| 84 ASSERT_TRUE(render_view_host_); | 92 ASSERT_TRUE(render_view_host_); |
| 85 pref_change_registrar_.Init(components::UserPrefs::Get(browser()->profile())); | 93 pref_change_registrar_.Init(components::UserPrefs::Get(browser()->profile())); |
| 86 pref_service_ = browser()->profile()->GetPrefs(); | 94 pref_service_ = browser()->profile()->GetPrefs(); |
| 87 ASSERT_TRUE(content::ExecuteScript(render_view_host_, | 95 ASSERT_TRUE(content::ExecuteScript(render_view_host_, |
| 88 "function TestEnv() {" | 96 "function TestEnv() {" |
| 89 " this.sentinelName_ = 'download.prompt_for_download';" | 97 " this.sentinelName_ = 'download.prompt_for_download';" |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 314 name != pref_names.end(); ++name) | 322 name != pref_names.end(); ++name) |
| 315 javascript << "testEnv.addPref('" << name->c_str() << "');"; | 323 javascript << "testEnv.addPref('" << name->c_str() << "');"; |
| 316 javascript << "testEnv.setupAndReply();"; | 324 javascript << "testEnv.setupAndReply();"; |
| 317 std::string temp_observed_json; | 325 std::string temp_observed_json; |
| 318 if (!observed_json) | 326 if (!observed_json) |
| 319 observed_json = &temp_observed_json; | 327 observed_json = &temp_observed_json; |
| 320 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 328 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 321 render_view_host_, javascript.str(), observed_json)); | 329 render_view_host_, javascript.str(), observed_json)); |
| 322 } | 330 } |
| 323 | 331 |
| 332 void PreferencesBrowserTest::SetPref(const std::string& name, | |
| 333 const std::string& type, | |
| 334 const base::Value* value, | |
| 335 bool commit, | |
| 336 std::string* observed_json) { | |
| 337 scoped_ptr<base::Value> commit_ptr(new base::FundamentalValue(commit)); | |
| 338 std::stringstream javascript; | |
| 339 javascript << "testEnv.runAndReply(function() {" | |
| 340 << " Preferences.set" << type.c_str() << "Pref(" | |
|
Dan Beam
2013/05/28 22:45:27
is there any reason you need .c_str()? std::strin
Dan Beam
2013/05/28 22:45:27
<< " Preferences.set" << type.c_str() << "Pref("
pneubeck (no reviews)
2013/05/29 08:41:02
Done.
pneubeck (no reviews)
2013/05/29 08:41:02
Done.
| |
| 341 << " '" << name.c_str() << "'," | |
| 342 << " " << *value << "," | |
| 343 << " " << *commit_ptr << ");});"; | |
|
Dan Beam
2013/05/28 22:45:27
<< " " << *commit_ptr << ");"
<< "
pneubeck (no reviews)
2013/05/29 08:41:02
Done.
| |
| 344 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
| 345 render_view_host_, javascript.str(), observed_json)); | |
| 346 } | |
| 347 | |
| 324 void PreferencesBrowserTest::VerifySetPref(const std::string& name, | 348 void PreferencesBrowserTest::VerifySetPref(const std::string& name, |
| 325 const std::string& type, | 349 const std::string& type, |
| 326 const base::Value* value, | 350 const base::Value* value, |
| 327 bool commit) { | 351 bool commit) { |
| 328 if (commit) | 352 if (commit) |
| 329 ExpectSetCommit(name, value); | 353 ExpectSetCommit(name, value); |
| 330 else | 354 else |
| 331 ExpectNoCommit(name); | 355 ExpectNoCommit(name); |
| 332 scoped_ptr<base::Value> commit_ptr(new base::FundamentalValue(commit)); | |
| 333 std::string value_json; | |
| 334 std::string commit_json; | |
| 335 base::JSONWriter::Write(value, &value_json); | |
| 336 base::JSONWriter::Write(commit_ptr.get(), &commit_json); | |
| 337 std::stringstream javascript; | |
| 338 javascript << "testEnv.runAndReply(function() {" | |
| 339 << " Preferences.set" << type.c_str() << "Pref(" | |
| 340 << " '" << name.c_str() << "'," | |
| 341 << " " << value_json.c_str() << "," | |
| 342 << " " << commit_json.c_str() << ");});"; | |
| 343 std::string observed_json; | 356 std::string observed_json; |
| 344 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 357 SetPref(name, type, value, commit, &observed_json); |
| 345 render_view_host_, javascript.str(), &observed_json)); | |
| 346 VerifyObservedPref(observed_json, name, value, std::string(), false, !commit); | 358 VerifyObservedPref(observed_json, name, value, std::string(), false, !commit); |
| 347 VerifyAndClearExpectations(); | 359 VerifyAndClearExpectations(); |
| 348 } | 360 } |
| 349 | 361 |
| 350 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, | 362 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, |
| 351 const base::Value* value, | 363 const base::Value* value, |
| 352 bool commit) { | 364 bool commit) { |
| 353 if (commit) | 365 if (commit) |
| 354 ExpectClearCommit(name); | 366 ExpectClearCommit(name); |
| 355 else | 367 else |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 700 // FIXME(bartfab): Find a way to simulate enterprise enrollment in browser | 712 // FIXME(bartfab): Find a way to simulate enterprise enrollment in browser |
| 701 // tests. Only if Chrome thinks that it is enrolled will the device prefs be | 713 // tests. Only if Chrome thinks that it is enrolled will the device prefs be |
| 702 // decorated with "controlledBy: policy". | 714 // decorated with "controlledBy: policy". |
| 703 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 715 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); |
| 704 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values, | 716 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values, |
| 705 "", true, false); | 717 "", true, false); |
| 706 | 718 |
| 707 STLDeleteElements(&decorated_non_default_values); | 719 STLDeleteElements(&decorated_non_default_values); |
| 708 } | 720 } |
| 709 | 721 |
| 710 // Verifies that initializing the JavaScript Preferences class fires the correct | 722 class ProxyPreferencesBrowserTest : public PreferencesBrowserTest { |
| 711 // notifications in JavaScript for pref values handled by the Chrome OS proxy | 723 public: |
| 712 // settings parser. | 724 virtual void SetUpOnMainThread() OVERRIDE { |
| 713 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSProxyFetchPrefs) { | 725 scoped_ptr<base::DictionaryValue> proxy_config_dict( |
| 714 std::string observed_json; | 726 ProxyConfigDictionary::CreateFixedServers( |
| 727 "127.0.0.1:8080", | |
| 728 "*.google.com, 1.2.3.4:22")); | |
| 715 | 729 |
| 730 std::string proxy_config; | |
| 731 base::JSONWriter::Write(proxy_config_dict.get(), &proxy_config); | |
| 732 | |
| 733 GetActiveNetwork()->SetProxyConfig(proxy_config); | |
| 734 | |
| 735 ui_test_utils::NavigateToURL(browser(), | |
| 736 GURL(chrome::kChromeUIProxySettingsURL)); | |
| 737 SetUpPrefs(); | |
| 738 } | |
| 739 | |
| 740 protected: | |
| 741 chromeos::Network* GetActiveNetwork() { | |
| 742 chromeos::NetworkLibrary* network_library = | |
| 743 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); | |
| 744 return const_cast<chromeos::Network*>(network_library->active_network()); | |
| 745 } | |
| 746 | |
| 747 void SetProxyPref(const std::string& name, const base::Value& value) { | |
| 748 std::string type; | |
| 749 switch (value.GetType()) { | |
| 750 case base::Value::TYPE_BOOLEAN: | |
| 751 type = "Boolean"; | |
| 752 break; | |
| 753 case base::Value::TYPE_INTEGER: | |
| 754 type = "Integer"; | |
| 755 break; | |
| 756 case base::Value::TYPE_STRING: | |
| 757 type = "String"; | |
| 758 break; | |
| 759 default: | |
| 760 ASSERT_TRUE(false); | |
| 761 } | |
| 762 | |
| 763 std::string observed_json; | |
| 764 SetPref(name, type, &value, true, &observed_json); | |
| 765 } | |
| 766 | |
| 767 void VerifyCurrentProxyServer(const std::string& expected_server) { | |
| 768 scoped_ptr<base::DictionaryValue> proxy_config = | |
| 769 chromeos::onc::ReadDictionaryFromJson( | |
| 770 GetActiveNetwork()->proxy_config()); | |
| 771 | |
| 772 ProxyConfigDictionary proxy_dict(proxy_config.get()); | |
| 773 std::string actual_proxy_server; | |
| 774 EXPECT_TRUE(proxy_dict.GetProxyServer(&actual_proxy_server)); | |
| 775 EXPECT_EQ(expected_server, actual_proxy_server); | |
| 776 } | |
| 777 }; | |
| 778 | |
| 779 // Verifies that proxy settings are correctly pushed to JavaScript during | |
| 780 // initialization of the proxy settings page. | |
| 781 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, | |
| 782 ChromeOSInitializeProxy) { | |
| 716 // Boolean pref. | 783 // Boolean pref. |
| 717 pref_names_.push_back(chromeos::kProxySingle); | 784 pref_names_.push_back(chromeos::kProxySingle); |
| 718 default_values_.push_back(new base::FundamentalValue(false)); | |
| 719 non_default_values_.push_back(new base::FundamentalValue(true)); | 785 non_default_values_.push_back(new base::FundamentalValue(true)); |
| 720 | 786 |
| 721 // Integer pref. | 787 // Integer pref. |
| 722 pref_names_.push_back(chromeos::kProxySingleHttpPort); | 788 pref_names_.push_back(chromeos::kProxySingleHttpPort); |
| 723 default_values_.push_back(new base::StringValue("")); | |
| 724 non_default_values_.push_back(new base::FundamentalValue(8080)); | 789 non_default_values_.push_back(new base::FundamentalValue(8080)); |
| 725 | 790 |
| 726 // String pref. | 791 // String pref. |
| 727 pref_names_.push_back(chromeos::kProxySingleHttp); | 792 pref_names_.push_back(chromeos::kProxySingleHttp); |
| 728 default_values_.push_back(new base::StringValue("")); | |
| 729 non_default_values_.push_back(new base::StringValue("127.0.0.1")); | 793 non_default_values_.push_back(new base::StringValue("127.0.0.1")); |
| 730 | 794 |
| 731 // List pref. | 795 // List pref. |
| 732 pref_names_.push_back(chromeos::kProxyIgnoreList); | 796 pref_names_.push_back(chromeos::kProxyIgnoreList); |
| 733 default_values_.push_back(new base::ListValue()); | |
| 734 base::ListValue* list = new base::ListValue(); | 797 base::ListValue* list = new base::ListValue(); |
| 735 list->Append(new base::StringValue("www.google.com")); | 798 list->Append(new base::StringValue("*.google.com")); |
| 736 list->Append(new base::StringValue("example.com")); | 799 list->Append(new base::StringValue("1.2.3.4:22")); |
| 737 non_default_values_.push_back(list); | 800 non_default_values_.push_back(list); |
| 738 | 801 |
| 739 // Verify notifications when default values are in effect. | 802 std::string observed_json; |
| 740 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | |
| 741 VerifyObservedPrefs(observed_json, pref_names_, default_values_, | |
| 742 "", false, false); | |
| 743 | |
| 744 // Verify notifications when user-modified values are in effect. | |
| 745 Profile* profile = browser()->profile(); | |
| 746 // Do not set the Boolean pref. It will toogle automatically. | |
| 747 for (size_t i = 1; i < pref_names_.size(); ++i) | |
| 748 chromeos::proxy_cros_settings_parser::SetProxyPrefValue( | |
| 749 profile, pref_names_[i], non_default_values_[i]->DeepCopy()); | |
| 750 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 803 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); |
| 751 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 804 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, |
| 752 "", false, false); | 805 "", false, false); |
| 753 } | 806 } |
| 754 | 807 |
| 808 // Verifies that modifications to the proxy settings are correctly pushed from | |
| 809 // JavaScript to the ProxyConfig property stored in the network configuration. | |
| 810 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSSetProxy) { | |
| 811 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | |
| 812 | |
| 813 SetProxyPref(chromeos::kProxySingleHttpPort, base::FundamentalValue(123)); | |
| 814 SetProxyPref(chromeos::kProxySingleHttp, base::StringValue("www.adomain.xy")); | |
| 815 | |
| 816 VerifyCurrentProxyServer("www.adomain.xy:123"); | |
| 817 } | |
| 818 | |
| 819 // Verify that default proxy ports are used and that ports can be updated | |
| 820 // without affecting the previously set hosts. | |
| 821 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSProxyDefaultPorts) { | |
| 822 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); | |
| 823 | |
| 824 // Set to manual, per scheme proxy. | |
| 825 SetProxyPref(chromeos::kProxySingle, base::FundamentalValue(false)); | |
| 826 | |
| 827 // Set hosts but no ports. | |
| 828 SetProxyPref(chromeos::kProxyHttpUrl, base::StringValue("a.com")); | |
| 829 SetProxyPref(chromeos::kProxyHttpsUrl, base::StringValue("4.3.2.1")); | |
| 830 SetProxyPref(chromeos::kProxyFtpUrl, base::StringValue("c.com")); | |
| 831 SetProxyPref(chromeos::kProxySocks, base::StringValue("d.com")); | |
| 832 | |
| 833 // Verify default ports. | |
| 834 VerifyCurrentProxyServer( | |
| 835 "http=a.com:80;https=4.3.2.1:80;ftp=c.com:80;socks=socks4://d.com:1080"); | |
| 836 | |
| 837 // Set and verify the ports. | |
| 838 SetProxyPref(chromeos::kProxyHttpPort, base::FundamentalValue(1)); | |
| 839 SetProxyPref(chromeos::kProxyHttpsPort, base::FundamentalValue(2)); | |
| 840 SetProxyPref(chromeos::kProxyFtpPort, base::FundamentalValue(3)); | |
| 841 SetProxyPref(chromeos::kProxySocksPort, base::FundamentalValue(4)); | |
| 842 | |
| 843 VerifyCurrentProxyServer( | |
| 844 "http=a.com:1;https=4.3.2.1:2;ftp=c.com:3;socks=socks4://d.com:4"); | |
| 845 } | |
| 846 | |
|
Dan Beam
2013/05/28 22:45:27
nit: remove \n
| |
| 847 | |
| 755 #endif | 848 #endif |
| OLD | NEW |