1 // Copyright 2013 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 CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_UNITTEST_H_
6 #define CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_UNITTEST_H_
9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram_samples.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
13 #include "net/url_request/test_url_fetcher_factory.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
18 class TestingPrefServiceSimple
;
20 using spdyproxy::ProbeURLFetchResult
;
21 using spdyproxy::ProxyStartupState
;
24 class MockDataReductionProxySettings
: public C
{
26 MOCK_METHOD0(GetURLFetcher
, net::URLFetcher
*());
27 MOCK_METHOD0(GetOriginalProfilePrefs
, PrefService
*());
28 MOCK_METHOD0(GetLocalStatePrefs
, PrefService
*());
29 MOCK_METHOD3(LogProxyState
, void(
30 bool enabled
, bool restricted
, bool at_startup
));
31 MOCK_METHOD1(RecordProbeURLFetchResult
,
32 void(ProbeURLFetchResult result
));
33 MOCK_METHOD1(RecordStartupState
,
34 void(ProxyStartupState state
));
36 // SetProxyConfigs should always call LogProxyState exactly once.
37 virtual void SetProxyConfigs(
38 bool enabled
, bool restricted
, bool at_startup
) OVERRIDE
{
39 EXPECT_CALL(*this, LogProxyState(enabled
, restricted
, at_startup
)).Times(1);
40 C::SetProxyConfigs(enabled
, restricted
, at_startup
);
44 class DataReductionProxySettingsTestBase
: public testing::Test
{
46 DataReductionProxySettingsTestBase();
47 virtual ~DataReductionProxySettingsTestBase();
49 void AddProxyToCommandLine();
51 virtual void SetUp() OVERRIDE
;
53 template <class C
> void ResetSettings();
54 virtual void ResetSettings() = 0;
56 template <class C
> void SetProbeResult(
57 const std::string
& test_url
,
58 const std::string
& response
,
59 ProbeURLFetchResult state
,
62 virtual void SetProbeResult(const std::string
& test_url
,
63 const std::string
& response
,
64 ProbeURLFetchResult result
,
66 int expected_calls
) = 0;
68 void CheckProxyPref(const std::string
& expected_servers
,
69 const std::string
& expected_mode
);
70 void CheckProxyConfigs(bool expected_enabled
, bool expected_restricted
);
71 void CheckProbe(bool initially_enabled
,
72 const std::string
& probe_url
,
73 const std::string
& response
,
75 bool expected_enabled
,
76 bool expected_restricted
);
77 void CheckProbeOnIPChange(const std::string
& probe_url
,
78 const std::string
& response
,
80 bool expected_enabled
);
81 void CheckOnPrefChange(bool enabled
, bool expected_enabled
);
82 void CheckInitDataReductionProxy(bool enabled_at_startup
);
84 TestingPrefServiceSimple pref_service_
;
85 scoped_ptr
<DataReductionProxySettings
> settings_
;
86 base::Time last_update_time_
;
87 // This is a singleton that will clear all set field trials on destruction.
88 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
91 // Test implementations should be subclasses of an instantiation of this
92 // class parameterized for whatever DataReductionProxySettings class
95 class ConcreteDataReductionProxySettingsTest
96 : public DataReductionProxySettingsTestBase
{
98 typedef MockDataReductionProxySettings
<C
> MockSettings
;
99 virtual void ResetSettings() OVERRIDE
{
100 return DataReductionProxySettingsTestBase::ResetSettings
<C
>();
103 virtual void SetProbeResult(const std::string
& test_url
,
104 const std::string
& response
,
105 ProbeURLFetchResult result
,
107 int expected_calls
) OVERRIDE
{
108 return DataReductionProxySettingsTestBase::SetProbeResult
<C
>(
109 test_url
, response
, result
, success
, expected_calls
);
113 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_UNITTEST_H_