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.
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_
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configurator.h"
12 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params_test_utils.h"
13 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
14 #include "net/base/net_util.h"
15 #include "net/url_request/test_url_fetcher_factory.h"
16 #include "net/url_request/url_request_test_util.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 class TestingPrefServiceSimple;
23 namespace data_reduction_proxy {
25 class TestDataReductionProxyConfig : public DataReductionProxyConfigurator {
27 TestDataReductionProxyConfig();
28 virtual ~TestDataReductionProxyConfig() {}
29 virtual void Enable(bool restricted,
30 bool fallback_restricted,
31 const std::string& primary_origin,
32 const std::string& fallback_origin,
33 const std::string& ssl_origin) OVERRIDE;
34 virtual void Disable() OVERRIDE;
35 virtual void AddHostPatternToBypass(const std::string& pattern) OVERRIDE {}
36 virtual void AddURLPatternToBypass(const std::string& pattern) OVERRIDE {}
38 // True if the proxy has been enabled, i.e., only after |Enable| has been
39 // called. Defaults to false.
42 // Describes whether the proxy has been put in a restricted mode. True if
43 // |Enable| is called with |restricted| set to true. Defaults to false.
46 // Describes whether the proxy has been put in a mode where the fallback
47 // configuration has been disallowed. True if |Enable| is called with
48 // |fallback_restricted| set to true. Defaults to false.
49 bool fallback_restricted_;
51 // The origins that are passed to |Enable|.
53 std::string fallback_origin_;
54 std::string ssl_origin_;
58 class MockDataReductionProxySettings : public C {
60 MockDataReductionProxySettings<C>() : DataReductionProxySettings(
61 new TestDataReductionProxyParams(
62 DataReductionProxyParams::kAllowed |
63 DataReductionProxyParams::kFallbackAllowed |
64 DataReductionProxyParams::kPromoAllowed,
65 TestDataReductionProxyParams::HAS_EVERYTHING &
66 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN)) {}
67 MockDataReductionProxySettings<C>(int flags)
68 : C(new TestDataReductionProxyParams(flags,
69 TestDataReductionProxyParams::HAS_EVERYTHING &
70 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN)) {}
71 MOCK_METHOD0(GetURLFetcherForAvailabilityCheck, net::URLFetcher*());
72 MOCK_METHOD0(GetOriginalProfilePrefs, PrefService*());
73 MOCK_METHOD0(GetLocalStatePrefs, PrefService*());
74 MOCK_METHOD3(LogProxyState, void(
75 bool enabled, bool restricted, bool at_startup));
76 MOCK_METHOD1(RecordProbeURLFetchResult,
77 void(ProbeURLFetchResult result));
78 MOCK_METHOD1(RecordStartupState,
79 void(ProxyStartupState state));
81 // SetProxyConfigs should always call LogProxyState exactly once.
82 virtual void SetProxyConfigs(bool enabled,
83 bool alternative_enabled,
85 bool at_startup) OVERRIDE {
86 EXPECT_CALL(*this, LogProxyState(enabled, restricted, at_startup)).Times(1);
87 C::SetProxyConfigs(enabled, alternative_enabled, restricted, at_startup);
89 virtual void GetNetworkList(net::NetworkInterfaceList* interfaces,
90 int policy) OVERRIDE {
91 if (!network_interfaces_.get())
93 for (size_t i = 0; i < network_interfaces_->size(); ++i)
94 interfaces->push_back(network_interfaces_->at(i));
97 scoped_ptr<net::NetworkInterfaceList> network_interfaces_;
100 class DataReductionProxySettingsTestBase : public testing::Test {
102 static void AddTestProxyToCommandLine();
104 DataReductionProxySettingsTestBase();
105 DataReductionProxySettingsTestBase(bool allowed,
106 bool fallback_allowed,
109 virtual ~DataReductionProxySettingsTestBase();
111 void AddProxyToCommandLine();
113 virtual void SetUp() OVERRIDE;
115 template <class C> void ResetSettings(bool allowed,
116 bool fallback_allowed,
120 virtual void ResetSettings(bool allowed,
121 bool fallback_allowed,
126 template <class C> void SetProbeResult(
127 const std::string& test_url,
128 const std::string& response,
129 ProbeURLFetchResult state,
132 virtual void SetProbeResult(const std::string& test_url,
133 const std::string& response,
134 ProbeURLFetchResult result,
136 int expected_calls) = 0;
138 void CheckProxyConfigs(bool expected_enabled,
139 bool expected_restricted,
140 bool expected_fallback_restricted);
141 void CheckProbe(bool initially_enabled,
142 const std::string& probe_url,
143 const std::string& response,
144 bool request_success,
145 bool expected_enabled,
146 bool expected_restricted,
147 bool expected_fallback_restricted);
148 void CheckProbeOnIPChange(const std::string& probe_url,
149 const std::string& response,
150 bool request_success,
151 bool expected_enabled,
152 bool expected_fallback_restricted);
153 void CheckOnPrefChange(bool enabled, bool expected_enabled, bool managed);
154 void CheckInitDataReductionProxy(bool enabled_at_startup);
155 void RegisterSyntheticFieldTrialCallback(bool proxy_enabled) {
156 proxy_enabled_ = proxy_enabled;
159 TestingPrefServiceSimple pref_service_;
160 scoped_ptr<DataReductionProxyConfigurator> configurator_;
161 scoped_ptr<DataReductionProxySettings> settings_;
162 scoped_ptr<TestDataReductionProxyParams> expected_params_;
163 base::Time last_update_time_;
167 // Test implementations should be subclasses of an instantiation of this
168 // class parameterized for whatever DataReductionProxySettings class
171 class ConcreteDataReductionProxySettingsTest
172 : public DataReductionProxySettingsTestBase {
174 typedef MockDataReductionProxySettings<C> MockSettings;
175 virtual void ResetSettings(bool allowed,
176 bool fallback_allowed,
179 bool holdback) OVERRIDE {
180 return DataReductionProxySettingsTestBase::ResetSettings<C>(
181 allowed, fallback_allowed, alt_allowed, promo_allowed, holdback);
184 virtual void SetProbeResult(const std::string& test_url,
185 const std::string& response,
186 ProbeURLFetchResult result,
188 int expected_calls) OVERRIDE {
189 return DataReductionProxySettingsTestBase::SetProbeResult<C>(
198 } // namespace data_reduction_proxy
200 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_