| 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/ref_counted.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/values.h" | 11 #include "base/values.h" |
| 11 #include "chrome/browser/prefs/command_line_pref_store.h" | 12 #include "chrome/browser/prefs/command_line_pref_store.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 } |
| 25 }; | 26 }; |
| 26 | 27 |
| 27 const char unknown_bool[] = "unknown_switch"; | 28 const char unknown_bool[] = "unknown_switch"; |
| 28 const char unknown_string[] = "unknown_other_switch"; | 29 const char unknown_string[] = "unknown_other_switch"; |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 // Tests a simple string pref on the command line. | 33 // Tests a simple string pref on the command line. |
| 33 TEST(CommandLinePrefStoreTest, SimpleStringPref) { | 34 TEST(CommandLinePrefStoreTest, SimpleStringPref) { |
| 34 CommandLine cl(CommandLine::NO_PROGRAM); | 35 CommandLine cl(CommandLine::NO_PROGRAM); |
| 35 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); | 36 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); |
| 36 CommandLinePrefStore store(&cl); | 37 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); |
| 37 | 38 |
| 38 Value* actual = NULL; | 39 Value* actual = NULL; |
| 39 EXPECT_EQ(PrefStore::READ_OK, | 40 EXPECT_EQ(PrefStore::READ_OK, |
| 40 store.GetValue(prefs::kApplicationLocale, &actual)); | 41 store->GetValue(prefs::kApplicationLocale, &actual)); |
| 41 std::string result; | 42 std::string result; |
| 42 EXPECT_TRUE(actual->GetAsString(&result)); | 43 EXPECT_TRUE(actual->GetAsString(&result)); |
| 43 EXPECT_EQ("hi-MOM", result); | 44 EXPECT_EQ("hi-MOM", result); |
| 44 } | 45 } |
| 45 | 46 |
| 46 // Tests a simple boolean pref on the command line. | 47 // Tests a simple boolean pref on the command line. |
| 47 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { | 48 TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { |
| 48 CommandLine cl(CommandLine::NO_PROGRAM); | 49 CommandLine cl(CommandLine::NO_PROGRAM); |
| 49 cl.AppendSwitch(switches::kNoProxyServer); | 50 cl.AppendSwitch(switches::kNoProxyServer); |
| 50 CommandLinePrefStore store(&cl); | 51 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); |
| 51 | 52 |
| 52 Value* actual = NULL; | 53 Value* actual = NULL; |
| 53 ASSERT_EQ(PrefStore::READ_OK, store.GetValue(prefs::kNoProxyServer, &actual)); | 54 ASSERT_EQ(PrefStore::READ_OK, store->GetValue(prefs::kNoProxyServer, |
| 55 &actual)); |
| 54 bool result; | 56 bool result; |
| 55 EXPECT_TRUE(actual->GetAsBoolean(&result)); | 57 EXPECT_TRUE(actual->GetAsBoolean(&result)); |
| 56 EXPECT_TRUE(result); | 58 EXPECT_TRUE(result); |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Tests a command line with no recognized prefs. | 61 // Tests a command line with no recognized prefs. |
| 60 TEST(CommandLinePrefStoreTest, NoPrefs) { | 62 TEST(CommandLinePrefStoreTest, NoPrefs) { |
| 61 CommandLine cl(CommandLine::NO_PROGRAM); | 63 CommandLine cl(CommandLine::NO_PROGRAM); |
| 62 cl.AppendSwitch(unknown_string); | 64 cl.AppendSwitch(unknown_string); |
| 63 cl.AppendSwitchASCII(unknown_bool, "a value"); | 65 cl.AppendSwitchASCII(unknown_bool, "a value"); |
| 64 CommandLinePrefStore store(&cl); | 66 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); |
| 65 | 67 |
| 66 Value* actual = NULL; | 68 Value* actual = NULL; |
| 67 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_bool, &actual)); | 69 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual)); |
| 68 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_string, &actual)); | 70 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual)); |
| 69 } | 71 } |
| 70 | 72 |
| 71 // Tests a complex command line with multiple known and unknown switches. | 73 // Tests a complex command line with multiple known and unknown switches. |
| 72 TEST(CommandLinePrefStoreTest, MultipleSwitches) { | 74 TEST(CommandLinePrefStoreTest, MultipleSwitches) { |
| 73 CommandLine cl(CommandLine::NO_PROGRAM); | 75 CommandLine cl(CommandLine::NO_PROGRAM); |
| 74 cl.AppendSwitch(unknown_string); | 76 cl.AppendSwitch(unknown_string); |
| 75 cl.AppendSwitch(switches::kProxyAutoDetect); | 77 cl.AppendSwitch(switches::kProxyAutoDetect); |
| 76 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); | 78 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); |
| 77 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); | 79 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); |
| 78 cl.AppendSwitchASCII(unknown_bool, "a value"); | 80 cl.AppendSwitchASCII(unknown_bool, "a value"); |
| 79 CommandLinePrefStore store(&cl); | 81 scoped_refptr<CommandLinePrefStore> store = new CommandLinePrefStore(&cl); |
| 80 | 82 |
| 81 Value* actual = NULL; | 83 Value* actual = NULL; |
| 82 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_bool, &actual)); | 84 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_bool, &actual)); |
| 83 ASSERT_EQ(PrefStore::READ_OK, | 85 ASSERT_EQ(PrefStore::READ_OK, |
| 84 store.GetValue(prefs::kProxyAutoDetect, &actual)); | 86 store->GetValue(prefs::kProxyAutoDetect, &actual)); |
| 85 bool bool_result = false; | 87 bool bool_result = false; |
| 86 EXPECT_TRUE(actual->GetAsBoolean(&bool_result)); | 88 EXPECT_TRUE(actual->GetAsBoolean(&bool_result)); |
| 87 EXPECT_TRUE(bool_result); | 89 EXPECT_TRUE(bool_result); |
| 88 | 90 |
| 89 EXPECT_EQ(PrefStore::READ_NO_VALUE, store.GetValue(unknown_string, &actual)); | 91 EXPECT_EQ(PrefStore::READ_NO_VALUE, store->GetValue(unknown_string, &actual)); |
| 90 std::string string_result = ""; | 92 std::string string_result = ""; |
| 91 ASSERT_EQ(PrefStore::READ_OK, store.GetValue(prefs::kProxyServer, &actual)); | 93 ASSERT_EQ(PrefStore::READ_OK, store->GetValue(prefs::kProxyServer, &actual)); |
| 92 EXPECT_TRUE(actual->GetAsString(&string_result)); | 94 EXPECT_TRUE(actual->GetAsString(&string_result)); |
| 93 EXPECT_EQ("proxy", string_result); | 95 EXPECT_EQ("proxy", string_result); |
| 94 ASSERT_EQ(PrefStore::READ_OK, | 96 ASSERT_EQ(PrefStore::READ_OK, |
| 95 store.GetValue(prefs::kProxyBypassList, &actual)); | 97 store->GetValue(prefs::kProxyBypassList, &actual)); |
| 96 EXPECT_TRUE(actual->GetAsString(&string_result)); | 98 EXPECT_TRUE(actual->GetAsString(&string_result)); |
| 97 EXPECT_EQ("list", string_result); | 99 EXPECT_EQ("list", string_result); |
| 98 } | 100 } |
| 99 | 101 |
| 100 // Tests proxy switch validation. | 102 // Tests proxy switch validation. |
| 101 TEST(CommandLinePrefStoreTest, ProxySwitchValidation) { | 103 TEST(CommandLinePrefStoreTest, ProxySwitchValidation) { |
| 102 CommandLine cl(CommandLine::NO_PROGRAM); | 104 CommandLine cl(CommandLine::NO_PROGRAM); |
| 103 | 105 |
| 104 // No switches. | 106 // No switches. |
| 105 TestCommandLinePrefStore store(&cl); | 107 scoped_refptr<TestCommandLinePrefStore> store = |
| 106 EXPECT_TRUE(store.ProxySwitchesAreValid()); | 108 new TestCommandLinePrefStore(&cl); |
| 109 EXPECT_TRUE(store->ProxySwitchesAreValid()); |
| 107 | 110 |
| 108 // Only no-proxy. | 111 // Only no-proxy. |
| 109 cl.AppendSwitch(switches::kNoProxyServer); | 112 cl.AppendSwitch(switches::kNoProxyServer); |
| 110 TestCommandLinePrefStore store2(&cl); | 113 scoped_refptr<TestCommandLinePrefStore> store2 = |
| 111 EXPECT_TRUE(store2.ProxySwitchesAreValid()); | 114 new TestCommandLinePrefStore(&cl); |
| 115 EXPECT_TRUE(store2->ProxySwitchesAreValid()); |
| 112 | 116 |
| 113 // Another proxy switch too. | 117 // Another proxy switch too. |
| 114 cl.AppendSwitch(switches::kProxyAutoDetect); | 118 cl.AppendSwitch(switches::kProxyAutoDetect); |
| 115 TestCommandLinePrefStore store3(&cl); | 119 scoped_refptr<TestCommandLinePrefStore> store3 = |
| 116 EXPECT_FALSE(store3.ProxySwitchesAreValid()); | 120 new TestCommandLinePrefStore(&cl); |
| 121 EXPECT_FALSE(store3->ProxySwitchesAreValid()); |
| 117 | 122 |
| 118 // All proxy switches except no-proxy. | 123 // All proxy switches except no-proxy. |
| 119 CommandLine cl2(CommandLine::NO_PROGRAM); | 124 CommandLine cl2(CommandLine::NO_PROGRAM); |
| 120 cl2.AppendSwitch(switches::kProxyAutoDetect); | 125 cl2.AppendSwitch(switches::kProxyAutoDetect); |
| 121 cl2.AppendSwitchASCII(switches::kProxyServer, "server"); | 126 cl2.AppendSwitchASCII(switches::kProxyServer, "server"); |
| 122 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url"); | 127 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url"); |
| 123 cl2.AppendSwitchASCII(switches::kProxyBypassList, "list"); | 128 cl2.AppendSwitchASCII(switches::kProxyBypassList, "list"); |
| 124 TestCommandLinePrefStore store4(&cl2); | 129 scoped_refptr<TestCommandLinePrefStore> store4 = |
| 125 EXPECT_TRUE(store4.ProxySwitchesAreValid()); | 130 new TestCommandLinePrefStore(&cl2); |
| 131 EXPECT_TRUE(store4->ProxySwitchesAreValid()); |
| 126 } | 132 } |
| OLD | NEW |