1 // Copyright 2014 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 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ 6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ 7 8 9 #include "base/prefs/testing_pref_service.h" 10 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configurator.h" 11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h" 12 #include "net/url_request/test_url_fetcher_factory.h" 13 #include "net/url_request/url_request_test_util.h" 14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 class PrefService; 18 class TestingPrefServiceSimple; 19 20 namespace data_reduction_proxy { 21 22 class TestDataReductionProxyConfig : public DataReductionProxyConfigurator { 23 public: 24 TestDataReductionProxyConfig(); ~TestDataReductionProxyConfig()25 virtual ~TestDataReductionProxyConfig() {} 26 virtual void Enable(bool restricted, 27 bool fallback_restricted, 28 const std::string& primary_origin, 29 const std::string& fallback_origin, 30 const std::string& ssl_origin) OVERRIDE; 31 virtual void Disable() OVERRIDE; AddHostPatternToBypass(const std::string & pattern)32 virtual void AddHostPatternToBypass(const std::string& pattern) OVERRIDE {} AddURLPatternToBypass(const std::string & pattern)33 virtual void AddURLPatternToBypass(const std::string& pattern) OVERRIDE {} 34 35 // True if the proxy has been enabled, i.e., only after |Enable| has been 36 // called. Defaults to false. 37 bool enabled_; 38 39 // Describes whether the proxy has been put in a restricted mode. True if 40 // |Enable| is called with |restricted| set to true. Defaults to false. 41 bool restricted_; 42 43 // Describes whether the proxy has been put in a mode where the fallback 44 // configuration has been disallowed. True if |Enable| is called with 45 // |fallback_restricted| set to true. Defaults to false. 46 bool fallback_restricted_; 47 48 // The origins that are passed to |Enable|. 49 std::string origin_; 50 std::string fallback_origin_; 51 std::string ssl_origin_; 52 }; 53 54 template <class C> 55 class MockDataReductionProxySettings : public C { 56 public: 57 MockDataReductionProxySettings<C>() : DataReductionProxySettings( 58 new DataReductionProxyParams( 59 DataReductionProxyParams::kAllowed | 60 DataReductionProxyParams::kFallbackAllowed | 61 DataReductionProxyParams::kPromoAllowed)) {} 62 MockDataReductionProxySettings<C>(int flags) C(new DataReductionProxyParams (flags))63 : C(new DataReductionProxyParams(flags)) {} 64 MOCK_METHOD0(GetURLFetcherForAvailabilityCheck, net::URLFetcher*()); 65 MOCK_METHOD0(GetURLFetcherForWarmup, net::URLFetcher*()); 66 MOCK_METHOD0(GetOriginalProfilePrefs, PrefService*()); 67 MOCK_METHOD0(GetLocalStatePrefs, PrefService*()); 68 MOCK_METHOD3(LogProxyState, void( 69 bool enabled, bool restricted, bool at_startup)); 70 MOCK_METHOD1(RecordProbeURLFetchResult, 71 void(ProbeURLFetchResult result)); 72 MOCK_METHOD1(RecordStartupState, 73 void(ProxyStartupState state)); 74 75 // SetProxyConfigs should always call LogProxyState exactly once. SetProxyConfigs(bool enabled,bool alternative_enabled,bool restricted,bool at_startup)76 virtual void SetProxyConfigs(bool enabled, 77 bool alternative_enabled, 78 bool restricted, 79 bool at_startup) OVERRIDE { 80 EXPECT_CALL(*this, LogProxyState(enabled, restricted, at_startup)).Times(1); 81 C::SetProxyConfigs(enabled, alternative_enabled, restricted, at_startup); 82 } 83 }; 84 85 class DataReductionProxySettingsTestBase : public testing::Test { 86 public: 87 static void AddTestProxyToCommandLine(); 88 89 DataReductionProxySettingsTestBase(); 90 DataReductionProxySettingsTestBase(bool allowed, 91 bool fallback_allowed, 92 bool alt_allowed, 93 bool promo_allowed); 94 virtual ~DataReductionProxySettingsTestBase(); 95 96 void AddProxyToCommandLine(); 97 98 virtual void SetUp() OVERRIDE; 99 100 template <class C> void ResetSettings(bool allowed, 101 bool fallback_allowed, 102 bool alt_allowed, 103 bool promo_allowed); 104 virtual void ResetSettings(bool allowed, 105 bool fallback_allowed, 106 bool alt_allowed, 107 bool promo_allowed) = 0; 108 109 template <class C> void SetProbeResult( 110 const std::string& test_url, 111 const std::string& warmup_test_url, 112 const std::string& response, 113 ProbeURLFetchResult state, 114 bool success, 115 int expected_calls); 116 virtual void SetProbeResult(const std::string& test_url, 117 const std::string& warmup_test_url, 118 const std::string& response, 119 ProbeURLFetchResult result, 120 bool success, 121 int expected_calls) = 0; 122 123 void CheckProxyConfigs(bool expected_enabled, 124 bool expected_restricted, 125 bool expected_fallback_restricted); 126 void CheckProbe(bool initially_enabled, 127 const std::string& probe_url, 128 const std::string& warmup_url, 129 const std::string& response, 130 bool request_success, 131 bool expected_enabled, 132 bool expected_restricted, 133 bool expected_fallback_restricted); 134 void CheckProbeOnIPChange(const std::string& probe_url, 135 const std::string& warmup_url, 136 const std::string& response, 137 bool request_success, 138 bool expected_enabled, 139 bool expected_fallback_restricted); 140 void CheckOnPrefChange(bool enabled, bool expected_enabled, bool managed); 141 void CheckInitDataReductionProxy(bool enabled_at_startup); 142 143 TestingPrefServiceSimple pref_service_; 144 scoped_ptr<DataReductionProxySettings> settings_; 145 base::Time last_update_time_; 146 }; 147 148 // Test implementations should be subclasses of an instantiation of this 149 // class parameterized for whatever DataReductionProxySettings class 150 // is being tested. 151 template <class C> 152 class ConcreteDataReductionProxySettingsTest 153 : public DataReductionProxySettingsTestBase { 154 public: 155 typedef MockDataReductionProxySettings<C> MockSettings; ResetSettings(bool allowed,bool fallback_allowed,bool alt_allowed,bool promo_allowed)156 virtual void ResetSettings(bool allowed, 157 bool fallback_allowed, 158 bool alt_allowed, 159 bool promo_allowed) OVERRIDE { 160 return DataReductionProxySettingsTestBase::ResetSettings<C>( 161 allowed, fallback_allowed, alt_allowed, promo_allowed); 162 } 163 SetProbeResult(const std::string & test_url,const std::string & warmup_test_url,const std::string & response,ProbeURLFetchResult result,bool success,int expected_calls)164 virtual void SetProbeResult(const std::string& test_url, 165 const std::string& warmup_test_url, 166 const std::string& response, 167 ProbeURLFetchResult result, 168 bool success, 169 int expected_calls) OVERRIDE { 170 return DataReductionProxySettingsTestBase::SetProbeResult<C>( 171 test_url, 172 warmup_test_url, 173 response, 174 result, 175 success, 176 expected_calls); 177 } 178 }; 179 180 } // namespace data_reduction_proxy 181 182 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_ 183