Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / spdyproxy / data_saving_metrics_unittest.cc
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/basictypes.h"
6 #include "base/compiler_specific.h"
7 #include "base/prefs/pref_registry_simple.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/prefs/scoped_user_pref_update.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "chrome/browser/net/spdyproxy/data_saving_metrics.h"
13 #include "chrome/common/pref_names.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace {
17
18 #if defined(OS_ANDROID) || defined(OS_IOS)
19 const size_t kNumDaysInHistory = 60;
20
21 int64 GetListPrefInt64Value(
22     const base::ListValue& list_update, size_t index) {
23   std::string string_value;
24   EXPECT_TRUE(list_update.GetString(index, &string_value));
25
26   int64 value = 0;
27   EXPECT_TRUE(base::StringToInt64(string_value, &value));
28   return value;
29 }
30
31 #endif  // defined(OS_ANDROID) || defined(OS_IOS)
32
33 // Test UpdateContentLengthPrefs.
34 class ChromeNetworkDataSavingMetricsTest : public testing::Test {
35  protected:
36   ChromeNetworkDataSavingMetricsTest() {}
37
38   virtual void SetUp() OVERRIDE {
39     PrefRegistrySimple* registry = pref_service_.registry();
40     registry->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0);
41     registry->RegisterInt64Pref(prefs::kHttpOriginalContentLength, 0);
42
43 #if defined(OS_ANDROID) || defined(OS_IOS)
44     registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
45     registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
46     registry->RegisterListPref(
47         prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled);
48     registry->RegisterListPref(
49         prefs::kDailyContentLengthWithDataReductionProxyEnabled);
50     registry->RegisterListPref(
51         prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled);
52     registry->RegisterListPref(
53         prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled);
54     registry->RegisterListPref(
55         prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled);
56     registry->RegisterListPref(
57         prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled);
58     registry->RegisterListPref(
59         prefs::kDailyOriginalContentLengthViaDataReductionProxy);
60     registry->RegisterListPref(
61         prefs::kDailyContentLengthViaDataReductionProxy);
62     registry->RegisterInt64Pref(
63         prefs::kDailyHttpContentLengthLastUpdateDate, 0L);
64 #endif  // defined(OS_ANDROID) || defined(OS_IOS)
65   }
66
67   TestingPrefServiceSimple pref_service_;
68 };
69
70 TEST_F(ChromeNetworkDataSavingMetricsTest, TotalLengths) {
71   const int64 kOriginalLength = 200;
72   const int64 kReceivedLength = 100;
73
74   spdyproxy::UpdateContentLengthPrefs(
75       kReceivedLength, kOriginalLength,
76       false, spdyproxy::UNKNOWN_TYPE, &pref_service_);
77   EXPECT_EQ(kReceivedLength,
78             pref_service_.GetInt64(prefs::kHttpReceivedContentLength));
79   EXPECT_EQ(kOriginalLength,
80             pref_service_.GetInt64(prefs::kHttpOriginalContentLength));
81
82   // Record the same numbers again, and total lengths should be dobuled.
83   spdyproxy::UpdateContentLengthPrefs(
84       kReceivedLength, kOriginalLength,
85       false, spdyproxy::UNKNOWN_TYPE, &pref_service_);
86   EXPECT_EQ(kReceivedLength * 2,
87             pref_service_.GetInt64(prefs::kHttpReceivedContentLength));
88   EXPECT_EQ(kOriginalLength * 2,
89             pref_service_.GetInt64(prefs::kHttpOriginalContentLength));
90 }
91
92 #if defined(OS_ANDROID) || defined(OS_IOS)
93
94 // The initial last update time used in test. There is no leap second a few
95 // days around this time used in the test.
96 // Note: No time zone is specified. Local time will be assumed by
97 // base::Time::FromString below.
98 const char kLastUpdateTime[] = "Wed, 18 Sep 2013 03:45:26";
99
100 class ChromeNetworkDailyDataSavingMetricsTest
101     : public ChromeNetworkDataSavingMetricsTest {
102  protected:
103   ChromeNetworkDailyDataSavingMetricsTest() {
104     base::Time::FromString(kLastUpdateTime, &now_);
105   }
106
107   virtual void SetUp() OVERRIDE {
108     ChromeNetworkDataSavingMetricsTest::SetUp();
109
110     // Only create two lists in Setup to test that adding new lists is fine.
111     CreatePrefList(prefs::kDailyHttpOriginalContentLength);
112     CreatePrefList(prefs::kDailyHttpReceivedContentLength);
113   }
114
115   base::Time FakeNow() const {
116     return now_ + now_delta_;
117   }
118
119   void SetFakeTimeDeltaInHours(int hours) {
120     now_delta_ = base::TimeDelta::FromHours(hours);
121   }
122
123   void AddFakeTimeDeltaInHours(int hours) {
124     now_delta_ += base::TimeDelta::FromHours(hours);
125   }
126
127   // Create daily pref list of |kNumDaysInHistory| zero values.
128   void CreatePrefList(const char* pref) {
129     ListPrefUpdate update(&pref_service_, pref);
130     update->Clear();
131     for (size_t i = 0; i < kNumDaysInHistory; ++i) {
132       update->Insert(0, new base::StringValue(base::Int64ToString(0)));
133     }
134   }
135
136   // Verify the pref list values are equal to the given values.
137   // If the count of values is less than kNumDaysInHistory, zeros are assumed
138   // at the beginning.
139   void VerifyPrefList(const char* pref, const int64* values, size_t count) {
140     ASSERT_GE(kNumDaysInHistory, count);
141     ListPrefUpdate update(&pref_service_, pref);
142     ASSERT_EQ(kNumDaysInHistory, update->GetSize()) << "Pref: " << pref;
143
144     for (size_t i = 0; i < count; ++i) {
145       EXPECT_EQ(
146           values[i],
147           GetListPrefInt64Value(*update, kNumDaysInHistory - count + i))
148           << "index=" << (kNumDaysInHistory - count + i);
149     }
150     for (size_t i = 0; i < kNumDaysInHistory - count; ++i) {
151       EXPECT_EQ(0, GetListPrefInt64Value(*update, i)) << "index=" << i;
152     }
153   }
154
155   // Verify all daily data saving pref list values.
156   void VerifyDailyDataSavingContentLengthPrefLists(
157       const int64* original_values, size_t original_count,
158       const int64* received_values, size_t received_count,
159       const int64* original_with_data_reduction_proxy_enabled_values,
160       size_t original_with_data_reduction_proxy_enabled_count,
161       const int64* received_with_data_reduction_proxy_enabled_values,
162       size_t received_with_data_reduction_proxy_count,
163       const int64* original_via_data_reduction_proxy_values,
164       size_t original_via_data_reduction_proxy_count,
165       const int64* received_via_data_reduction_proxy_values,
166       size_t received_via_data_reduction_proxy_count) {
167     VerifyPrefList(prefs::kDailyHttpOriginalContentLength,
168                    original_values, original_count);
169     VerifyPrefList(prefs::kDailyHttpReceivedContentLength,
170                    received_values, received_count);
171     VerifyPrefList(
172         prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled,
173         original_with_data_reduction_proxy_enabled_values,
174         original_with_data_reduction_proxy_enabled_count);
175     VerifyPrefList(
176         prefs::kDailyContentLengthWithDataReductionProxyEnabled,
177         received_with_data_reduction_proxy_enabled_values,
178         received_with_data_reduction_proxy_count);
179     VerifyPrefList(
180         prefs::kDailyOriginalContentLengthViaDataReductionProxy,
181         original_via_data_reduction_proxy_values,
182         original_via_data_reduction_proxy_count);
183     VerifyPrefList(
184         prefs::kDailyContentLengthViaDataReductionProxy,
185         received_via_data_reduction_proxy_values,
186         received_via_data_reduction_proxy_count);
187   }
188
189   // Verify daily data saving pref for request types.
190   void VerifyDailyRequestTypeContentLengthPrefLists(
191       const int64* original_values, size_t original_count,
192       const int64* received_values, size_t received_count,
193       const int64* original_with_data_reduction_proxy_enabled_values,
194       size_t original_with_data_reduction_proxy_enabled_count,
195       const int64* received_with_data_reduction_proxy_enabled_values,
196       size_t received_with_data_reduction_proxy_count,
197       const int64* https_with_data_reduction_proxy_enabled_values,
198       size_t https_with_data_reduction_proxy_enabled_count,
199       const int64* short_bypass_with_data_reduction_proxy_enabled_values,
200       size_t short_bypass_with_data_reduction_proxy_enabled_count,
201       const int64* long_bypass_with_data_reduction_proxy_enabled_values,
202       size_t long_bypass_with_data_reduction_proxy_enabled_count,
203       const int64* unknown_with_data_reduction_proxy_enabled_values,
204       size_t unknown_with_data_reduction_proxy_enabled_count) {
205     VerifyPrefList(prefs::kDailyHttpOriginalContentLength,
206                    original_values, original_count);
207     VerifyPrefList(prefs::kDailyHttpReceivedContentLength,
208                    received_values, received_count);
209     VerifyPrefList(
210         prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled,
211         original_with_data_reduction_proxy_enabled_values,
212         original_with_data_reduction_proxy_enabled_count);
213     VerifyPrefList(
214         prefs::kDailyContentLengthWithDataReductionProxyEnabled,
215         received_with_data_reduction_proxy_enabled_values,
216         received_with_data_reduction_proxy_count);
217     VerifyPrefList(
218         prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled,
219         https_with_data_reduction_proxy_enabled_values,
220         https_with_data_reduction_proxy_enabled_count);
221     VerifyPrefList(
222         prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled,
223         short_bypass_with_data_reduction_proxy_enabled_values,
224         short_bypass_with_data_reduction_proxy_enabled_count);
225     VerifyPrefList(
226         prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled,
227         long_bypass_with_data_reduction_proxy_enabled_values,
228         long_bypass_with_data_reduction_proxy_enabled_count);
229     VerifyPrefList(
230         prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled,
231         unknown_with_data_reduction_proxy_enabled_values,
232         unknown_with_data_reduction_proxy_enabled_count);
233   }
234
235  private:
236   base::Time now_;
237   base::TimeDelta now_delta_;
238 };
239
240 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, OneResponse) {
241   const int64 kOriginalLength = 200;
242   const int64 kReceivedLength = 100;
243   int64 original[] = {kOriginalLength};
244   int64 received[] = {kReceivedLength};
245
246   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
247       kReceivedLength, kOriginalLength,
248       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
249       FakeNow(), &pref_service_);
250   VerifyDailyDataSavingContentLengthPrefLists(
251       original, 1, received, 1,
252       original, 1, received, 1,
253       original, 1, received, 1);
254 }
255
256 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, MultipleResponses) {
257   const int64 kOriginalLength = 150;
258   const int64 kReceivedLength = 100;
259   int64 original[] = {kOriginalLength};
260   int64 received[] = {kReceivedLength};
261   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
262       kReceivedLength, kOriginalLength,
263       false, spdyproxy::UNKNOWN_TYPE,
264       FakeNow(), &pref_service_);
265   VerifyDailyDataSavingContentLengthPrefLists(
266       original, 1, received, 1,
267       NULL, 0, NULL, 0, NULL, 0, NULL, 0);
268
269   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
270       kReceivedLength, kOriginalLength,
271       true, spdyproxy::UNKNOWN_TYPE,
272       FakeNow(), &pref_service_);
273   original[0] += kOriginalLength;
274   received[0] += kReceivedLength;
275   int64 original_proxy_enabled[] = {kOriginalLength};
276   int64 received_proxy_enabled[] = {kReceivedLength};
277   VerifyDailyDataSavingContentLengthPrefLists(
278       original, 1, received, 1,
279       original_proxy_enabled, 1, received_proxy_enabled, 1,
280       NULL, 0, NULL, 0);
281
282   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
283       kReceivedLength, kOriginalLength,
284       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
285       FakeNow(), &pref_service_);
286   original[0] += kOriginalLength;
287   received[0] += kReceivedLength;
288   original_proxy_enabled[0] += kOriginalLength;
289   received_proxy_enabled[0] += kReceivedLength;
290   int64 original_via_proxy[] = {kOriginalLength};
291   int64 received_via_proxy[] = {kReceivedLength};
292   VerifyDailyDataSavingContentLengthPrefLists(
293       original, 1, received, 1,
294       original_proxy_enabled, 1, received_proxy_enabled, 1,
295       original_via_proxy, 1, received_via_proxy, 1);
296
297   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
298       kReceivedLength, kOriginalLength,
299       true, spdyproxy::UNKNOWN_TYPE, FakeNow(), &pref_service_);
300   original[0] += kOriginalLength;
301   received[0] += kReceivedLength;
302   original_proxy_enabled[0] += kOriginalLength;
303   received_proxy_enabled[0] += kReceivedLength;
304   VerifyDailyDataSavingContentLengthPrefLists(
305       original, 1, received, 1,
306       original_proxy_enabled, 1, received_proxy_enabled, 1,
307       original_via_proxy, 1, received_via_proxy, 1);
308
309   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
310       kReceivedLength, kOriginalLength,
311       false, spdyproxy::UNKNOWN_TYPE, FakeNow(), &pref_service_);
312   original[0] += kOriginalLength;
313   received[0] += kReceivedLength;
314   VerifyDailyDataSavingContentLengthPrefLists(
315       original, 1, received, 1,
316       original_proxy_enabled, 1, received_proxy_enabled, 1,
317       original_via_proxy, 1, received_via_proxy, 1);
318 }
319
320 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, RequestType) {
321   const int64 kContentLength = 200;
322   int64 received[] = {0};
323   int64 https_received[] = {0};
324   int64 total_received[] = {0};
325   int64 proxy_enabled_received[] = {0};
326
327   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
328       kContentLength, kContentLength,
329       true, spdyproxy::HTTPS,
330       FakeNow(), &pref_service_);
331   total_received[0] += kContentLength;
332   proxy_enabled_received[0] += kContentLength;
333   https_received[0] += kContentLength;
334   VerifyDailyRequestTypeContentLengthPrefLists(
335       total_received, 1, total_received, 1,
336       proxy_enabled_received, 1, proxy_enabled_received, 1,
337       https_received, 1,
338       received, 0,  // short bypass
339       received, 0,  // long bypass
340       received, 0);  // unknown
341
342   // Data reduction proxy is not enabled.
343   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
344       kContentLength, kContentLength,
345       false, spdyproxy::HTTPS,
346       FakeNow(), &pref_service_);
347   total_received[0] += kContentLength;
348   VerifyDailyRequestTypeContentLengthPrefLists(
349       total_received, 1, total_received, 1,
350       proxy_enabled_received, 1, proxy_enabled_received, 1,
351       https_received, 1,
352       received, 0,  // short bypass
353       received, 0,  // long bypass
354       received, 0);  // unknown
355
356   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
357       kContentLength, kContentLength,
358       true, spdyproxy::HTTPS,
359       FakeNow(), &pref_service_);
360   total_received[0] += kContentLength;
361   proxy_enabled_received[0] += kContentLength;
362   https_received[0] += kContentLength;
363   VerifyDailyRequestTypeContentLengthPrefLists(
364       total_received, 1, total_received, 1,
365       proxy_enabled_received, 1, proxy_enabled_received, 1,
366       https_received, 1,
367       received, 0,  // short bypass
368       received, 0,  // long bypass
369       received, 0);  // unknown
370
371   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
372       kContentLength, kContentLength,
373       true, spdyproxy::SHORT_BYPASS,
374       FakeNow(), &pref_service_);
375   total_received[0] += kContentLength;
376   proxy_enabled_received[0] += kContentLength;
377   received[0] += kContentLength;
378   VerifyDailyRequestTypeContentLengthPrefLists(
379       total_received, 1, total_received, 1,
380       proxy_enabled_received, 1, proxy_enabled_received, 1,
381       https_received, 1,
382       received, 1,  // short bypass
383       received, 0,  // long bypass
384       received, 0);  // unknown
385
386   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
387       kContentLength, kContentLength,
388       true, spdyproxy::LONG_BYPASS,
389       FakeNow(), &pref_service_);
390   total_received[0] += kContentLength;
391   proxy_enabled_received[0] += kContentLength;
392   VerifyDailyRequestTypeContentLengthPrefLists(
393       total_received, 1, total_received, 1,  // total
394       proxy_enabled_received, 1, proxy_enabled_received, 1,
395       https_received, 1,
396       received, 1,  // short bypass
397       received, 1,  // long bypass
398       received, 0);  // unknown
399
400   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
401       kContentLength, kContentLength,
402       true, spdyproxy::UNKNOWN_TYPE,
403       FakeNow(), &pref_service_);
404   total_received[0] += kContentLength;
405   proxy_enabled_received[0] += kContentLength;
406   VerifyDailyRequestTypeContentLengthPrefLists(
407       total_received, 1, total_received, 1,
408       proxy_enabled_received, 1, proxy_enabled_received, 1,
409       https_received, 1,
410       received, 1,  // short bypass
411       received, 1,  // long bypass
412       received, 1);  // unknown
413 }
414
415 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, ForwardOneDay) {
416   const int64 kOriginalLength = 200;
417   const int64 kReceivedLength = 100;
418
419   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
420       kReceivedLength, kOriginalLength,
421       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
422       FakeNow(), &pref_service_);
423
424   // Forward one day.
425   SetFakeTimeDeltaInHours(24);
426
427   // Proxy not enabled. Not via proxy.
428   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
429       kReceivedLength, kOriginalLength,
430       false, spdyproxy::UNKNOWN_TYPE, FakeNow(), &pref_service_);
431
432   int64 original[] = {kOriginalLength, kOriginalLength};
433   int64 received[] = {kReceivedLength, kReceivedLength};
434   int64 original_with_data_reduction_proxy_enabled[] = {kOriginalLength, 0};
435   int64 received_with_data_reduction_proxy_enabled[] = {kReceivedLength, 0};
436   int64 original_via_data_reduction_proxy[] = {kOriginalLength, 0};
437   int64 received_via_data_reduction_proxy[] = {kReceivedLength, 0};
438   VerifyDailyDataSavingContentLengthPrefLists(
439       original, 2,
440       received, 2,
441       original_with_data_reduction_proxy_enabled, 2,
442       received_with_data_reduction_proxy_enabled, 2,
443       original_via_data_reduction_proxy, 2,
444       received_via_data_reduction_proxy, 2);
445
446   // Proxy enabled. Not via proxy.
447   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
448       kReceivedLength, kOriginalLength,
449       true, spdyproxy::UNKNOWN_TYPE, FakeNow(), &pref_service_);
450   original[1] += kOriginalLength;
451   received[1] += kReceivedLength;
452   original_with_data_reduction_proxy_enabled[1] += kOriginalLength;
453   received_with_data_reduction_proxy_enabled[1] += kReceivedLength;
454   VerifyDailyDataSavingContentLengthPrefLists(
455       original, 2,
456       received, 2,
457       original_with_data_reduction_proxy_enabled, 2,
458       received_with_data_reduction_proxy_enabled, 2,
459       original_via_data_reduction_proxy, 2,
460       received_via_data_reduction_proxy, 2);
461
462   // Proxy enabled and via proxy.
463   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
464       kReceivedLength, kOriginalLength,
465       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
466       FakeNow(), &pref_service_);
467   original[1] += kOriginalLength;
468   received[1] += kReceivedLength;
469   original_with_data_reduction_proxy_enabled[1] += kOriginalLength;
470   received_with_data_reduction_proxy_enabled[1] += kReceivedLength;
471   original_via_data_reduction_proxy[1] += kOriginalLength;
472   received_via_data_reduction_proxy[1] += kReceivedLength;
473   VerifyDailyDataSavingContentLengthPrefLists(
474       original, 2,
475       received, 2,
476       original_with_data_reduction_proxy_enabled, 2,
477       received_with_data_reduction_proxy_enabled, 2,
478       original_via_data_reduction_proxy, 2,
479       received_via_data_reduction_proxy, 2);
480 }
481
482 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, PartialDayTimeChange) {
483   const int64 kOriginalLength = 200;
484   const int64 kReceivedLength = 100;
485   int64 original[] = {0, kOriginalLength};
486   int64 received[] = {0, kReceivedLength};
487
488   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
489       kReceivedLength, kOriginalLength,
490       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
491       FakeNow(), &pref_service_);
492   VerifyDailyDataSavingContentLengthPrefLists(
493       original, 2, received, 2,
494       original, 2, received, 2,
495       original, 2, received, 2);
496
497   // Forward 10 hours, stay in the same day.
498   // See kLastUpdateTime: "Now" in test is 03:45am.
499   SetFakeTimeDeltaInHours(10);
500   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
501       kReceivedLength, kOriginalLength,
502       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
503       FakeNow(), &pref_service_);
504   original[1] += kOriginalLength;
505   received[1] += kReceivedLength;
506   VerifyDailyDataSavingContentLengthPrefLists(
507       original, 2, received, 2,
508       original, 2, received, 2,
509       original, 2, received, 2);
510
511   // Forward 11 more hours, comes to tomorrow.
512   AddFakeTimeDeltaInHours(11);
513   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
514       kReceivedLength, kOriginalLength,
515       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
516       FakeNow(), &pref_service_);
517   int64 original2[] = {kOriginalLength * 2, kOriginalLength};
518   int64 received2[] = {kReceivedLength * 2, kReceivedLength};
519   VerifyDailyDataSavingContentLengthPrefLists(
520       original2, 2, received2, 2,
521       original2, 2, received2, 2,
522       original2, 2, received2, 2);
523 }
524
525 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, ForwardMultipleDays) {
526   const int64 kOriginalLength = 200;
527   const int64 kReceivedLength = 100;
528   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
529       kReceivedLength, kOriginalLength,
530       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
531       FakeNow(), &pref_service_);
532
533   // Forward three days.
534   SetFakeTimeDeltaInHours(3 * 24);
535
536   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
537       kReceivedLength, kOriginalLength,
538       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
539       FakeNow(), &pref_service_);
540
541   int64 original[] = {kOriginalLength, 0, 0, kOriginalLength};
542   int64 received[] = {kReceivedLength, 0, 0, kReceivedLength};
543   VerifyDailyDataSavingContentLengthPrefLists(
544       original, 4, received, 4,
545       original, 4, received, 4,
546       original, 4, received, 4);
547
548   // Forward four more days.
549   AddFakeTimeDeltaInHours(4 * 24);
550   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
551       kReceivedLength, kOriginalLength,
552       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
553       FakeNow(), &pref_service_);
554   int64 original2[] = {
555     kOriginalLength, 0, 0, kOriginalLength, 0, 0, 0, kOriginalLength,
556   };
557   int64 received2[] = {
558     kReceivedLength, 0, 0, kReceivedLength, 0, 0, 0, kReceivedLength,
559   };
560   VerifyDailyDataSavingContentLengthPrefLists(
561       original2, 8, received2, 8,
562       original2, 8, received2, 8,
563       original2, 8, received2, 8);
564
565   // Forward |kNumDaysInHistory| more days.
566   AddFakeTimeDeltaInHours(kNumDaysInHistory * 24);
567   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
568       kReceivedLength, kOriginalLength,
569       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
570       FakeNow(), &pref_service_);
571   int64 original3[] = {kOriginalLength};
572   int64 received3[] = {kReceivedLength};
573   VerifyDailyDataSavingContentLengthPrefLists(
574       original3, 1, received3, 1,
575       original3, 1, received3, 1,
576       original3, 1, received3, 1);
577
578   // Forward |kNumDaysInHistory| + 1 more days.
579   AddFakeTimeDeltaInHours((kNumDaysInHistory + 1)* 24);
580   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
581       kReceivedLength, kOriginalLength,
582       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
583       FakeNow(), &pref_service_);
584   VerifyDailyDataSavingContentLengthPrefLists(
585       original3, 1, received3, 1,
586       original3, 1, received3, 1,
587       original3, 1, received3, 1);
588 }
589
590 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, BackwardAndForwardOneDay) {
591   const int64 kOriginalLength = 200;
592   const int64 kReceivedLength = 100;
593   int64 original[] = {kOriginalLength};
594   int64 received[] = {kReceivedLength};
595
596   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
597       kReceivedLength, kOriginalLength,
598       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
599       FakeNow(), &pref_service_);
600
601   // Backward one day.
602   SetFakeTimeDeltaInHours(-24);
603   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
604       kReceivedLength, kOriginalLength,
605       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
606       FakeNow(), &pref_service_);
607   original[0] += kOriginalLength;
608   received[0] += kReceivedLength;
609   VerifyDailyDataSavingContentLengthPrefLists(
610       original, 1, received, 1,
611       original, 1, received, 1,
612       original, 1, received, 1);
613
614   // Then, Forward one day
615   AddFakeTimeDeltaInHours(24);
616   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
617       kReceivedLength, kOriginalLength,
618       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
619       FakeNow(), &pref_service_);
620   int64 original2[] = {kOriginalLength * 2, kOriginalLength};
621   int64 received2[] = {kReceivedLength * 2, kReceivedLength};
622   VerifyDailyDataSavingContentLengthPrefLists(
623       original2, 2, received2, 2,
624       original2, 2, received2, 2,
625       original2, 2, received2, 2);
626 }
627
628 TEST_F(ChromeNetworkDailyDataSavingMetricsTest, BackwardTwoDays) {
629   const int64 kOriginalLength = 200;
630   const int64 kReceivedLength = 100;
631   int64 original[] = {kOriginalLength};
632   int64 received[] = {kReceivedLength};
633
634   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
635       kReceivedLength, kOriginalLength,
636       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
637       FakeNow(), &pref_service_);
638   // Backward two days.
639   SetFakeTimeDeltaInHours(-2 * 24);
640   spdyproxy::UpdateContentLengthPrefsForDataReductionProxy(
641       kReceivedLength, kOriginalLength,
642       true, spdyproxy::VIA_DATA_REDUCTION_PROXY,
643       FakeNow(), &pref_service_);
644   VerifyDailyDataSavingContentLengthPrefLists(
645       original, 1, received, 1,
646       original, 1, received, 1,
647       original, 1, received, 1);
648 }
649 #endif  // defined(OS_ANDROID) || defined(OS_IOS)
650
651 }  // namespace