| 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/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" | 17 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h" |
| 18 #include "chrome/browser/prefs/proxy_prefs.h" | 18 #include "chrome/browser/prefs/proxy_prefs.h" |
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_conf
ig_test_utils.h" |
| 21 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings_test_utils.h" | 22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_sett
ings_test_utils.h" |
| 22 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_stat
istics_prefs.h" | 23 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test
_utils.h" |
| 24 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s.h" |
| 25 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_param
s_test_utils.h" |
| 23 #include "net/proxy/proxy_server.h" | 26 #include "net/proxy/proxy_server.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 27 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 27 | 30 |
| 28 using testing::_; | 31 using testing::_; |
| 29 using testing::AnyNumber; | 32 using testing::AnyNumber; |
| 30 using testing::Return; | 33 using testing::Return; |
| 31 | 34 |
| 32 const char kDataReductionProxyDev[] = "foo-dev.com:80"; | 35 const char kDataReductionProxyDev[] = "foo-dev.com:80"; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 63 flags |= DataReductionProxyParams::kAllowed; | 66 flags |= DataReductionProxyParams::kAllowed; |
| 64 if (fallback_allowed) | 67 if (fallback_allowed) |
| 65 flags |= DataReductionProxyParams::kFallbackAllowed; | 68 flags |= DataReductionProxyParams::kFallbackAllowed; |
| 66 if (alt_allowed) | 69 if (alt_allowed) |
| 67 flags |= DataReductionProxyParams::kAlternativeAllowed; | 70 flags |= DataReductionProxyParams::kAlternativeAllowed; |
| 68 if (promo_allowed) | 71 if (promo_allowed) |
| 69 flags |= DataReductionProxyParams::kPromoAllowed; | 72 flags |= DataReductionProxyParams::kPromoAllowed; |
| 70 if (holdback) | 73 if (holdback) |
| 71 flags |= DataReductionProxyParams::kHoldback; | 74 flags |= DataReductionProxyParams::kHoldback; |
| 72 MockDataReductionProxySettings<C>* settings = | 75 MockDataReductionProxySettings<C>* settings = |
| 73 new MockDataReductionProxySettings<C>(flags); | 76 new MockDataReductionProxySettings<C>(); |
| 77 settings->config_ = test_context_->config(); |
| 78 settings->data_reduction_proxy_service_ = |
| 79 test_context_->CreateDataReductionProxyService(); |
| 80 test_context_->config()->ResetParamFlagsForTest(flags); |
| 81 settings->UpdateConfigValues(); |
| 74 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) | 82 EXPECT_CALL(*settings, GetOriginalProfilePrefs()) |
| 75 .Times(AnyNumber()) | 83 .Times(AnyNumber()) |
| 76 .WillRepeatedly(Return(&pref_service_)); | 84 .WillRepeatedly(Return(test_context_->pref_service())); |
| 77 EXPECT_CALL(*settings, GetLocalStatePrefs()) | 85 EXPECT_CALL(*settings, GetLocalStatePrefs()) |
| 78 .Times(AnyNumber()) | 86 .Times(AnyNumber()) |
| 79 .WillRepeatedly(Return(&pref_service_)); | 87 .WillRepeatedly(Return(test_context_->pref_service())); |
| 80 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0); | |
| 81 EXPECT_CALL(*settings, LogProxyState(_, _, _)).Times(0); | |
| 82 settings_.reset(settings); | 88 settings_.reset(settings); |
| 83 settings_->SetDataReductionProxyStatisticsPrefs( | |
| 84 scoped_ptr<DataReductionProxyStatisticsPrefs>( | |
| 85 new DataReductionProxyStatisticsPrefs( | |
| 86 &pref_service_, | |
| 87 scoped_refptr<base::TestSimpleTaskRunner>( | |
| 88 new base::TestSimpleTaskRunner()), | |
| 89 base::TimeDelta()))); | |
| 90 } | |
| 91 | |
| 92 template <class C> | |
| 93 void data_reduction_proxy::DataReductionProxySettingsTestBase::SetProbeResult( | |
| 94 const std::string& test_url, | |
| 95 const std::string& response, | |
| 96 ProbeURLFetchResult result, | |
| 97 bool success, | |
| 98 int expected_calls) { | |
| 99 MockDataReductionProxySettings<C>* settings = | |
| 100 static_cast<MockDataReductionProxySettings<C>*>(settings_.get()); | |
| 101 if (0 == expected_calls) { | |
| 102 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()).Times(0); | |
| 103 EXPECT_CALL(*settings, RecordProbeURLFetchResult(_)).Times(0); | |
| 104 } else { | |
| 105 EXPECT_CALL(*settings, RecordProbeURLFetchResult(result)).Times(1); | |
| 106 EXPECT_CALL(*settings, GetURLFetcherForAvailabilityCheck()) | |
| 107 .Times(expected_calls) | |
| 108 .WillRepeatedly(Return(new net::FakeURLFetcher( | |
| 109 GURL(test_url), | |
| 110 settings, | |
| 111 response, | |
| 112 success ? net::HTTP_OK : net::HTTP_INTERNAL_SERVER_ERROR, | |
| 113 success ? net::URLRequestStatus::SUCCESS : | |
| 114 net::URLRequestStatus::FAILED))); | |
| 115 } | |
| 116 } | 89 } |
| 117 | 90 |
| 118 template void | 91 template void |
| 119 data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings< | 92 data_reduction_proxy::DataReductionProxySettingsTestBase::ResetSettings< |
| 120 DataReductionProxyChromeSettings>(bool allowed, | 93 DataReductionProxyChromeSettings>(bool allowed, |
| 121 bool fallback_allowed, | 94 bool fallback_allowed, |
| 122 bool alt_allowed, | 95 bool alt_allowed, |
| 123 bool promo_allowed, | 96 bool promo_allowed, |
| 124 bool holdback); | 97 bool holdback); |
| 125 | 98 |
| 126 template void | |
| 127 data_reduction_proxy::DataReductionProxySettingsTestBase::SetProbeResult< | |
| 128 DataReductionProxyChromeSettings>(const std::string& test_url, | |
| 129 const std::string& response, | |
| 130 ProbeURLFetchResult result, | |
| 131 bool success, | |
| 132 int expected_calls); | |
| 133 | |
| 134 class DataReductionProxySettingsAndroidTest | 99 class DataReductionProxySettingsAndroidTest |
| 135 : public data_reduction_proxy::ConcreteDataReductionProxySettingsTest< | 100 : public data_reduction_proxy::ConcreteDataReductionProxySettingsTest< |
| 136 DataReductionProxyChromeSettings> { | 101 DataReductionProxyChromeSettings> { |
| 137 public: | 102 public: |
| 138 // DataReductionProxySettingsTest implementation: | 103 // DataReductionProxySettingsTest implementation: |
| 139 void SetUp() override { | 104 void SetUp() override { |
| 140 env_ = base::android::AttachCurrentThread(); | 105 env_ = base::android::AttachCurrentThread(); |
| 141 DataReductionProxySettingsAndroid::Register(env_); | 106 DataReductionProxySettingsAndroid::Register(env_); |
| 142 DataReductionProxySettingsTestBase::SetUp(); | 107 DataReductionProxySettingsTestBase::SetUp(); |
| 143 ResetSettingsAndroid(); | 108 ResetSettingsAndroid(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 159 scoped_ptr<DataReductionProxySettingsAndroid> settings_android_; | 124 scoped_ptr<DataReductionProxySettingsAndroid> settings_android_; |
| 160 JNIEnv* env_; | 125 JNIEnv* env_; |
| 161 }; | 126 }; |
| 162 | 127 |
| 163 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { | 128 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { |
| 164 // SetUp() adds the origin to the command line, which should be returned here. | 129 // SetUp() adds the origin to the command line, which should be returned here. |
| 165 ScopedJavaLocalRef<jstring> result = | 130 ScopedJavaLocalRef<jstring> result = |
| 166 SettingsAndroid()->GetDataReductionProxyOrigin(env_, NULL); | 131 SettingsAndroid()->GetDataReductionProxyOrigin(env_, NULL); |
| 167 ASSERT_TRUE(result.obj()); | 132 ASSERT_TRUE(result.obj()); |
| 168 const base::android::JavaRef<jstring>& str_ref = result; | 133 const base::android::JavaRef<jstring>& str_ref = result; |
| 169 EXPECT_EQ(expected_params_->DefaultOrigin(), | 134 EXPECT_EQ(test_context_->config()->test_params()->DefaultOrigin(), |
| 170 ConvertJavaStringToUTF8(str_ref)); | 135 ConvertJavaStringToUTF8(str_ref)); |
| 171 } | 136 } |
| 172 | 137 |
| 173 TEST_F(DataReductionProxySettingsAndroidTest, | 138 TEST_F(DataReductionProxySettingsAndroidTest, |
| 174 TestGetDataReductionProxyDevOrigin) { | 139 TestGetDataReductionProxyDevOrigin) { |
| 175 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 140 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 176 data_reduction_proxy::switches::kDataReductionProxyDev, | 141 data_reduction_proxy::switches::kDataReductionProxyDev, |
| 177 kDataReductionProxyDev); | 142 kDataReductionProxyDev); |
| 178 ResetSettings(true, true, false, true, false); | 143 ResetSettings(true, true, false, true, false); |
| 179 ResetSettingsAndroid(); | 144 ResetSettingsAndroid(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 197 | 162 |
| 198 jlong value; | 163 jlong value; |
| 199 for (size_t i = 0; i < data_reduction_proxy::kNumDaysInHistory; ++i) { | 164 for (size_t i = 0; i < data_reduction_proxy::kNumDaysInHistory; ++i) { |
| 200 env_->GetLongArrayRegion(result.obj(), i, 1, &value); | 165 env_->GetLongArrayRegion(result.obj(), i, 1, &value); |
| 201 ASSERT_EQ( | 166 ASSERT_EQ( |
| 202 static_cast<long>( | 167 static_cast<long>( |
| 203 (data_reduction_proxy::kNumDaysInHistory - 1 - i) * 2), value); | 168 (data_reduction_proxy::kNumDaysInHistory - 1 - i) * 2), value); |
| 204 } | 169 } |
| 205 } | 170 } |
| 206 | 171 |
| OLD | NEW |