Upstream version 10.38.208.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(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));
80
81   // SetProxyConfigs should always call LogProxyState exactly once.
82   virtual void SetProxyConfigs(bool enabled,
83                                bool alternative_enabled,
84                                bool restricted,
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);
88   }
89   virtual void GetNetworkList(net::NetworkInterfaceList* interfaces,
90                               int policy) OVERRIDE {
91     if (!network_interfaces_.get())
92       return;
93     for (size_t i = 0; i < network_interfaces_->size(); ++i)
94       interfaces->push_back(network_interfaces_->at(i));
95   }
96
97   scoped_ptr<net::NetworkInterfaceList> network_interfaces_;
98 };
99
100 class DataReductionProxySettingsTestBase : public testing::Test {
101  public:
102   static void AddTestProxyToCommandLine();
103
104   DataReductionProxySettingsTestBase();
105   DataReductionProxySettingsTestBase(bool allowed,
106                                      bool fallback_allowed,
107                                      bool alt_allowed,
108                                      bool promo_allowed);
109   virtual ~DataReductionProxySettingsTestBase();
110
111   void AddProxyToCommandLine();
112
113   virtual void SetUp() OVERRIDE;
114
115   template <class C> void ResetSettings(bool allowed,
116                                         bool fallback_allowed,
117                                         bool alt_allowed,
118                                         bool promo_allowed,
119                                         bool holdback);
120   virtual void ResetSettings(bool allowed,
121                              bool fallback_allowed,
122                              bool alt_allowed,
123                              bool promo_allowed,
124                              bool holdback) = 0;
125
126   template <class C> void SetProbeResult(
127       const std::string& test_url,
128       const std::string& response,
129       ProbeURLFetchResult state,
130       bool success,
131       int expected_calls);
132   virtual void SetProbeResult(const std::string& test_url,
133                               const std::string& response,
134                               ProbeURLFetchResult result,
135                               bool success,
136                               int expected_calls) = 0;
137
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;
157   }
158
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_;
164   bool proxy_enabled_;
165 };
166
167 // Test implementations should be subclasses of an instantiation of this
168 // class parameterized for whatever DataReductionProxySettings class
169 // is being tested.
170 template <class C>
171 class ConcreteDataReductionProxySettingsTest
172     : public DataReductionProxySettingsTestBase {
173  public:
174   typedef MockDataReductionProxySettings<C> MockSettings;
175   virtual void ResetSettings(bool allowed,
176                              bool fallback_allowed,
177                              bool alt_allowed,
178                              bool promo_allowed,
179                              bool holdback) OVERRIDE {
180     return DataReductionProxySettingsTestBase::ResetSettings<C>(
181         allowed, fallback_allowed, alt_allowed, promo_allowed, holdback);
182   }
183
184   virtual void SetProbeResult(const std::string& test_url,
185                               const std::string& response,
186                               ProbeURLFetchResult result,
187                               bool success,
188                               int expected_calls) OVERRIDE {
189     return DataReductionProxySettingsTestBase::SetProbeResult<C>(
190         test_url,
191         response,
192         result,
193         success,
194         expected_calls);
195   }
196 };
197
198 }  // namespace data_reduction_proxy
199
200 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_