| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/prefs/command_line_pref_store.h" | 11 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 12 #include "chrome/browser/prefs/proxy_prefs.h" |
| 12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 class TestCommandLinePrefStore : public CommandLinePrefStore { | 18 class TestCommandLinePrefStore : public CommandLinePrefStore { |
| 18 public: | 19 public: |
| 19 explicit TestCommandLinePrefStore(CommandLine* cl) | 20 explicit TestCommandLinePrefStore(CommandLine* cl) |
| 20 : CommandLinePrefStore(cl) {} | 21 : CommandLinePrefStore(cl) {} |
| 21 | 22 |
| 22 bool ProxySwitchesAreValid() { | 23 bool ProxySwitchesAreValid() { |
| 23 return ValidateProxySwitches(); | 24 return ValidateProxySwitches(); |
| 24 } | 25 } |
| 26 |
| 27 void VerifyIntPref(const std::string& path, int expected_value) { |
| 28 Value* actual = NULL; |
| 29 ASSERT_EQ(PrefStore::READ_OK, GetValue(path, &actual)); |
| 30 EXPECT_TRUE(FundamentalValue(expected_value).Equals(actual)); |
| 31 } |
| 25 }; | 32 }; |
| 26 | 33 |
| 27 const char unknown_bool[] = "unknown_switch"; | 34 const char unknown_bool[] = "unknown_switch"; |
| 28 const char unknown_string[] = "unknown_other_switch"; | 35 const char unknown_string[] = "unknown_other_switch"; |
| 29 | 36 |
| 30 } // namespace | 37 } // namespace |
| 31 | 38 |
| 32 // Tests a simple string pref on the command line. | 39 // Tests a simple string pref on the command line. |
| 33 TEST(CommandLinePrefStoreTest, SimpleStringPref) { | 40 TEST(CommandLinePrefStoreTest, SimpleStringPref) { |
| 34 CommandLine cl(CommandLine::NO_PROGRAM); | 41 CommandLine cl(CommandLine::NO_PROGRAM); |
| 35 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); | 42 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); |
| 36 CommandLinePrefStore store(&cl); | 43 CommandLinePrefStore store(&cl); |
| 37 | 44 |
| 38 Value* actual = NULL; | 45 Value* actual = NULL; |
| 39 EXPECT_EQ(PrefStore::READ_OK, | 46 EXPECT_EQ(PrefStore::READ_OK, |
| 40 store.GetValue(prefs::kApplicationLocale, &actual)); | 47 store.GetValue(prefs::kApplicationLocale, &actual)); |
| 41 std::string result; | 48 std::string result; |
| 42 EXPECT_TRUE(actual->GetAsString(&result)); | 49 EXPECT_TRUE(actual->GetAsString(&result)); |
| 43 EXPECT_EQ("hi-MOM", result); | 50 EXPECT_EQ("hi-MOM", result); |
| 44 } | 51 } |
| 45 | 52 |
| 46 // Tests a simple boolean pref on the command line. | 53 // Tests a simple boolean pref on the command line. |
| 47 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { | 54 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { |
| 48 CommandLine cl(CommandLine::NO_PROGRAM); | 55 CommandLine cl(CommandLine::NO_PROGRAM); |
| 49 cl.AppendSwitch(switches::kNoProxyServer); | 56 cl.AppendSwitch(switches::kNoProxyServer); |
| 50 CommandLinePrefStore store(&cl); | 57 TestCommandLinePrefStore store(&cl); |
| 51 | 58 |
| 52 Value* actual = NULL; | 59 store.VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_DIRECT); |
| 53 ASSERT_EQ(PrefStore::READ_OK, store.GetValue(prefs::kNoProxyServer, &actual)); | |
| 54 bool result; | |
| 55 EXPECT_TRUE(actual->GetAsBoolean(&result)); | |
| 56 EXPECT_TRUE(result); | |
| 57 } | 60 } |
| 58 | 61 |
| 59 // Tests a command line with no recognized prefs. | 62 // Tests a command line with no recognized prefs. |
| 60 TEST(CommandLinePrefStoreTest, NoPrefs) { | 63 TEST(CommandLinePrefStoreTest, NoPrefs) { |
| 61 CommandLine cl(CommandLine::NO_PROGRAM); | 64 CommandLine cl(CommandLine::NO_PROGRAM); |
| 62 cl.AppendSwitch(unknown_string); | 65 cl.AppendSwitch(unknown_string); |
| 63 cl.AppendSwitchASCII(unknown_bool, "a value"); | 66 cl.AppendSwitchASCII(unknown_bool, "a value"); |
| 64 CommandLinePrefStore store(&cl); | 67 CommandLinePrefStore store(&cl); |
| 65 | 68 |
| 66 Value* actual = NULL; | 69 Value* actual = NULL; |
| 67 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_bool, &actual)); | 70 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_bool, &actual)); |
| 68 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_string, &actual)); | 71 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_string, &actual)); |
| 69 } | 72 } |
| 70 | 73 |
| 71 // Tests a complex command line with multiple known and unknown switches. | 74 // Tests a complex command line with multiple known and unknown switches. |
| 72 TEST(CommandLinePrefStoreTest, MultipleSwitches) { | 75 TEST(CommandLinePrefStoreTest, MultipleSwitches) { |
| 73 CommandLine cl(CommandLine::NO_PROGRAM); | 76 CommandLine cl(CommandLine::NO_PROGRAM); |
| 74 cl.AppendSwitch(unknown_string); | 77 cl.AppendSwitch(unknown_string); |
| 75 cl.AppendSwitch(switches::kProxyAutoDetect); | 78 cl.AppendSwitch(switches::kProxyAutoDetect); |
| 76 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); | 79 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); |
| 77 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); | 80 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); |
| 78 cl.AppendSwitchASCII(unknown_bool, "a value"); | 81 cl.AppendSwitchASCII(unknown_bool, "a value"); |
| 79 CommandLinePrefStore store(&cl); | 82 TestCommandLinePrefStore store(&cl); |
| 80 | 83 |
| 81 Value* actual = NULL; | 84 Value* actual = NULL; |
| 82 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_bool, &actual)); | 85 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_bool, &actual)); |
| 83 ASSERT_EQ(PrefStore::READ_OK, | 86 store.VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_AUTO_DETECT); |
| 84 store.GetValue(prefs::kProxyAutoDetect, &actual)); | |
| 85 bool bool_result = false; | |
| 86 EXPECT_TRUE(actual->GetAsBoolean(&bool_result)); | |
| 87 EXPECT_TRUE(bool_result); | |
| 88 | 87 |
| 89 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_string, &actual)); | 88 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_string, &actual)); |
| 90 std::string string_result = ""; | 89 std::string string_result = ""; |
| 91 ASSERT_EQ(PrefStore::READ_OK, store.GetValue(prefs::kProxyServer, &actual)); | 90 ASSERT_EQ(PrefStore::READ_OK, store.GetValue(prefs::kProxyServer, &actual)); |
| 92 EXPECT_TRUE(actual->GetAsString(&string_result)); | 91 EXPECT_TRUE(actual->GetAsString(&string_result)); |
| 93 EXPECT_EQ("proxy", string_result); | 92 EXPECT_EQ("proxy", string_result); |
| 94 ASSERT_EQ(PrefStore::READ_OK, | 93 ASSERT_EQ(PrefStore::READ_OK, |
| 95 store.GetValue(prefs::kProxyBypassList, &actual)); | 94 store.GetValue(prefs::kProxyBypassList, &actual)); |
| 96 EXPECT_TRUE(actual->GetAsString(&string_result)); | 95 EXPECT_TRUE(actual->GetAsString(&string_result)); |
| 97 EXPECT_EQ("list", string_result); | 96 EXPECT_EQ("list", string_result); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 117 | 116 |
| 118 // All proxy switches except no-proxy. | 117 // All proxy switches except no-proxy. |
| 119 CommandLine cl2(CommandLine::NO_PROGRAM); | 118 CommandLine cl2(CommandLine::NO_PROGRAM); |
| 120 cl2.AppendSwitch(switches::kProxyAutoDetect); | 119 cl2.AppendSwitch(switches::kProxyAutoDetect); |
| 121 cl2.AppendSwitchASCII(switches::kProxyServer, "server"); | 120 cl2.AppendSwitchASCII(switches::kProxyServer, "server"); |
| 122 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url"); | 121 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url"); |
| 123 cl2.AppendSwitchASCII(switches::kProxyBypassList, "list"); | 122 cl2.AppendSwitchASCII(switches::kProxyBypassList, "list"); |
| 124 TestCommandLinePrefStore store4(&cl2); | 123 TestCommandLinePrefStore store4(&cl2); |
| 125 EXPECT_TRUE(store4.ProxySwitchesAreValid()); | 124 EXPECT_TRUE(store4.ProxySwitchesAreValid()); |
| 126 } | 125 } |
| 126 |
| 127 TEST(CommandLinePrefStoreTest, ManualProxyModeInference) { |
| 128 CommandLine cl1(CommandLine::NO_PROGRAM); |
| 129 cl1.AppendSwitch(unknown_string); |
| 130 cl1.AppendSwitchASCII(switches::kProxyServer, "proxy"); |
| 131 TestCommandLinePrefStore store1(&cl1); |
| 132 store1.VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_FIXED_SERVERS); |
| 133 |
| 134 CommandLine cl2(CommandLine::NO_PROGRAM); |
| 135 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "proxy"); |
| 136 TestCommandLinePrefStore store2(&cl2); |
| 137 store2.VerifyIntPref(prefs::kProxyMode, ProxyPrefs::MODE_PAC_SCRIPT); |
| 138 } |
| OLD | NEW |