| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <gtest/gtest.h> | 5 #include <gtest/gtest.h> |
| 6 | 6 |
| 7 #include "app/app_switches.h" | 7 #include "app/app_switches.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/ref_counted.h" | 9 #include "base/ref_counted.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/prefs/command_line_pref_store.h" | 12 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 13 #include "chrome/browser/prefs/proxy_prefs.h" | 13 #include "chrome/browser/prefs/proxy_prefs.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "ui/base/ui_base_switches.h" | 16 #include "ui/base/ui_base_switches.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class TestCommandLinePrefStore : public CommandLinePrefStore { | 20 class TestCommandLinePrefStore : public CommandLinePrefStore { |
| 21 public: | 21 public: |
| 22 explicit TestCommandLinePrefStore(CommandLine* cl) | 22 explicit TestCommandLinePrefStore(CommandLine* cl) |
| 23 : CommandLinePrefStore(cl) {} | 23 : CommandLinePrefStore(cl) {} |
| 24 | 24 |
| 25 bool ProxySwitchesAreValid() { | 25 bool ProxySwitchesAreValid() { |
| 26 return ValidateProxySwitches(); | 26 return ValidateProxySwitches(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void VerifyIntPref(const std::string& path, int expected_value) { | 29 void VerifyProxyMode(ProxyPrefs::ProxyMode expected_mode) { |
| 30 Value* actual = NULL; | 30 Value* value = NULL; |
| 31 ASSERT_EQ(PrefStore::READ_OK, GetValue(path, &actual)); | 31 ASSERT_EQ(PrefStore::READ_OK, GetValue(prefs::kProxy, &value)); |
| 32 EXPECT_TRUE(FundamentalValue(expected_value).Equals(actual)); | 32 ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType()); |
| 33 ProxyPrefsDictionary dict(static_cast<DictionaryValue*>(value)); |
| 34 ProxyPrefs::ProxyMode actual_mode; |
| 35 ASSERT_TRUE(dict.GetMode(&actual_mode)); |
| 36 EXPECT_EQ(expected_mode, actual_mode); |
| 33 } | 37 } |
| 34 }; | 38 }; |
| 35 | 39 |
| 36 const char unknown_bool[] = "unknown_switch"; | 40 const char unknown_bool[] = "unknown_switch"; |
| 37 const char unknown_string[] = "unknown_other_switch"; | 41 const char unknown_string[] = "unknown_other_switch"; |
| 38 | 42 |
| 39 } // namespace | 43 } // namespace |
| 40 | 44 |
| 41 // Tests a simple string pref on the command line. | 45 // Tests a simple string pref on the command line. |
| 42 TEST(CommandLinePrefStoreTest, SimpleStringPref) { | 46 TEST(CommandLinePrefStoreTest, SimpleStringPref) { |
| 43 CommandLine cl(CommandLine::NO_PROGRAM); | 47 CommandLine cl(CommandLine::NO_PROGRAM); |
| 44 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); | 48 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); |
| 45 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); | 49 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); |
| 46 | 50 |
| 47 Value* actual = NULL; | 51 Value* actual = NULL; |
| 48 EXPECT_EQ(PrefStore::READ_OK, | 52 EXPECT_EQ(PrefStore::READ_OK, |
| 49 store->GetValue(prefs::kApplicationLocale, &actual)); | 53 store->GetValue(prefs::kApplicationLocale, &actual)); |
| 50 std::string result; | 54 std::string result; |
| 51 EXPECT_TRUE(actual->GetAsString(&result)); | 55 EXPECT_TRUE(actual->GetAsString(&result)); |
| 52 EXPECT_EQ("hi-MOM", result); | 56 EXPECT_EQ("hi-MOM", result); |
| 53 } | 57 } |
| 54 | 58 |
| 55 // Tests a simple boolean pref on the command line. | 59 // Tests a simple boolean pref on the command line. |
| 56 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { | 60 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { |
| 57 CommandLine cl(CommandLine::NO_PROGRAM); | 61 CommandLine cl(CommandLine::NO_PROGRAM); |
| 58 cl.AppendSwitch(switches::kNoProxyServer); | 62 cl.AppendSwitch(switches::kNoProxyServer); |
| 59 scoped_refptr<TestCommandLinePrefStore> store = | 63 scoped_refptr<TestCommandLinePrefStore> store = |
| 60 new TestCommandLinePrefStore(&cl); | 64 new TestCommandLinePrefStore(&cl); |
| 61 | 65 |
| 62 store->VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_DIRECT); | 66 store->VerifyProxyMode(ProxyPrefs::MODE_DIRECT); |
| 63 } | 67 } |
| 64 | 68 |
| 65 // Tests a command line with no recognized prefs. | 69 // Tests a command line with no recognized prefs. |
| 66 TEST(CommandLinePrefStoreTest, NoPrefs) { | 70 TEST(CommandLinePrefStoreTest, NoPrefs) { |
| 67 CommandLine cl(CommandLine::NO_PROGRAM); | 71 CommandLine cl(CommandLine::NO_PROGRAM); |
| 68 cl.AppendSwitch(unknown_string); | 72 cl.AppendSwitch(unknown_string); |
| 69 cl.AppendSwitchASCII(unknown_bool, "a value"); | 73 cl.AppendSwitchASCII(unknown_bool, "a value"); |
| 70 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); | 74 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); |
| 71 | 75 |
| 72 Value* actual = NULL; | 76 Value* actual = NULL; |
| 73 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual)); | 77 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual)); |
| 74 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual)); | 78 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual)); |
| 75 } | 79 } |
| 76 | 80 |
| 77 // Tests a complex command line with multiple known and unknown switches. | 81 // Tests a complex command line with multiple known and unknown switches. |
| 78 TEST(CommandLinePrefStoreTest, MultipleSwitches) { | 82 TEST(CommandLinePrefStoreTest, MultipleSwitches) { |
| 79 CommandLine cl(CommandLine::NO_PROGRAM); | 83 CommandLine cl(CommandLine::NO_PROGRAM); |
| 80 cl.AppendSwitch(unknown_string); | 84 cl.AppendSwitch(unknown_string); |
| 81 cl.AppendSwitch(switches::kProxyAutoDetect); | |
| 82 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); | 85 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); |
| 83 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); | 86 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); |
| 84 cl.AppendSwitchASCII(unknown_bool, "a value"); | 87 cl.AppendSwitchASCII(unknown_bool, "a value"); |
| 85 scoped_refptr<TestCommandLinePrefStore> store = | 88 scoped_refptr<TestCommandLinePrefStore> store = |
| 86 new TestCommandLinePrefStore(&cl); | 89 new TestCommandLinePrefStore(&cl); |
| 87 | 90 |
| 88 Value* actual = NULL; | 91 Value* actual = NULL; |
| 89 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual)); | 92 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual)); |
| 90 store->VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_AUTO_DETECT); | 93 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual)); |
| 91 | 94 |
| 92 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual)); | 95 store->VerifyProxyMode(ProxyPrefs::MODE_FIXED_SERVERS); |
| 96 |
| 97 Value* value = NULL; |
| 98 ASSERT_EQ(PrefStore::READ_OK, store->GetValue(prefs::kProxy, &value)); |
| 99 ASSERT_EQ(Value::TYPE_DICTIONARY, value->GetType()); |
| 100 ProxyPrefsDictionary dict(static_cast<DictionaryValue*>(value)); |
| 101 |
| 93 std::string string_result = ""; | 102 std::string string_result = ""; |
| 94 ASSERT_EQ(PrefStore::READ_OK, store->GetValue(prefs::kProxyServer, &actual)); | 103 |
| 95 EXPECT_TRUE(actual->GetAsString(&string_result)); | 104 ASSERT_TRUE(dict.GetProxyServer(&string_result)); |
| 96 EXPECT_EQ("proxy", string_result); | 105 EXPECT_EQ("proxy", string_result); |
| 97 ASSERT_EQ(PrefStore::READ_OK, | 106 |
| 98 store->GetValue(prefs::kProxyBypassList, &actual)); | 107 ASSERT_TRUE(dict.GetBypassList(&string_result)); |
| 99 EXPECT_TRUE(actual->GetAsString(&string_result)); | |
| 100 EXPECT_EQ("list", string_result); | 108 EXPECT_EQ("list", string_result); |
| 101 } | 109 } |
| 102 | 110 |
| 103 // Tests proxy switch validation. | 111 // Tests proxy switch validation. |
| 104 TEST(CommandLinePrefStoreTest, ProxySwitchValidation) { | 112 TEST(CommandLinePrefStoreTest, ProxySwitchValidation) { |
| 105 CommandLine cl(CommandLine::NO_PROGRAM); | 113 CommandLine cl(CommandLine::NO_PROGRAM); |
| 106 | 114 |
| 107 // No switches. | 115 // No switches. |
| 108 scoped_refptr<TestCommandLinePrefStore> store = | 116 scoped_refptr<TestCommandLinePrefStore> store = |
| 109 new TestCommandLinePrefStore(&cl); | 117 new TestCommandLinePrefStore(&cl); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 131 new TestCommandLinePrefStore(&cl2); | 139 new TestCommandLinePrefStore(&cl2); |
| 132 EXPECT_TRUE(store4->ProxySwitchesAreValid()); | 140 EXPECT_TRUE(store4->ProxySwitchesAreValid()); |
| 133 } | 141 } |
| 134 | 142 |
| 135 TEST(CommandLinePrefStoreTest, ManualProxyModeInference) { | 143 TEST(CommandLinePrefStoreTest, ManualProxyModeInference) { |
| 136 CommandLine cl1(CommandLine::NO_PROGRAM); | 144 CommandLine cl1(CommandLine::NO_PROGRAM); |
| 137 cl1.AppendSwitch(unknown_string); | 145 cl1.AppendSwitch(unknown_string); |
| 138 cl1.AppendSwitchASCII(switches::kProxyServer, "proxy"); | 146 cl1.AppendSwitchASCII(switches::kProxyServer, "proxy"); |
| 139 scoped_refptr<TestCommandLinePrefStore> store1 = | 147 scoped_refptr<TestCommandLinePrefStore> store1 = |
| 140 new TestCommandLinePrefStore(&cl1); | 148 new TestCommandLinePrefStore(&cl1); |
| 141 store1->VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_FIXED_SERVERS); | 149 store1->VerifyProxyMode(ProxyPrefs::MODE_FIXED_SERVERS); |
| 142 | 150 |
| 143 CommandLine cl2(CommandLine::NO_PROGRAM); | 151 CommandLine cl2(CommandLine::NO_PROGRAM); |
| 144 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "proxy"); | 152 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "proxy"); |
| 145 scoped_refptr<TestCommandLinePrefStore> store2 = | 153 scoped_refptr<TestCommandLinePrefStore> store2 = |
| 146 new TestCommandLinePrefStore(&cl2); | 154 new TestCommandLinePrefStore(&cl2); |
| 147 store2->VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_PAC_SCRIPT); | 155 store2->VerifyProxyMode(ProxyPrefs::MODE_PAC_SCRIPT); |
| 148 } | 156 } |
| OLD | NEW |