Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_settings_test_utils.h
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/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"
19
20 class PrefService;
21 class TestingPrefServiceSimple;
22
23 namespace data_reduction_proxy {
24
25 class TestDataReductionProxyConfig : public DataReductionProxyConfigurator {
26  public:
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 {}
37
38   // True if the proxy has been enabled, i.e., only after |Enable| has been
39   // called. Defaults to false.
40   bool enabled_;
41
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.
44   bool restricted_;
45
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_;
50
51   // The origins that are passed to |Enable|.
52   std::string origin_;
53   std::string fallback_origin_;
54   std::string ssl_origin_;
55 };
56
57 template <class C>
58 class MockDataReductionProxySettings : public C {
59  public:
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(GetURLFetcherForWarmup, net::URLFetcher*());
73   MOCK_METHOD0(GetOriginalProfilePrefs, PrefService*());
74   MOCK_METHOD0(GetLocalStatePrefs, PrefService*());
75   MOCK_METHOD3(LogProxyState, void(
76       bool enabled, bool restricted, bool at_startup));
77   MOCK_METHOD1(RecordProbeURLFetchResult,
78                void(ProbeURLFetchResult result));
79   MOCK_METHOD1(RecordStartupState,
80                void(ProxyStartupState state));
81
82   // SetProxyConfigs should always call LogProxyState exactly once.
83   virtual void SetProxyConfigs(bool enabled,
84                                bool alternative_enabled,
85                                bool restricted,
86                                bool at_startup) OVERRIDE {
87     EXPECT_CALL(*this, LogProxyState(enabled, restricted, at_startup)).Times(1);
88     C::SetProxyConfigs(enabled, alternative_enabled, restricted, at_startup);
89   }
90   virtual void GetNetworkList(net::NetworkInterfaceList* interfaces,
91                               int policy) OVERRIDE {
92     if (!network_interfaces_.get())
93       return;
94     for (size_t i = 0; i < network_interfaces_->size(); ++i)
95       interfaces->push_back(network_interfaces_->at(i));
96   }
97
98   scoped_ptr<net::NetworkInterfaceList> network_interfaces_;
99 };
100
101 class DataReductionProxySettingsTestBase : public testing::Test {
102  public:
103   static void AddTestProxyToCommandLine();
104
105   DataReductionProxySettingsTestBase();
106   DataReductionProxySettingsTestBase(bool allowed,
107                                      bool fallback_allowed,
108                                      bool alt_allowed,
109                                      bool promo_allowed);
110   virtual ~DataReductionProxySettingsTestBase();
111
112   void AddProxyToCommandLine();
113
114   virtual void SetUp() OVERRIDE;
115
116   template <class C> void ResetSettings(bool allowed,
117                                         bool fallback_allowed,
118                                         bool alt_allowed,
119                                         bool promo_allowed,
120                                         bool holdback);
121   virtual void ResetSettings(bool allowed,
122                              bool fallback_allowed,
123                              bool alt_allowed,
124                              bool promo_allowed,
125                              bool holdback) = 0;
126
127   template <class C> void SetProbeResult(
128       const std::string& test_url,
129       const std::string& warmup_test_url,
130       const std::string& response,
131       ProbeURLFetchResult state,
132       bool success,
133       int expected_calls);
134   virtual void SetProbeResult(const std::string& test_url,
135                               const std::string& warmup_test_url,
136                               const std::string& response,
137                               ProbeURLFetchResult result,
138                               bool success,
139                               int expected_calls) = 0;
140
141   void CheckProxyConfigs(bool expected_enabled,
142                          bool expected_restricted,
143                          bool expected_fallback_restricted);
144   void CheckProbe(bool initially_enabled,
145                   const std::string& probe_url,
146                   const std::string& warmup_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& warmup_url,
154                             const std::string& response,
155                             bool request_success,
156                             bool expected_enabled,
157                             bool expected_fallback_restricted);
158   void CheckOnPrefChange(bool enabled, bool expected_enabled, bool managed);
159   void CheckInitDataReductionProxy(bool enabled_at_startup);
160   void RegisterSyntheticFieldTrialCallback(bool proxy_enabled) {
161     proxy_enabled_ = proxy_enabled;
162   }
163
164   TestingPrefServiceSimple pref_service_;
165   scoped_ptr<DataReductionProxyConfigurator> configurator_;
166   scoped_ptr<DataReductionProxySettings> settings_;
167   scoped_ptr<TestDataReductionProxyParams> expected_params_;
168   base::Time last_update_time_;
169   bool proxy_enabled_;
170 };
171
172 // Test implementations should be subclasses of an instantiation of this
173 // class parameterized for whatever DataReductionProxySettings class
174 // is being tested.
175 template <class C>
176 class ConcreteDataReductionProxySettingsTest
177     : public DataReductionProxySettingsTestBase {
178  public:
179   typedef MockDataReductionProxySettings<C> MockSettings;
180   virtual void ResetSettings(bool allowed,
181                              bool fallback_allowed,
182                              bool alt_allowed,
183                              bool promo_allowed,
184                              bool holdback) OVERRIDE {
185     return DataReductionProxySettingsTestBase::ResetSettings<C>(
186         allowed, fallback_allowed, alt_allowed, promo_allowed, holdback);
187   }
188
189   virtual void SetProbeResult(const std::string& test_url,
190                               const std::string& warmup_test_url,
191                               const std::string& response,
192                               ProbeURLFetchResult result,
193                               bool success,
194                               int expected_calls) OVERRIDE {
195     return DataReductionProxySettingsTestBase::SetProbeResult<C>(
196         test_url,
197         warmup_test_url,
198         response,
199         result,
200         success,
201         expected_calls);
202   }
203 };
204
205 }  // namespace data_reduction_proxy
206
207 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_