OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" | 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/android/scoped_java_ref.h" | 9 #include "base/android/scoped_java_ref.h" |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 13 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
13 #include "chrome/browser/prefs/proxy_prefs.h" | 14 #include "chrome/browser/prefs/proxy_prefs.h" |
14 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
16 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_
test_utils.h" | 17 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings_
test_utils.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "url/gurl.h" | 20 #include "url/gurl.h" |
20 | 21 |
21 using testing::_; | 22 using testing::_; |
22 using testing::AnyNumber; | 23 using testing::AnyNumber; |
23 using testing::Return; | 24 using testing::Return; |
24 | 25 |
25 const char kDataReductionProxyDev[] = "http://foo-dev.com:80"; | 26 const char kDataReductionProxyDev[] = "http://foo-dev.com:80"; |
26 | 27 |
| 28 using data_reduction_proxy::DataReductionProxySettings; |
| 29 |
| 30 // Used for testing the DataReductionProxySettingsAndroid class. |
| 31 class TestDataReductionProxySettingsAndroid |
| 32 : public DataReductionProxySettingsAndroid { |
| 33 public: |
| 34 // Constructs an Android settings object for test that wraps the provided |
| 35 // settings object. |
| 36 TestDataReductionProxySettingsAndroid(DataReductionProxySettings* settings) |
| 37 : DataReductionProxySettingsAndroid(), |
| 38 settings_(settings) {} |
| 39 |
| 40 // Returns the provided setting object. Used by wrapping methods. |
| 41 virtual DataReductionProxySettings* Settings() OVERRIDE { |
| 42 return settings_; |
| 43 } |
| 44 |
| 45 // The wrapped settings object. |
| 46 DataReductionProxySettings* settings_; |
| 47 }; |
| 48 |
27 template <class C> | 49 template <class C> |
28 void data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings( | 50 void data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings( |
29 bool allowed, | 51 bool allowed, |
30 bool fallback_allowed, | 52 bool fallback_allowed, |
31 bool alt_allowed, | 53 bool alt_allowed, |
32 bool promo_allowed, | 54 bool promo_allowed, |
33 bool holdback) { | 55 bool holdback) { |
34 int flags = 0; | 56 int flags = 0; |
35 if (allowed) | 57 if (allowed) |
36 flags |= DataReductionProxyParams::kAllowed; | 58 flags |= DataReductionProxyParams::kAllowed; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 settings, | 99 settings, |
78 response, | 100 response, |
79 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, | 101 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, |
80 success ? net::URLRequestStatus::SUCCESS : | 102 success ? net::URLRequestStatus::SUCCESS : |
81 net::URLRequestStatus::FAILED))); | 103 net::URLRequestStatus::FAILED))); |
82 } | 104 } |
83 } | 105 } |
84 | 106 |
85 template void | 107 template void |
86 data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings< | 108 data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings< |
87 DataReductionProxySettingsAndroid>(bool allowed, | 109 DataReductionProxyChromeSettings>(bool allowed, |
88 bool fallback_allowed, | 110 bool fallback_allowed, |
89 bool alt_allowed, | 111 bool alt_allowed, |
90 bool promo_allowed, | 112 bool promo_allowed, |
91 bool holdback); | 113 bool holdback); |
92 | 114 |
93 template void | 115 template void |
94 data_reduction_proxy::DataReductionProxySettingsTestBase::SetProbeResult< | 116 data_reduction_proxy::DataReductionProxySettingsTestBase::SetProbeResult< |
95 DataReductionProxySettingsAndroid>(const std::string& test_url, | 117 DataReductionProxyChromeSettings>(const std::string& test_url, |
96 const std::string& warmup_test_url, | 118 const std::string& warmup_test_url, |
97 const std::string& response, | 119 const std::string& response, |
98 ProbeURLFetchResult result, | 120 ProbeURLFetchResult result, |
99 bool success, | 121 bool success, |
100 int expected_calls); | 122 int expected_calls); |
101 | 123 |
102 class DataReductionProxySettingsAndroidTest | 124 class DataReductionProxySettingsAndroidTest |
103 : public data_reduction_proxy::ConcreteDataReductionProxySettingsTest< | 125 : public data_reduction_proxy::ConcreteDataReductionProxySettingsTest< |
104 DataReductionProxySettingsAndroid> { | 126 DataReductionProxyChromeSettings> { |
105 public: | 127 public: |
106 // DataReductionProxySettingsTest implementation: | 128 // DataReductionProxySettingsTest implementation: |
107 virtual void SetUp() OVERRIDE { | 129 virtual void SetUp() OVERRIDE { |
108 env_ = base::android::AttachCurrentThread(); | 130 env_ = base::android::AttachCurrentThread(); |
109 DataReductionProxySettingsAndroid::Register(env_); | 131 DataReductionProxySettingsAndroid::Register(env_); |
110 DataReductionProxySettingsTestBase::SetUp(); | 132 DataReductionProxySettingsTestBase::SetUp(); |
| 133 ResetSettingsAndroid(); |
111 } | 134 } |
112 | 135 |
113 DataReductionProxySettingsAndroid* Settings() { | 136 void ResetSettingsAndroid() { |
114 return static_cast<DataReductionProxySettingsAndroid*>(settings_.get()); | 137 settings_android_.reset(new TestDataReductionProxySettingsAndroid( |
| 138 settings_.get())); |
115 } | 139 } |
116 | 140 |
| 141 DataReductionProxySettings* Settings() { |
| 142 return settings_.get(); |
| 143 } |
| 144 |
| 145 DataReductionProxySettingsAndroid* SettingsAndroid() { |
| 146 return settings_android_.get(); |
| 147 } |
| 148 |
| 149 scoped_ptr<DataReductionProxySettingsAndroid> settings_android_; |
117 JNIEnv* env_; | 150 JNIEnv* env_; |
118 }; | 151 }; |
119 | 152 |
120 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { | 153 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { |
121 // SetUp() adds the origin to the command line, which should be returned here. | 154 // SetUp() adds the origin to the command line, which should be returned here. |
122 ScopedJavaLocalRef<jstring> result = | 155 ScopedJavaLocalRef<jstring> result = |
123 Settings()->GetDataReductionProxyOrigin(env_, NULL); | 156 SettingsAndroid()->GetDataReductionProxyOrigin(env_, NULL); |
124 ASSERT_TRUE(result.obj()); | 157 ASSERT_TRUE(result.obj()); |
125 const base::android::JavaRef<jstring>& str_ref = result; | 158 const base::android::JavaRef<jstring>& str_ref = result; |
126 EXPECT_EQ(GURL(expected_params_->DefaultOrigin()), | 159 EXPECT_EQ(GURL(expected_params_->DefaultOrigin()), |
127 GURL(ConvertJavaStringToUTF8(str_ref))); | 160 GURL(ConvertJavaStringToUTF8(str_ref))); |
128 } | 161 } |
129 | 162 |
130 TEST_F(DataReductionProxySettingsAndroidTest, | 163 TEST_F(DataReductionProxySettingsAndroidTest, |
131 TestGetDataReductionProxyDevOrigin) { | 164 TestGetDataReductionProxyDevOrigin) { |
132 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 165 CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
133 data_reduction_proxy::switches::kDataReductionProxyDev, | 166 data_reduction_proxy::switches::kDataReductionProxyDev, |
134 kDataReductionProxyDev); | 167 kDataReductionProxyDev); |
135 ResetSettings(true, true, false, true, false); | 168 ResetSettings(true, true, false, true, false); |
| 169 ResetSettingsAndroid(); |
136 ScopedJavaLocalRef<jstring> result = | 170 ScopedJavaLocalRef<jstring> result = |
137 Settings()->GetDataReductionProxyOrigin(env_, NULL); | 171 SettingsAndroid()->GetDataReductionProxyOrigin(env_, NULL); |
138 ASSERT_TRUE(result.obj()); | 172 ASSERT_TRUE(result.obj()); |
139 const base::android::JavaRef<jstring>& str_ref = result; | 173 const base::android::JavaRef<jstring>& str_ref = result; |
140 EXPECT_EQ(GURL(kDataReductionProxyDev), | 174 EXPECT_EQ(GURL(kDataReductionProxyDev), |
141 GURL(ConvertJavaStringToUTF8(str_ref))); | 175 GURL(ConvertJavaStringToUTF8(str_ref))); |
142 } | 176 } |
143 | 177 |
144 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) { | 178 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) { |
145 ScopedJavaLocalRef<jlongArray> result = Settings()->GetDailyContentLengths( | 179 ScopedJavaLocalRef<jlongArray> result = |
146 env_, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); | 180 SettingsAndroid()->GetDailyContentLengths( |
| 181 env_, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); |
147 ASSERT_TRUE(result.obj()); | 182 ASSERT_TRUE(result.obj()); |
148 | 183 |
149 jsize java_array_len = env_->GetArrayLength(result.obj()); | 184 jsize java_array_len = env_->GetArrayLength(result.obj()); |
150 ASSERT_EQ(static_cast<jsize>(data_reduction_proxy::kNumDaysInHistory), | 185 ASSERT_EQ(static_cast<jsize>(data_reduction_proxy::kNumDaysInHistory), |
151 java_array_len); | 186 java_array_len); |
152 | 187 |
153 jlong value; | 188 jlong value; |
154 for (size_t i = 0; i < data_reduction_proxy::kNumDaysInHistory; ++i) { | 189 for (size_t i = 0; i < data_reduction_proxy::kNumDaysInHistory; ++i) { |
155 env_->GetLongArrayRegion(result.obj(), i, 1, &value); | 190 env_->GetLongArrayRegion(result.obj(), i, 1, &value); |
156 ASSERT_EQ( | 191 ASSERT_EQ( |
157 static_cast<long>( | 192 static_cast<long>( |
158 (data_reduction_proxy::kNumDaysInHistory - 1 - i) * 2), | 193 (data_reduction_proxy::kNumDaysInHistory - 1 - i) * 2), value); |
159 value); | |
160 } | 194 } |
161 } | 195 } |
162 | 196 |
OLD | NEW |