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 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/base64.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
17 #include "chrome/browser/prefs/proxy_prefs.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_config_test_utils.h"
21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings_test_utils.h"
22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
23 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params_test_utils.h"
25 #include "net/proxy/proxy_server.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h"
31 using testing::AnyNumber
;
32 using testing::Return
;
34 using data_reduction_proxy::DataReductionProxySettings
;
36 // Used for testing the DataReductionProxySettingsAndroid class.
37 class TestDataReductionProxySettingsAndroid
38 : public DataReductionProxySettingsAndroid
{
40 // Constructs an Android settings object for test that wraps the provided
42 explicit TestDataReductionProxySettingsAndroid(
43 DataReductionProxySettings
* settings
)
44 : DataReductionProxySettingsAndroid(),
45 settings_(settings
) {}
47 // Returns the provided setting object. Used by wrapping methods.
48 DataReductionProxySettings
* Settings() override
{ return settings_
; }
50 // The wrapped settings object.
51 DataReductionProxySettings
* settings_
;
55 void data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings(
57 bool fallback_allowed
,
63 flags
|= DataReductionProxyParams::kAllowed
;
65 flags
|= DataReductionProxyParams::kFallbackAllowed
;
67 flags
|= DataReductionProxyParams::kAlternativeAllowed
;
69 flags
|= DataReductionProxyParams::kPromoAllowed
;
71 flags
|= DataReductionProxyParams::kHoldback
;
72 MockDataReductionProxySettings
<C
>* settings
=
73 new MockDataReductionProxySettings
<C
>();
74 settings
->config_
= test_context_
->config();
75 settings
->data_reduction_proxy_service_
=
76 test_context_
->CreateDataReductionProxyService();
77 test_context_
->config()->ResetParamFlagsForTest(flags
);
78 settings
->UpdateConfigValues();
79 EXPECT_CALL(*settings
, GetOriginalProfilePrefs())
81 .WillRepeatedly(Return(test_context_
->pref_service()));
82 EXPECT_CALL(*settings
, GetLocalStatePrefs())
84 .WillRepeatedly(Return(test_context_
->pref_service()));
85 settings_
.reset(settings
);
89 data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings
<
90 DataReductionProxyChromeSettings
>(bool allowed
,
91 bool fallback_allowed
,
96 class DataReductionProxySettingsAndroidTest
97 : public data_reduction_proxy::ConcreteDataReductionProxySettingsTest
<
98 DataReductionProxyChromeSettings
> {
100 // DataReductionProxySettingsTest implementation:
101 void SetUp() override
{
102 env_
= base::android::AttachCurrentThread();
103 DataReductionProxySettingsAndroid::Register(env_
);
104 DataReductionProxySettingsTestBase::SetUp();
105 ResetSettingsAndroid();
108 void ResetSettingsAndroid() {
109 settings_android_
.reset(new TestDataReductionProxySettingsAndroid(
113 DataReductionProxySettings
* Settings() {
114 return settings_
.get();
117 DataReductionProxySettingsAndroid
* SettingsAndroid() {
118 return settings_android_
.get();
121 scoped_ptr
<DataReductionProxySettingsAndroid
> settings_android_
;
125 TEST_F(DataReductionProxySettingsAndroidTest
, TestGetDailyContentLengths
) {
126 ScopedJavaLocalRef
<jlongArray
> result
=
127 SettingsAndroid()->GetDailyContentLengths(
128 env_
, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength
);
129 ASSERT_TRUE(result
.obj());
131 jsize java_array_len
= env_
->GetArrayLength(result
.obj());
132 ASSERT_EQ(static_cast<jsize
>(data_reduction_proxy::kNumDaysInHistory
),
136 for (size_t i
= 0; i
< data_reduction_proxy::kNumDaysInHistory
; ++i
) {
137 env_
->GetLongArrayRegion(result
.obj(), i
, 1, &value
);
140 (data_reduction_proxy::kNumDaysInHistory
- 1 - i
) * 2), value
);