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/command_line.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/prefs/proxy_prefs.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_test_utils.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
22 using testing::AnyNumber
;
23 using testing::Return
;
25 const char kDataReductionProxyOrigin
[] = "https://foo.com:443/";
26 const char kDataReductionProxyDev
[] = "http://foo-dev.com:80";
29 void data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings() {
30 MockDataReductionProxySettings
<C
>* settings
=
31 new MockDataReductionProxySettings
<C
>;
32 EXPECT_CALL(*settings
, GetOriginalProfilePrefs())
34 .WillRepeatedly(Return(&pref_service_
));
35 EXPECT_CALL(*settings
, GetLocalStatePrefs())
37 .WillRepeatedly(Return(&pref_service_
));
38 EXPECT_CALL(*settings
, GetURLFetcher()).Times(0);
39 EXPECT_CALL(*settings
, LogProxyState(_
, _
, _
)).Times(0);
40 settings_
.reset(settings
);
44 void data_reduction_proxy::DataReductionProxySettingsTestBase::SetProbeResult(
45 const std::string
& test_url
,
46 const std::string
& response
,
47 ProbeURLFetchResult result
,
50 MockDataReductionProxySettings
<C
>* settings
=
51 static_cast<MockDataReductionProxySettings
<C
>*>(settings_
.get());
52 if (0 == expected_calls
) {
53 EXPECT_CALL(*settings
, GetURLFetcher()).Times(0);
54 EXPECT_CALL(*settings
, RecordProbeURLFetchResult(_
)).Times(0);
56 EXPECT_CALL(*settings
, RecordProbeURLFetchResult(result
)).Times(1);
57 EXPECT_CALL(*settings
, GetURLFetcher())
58 .Times(expected_calls
)
59 .WillRepeatedly(Return(new net::FakeURLFetcher(
63 success
? net::HTTP_OK
: net::HTTP_INTERNAL_SERVER_ERROR
,
64 success
? net::URLRequestStatus::SUCCESS
:
65 net::URLRequestStatus::FAILED
)));
70 data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings
<
71 DataReductionProxySettingsAndroid
>();
74 data_reduction_proxy::DataReductionProxySettingsTestBase::SetProbeResult
<
75 DataReductionProxySettingsAndroid
>(const std::string
& test_url
,
76 const std::string
& response
,
77 ProbeURLFetchResult result
,
81 class DataReductionProxySettingsAndroidTest
82 : public data_reduction_proxy::ConcreteDataReductionProxySettingsTest
<
83 DataReductionProxySettingsAndroid
> {
85 // DataReductionProxySettingsTest implementation:
86 virtual void SetUp() OVERRIDE
{
87 env_
= base::android::AttachCurrentThread();
88 DataReductionProxySettingsAndroid::Register(env_
);
89 DataReductionProxySettingsTestBase::SetUp();
92 DataReductionProxySettingsAndroid
* Settings() {
93 return static_cast<DataReductionProxySettingsAndroid
*>(settings_
.get());
99 TEST_F(DataReductionProxySettingsAndroidTest
, TestGetDataReductionProxyOrigin
) {
100 AddProxyToCommandLine();
101 // SetUp() adds the origin to the command line, which should be returned here.
102 ScopedJavaLocalRef
<jstring
> result
=
103 Settings()->GetDataReductionProxyOrigin(env_
, NULL
);
104 ASSERT_TRUE(result
.obj());
105 const base::android::JavaRef
<jstring
>& str_ref
= result
;
106 EXPECT_EQ(kDataReductionProxyOrigin
, ConvertJavaStringToUTF8(str_ref
));
109 TEST_F(DataReductionProxySettingsAndroidTest
,
110 TestGetDataReductionProxyDevOrigin
) {
111 AddProxyToCommandLine();
112 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
113 data_reduction_proxy::switches::kDataReductionProxyDev
,
114 kDataReductionProxyDev
);
115 ScopedJavaLocalRef
<jstring
> result
=
116 Settings()->GetDataReductionProxyOrigin(env_
, NULL
);
117 ASSERT_TRUE(result
.obj());
118 const base::android::JavaRef
<jstring
>& str_ref
= result
;
119 EXPECT_EQ(kDataReductionProxyDev
, ConvertJavaStringToUTF8(str_ref
));
122 TEST_F(DataReductionProxySettingsAndroidTest
, TestGetDailyContentLengths
) {
123 ScopedJavaLocalRef
<jlongArray
> result
= Settings()->GetDailyContentLengths(
124 env_
, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength
);
125 ASSERT_TRUE(result
.obj());
127 jsize java_array_len
= env_
->GetArrayLength(result
.obj());
128 ASSERT_EQ(static_cast<jsize
>(data_reduction_proxy::kNumDaysInHistory
),
132 for (size_t i
= 0; i
< data_reduction_proxy::kNumDaysInHistory
; ++i
) {
133 env_
->GetLongArrayRegion(result
.obj(), i
, 1, &value
);
136 (data_reduction_proxy::kNumDaysInHistory
- 1 - i
) * 2),