command_line_pref_store_unittest.cc revision 731df977c0511bca2206b5f333555b1205ff1f43
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 3// found in the LICENSE file. 4 5#include <gtest/gtest.h> 6 7#include "app/app_switches.h" 8#include "base/command_line.h" 9#include "base/string_util.h" 10#include "base/values.h" 11#include "chrome/browser/prefs/command_line_pref_store.h" 12#include "chrome/common/chrome_switches.h" 13#include "chrome/common/pref_names.h" 14 15namespace { 16 17class TestCommandLinePrefStore : public CommandLinePrefStore { 18 public: 19 explicit TestCommandLinePrefStore(CommandLine* cl) 20 : CommandLinePrefStore(cl) {} 21 22 bool ProxySwitchesAreValid() { 23 return ValidateProxySwitches(); 24 } 25}; 26 27const char unknown_bool[] = "unknown_switch"; 28const char unknown_string[] = "unknown_other_switch"; 29 30} // namespace 31 32// Tests a simple string pref on the command line. 33TEST(CommandLinePrefStoreTest, SimpleStringPref) { 34 CommandLine cl(CommandLine::NO_PROGRAM); 35 cl.AppendSwitchASCII(switches::kLang, "hi-MOM"); 36 CommandLinePrefStore store(&cl); 37 EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 38 39 std::string result; 40 EXPECT_TRUE(store.prefs()->GetString(prefs::kApplicationLocale, &result)); 41 EXPECT_EQ("hi-MOM", result); 42} 43 44// Tests a simple boolean pref on the command line. 45TEST(CommandLinePrefStoreTest, SimpleBooleanPref) { 46 CommandLine cl(CommandLine::NO_PROGRAM); 47 cl.AppendSwitch(switches::kNoProxyServer); 48 CommandLinePrefStore store(&cl); 49 EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 50 51 bool result; 52 EXPECT_TRUE(store.prefs()->GetBoolean(prefs::kNoProxyServer, &result)); 53 EXPECT_TRUE(result); 54} 55 56// Tests a command line with no recognized prefs. 57TEST(CommandLinePrefStoreTest, NoPrefs) { 58 CommandLine cl(CommandLine::NO_PROGRAM); 59 cl.AppendSwitch(unknown_string); 60 cl.AppendSwitchASCII(unknown_bool, "a value"); 61 CommandLinePrefStore store(&cl); 62 EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 63 64 bool bool_result = false; 65 EXPECT_FALSE(store.prefs()->GetBoolean(unknown_bool, &bool_result)); 66 EXPECT_FALSE(bool_result); 67 68 std::string string_result = ""; 69 EXPECT_FALSE(store.prefs()->GetString(unknown_string, &string_result)); 70 EXPECT_EQ("", string_result); 71} 72 73// Tests a complex command line with multiple known and unknown switches. 74TEST(CommandLinePrefStoreTest, MultipleSwitches) { 75 CommandLine cl(CommandLine::NO_PROGRAM); 76 cl.AppendSwitch(unknown_string); 77 cl.AppendSwitch(switches::kProxyAutoDetect); 78 cl.AppendSwitchASCII(switches::kProxyServer, "proxy"); 79 cl.AppendSwitchASCII(switches::kProxyBypassList, "list"); 80 cl.AppendSwitchASCII(unknown_bool, "a value"); 81 CommandLinePrefStore store(&cl); 82 EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 83 84 bool bool_result = false; 85 EXPECT_FALSE(store.prefs()->GetBoolean(unknown_bool, &bool_result)); 86 EXPECT_FALSE(bool_result); 87 EXPECT_TRUE(store.prefs()->GetBoolean(prefs::kProxyAutoDetect, &bool_result)); 88 EXPECT_TRUE(bool_result); 89 90 std::string string_result = ""; 91 EXPECT_FALSE(store.prefs()->GetString(unknown_string, &string_result)); 92 EXPECT_EQ("", string_result); 93 EXPECT_TRUE(store.prefs()->GetString(prefs::kProxyServer, &string_result)); 94 EXPECT_EQ("proxy", string_result); 95 EXPECT_TRUE(store.prefs()->GetString(prefs::kProxyBypassList, 96 &string_result)); 97 EXPECT_EQ("list", string_result); 98} 99 100// Tests proxy switch validation. 101TEST(CommandLinePrefStoreTest, ProxySwitchValidation) { 102 CommandLine cl(CommandLine::NO_PROGRAM); 103 104 // No switches. 105 TestCommandLinePrefStore store(&cl); 106 EXPECT_EQ(store.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 107 EXPECT_TRUE(store.ProxySwitchesAreValid()); 108 109 // Only no-proxy. 110 cl.AppendSwitch(switches::kNoProxyServer); 111 TestCommandLinePrefStore store2(&cl); 112 EXPECT_EQ(store2.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 113 EXPECT_TRUE(store2.ProxySwitchesAreValid()); 114 115 // Another proxy switch too. 116 cl.AppendSwitch(switches::kProxyAutoDetect); 117 TestCommandLinePrefStore store3(&cl); 118 EXPECT_EQ(store3.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 119 EXPECT_FALSE(store3.ProxySwitchesAreValid()); 120 121 // All proxy switches except no-proxy. 122 CommandLine cl2(CommandLine::NO_PROGRAM); 123 cl2.AppendSwitch(switches::kProxyAutoDetect); 124 cl2.AppendSwitchASCII(switches::kProxyServer, "server"); 125 cl2.AppendSwitchASCII(switches::kProxyPacUrl, "url"); 126 cl2.AppendSwitchASCII(switches::kProxyBypassList, "list"); 127 TestCommandLinePrefStore store4(&cl2); 128 EXPECT_EQ(store4.ReadPrefs(), PrefStore::PREF_READ_ERROR_NONE); 129 EXPECT_TRUE(store4.ProxySwitchesAreValid()); 130} 131