Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4354)

Unified Diff: chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc

Issue 23458016: Added probe to determine if data reduction proxy can be used (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed changes Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc
diff --git a/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc b/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..638d960d95a81eb7997fb53c34b034a88694ec37
--- /dev/null
+++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc
@@ -0,0 +1,340 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/android/scoped_java_ref.h"
+#include "base/base64.h"
+#include "base/command_line.h"
+#include "base/metrics/field_trial.h"
+#include "base/prefs/pref_registry_simple.h"
+#include "base/prefs/pref_service.h"
+#include "base/prefs/testing_pref_service.h"
+#include "base/strings/string_number_conversions.h"
+#include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
+#include "chrome/browser/prefs/proxy_prefs.h"
+#include "chrome/browser/prefs/scoped_user_pref_update.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/common/metrics/variations/variations_util.h"
+#include "chrome/common/pref_names.h"
+#include "components/variations/entropy_provider.h"
+#include "net/url_request/test_url_fetcher_factory.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+
+const char kDataReductionProxyOrigin[] = "https://foo:443/";
+const char kDataReductionProxyOriginHostPort[] = "foo:443";
+const char kDataReductionProxyAuth[] = "12345";
+
+const char kProbeURLWithOKResponse[] = "http://ok.org/";
+const char kProbeURLWithBadResponse[] = "http://bad.org/";
+const char kProbeURLWithNoResponse[] = "http://no.org/";
+
+class TestDataReductionProxySettingsAndroid
+ : public DataReductionProxySettingsAndroid {
+ public:
+ TestDataReductionProxySettingsAndroid(JNIEnv* env, jobject obj)
+ : DataReductionProxySettingsAndroid(env, obj),
+ success_(false),
+ fake_fetcher_request_cnt_(0),
+ profile_prefs_for_testing_(NULL),
+ local_state_prefs_for_testing_(NULL) {
+ }
+
mmenke 2013/09/17 20:25:25 nit: "// DataReductionProxySettingsAndroid implem
bengr 2013/09/18 22:15:26 Done.
+ virtual net::URLFetcher* GetURLFetcher() OVERRIDE {
+ fake_fetcher_.reset(
+ new net::FakeURLFetcher(GURL(test_url_), this, response_, success_));
+ fake_fetcher_request_cnt_++;
+ return fake_fetcher_.get();
+ }
+
+ virtual PrefService* GetOriginalProfilePrefs() OVERRIDE {
+ return profile_prefs_for_testing_;
+ }
+ virtual PrefService* GetLocalStatePrefs() OVERRIDE {
+ return local_state_prefs_for_testing_;
+ }
+
+ void set_probe_for_testing(const std::string& test_url,
mmenke 2013/09/17 20:25:25 Maybe "set_probe_result" Since this is in a unit
bengr 2013/09/18 22:15:26 Done.
+ const std::string& response,
+ bool success) {
mmenke 2013/09/17 20:25:25 nit: Fix ident.
bengr 2013/09/18 22:15:26 Done.
+ test_url_ = test_url;
+ response_ = response;
+ success_ = success;
+ }
+
+ void set_local_state_prefs_for_testing(PrefService* pref_service) {
+ profile_prefs_for_testing_ = pref_service;
+ }
+
+ void set_profile_prefs_for_testing(PrefService* pref_service) {
+ local_state_prefs_for_testing_ = pref_service;
+ }
+
+ int fake_fetcher_request_cnt() {
mmenke 2013/09/17 20:25:25 nit: Spell out "count".
bengr 2013/09/18 22:15:26 Done.
+ return fake_fetcher_request_cnt_;
+ }
+
+ private:
+ friend class DataReductionProxySettingsAndroidTest;
mmenke 2013/09/17 20:25:25 I don't think this is needed, since the parent cla
mmenke 2013/09/17 20:25:25 optional: You could get rid of all the friends in
bengr 2013/09/18 22:15:26 Done.
bengr 2013/09/18 22:15:26 I'll leave things as is but will consider on a sub
+ std::string test_url_;
+ std::string response_;
+ bool success_;
+ int fake_fetcher_request_cnt_;
+ scoped_ptr<net::URLFetcher> fake_fetcher_;
+ PrefService* profile_prefs_for_testing_;
+ PrefService* local_state_prefs_for_testing_;
+};
+
+class DataReductionProxySettingsAndroidTest : public testing::Test {
+ protected:
+ DataReductionProxySettingsAndroidTest() {
+ ResetFieldTrialList();
+ }
+
+ void ResetFieldTrialList() {
+ // Destroy the existing FieldTrialList before creating a new one to avoid
+ // a DCHECK.
+ field_trial_list_.reset();
+ field_trial_list_.reset(new base::FieldTrialList(
+ new metrics::SHA1EntropyProvider("foo")));
+ chrome_variations::testing::ClearAllVariationParams();
+ }
+
+ // Creates and activates a field trial.
+ static base::FieldTrial* CreateTestTrial(const std::string& name,
+ const std::string& group_name) {
+ base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
+ name, group_name);
+ trial->group();
+ return trial;
+ }
+
+ // testing::Test implementation:
+ virtual void SetUp() OVERRIDE {
+ CreateTestTrial("DataCompressionProxyRollout", "Enabled");
+ CreateTestTrial("DataCompressionProxyPromoVisibility", "Enabled");
+ CommandLine::ForCurrentProcess()->AppendSwitchASCII(
+ switches::kSpdyProxyAuthOrigin, kDataReductionProxyOrigin);
+ CommandLine::ForCurrentProcess()->AppendSwitchASCII(
+ switches::kSpdyProxyAuthValue, kDataReductionProxyAuth);
+ settings_.reset(new TestDataReductionProxySettingsAndroid(NULL, NULL));
+ PrefRegistrySimple* registry = pref_service_.registry();
+ registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
+ registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
+ registry->RegisterInt64Pref(
+ prefs::kDailyHttpContentLengthLastUpdateDate, 0L);
+ registry->RegisterDictionaryPref(prefs::kProxy);
+ registry->RegisterBooleanPref(prefs::kSpdyProxyAuthEnabled, false);
+ registry->RegisterBooleanPref(prefs::kSpdyProxyAuthWasEnabledBefore, false);
+ settings_->set_local_state_prefs_for_testing(&pref_service_);
+ settings_->set_profile_prefs_for_testing(&pref_service_);
+ ListPrefUpdate original_update(&pref_service_,
+ prefs::kDailyHttpOriginalContentLength);
+ ListPrefUpdate received_update(&pref_service_,
+ prefs::kDailyHttpReceivedContentLength);
+ for (int64 i = 0; i < spdyproxy::kNumDaysInHistory; i++) {
+ original_update->Insert(0, new StringValue(base::Int64ToString(2 * i)));
+ received_update->Insert(0, new StringValue(base::Int64ToString(i)));
+ }
+ last_update_time_ =
+ base::Time::Now().LocalMidnight() -
+ base::TimeDelta::FromDays(spdyproxy::kNumDaysInHistory);
mmenke 2013/09/17 20:25:25 Any motivation for this?
bengr 2013/09/18 22:15:26 None that I recall. Done.
+ pref_service_.SetInt64(
+ prefs::kDailyHttpContentLengthLastUpdateDate,
+ last_update_time_.ToInternalValue());
+ }
+
+ void CheckProxyPrefSetToDataReductionProxy(
+ const std::string& expected_pac_url) {
+ const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
+ std::string mode;
+ std::string pac_url;
+ dict->GetString("mode", &mode);
+ EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT), mode);
+ dict->GetString("pac_url", &pac_url);
+ EXPECT_EQ(expected_pac_url, pac_url);
+ }
+ void CheckProxyPrefSetToSystem() {
+ const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy);
+ std::string mode;
+ std::string pac_url;
+ dict->GetString("mode", &mode);
+ EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_SYSTEM), mode);
+ dict->GetString("pac_url", &pac_url);
+ EXPECT_EQ(std::string(), pac_url);
+ }
+
+ void CheckProbe(bool initially_enabled, const std::string& probe_url,
+ const std::string& response, bool request_success,
+ bool expected_enabled) {
mmenke 2013/09/17 20:25:25 Fix indent.
bengr 2013/09/18 22:15:26 Done.
+ settings_->AddDefaultProxyBypassRules();
+ std::string pac;
+ base::Base64Encode(settings_->GetProxyPacScript(), &pac);
+ std::string expected_pac_url =
+ "data:application/x-ns-proxy-autoconfig;base64," + pac;
+ pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, initially_enabled);
+ settings_->set_probe_for_testing(probe_url, response, request_success);
+ settings_->MaybeActivateDataReductionProxy(false);
+ base::MessageLoop::current()->RunUntilIdle();
+ if (expected_enabled)
+ CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
+ else
+ CheckProxyPrefSetToSystem();
+ }
mmenke 2013/09/17 20:25:25 Fix indent.
bengr 2013/09/18 22:15:26 Done.
+
+ TestingPrefServiceSimple pref_service_;
+ scoped_ptr<TestDataReductionProxySettingsAndroid> settings_;
+ base::Time last_update_time_;
+ // This is a singleton that will clear all set field trials on destruction.
+ scoped_ptr<base::FieldTrialList> field_trial_list_;
+};
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) {
mmenke 2013/09/17 20:25:25 Oh, right...then should have a test that checks th
bengr 2013/09/18 22:15:26 I don't know what you mean here.
mmenke 2013/09/18 22:27:31 Sorry, field trial. Right now, you're testing the
mmenke 2013/09/18 22:30:02 Alternatively, could just get rid of the field tri
bengr 2013/09/19 20:18:15 I removed the field trial stuff. All of these test
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DataReductionProxySettingsAndroid::Register(env);
+ // SetUp() adds the origin to the command line, which should be returned here.
+ ScopedJavaLocalRef<jstring> result =
+ settings_->GetDataReductionProxyOrigin(env, NULL);
+ ASSERT_TRUE(result.obj());
+ const base::android::JavaRef<jstring>& str_ref = result;
+ EXPECT_EQ(kDataReductionProxyOrigin, ConvertJavaStringToUTF8(str_ref));
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DataReductionProxySettingsAndroid::Register(env);
+ // SetUp() adds the auth string to the command line, which should be returned
+ // here.
+ ScopedJavaLocalRef<jstring> result =
+ settings_->GetDataReductionProxyAuth(env, NULL);
+ ASSERT_TRUE(result.obj());
+ const base::android::JavaRef<jstring>& str_ref = result;
+ EXPECT_EQ(kDataReductionProxyAuth, ConvertJavaStringToUTF8(str_ref));
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestBypassRules) {
+ // Confirm that the bypass rule functions generate the intended JavaScript
+ // code for the Proxy PAC.
+ settings_->AddURLPatternToBypass("http://foo.com/*");
+ settings_->AddHostPatternToBypass("bar.com");
+ settings_->AddHostToBypass("127.0.0.1");
+
+ std::string expected[] = {
+ "shExpMatch(url, \"http://foo.com/*\")",
+ "shExpMatch(host, \"bar.com\")",
+ "host == \"127.0.0.1\""
+ };
+
+ int i = 0;
+ for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin();
+ it != settings_->bypass_rules_.end(); ++it) {
mmenke 2013/09/17 20:25:25 nit: Seems much more common to align the next lin
bengr 2013/09/18 22:15:26 Done.
+ EXPECT_EQ(expected[i++], *it);
+ }
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest,
+ TestGetDataReductionProxyOriginHostPort) {
+ // The origin is specified on the command line in SetUp().
+ EXPECT_EQ(kDataReductionProxyOriginHostPort,
+ settings_->GetDataReductionProxyOriginHostPort());
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestIsProxyEnabledOrManaged) {
+ EXPECT_FALSE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
+ EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
+
+ pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true);
+ EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
+ EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL));
+
+ pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled,
+ base::Value::CreateBooleanValue(true));
+ EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL));
+ EXPECT_TRUE(settings_->IsDataReductionProxyManaged(NULL, NULL));
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) {
+ settings_->AddDefaultProxyBypassRules();
+ std::string pac;
+ base::Base64Encode(settings_->GetProxyPacScript(), &pac);
+ std::string expected_pac_url =
+ "data:application/x-ns-proxy-autoconfig;base64," + pac;
+ // Test setting the PAC, without generating histograms.
+ settings_->has_turned_on_ = true;
+ settings_->SetProxyPac(true, false);
+ CheckProxyPrefSetToDataReductionProxy(expected_pac_url);
+
+ // Test disabling the PAC, without generating histograms.
+ settings_->has_turned_off_ = true;
+ settings_->SetProxyPac(false, false);
+ CheckProxyPrefSetToSystem();
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) {
+ JNIEnv* env = base::android::AttachCurrentThread();
+ DataReductionProxySettingsAndroid::Register(env);
+ ScopedJavaLocalRef<jlongArray> result =
+ settings_->GetDailyContentLengths(env,
+ prefs::kDailyHttpOriginalContentLength);
+ ASSERT_TRUE(result.obj());
+
+ jsize java_array_len = env->GetArrayLength(result.obj());
+ ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len);
+
+ jlong value;
+ for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
+ env->GetLongArrayRegion(result.obj(), i, 1, &value);
+ ASSERT_EQ(static_cast<long>(118 - (2 * i)), value);
+ }
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest,
+ TestResetDataReductionStatistics) {
+ int64 original_content_length;
+ int64 received_content_length;
+ int64 last_update_time;
+ settings_->ResetDataReductionStatistics();
+ settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
+ &original_content_length,
+ &received_content_length,
+ &last_update_time);
+ EXPECT_EQ(0L, original_content_length);
+ EXPECT_EQ(0L, received_content_length);
+ EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
+}
+
+
+TEST_F(DataReductionProxySettingsAndroidTest, TestContentLengthsInternal) {
+ int64 original_content_length;
+ int64 received_content_length;
+ int64 last_update_time;
+ settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory,
+ &original_content_length,
+ &received_content_length,
+ &last_update_time);
+ EXPECT_EQ(3540L, original_content_length);
+ EXPECT_EQ(1770L, received_content_length);
+ EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time);
+}
+
+TEST_F(DataReductionProxySettingsAndroidTest,
+ TestMaybeActivateDataReductionProxy) {
+ // TODO(bengr): Test enabling/disabling while a probe is outstanding.
+ base::MessageLoop loop(base::MessageLoop::TYPE_UI);
+ // The proxy is enabled initially.
+ // Request succeeded but with bad response, expect proxy to be disabled.
+ CheckProbe(true, kProbeURLWithBadResponse, "Bad", true, false);
+ // Request succeeded with valid response, expect proxy to be enabled.
+ CheckProbe(true, kProbeURLWithOKResponse, "OK", true, true);
+ // Request failed, expect proxy to be disabled.
+ CheckProbe(true, kProbeURLWithNoResponse, "", false, false);
+
+ // The proxy is disabled initially. Probes should not be emitted to change
+ // state.
+ EXPECT_EQ(3, settings_->fake_fetcher_request_cnt());
+ CheckProbe(false, kProbeURLWithOKResponse, "OK", true, false);
+ EXPECT_EQ(3, settings_->fake_fetcher_request_cnt());
+}
+

Powered by Google App Engine
This is Rietveld 408576698