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 DataReductionProxyStatisticsPrefs
;
27 class TestDataReductionProxyConfig
: public DataReductionProxyConfigurator
{
29 TestDataReductionProxyConfig();
30 virtual ~TestDataReductionProxyConfig() {}
31 virtual void Enable(bool restricted
,
32 bool fallback_restricted
,
33 const std::string
& primary_origin
,
34 const std::string
& fallback_origin
,
35 const std::string
& ssl_origin
) override
;
36 virtual void Disable() override
;
37 virtual void AddHostPatternToBypass(const std::string
& pattern
) override
{}
38 virtual void AddURLPatternToBypass(const std::string
& pattern
) override
{}
40 // True if the proxy has been enabled, i.e., only after |Enable| has been
41 // called. Defaults to false.
44 // Describes whether the proxy has been put in a restricted mode. True if
45 // |Enable| is called with |restricted| set to true. Defaults to false.
48 // Describes whether the proxy has been put in a mode where the fallback
49 // configuration has been disallowed. True if |Enable| is called with
50 // |fallback_restricted| set to true. Defaults to false.
51 bool fallback_restricted_
;
53 // The origins that are passed to |Enable|.
55 std::string fallback_origin_
;
56 std::string ssl_origin_
;
60 class MockDataReductionProxySettings
: public C
{
62 MockDataReductionProxySettings
<C
>() : DataReductionProxySettings(
63 new TestDataReductionProxyParams(
64 DataReductionProxyParams::kAllowed
|
65 DataReductionProxyParams::kFallbackAllowed
|
66 DataReductionProxyParams::kPromoAllowed
,
67 TestDataReductionProxyParams::HAS_EVERYTHING
&
68 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN
&
69 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
)) {}
70 MockDataReductionProxySettings
<C
>(int flags
)
71 : C(new TestDataReductionProxyParams(flags
,
72 TestDataReductionProxyParams::HAS_EVERYTHING
&
73 ~TestDataReductionProxyParams::HAS_DEV_ORIGIN
&
74 ~TestDataReductionProxyParams::HAS_DEV_FALLBACK_ORIGIN
)) {}
75 MOCK_METHOD0(GetURLFetcherForAvailabilityCheck
, net::URLFetcher
*());
76 MOCK_METHOD0(GetOriginalProfilePrefs
, PrefService
*());
77 MOCK_METHOD0(GetLocalStatePrefs
, PrefService
*());
78 MOCK_METHOD3(LogProxyState
, void(
79 bool enabled
, bool restricted
, bool at_startup
));
80 MOCK_METHOD1(RecordProbeURLFetchResult
,
81 void(ProbeURLFetchResult result
));
82 MOCK_METHOD1(RecordStartupState
,
83 void(ProxyStartupState state
));
85 // SetProxyConfigs should always call LogProxyState exactly once.
86 virtual void SetProxyConfigs(bool enabled
,
87 bool alternative_enabled
,
89 bool at_startup
) override
{
90 EXPECT_CALL(*this, LogProxyState(enabled
, restricted
, at_startup
)).Times(1);
91 C::SetProxyConfigs(enabled
, alternative_enabled
, restricted
, at_startup
);
93 virtual void GetNetworkList(net::NetworkInterfaceList
* interfaces
,
94 int policy
) override
{
95 if (!network_interfaces_
.get())
97 for (size_t i
= 0; i
< network_interfaces_
->size(); ++i
)
98 interfaces
->push_back(network_interfaces_
->at(i
));
101 scoped_ptr
<net::NetworkInterfaceList
> network_interfaces_
;
104 class DataReductionProxySettingsTestBase
: public testing::Test
{
106 static void AddTestProxyToCommandLine();
108 DataReductionProxySettingsTestBase();
109 DataReductionProxySettingsTestBase(bool allowed
,
110 bool fallback_allowed
,
113 virtual ~DataReductionProxySettingsTestBase();
115 void AddProxyToCommandLine();
117 virtual void SetUp() override
;
119 template <class C
> void ResetSettings(bool allowed
,
120 bool fallback_allowed
,
124 virtual void ResetSettings(bool allowed
,
125 bool fallback_allowed
,
130 template <class C
> void SetProbeResult(
131 const std::string
& test_url
,
132 const std::string
& response
,
133 ProbeURLFetchResult state
,
136 virtual void SetProbeResult(const std::string
& test_url
,
137 const std::string
& response
,
138 ProbeURLFetchResult result
,
140 int expected_calls
) = 0;
142 void CheckProxyConfigs(bool expected_enabled
,
143 bool expected_restricted
,
144 bool expected_fallback_restricted
);
145 void CheckProbe(bool initially_enabled
,
146 const std::string
& probe_url
,
147 const std::string
& response
,
148 bool request_success
,
149 bool expected_enabled
,
150 bool expected_restricted
,
151 bool expected_fallback_restricted
);
152 void CheckProbeOnIPChange(const std::string
& probe_url
,
153 const std::string
& response
,
154 bool request_success
,
155 bool expected_enabled
,
156 bool expected_fallback_restricted
);
157 void CheckOnPrefChange(bool enabled
, bool expected_enabled
, bool managed
);
158 void CheckInitDataReductionProxy(bool enabled_at_startup
);
159 void RegisterSyntheticFieldTrialCallback(bool proxy_enabled
) {
160 proxy_enabled_
= proxy_enabled
;
163 TestingPrefServiceSimple pref_service_
;
164 scoped_ptr
<DataReductionProxyConfigurator
> configurator_
;
165 scoped_ptr
<DataReductionProxySettings
> settings_
;
166 scoped_ptr
<TestDataReductionProxyParams
> expected_params_
;
167 base::Time last_update_time_
;
169 scoped_ptr
<DataReductionProxyStatisticsPrefs
> statistics_prefs_
;
172 // Test implementations should be subclasses of an instantiation of this
173 // class parameterized for whatever DataReductionProxySettings class
176 class ConcreteDataReductionProxySettingsTest
177 : public DataReductionProxySettingsTestBase
{
179 typedef MockDataReductionProxySettings
<C
> MockSettings
;
180 virtual void ResetSettings(bool allowed
,
181 bool fallback_allowed
,
184 bool holdback
) override
{
185 return DataReductionProxySettingsTestBase::ResetSettings
<C
>(
186 allowed
, fallback_allowed
, alt_allowed
, promo_allowed
, holdback
);
189 virtual void SetProbeResult(const std::string
& test_url
,
190 const std::string
& response
,
191 ProbeURLFetchResult result
,
193 int expected_calls
) override
{
194 return DataReductionProxySettingsTestBase::SetProbeResult
<C
>(
203 } // namespace data_reduction_proxy
205 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_