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