OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "base/android/jni_android.h" | |
6 #include "base/android/jni_string.h" | |
7 #include "base/android/scoped_java_ref.h" | |
8 #include "base/base64.h" | |
9 #include "base/command_line.h" | |
10 #include "base/metrics/field_trial.h" | |
11 #include "base/prefs/pref_registry_simple.h" | |
12 #include "base/prefs/pref_service.h" | |
13 #include "base/prefs/testing_pref_service.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" | |
16 #include "chrome/browser/prefs/proxy_prefs.h" | |
17 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
18 #include "chrome/common/chrome_switches.h" | |
19 #include "chrome/common/metrics/variations/variations_util.h" | |
20 #include "chrome/common/pref_names.h" | |
21 #include "components/variations/entropy_provider.h" | |
22 #include "testing/gtest/include/gtest/gtest.h" | |
23 | |
24 class DataReductionProxySettingsAndroidTest : public testing::Test { | |
25 protected: | |
26 | |
mmenke
2013/09/12 16:30:41
nit: Blank line not needed.
bengr
2013/09/17 01:06:19
Done.
| |
27 DataReductionProxySettingsAndroidTest() { | |
28 ResetFieldTrialList(); | |
29 } | |
30 | |
31 void ResetFieldTrialList() { | |
32 // Destroy the existing FieldTrialList before creating a new one to avoid | |
33 // a DCHECK. | |
34 field_trial_list_.reset(); | |
35 field_trial_list_.reset(new base::FieldTrialList( | |
36 new metrics::SHA1EntropyProvider("foo"))); | |
37 chrome_variations::testing::ClearAllVariationParams(); | |
38 } | |
39 | |
40 // Creates and activates a field trial. | |
41 static base::FieldTrial* CreateTestTrial(const std::string& name, | |
42 const std::string& group_name) { | |
43 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( | |
44 name, group_name); | |
45 trial->group(); | |
46 return trial; | |
47 } | |
48 | |
49 // testing::Test implementation: | |
50 virtual void SetUp() OVERRIDE { | |
51 CreateTestTrial("DataCompressionProxyRollout", "Enabled"); | |
52 CreateTestTrial("DataCompressionProxyPromoVisibility", "Enabled"); | |
53 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
54 switches::kSpdyProxyAuthOrigin, "https://foo:443/"); | |
55 CommandLine::ForCurrentProcess()->AppendSwitchASCII( | |
56 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.
| |
57 settings_.reset(new DataReductionProxySettingsAndroid(NULL, NULL)); | |
58 PrefRegistrySimple* registry = pref_service_.registry(); | |
59 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength); | |
60 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength); | |
61 registry->RegisterInt64Pref( | |
62 prefs::kDailyHttpContentLengthLastUpdateDate, 0L); | |
63 registry->RegisterDictionaryPref(prefs::kProxy); | |
64 registry->RegisterBooleanPref(prefs::kSpdyProxyAuthEnabled, false); | |
65 settings_->set_local_state_prefs_for_testing(&pref_service_); | |
66 settings_->set_profile_prefs_for_testing(&pref_service_); | |
67 ListPrefUpdate original_update(&pref_service_, | |
68 prefs::kDailyHttpOriginalContentLength); | |
69 ListPrefUpdate received_update(&pref_service_, | |
70 prefs::kDailyHttpReceivedContentLength); | |
71 for (int64 i = 0; i < spdyproxy::kNumDaysInHistory; i++) { | |
72 original_update->Insert(0, new StringValue(base::Int64ToString(2 * i))); | |
73 received_update->Insert(0, new StringValue(base::Int64ToString(i))); | |
74 } | |
75 last_update_time_ = | |
76 base::Time::Now().LocalMidnight() - | |
77 base::TimeDelta::FromDays(spdyproxy::kNumDaysInHistory); | |
78 pref_service_.SetInt64( | |
79 prefs::kDailyHttpContentLengthLastUpdateDate, | |
80 last_update_time_.ToInternalValue()); | |
81 } | |
82 | |
83 void VerifyProxyPac() { | |
84 } | |
mmenke
2013/09/12 16:30:41
This isn't used, and doesn't do anything.
bengr
2013/09/17 01:06:19
Done.
| |
85 | |
86 TestingPrefServiceSimple pref_service_; | |
87 scoped_ptr<DataReductionProxySettingsAndroid> settings_; | |
88 base::Time last_update_time_; | |
89 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.
| |
90 }; | |
91 | |
92 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
| |
93 JNIEnv* env = base::android::AttachCurrentThread(); | |
94 DataReductionProxySettingsAndroid::Register(env); | |
95 ScopedJavaLocalRef<jstring> result = | |
96 settings_->GetDataReductionProxyOrigin(env, NULL); | |
97 ASSERT_TRUE(result.obj()); | |
98 const base::android::JavaRef<jstring>& str_ref = result; | |
99 EXPECT_EQ("https://foo:443/", ConvertJavaStringToUTF8(str_ref)); | |
100 } | |
101 | |
102 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDataReductionProxyAuth) { | |
103 JNIEnv* env = base::android::AttachCurrentThread(); | |
104 DataReductionProxySettingsAndroid::Register(env); | |
105 ScopedJavaLocalRef<jstring> result = | |
106 settings_->GetDataReductionProxyAuth(env, NULL); | |
107 ASSERT_TRUE(result.obj()); | |
108 const base::android::JavaRef<jstring>& str_ref = result; | |
109 EXPECT_EQ("12345", ConvertJavaStringToUTF8(str_ref)); | |
110 } | |
111 | |
112 TEST_F(DataReductionProxySettingsAndroidTest, TestBypassRules) { | |
113 // Confirm that the bypass rule functions generate the intended JavaScript | |
114 // code for the Proxy PAC. | |
115 settings_->AddURLPatternToBypass("http://foo.com/*"); | |
116 settings_->AddHostPatternToBypass("bar.com"); | |
117 settings_->AddHostToBypass("127.0.0.1"); | |
118 | |
119 std::string expected[] = { | |
120 "shExpMatch(url, \"http://foo.com/*\")", | |
121 "shExpMatch(host, \"bar.com\")", | |
122 "host == \"127.0.0.1\"" | |
123 }; | |
124 | |
125 int i = 0; | |
126 for (std::vector<std::string>::iterator it = settings_->bypass_rules_.begin(); | |
127 it != settings_->bypass_rules_.end(); ++it) { | |
128 EXPECT_EQ(expected[i++], *it); | |
129 } | |
130 } | |
131 | |
132 TEST_F(DataReductionProxySettingsAndroidTest, | |
133 TestGetDataReductionProxyOriginHostPort) { | |
134 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().
| |
135 } | |
136 | |
137 TEST_F(DataReductionProxySettingsAndroidTest, | |
138 TestResetDataReductionStatistics) { | |
mmenke
2013/09/12 16:30:41
Think this should go just after TestContentLengths
bengr
2013/09/17 01:06:19
Done.
| |
139 int64 original_content_length; | |
140 int64 received_content_length; | |
141 int64 last_update_time; | |
142 settings_->ResetDataReductionStatistics(); | |
143 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory, | |
144 &original_content_length, | |
145 &received_content_length, | |
146 &last_update_time); | |
147 EXPECT_EQ(0L, original_content_length); | |
148 EXPECT_EQ(0L, received_content_length); | |
149 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time); | |
150 } | |
151 | |
152 TEST_F(DataReductionProxySettingsAndroidTest, TestIsProxyEnabledOrManaged) { | |
153 EXPECT_FALSE(settings_->IsDataReductionProxyEnabled(NULL, NULL)); | |
154 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL)); | |
155 | |
156 pref_service_.SetBoolean(prefs::kSpdyProxyAuthEnabled, true); | |
157 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL)); | |
158 EXPECT_FALSE(settings_->IsDataReductionProxyManaged(NULL, NULL)); | |
159 | |
160 pref_service_.SetManagedPref(prefs::kSpdyProxyAuthEnabled, | |
161 base::Value::CreateBooleanValue(true)); | |
162 EXPECT_TRUE(settings_->IsDataReductionProxyEnabled(NULL, NULL)); | |
163 EXPECT_TRUE(settings_->IsDataReductionProxyManaged(NULL, NULL)); | |
164 } | |
165 | |
166 TEST_F(DataReductionProxySettingsAndroidTest, TestSetProxyPac) { | |
167 settings_->AddDefaultProxyBypassRules(); | |
168 std::string pac; | |
169 base::Base64Encode(settings_->GetProxyPacScript(), &pac); | |
170 std::string expected_pac_url = | |
171 "data:application/x-ns-proxy-autoconfig;base64," + pac; | |
172 // Test setting the PAC, without generating histograms. | |
173 settings_->has_turned_on_ = true; | |
174 settings_->SetProxyPac(true, false); | |
175 const DictionaryValue* dict = pref_service_.GetDictionary(prefs::kProxy); | |
176 std::string mode; | |
177 std::string pac_url; | |
178 dict->GetString("mode", &mode); | |
179 EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT), mode); | |
180 dict->GetString("pac_url", &pac_url); | |
181 EXPECT_EQ(expected_pac_url, pac_url); | |
182 | |
183 // Test disabling the PAC, without generating histograms. | |
184 settings_->has_turned_off_ = true; | |
185 settings_->SetProxyPac(false, false); | |
186 dict->GetString("mode", &mode); | |
187 EXPECT_EQ(ProxyModeToString(ProxyPrefs::MODE_SYSTEM), mode); | |
188 dict->GetString("pac_url", &pac_url); | |
189 EXPECT_EQ(std::string(), pac_url); | |
190 } | |
191 | |
192 TEST_F(DataReductionProxySettingsAndroidTest, TestGetDailyContentLengths) { | |
193 JNIEnv* env = base::android::AttachCurrentThread(); | |
194 DataReductionProxySettingsAndroid::Register(env); | |
195 ScopedJavaLocalRef<jlongArray> result = | |
196 settings_->GetDailyContentLengths(env, | |
197 prefs::kDailyHttpOriginalContentLength); | |
198 ASSERT_TRUE(result.obj()); | |
199 | |
200 jsize java_array_len = env->GetArrayLength(result.obj()); | |
201 ASSERT_EQ(static_cast<jsize>(spdyproxy::kNumDaysInHistory), java_array_len); | |
202 | |
203 jlong value; | |
204 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
205 env->GetLongArrayRegion(result.obj(), i, 1, &value); | |
206 ASSERT_EQ(static_cast<long>(118 - (2 * i)), value); | |
207 } | |
208 } | |
209 | |
210 TEST_F(DataReductionProxySettingsAndroidTest, TestContentLengthsInternal) { | |
211 int64 original_content_length; | |
212 int64 received_content_length; | |
213 int64 last_update_time; | |
214 settings_->GetContentLengthsInternal(spdyproxy::kNumDaysInHistory, | |
215 &original_content_length, | |
216 &received_content_length, | |
217 &last_update_time); | |
218 EXPECT_EQ(3540L, original_content_length); | |
219 EXPECT_EQ(1770L, received_content_length); | |
220 EXPECT_EQ(last_update_time_.ToInternalValue(), last_update_time); | |
221 } | |
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
| |
OLD | NEW |