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..c2fbe770ea0688475804846f641da8828abb8524 |
--- /dev/null |
+++ b/chrome/browser/net/spdyproxy/data_reduction_proxy_settings_unittest_android.cc |
@@ -0,0 +1,221 @@ |
+// 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 "testing/gtest/include/gtest/gtest.h" |
+ |
+class DataReductionProxySettingsAndroidTest : public testing::Test { |
+ protected: |
+ |
mmenke
2013/09/12 16:30:41
nit: Blank line not needed.
bengr
2013/09/17 01:06:19
Done.
|
+ 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, "https://foo:443/"); |
+ CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
+ switches::kSpdyProxyAuthValue, "12345"); |
mmenke
2013/09/12 16:30:41
Suggest making both of these strings consts char[]
bengr
2013/09/17 01:06:19
Done.
|
+ settings_.reset(new DataReductionProxySettingsAndroid(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); |
+ 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); |
+ pref_service_.SetInt64( |
+ prefs::kDailyHttpContentLengthLastUpdateDate, |
+ last_update_time_.ToInternalValue()); |
+ } |
+ |
+ void VerifyProxyPac() { |
+ } |
mmenke
2013/09/12 16:30:41
This isn't used, and doesn't do anything.
bengr
2013/09/17 01:06:19
Done.
|
+ |
+ TestingPrefServiceSimple pref_service_; |
+ scoped_ptr<DataReductionProxySettingsAndroid> settings_; |
+ base::Time last_update_time_; |
+ scoped_ptr<base::FieldTrialList> field_trial_list_; |
mmenke
2013/09/12 16:30:41
Think it's worth mentioning somewhere that this is
bengr
2013/09/17 01:06:19
Done.
|
+}; |
+ |
+TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyOrigin) { |
mmenke
2013/09/12 16:30:41
Suggest also testing setting this via the command
bengr
2013/09/17 01:06:19
I do test the via the command line. See SetUp(). I
|
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ DataReductionProxySettingsAndroid::Register(env); |
+ ScopedJavaLocalRef<jstring> result = |
+ settings_->GetDataReductionProxyOrigin(env, NULL); |
+ ASSERT_TRUE(result.obj()); |
+ const base::android::JavaRef<jstring>& str_ref = result; |
+ EXPECT_EQ("https://foo:443/", ConvertJavaStringToUTF8(str_ref)); |
+} |
+ |
+TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ DataReductionProxySettingsAndroid::Register(env); |
+ ScopedJavaLocalRef<jstring> result = |
+ settings_->GetDataReductionProxyAuth(env, NULL); |
+ ASSERT_TRUE(result.obj()); |
+ const base::android::JavaRef<jstring>& str_ref = result; |
+ EXPECT_EQ("12345", 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) { |
+ EXPECT_EQ(expected[i++], *it); |
+ } |
+} |
+ |
+TEST_F(DataReductionProxySettingsAndroidTest, |
+ TestGetDataReductionProxyOriginHostPort) { |
+ EXPECT_EQ("foo:443", settings_->GetDataReductionProxyOriginHostPort()); |
mmenke
2013/09/12 16:30:41
Again, think we want to test with a value set by t
bengr
2013/09/17 01:06:19
Configured in SetUp().
|
+} |
+ |
+TEST_F(DataReductionProxySettingsAndroidTest, |
+ TestResetDataReductionStatistics) { |
mmenke
2013/09/12 16:30:41
Think this should go just after TestContentLengths
bengr
2013/09/17 01:06:19
Done.
|
+ 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, 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); |
+ 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); |
+ |
+ // Test disabling the PAC, without generating histograms. |
+ settings_->has_turned_off_ = true; |
+ settings_->SetProxyPac(false, false); |
+ dict->GetString("mode", &mode); |
+ EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_SYSTEM), mode); |
+ dict->GetString("pac_url", &pac_url); |
+ EXPECT_EQ(std::string(), pac_url); |
+} |
+ |
+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, 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); |
+} |
mmenke
2013/09/12 16:30:41
I'd really like to see some some pretty extensive
bengr
2013/09/17 01:06:19
Done. I didn't test enabling/disabling while a pro
|