OLD | NEW |
| (Empty) |
1 // Copyright 2014 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/command_line.h" | |
6 #include "base/memory/scoped_ptr.h" | |
7 #include "base/prefs/pref_registry_simple.h" | |
8 #include "base/prefs/testing_pref_service.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/test/test_simple_task_runner.h" | |
11 #include "base/time/time.h" | |
12 #include "base/values.h" | |
13 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_pref
s.h" | |
14 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_stat
istics_prefs.h" | |
15 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_
names.h" | |
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_switc
hes.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace { | |
20 | |
21 // TODO Make kNumDaysInHistory accessible from DataReductionProxySettings. | |
22 const size_t kNumDaysInHistory = 60; | |
23 const int kWriteDelayMinutes = 60; | |
24 | |
25 int64 GetListPrefInt64Value( | |
26 const base::ListValue& list_update, size_t index) { | |
27 std::string string_value; | |
28 EXPECT_TRUE(list_update.GetString(index, &string_value)); | |
29 | |
30 int64 value = 0; | |
31 EXPECT_TRUE(base::StringToInt64(string_value, &value)); | |
32 return value; | |
33 } | |
34 | |
35 } // namespace | |
36 | |
37 namespace data_reduction_proxy { | |
38 | |
39 class DataReductionProxyStatisticsPrefsTest : public testing::Test { | |
40 protected: | |
41 DataReductionProxyStatisticsPrefsTest() | |
42 : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>( | |
43 new base::TestSimpleTaskRunner())) {} | |
44 | |
45 void SetUp() override { RegisterPrefs(simple_pref_service_.registry()); } | |
46 | |
47 void SetUpPrefs() { | |
48 CreatePrefList(prefs::kDailyHttpOriginalContentLength); | |
49 CreatePrefList(prefs::kDailyHttpReceivedContentLength); | |
50 | |
51 const int64 kOriginalLength = 150; | |
52 const int64 kReceivedLength = 100; | |
53 | |
54 statistics_prefs_->SetInt64( | |
55 prefs::kHttpOriginalContentLength, kOriginalLength); | |
56 statistics_prefs_->SetInt64( | |
57 prefs::kHttpReceivedContentLength, kReceivedLength); | |
58 | |
59 base::ListValue* original_daily_content_length_list = | |
60 statistics_prefs_->GetList(prefs::kDailyHttpOriginalContentLength); | |
61 base::ListValue* received_daily_content_length_list = | |
62 statistics_prefs_->GetList(prefs::kDailyHttpReceivedContentLength); | |
63 | |
64 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | |
65 original_daily_content_length_list->Set( | |
66 i, new base::StringValue(base::Int64ToString(i))); | |
67 } | |
68 | |
69 received_daily_content_length_list->Clear(); | |
70 for (size_t i = 0; i < kNumDaysInHistory/2; ++i) { | |
71 received_daily_content_length_list->Set( | |
72 i, new base::StringValue(base::Int64ToString(i))); | |
73 } | |
74 } | |
75 | |
76 // Create daily pref list of |kNumDaysInHistory| zero values. | |
77 void CreatePrefList(const char* pref) { | |
78 base::ListValue* update = statistics_prefs_->GetList(pref); | |
79 update->Clear(); | |
80 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | |
81 update->Insert(0, new base::StringValue(base::Int64ToString(0))); | |
82 } | |
83 } | |
84 | |
85 // Verify the pref list values in |pref_service_| are equal to those in | |
86 // |simple_pref_service| for |pref|. | |
87 void VerifyPrefListWasWritten(const char* pref) { | |
88 const base::ListValue* delayed_list = statistics_prefs_->GetList(pref); | |
89 const base::ListValue* written_list = simple_pref_service_.GetList(pref); | |
90 ASSERT_EQ(delayed_list->GetSize(), written_list->GetSize()); | |
91 size_t count = delayed_list->GetSize(); | |
92 | |
93 for (size_t i = 0; i < count; ++i) { | |
94 EXPECT_EQ(GetListPrefInt64Value(*delayed_list, i), | |
95 GetListPrefInt64Value(*written_list, i)); | |
96 } | |
97 } | |
98 | |
99 // Verify the pref value in |pref_service_| are equal to that in | |
100 // |simple_pref_service|. | |
101 void VerifyPrefWasWritten(const char* pref) { | |
102 int64 delayed_pref = statistics_prefs_->GetInt64(pref); | |
103 int64 written_pref = simple_pref_service_.GetInt64(pref); | |
104 EXPECT_EQ(delayed_pref, written_pref); | |
105 } | |
106 | |
107 // Verify the pref values in |dict| are equal to that in |statistics_prefs_|. | |
108 void VerifyPrefs(base::DictionaryValue* dict) { | |
109 base::string16 dict_pref_string; | |
110 int64 dict_pref; | |
111 int64 service_pref; | |
112 | |
113 dict->GetString("historic_original_content_length", &dict_pref_string); | |
114 base::StringToInt64(dict_pref_string, &dict_pref); | |
115 service_pref = statistics_prefs_->GetInt64( | |
116 prefs::kHttpOriginalContentLength); | |
117 EXPECT_EQ(service_pref, dict_pref); | |
118 | |
119 dict->GetString("historic_received_content_length", &dict_pref_string); | |
120 base::StringToInt64(dict_pref_string, &dict_pref); | |
121 service_pref = statistics_prefs_->GetInt64( | |
122 prefs::kHttpReceivedContentLength); | |
123 EXPECT_EQ(service_pref, dict_pref); | |
124 } | |
125 | |
126 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
127 TestingPrefServiceSimple simple_pref_service_; | |
128 scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_; | |
129 }; | |
130 | |
131 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefsDirect) { | |
132 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
133 &simple_pref_service_, | |
134 task_runner_, | |
135 base::TimeDelta())); | |
136 SetUpPrefs(); | |
137 | |
138 VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); | |
139 VerifyPrefWasWritten(prefs::kHttpReceivedContentLength); | |
140 VerifyPrefListWasWritten(prefs::kDailyHttpOriginalContentLength); | |
141 VerifyPrefListWasWritten(prefs::kDailyHttpReceivedContentLength); | |
142 } | |
143 | |
144 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefsDelayed) { | |
145 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
146 &simple_pref_service_, | |
147 task_runner_, | |
148 base::TimeDelta::FromMinutes(kWriteDelayMinutes))); | |
149 SetUpPrefs(); | |
150 | |
151 task_runner_->RunPendingTasks(); | |
152 | |
153 VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); | |
154 VerifyPrefWasWritten(prefs::kHttpReceivedContentLength); | |
155 VerifyPrefListWasWritten(prefs::kDailyHttpOriginalContentLength); | |
156 VerifyPrefListWasWritten(prefs::kDailyHttpReceivedContentLength); | |
157 } | |
158 | |
159 TEST_F(DataReductionProxyStatisticsPrefsTest, | |
160 WritePrefsOnUpdateDailyReceivedContentLengths) { | |
161 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
162 &simple_pref_service_, | |
163 task_runner_, | |
164 base::TimeDelta::FromMinutes(kWriteDelayMinutes))); | |
165 SetUpPrefs(); | |
166 | |
167 simple_pref_service_.SetBoolean( | |
168 prefs::kUpdateDailyReceivedContentLengths, true); | |
169 | |
170 VerifyPrefWasWritten(prefs::kHttpOriginalContentLength); | |
171 VerifyPrefWasWritten(prefs::kHttpReceivedContentLength); | |
172 VerifyPrefListWasWritten(prefs::kDailyHttpOriginalContentLength); | |
173 VerifyPrefListWasWritten(prefs::kDailyHttpReceivedContentLength); | |
174 } | |
175 | |
176 TEST_F(DataReductionProxyStatisticsPrefsTest, | |
177 HistoricNetworkStatsInfoToValue) { | |
178 const int64 kOriginalLength = 150; | |
179 const int64 kReceivedLength = 100; | |
180 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
181 &simple_pref_service_, | |
182 task_runner_, | |
183 base::TimeDelta::FromMinutes(kWriteDelayMinutes))); | |
184 | |
185 base::DictionaryValue* dict = nullptr; | |
186 scoped_ptr<base::Value> stats_value( | |
187 statistics_prefs_->HistoricNetworkStatsInfoToValue()); | |
188 EXPECT_TRUE(stats_value->GetAsDictionary(&dict)); | |
189 VerifyPrefs(dict); | |
190 | |
191 statistics_prefs_->SetInt64(prefs::kHttpOriginalContentLength, | |
192 kOriginalLength); | |
193 statistics_prefs_->SetInt64(prefs::kHttpReceivedContentLength, | |
194 kReceivedLength); | |
195 | |
196 stats_value.reset(statistics_prefs_->HistoricNetworkStatsInfoToValue()); | |
197 EXPECT_TRUE(stats_value->GetAsDictionary(&dict)); | |
198 VerifyPrefs(dict); | |
199 } | |
200 | |
201 TEST_F(DataReductionProxyStatisticsPrefsTest, | |
202 HistoricNetworkStatsInfoToValueDirect) { | |
203 const int64 kOriginalLength = 150; | |
204 const int64 kReceivedLength = 100; | |
205 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
206 &simple_pref_service_, | |
207 task_runner_, | |
208 base::TimeDelta())); | |
209 | |
210 base::DictionaryValue* dict = nullptr; | |
211 scoped_ptr<base::Value> stats_value( | |
212 statistics_prefs_->HistoricNetworkStatsInfoToValue()); | |
213 EXPECT_TRUE(stats_value->GetAsDictionary(&dict)); | |
214 VerifyPrefs(dict); | |
215 | |
216 statistics_prefs_->SetInt64(prefs::kHttpOriginalContentLength, | |
217 kOriginalLength); | |
218 statistics_prefs_->SetInt64(prefs::kHttpReceivedContentLength, | |
219 kReceivedLength); | |
220 | |
221 stats_value.reset(statistics_prefs_->HistoricNetworkStatsInfoToValue()); | |
222 EXPECT_TRUE(stats_value->GetAsDictionary(&dict)); | |
223 VerifyPrefs(dict); | |
224 } | |
225 | |
226 TEST_F(DataReductionProxyStatisticsPrefsTest, | |
227 ClearPrefsOnRestartEnabled) { | |
228 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
229 command_line->AppendSwitch( | |
230 data_reduction_proxy::switches::kClearDataReductionProxyDataSavings); | |
231 | |
232 base::ListValue list_value; | |
233 list_value.Insert(0, new base::StringValue(base::Int64ToString(1234))); | |
234 simple_pref_service_.Set(prefs::kDailyHttpOriginalContentLength, list_value); | |
235 | |
236 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
237 &simple_pref_service_, | |
238 task_runner_, | |
239 base::TimeDelta::FromMinutes(kWriteDelayMinutes))); | |
240 | |
241 const base::ListValue* value = simple_pref_service_.GetList( | |
242 prefs::kDailyHttpOriginalContentLength); | |
243 EXPECT_EQ(0u, value->GetSize()); | |
244 } | |
245 | |
246 TEST_F(DataReductionProxyStatisticsPrefsTest, | |
247 ClearPrefsOnRestartDisabled) { | |
248 base::ListValue list_value; | |
249 list_value.Insert(0, new base::StringValue(base::Int64ToString(1234))); | |
250 simple_pref_service_.Set(prefs::kDailyHttpOriginalContentLength, list_value); | |
251 | |
252 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs( | |
253 &simple_pref_service_, | |
254 task_runner_, | |
255 base::TimeDelta::FromMinutes(kWriteDelayMinutes))); | |
256 | |
257 const base::ListValue* value = simple_pref_service_.GetList( | |
258 prefs::kDailyHttpOriginalContentLength); | |
259 std::string string_value; | |
260 value->GetString(0, &string_value); | |
261 EXPECT_EQ("1234", string_value); | |
262 } | |
263 | |
264 } // namespace data_reduction_proxy | |
OLD | NEW |