| 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 << "Pref(" |
| 341 << " '" << name << "'," |
| 342 << " " << *value << "," |
| 343 << " " << *commit_ptr << ");" |
| 344 << "});"; |
| 345 ASSERT_TRUE(content::ExecuteScriptAndExtractString( |
| 346 render_view_host_, javascript.str(), observed_json)); |
| 347 } |
| 348 |
| 324 void PreferencesBrowserTest::VerifySetPref(const std::string& name, | 349 void PreferencesBrowserTest::VerifySetPref(const std::string& name, |
| 325 const std::string& type, | 350 const std::string& type, |
| 326 const base::Value* value, | 351 const base::Value* value, |
| 327 bool commit) { | 352 bool commit) { |
| 328 if (commit) | 353 if (commit) |
| 329 ExpectSetCommit(name, value); | 354 ExpectSetCommit(name, value); |
| 330 else | 355 else |
| 331 ExpectNoCommit(name); | 356 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; | 357 std::string observed_json; |
| 344 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | 358 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); | 359 VerifyObservedPref(observed_json, name, value, std::string(), false, !commit); |
| 347 VerifyAndClearExpectations(); | 360 VerifyAndClearExpectations(); |
| 348 } | 361 } |
| 349 | 362 |
| 350 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, | 363 void PreferencesBrowserTest::VerifyClearPref(const std::string& name, |
| 351 const base::Value* value, | 364 const base::Value* value, |
| 352 bool commit) { | 365 bool commit) { |
| 353 if (commit) | 366 if (commit) |
| 354 ExpectClearCommit(name); | 367 ExpectClearCommit(name); |
| 355 else | 368 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 | 713 // 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 | 714 // tests. Only if Chrome thinks that it is enrolled will the device prefs be |
| 702 // decorated with "controlledBy: policy". | 715 // decorated with "controlledBy: policy". |
| 703 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); | 716 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); |
| 704 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values, | 717 VerifyObservedPrefs(observed_json, pref_names_, decorated_non_default_values, |
| 705 "", true, false); | 718 "", true, false); |
| 706 | 719 |
| 707 STLDeleteElements(&decorated_non_default_values); | 720 STLDeleteElements(&decorated_non_default_values); |
| 708 } | 721 } |
| 709 | 722 |
| 710 // Verifies that initializing the JavaScript Preferences class fires the correct | 723 class ProxyPreferencesBrowserTest : public PreferencesBrowserTest { |
| 711 // notifications in JavaScript for pref values handled by the Chrome OS proxy | 724 public: |
| 712 // settings parser. | 725 virtual void SetUpOnMainThread() OVERRIDE { |
| 713 IN_PROC_BROWSER_TEST_F(PreferencesBrowserTest, ChromeOSProxyFetchPrefs) { | 726 scoped_ptr<base::DictionaryValue> proxy_config_dict( |
| 714 std::string observed_json; | 727 ProxyConfigDictionary::CreateFixedServers( |
| 728 "127.0.0.1:8080", |
| 729 "*.google.com, 1.2.3.4:22")); |
| 715 | 730 |
| 731 std::string proxy_config; |
| 732 base::JSONWriter::Write(proxy_config_dict.get(), &proxy_config); |
| 733 |
| 734 GetActiveNetwork()->SetProxyConfig(proxy_config); |
| 735 |
| 736 ui_test_utils::NavigateToURL(browser(), |
| 737 GURL(chrome::kChromeUIProxySettingsURL)); |
| 738 SetUpPrefs(); |
| 739 } |
| 740 |
| 741 protected: |
| 742 chromeos::Network* GetActiveNetwork() { |
| 743 chromeos::NetworkLibrary* network_library = |
| 744 chromeos::CrosLibrary::Get()->GetNetworkLibrary(); |
| 745 return const_cast<chromeos::Network*>(network_library->active_network()); |
| 746 } |
| 747 |
| 748 void SetProxyPref(const std::string& name, const base::Value& value) { |
| 749 std::string type; |
| 750 switch (value.GetType()) { |
| 751 case base::Value::TYPE_BOOLEAN: |
| 752 type = "Boolean"; |
| 753 break; |
| 754 case base::Value::TYPE_INTEGER: |
| 755 type = "Integer"; |
| 756 break; |
| 757 case base::Value::TYPE_STRING: |
| 758 type = "String"; |
| 759 break; |
| 760 default: |
| 761 ASSERT_TRUE(false); |
| 762 } |
| 763 |
| 764 std::string observed_json; |
| 765 SetPref(name, type, &value, true, &observed_json); |
| 766 } |
| 767 |
| 768 void VerifyCurrentProxyServer(const std::string& expected_server) { |
| 769 scoped_ptr<base::DictionaryValue> proxy_config = |
| 770 chromeos::onc::ReadDictionaryFromJson( |
| 771 GetActiveNetwork()->proxy_config()); |
| 772 |
| 773 ProxyConfigDictionary proxy_dict(proxy_config.get()); |
| 774 std::string actual_proxy_server; |
| 775 EXPECT_TRUE(proxy_dict.GetProxyServer(&actual_proxy_server)); |
| 776 EXPECT_EQ(expected_server, actual_proxy_server); |
| 777 } |
| 778 }; |
| 779 |
| 780 // Verifies that proxy settings are correctly pushed to JavaScript during |
| 781 // initialization of the proxy settings page. |
| 782 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, |
| 783 ChromeOSInitializeProxy) { |
| 716 // Boolean pref. | 784 // Boolean pref. |
| 717 pref_names_.push_back(chromeos::kProxySingle); | 785 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)); | 786 non_default_values_.push_back(new base::FundamentalValue(true)); |
| 720 | 787 |
| 721 // Integer pref. | 788 // Integer pref. |
| 722 pref_names_.push_back(chromeos::kProxySingleHttpPort); | 789 pref_names_.push_back(chromeos::kProxySingleHttpPort); |
| 723 default_values_.push_back(new base::StringValue("")); | |
| 724 non_default_values_.push_back(new base::FundamentalValue(8080)); | 790 non_default_values_.push_back(new base::FundamentalValue(8080)); |
| 725 | 791 |
| 726 // String pref. | 792 // String pref. |
| 727 pref_names_.push_back(chromeos::kProxySingleHttp); | 793 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")); | 794 non_default_values_.push_back(new base::StringValue("127.0.0.1")); |
| 730 | 795 |
| 731 // List pref. | 796 // List pref. |
| 732 pref_names_.push_back(chromeos::kProxyIgnoreList); | 797 pref_names_.push_back(chromeos::kProxyIgnoreList); |
| 733 default_values_.push_back(new base::ListValue()); | |
| 734 base::ListValue* list = new base::ListValue(); | 798 base::ListValue* list = new base::ListValue(); |
| 735 list->Append(new base::StringValue("www.google.com")); | 799 list->Append(new base::StringValue("*.google.com")); |
| 736 list->Append(new base::StringValue("example.com")); | 800 list->Append(new base::StringValue("1.2.3.4:22")); |
| 737 non_default_values_.push_back(list); | 801 non_default_values_.push_back(list); |
| 738 | 802 |
| 739 // Verify notifications when default values are in effect. | 803 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); | 804 SetupJavaScriptTestEnvironment(pref_names_, &observed_json); |
| 751 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, | 805 VerifyObservedPrefs(observed_json, pref_names_, non_default_values_, |
| 752 "", false, false); | 806 "", false, false); |
| 753 } | 807 } |
| 754 | 808 |
| 809 // Verifies that modifications to the proxy settings are correctly pushed from |
| 810 // JavaScript to the ProxyConfig property stored in the network configuration. |
| 811 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSSetProxy) { |
| 812 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); |
| 813 |
| 814 SetProxyPref(chromeos::kProxySingleHttpPort, base::FundamentalValue(123)); |
| 815 SetProxyPref(chromeos::kProxySingleHttp, base::StringValue("www.adomain.xy")); |
| 816 |
| 817 VerifyCurrentProxyServer("www.adomain.xy:123"); |
| 818 } |
| 819 |
| 820 // Verify that default proxy ports are used and that ports can be updated |
| 821 // without affecting the previously set hosts. |
| 822 IN_PROC_BROWSER_TEST_F(ProxyPreferencesBrowserTest, ChromeOSProxyDefaultPorts) { |
| 823 ASSERT_NO_FATAL_FAILURE(SetupJavaScriptTestEnvironment(pref_names_, NULL)); |
| 824 |
| 825 // Set to manual, per scheme proxy. |
| 826 SetProxyPref(chromeos::kProxySingle, base::FundamentalValue(false)); |
| 827 |
| 828 // Set hosts but no ports. |
| 829 SetProxyPref(chromeos::kProxyHttpUrl, base::StringValue("a.com")); |
| 830 SetProxyPref(chromeos::kProxyHttpsUrl, base::StringValue("4.3.2.1")); |
| 831 SetProxyPref(chromeos::kProxyFtpUrl, base::StringValue("c.com")); |
| 832 SetProxyPref(chromeos::kProxySocks, base::StringValue("d.com")); |
| 833 |
| 834 // Verify default ports. |
| 835 VerifyCurrentProxyServer( |
| 836 "http=a.com:80;https=4.3.2.1:80;ftp=c.com:80;socks=socks4://d.com:1080"); |
| 837 |
| 838 // Set and verify the ports. |
| 839 SetProxyPref(chromeos::kProxyHttpPort, base::FundamentalValue(1)); |
| 840 SetProxyPref(chromeos::kProxyHttpsPort, base::FundamentalValue(2)); |
| 841 SetProxyPref(chromeos::kProxyFtpPort, base::FundamentalValue(3)); |
| 842 SetProxyPref(chromeos::kProxySocksPort, base::FundamentalValue(4)); |
| 843 |
| 844 VerifyCurrentProxyServer( |
| 845 "http=a.com:1;https=4.3.2.1:2;ftp=c.com:3;socks=socks4://d.com:4"); |
| 846 } |
| 847 |
| 755 #endif | 848 #endif |
| OLD | NEW |