Home | History | Annotate | Download | only in browser
      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 DataReductionProxyStatisticsPrefs;
     26 
     27 class TestDataReductionProxyConfig : public DataReductionProxyConfigurator {
     28  public:
     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 {}
     39 
     40   // True if the proxy has been enabled, i.e., only after |Enable| has been
     41   // called. Defaults to false.
     42   bool enabled_;
     43 
     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.
     46   bool restricted_;
     47 
     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_;
     52 
     53   // The origins that are passed to |Enable|.
     54   std::string origin_;
     55   std::string fallback_origin_;
     56   std::string ssl_origin_;
     57 };
     58 
     59 template <class C>
     60 class MockDataReductionProxySettings : public C {
     61  public:
     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));
     84 
     85   // SetProxyConfigs should always call LogProxyState exactly once.
     86   virtual void SetProxyConfigs(bool enabled,
     87                                bool alternative_enabled,
     88                                bool restricted,
     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);
     92   }
     93   virtual void GetNetworkList(net::NetworkInterfaceList* interfaces,
     94                               int policy) OVERRIDE {
     95     if (!network_interfaces_.get())
     96       return;
     97     for (size_t i = 0; i < network_interfaces_->size(); ++i)
     98       interfaces->push_back(network_interfaces_->at(i));
     99   }
    100 
    101   scoped_ptr<net::NetworkInterfaceList> network_interfaces_;
    102 };
    103 
    104 class DataReductionProxySettingsTestBase : public testing::Test {
    105  public:
    106   static void AddTestProxyToCommandLine();
    107 
    108   DataReductionProxySettingsTestBase();
    109   DataReductionProxySettingsTestBase(bool allowed,
    110                                      bool fallback_allowed,
    111                                      bool alt_allowed,
    112                                      bool promo_allowed);
    113   virtual ~DataReductionProxySettingsTestBase();
    114 
    115   void AddProxyToCommandLine();
    116 
    117   virtual void SetUp() OVERRIDE;
    118 
    119   template <class C> void ResetSettings(bool allowed,
    120                                         bool fallback_allowed,
    121                                         bool alt_allowed,
    122                                         bool promo_allowed,
    123                                         bool holdback);
    124   virtual void ResetSettings(bool allowed,
    125                              bool fallback_allowed,
    126                              bool alt_allowed,
    127                              bool promo_allowed,
    128                              bool holdback) = 0;
    129 
    130   template <class C> void SetProbeResult(
    131       const std::string& test_url,
    132       const std::string& response,
    133       ProbeURLFetchResult state,
    134       bool success,
    135       int expected_calls);
    136   virtual void SetProbeResult(const std::string& test_url,
    137                               const std::string& response,
    138                               ProbeURLFetchResult result,
    139                               bool success,
    140                               int expected_calls) = 0;
    141 
    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;
    161   }
    162 
    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_;
    168   bool proxy_enabled_;
    169   scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
    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& response,
    191                               ProbeURLFetchResult result,
    192                               bool success,
    193                               int expected_calls) OVERRIDE {
    194     return DataReductionProxySettingsTestBase::SetProbeResult<C>(
    195         test_url,
    196         response,
    197         result,
    198         success,
    199         expected_calls);
    200   }
    201 };
    202 
    203 }  // namespace data_reduction_proxy
    204 
    205 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_TEST_UTILS_H_
    206