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

Side by Side Diff: components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698