ProjectingObserverChromeos: Drop DBusThreadManager dependency for better testing.
[chromium-blink-merge.git] / components / data_reduction_proxy / browser / data_reduction_proxy_statistics_prefs_unittest.cc
blob50729600b6427730f2630240c48b989b1ecd3952
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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/prefs/pref_registry_simple.h"
7 #include "base/prefs/testing_pref_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "components/data_reduction_proxy/browser/data_reduction_proxy_prefs.h"
11 #include "components/data_reduction_proxy/browser/data_reduction_proxy_statistics_prefs.h"
12 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace {
17 // TODO Make kNumDaysInHistory accessible from DataReductionProxySettings.
18 const size_t kNumDaysInHistory = 60;
19 const int kWriteDelayMinutes = 60;
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));
26 int64 value = 0;
27 EXPECT_TRUE(base::StringToInt64(string_value, &value));
28 return value;
31 } // namespace
33 namespace data_reduction_proxy {
35 class DataReductionProxyStatisticsPrefsTest : public testing::Test {
36 protected:
37 DataReductionProxyStatisticsPrefsTest()
38 : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>(
39 new base::TestSimpleTaskRunner())) {}
41 virtual void SetUp() override {
42 RegisterPrefs(simple_pref_service_.registry());
45 // Create daily pref list of |kNumDaysInHistory| zero values.
46 void CreatePrefList(const char* pref) {
47 base::ListValue* update = statistics_prefs_->GetList(pref);
48 update->Clear();
49 for (size_t i = 0; i < kNumDaysInHistory; ++i) {
50 update->Insert(0, new base::StringValue(base::Int64ToString(0)));
54 // Verify the pref list values in |pref_service_| are equal to those in
55 // |simple_pref_service| for |pref|.
56 void VerifyPrefListWasWritten(const char* pref) {
57 const base::ListValue* delayed_list = statistics_prefs_->GetList(pref);
58 const base::ListValue* written_list = simple_pref_service_.GetList(pref);
59 ASSERT_EQ(delayed_list->GetSize(), written_list->GetSize());
60 size_t count = delayed_list->GetSize();
62 for (size_t i = 0; i < count; ++i) {
63 EXPECT_EQ(GetListPrefInt64Value(*delayed_list, i),
64 GetListPrefInt64Value(*written_list, i));
68 // Verify the pref value in |pref_service_| are equal to that in
69 // |simple_pref_service|.
70 void VerifyPrefWasWritten(const char* pref) {
71 int64 delayed_pref = statistics_prefs_->GetInt64(pref);
72 int64 written_pref = simple_pref_service_.GetInt64(pref);
73 EXPECT_EQ(delayed_pref, written_pref);
76 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
77 TestingPrefServiceSimple simple_pref_service_;
78 scoped_ptr<DataReductionProxyStatisticsPrefs> statistics_prefs_;
81 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefsDirect) {
82 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
83 &simple_pref_service_,
84 task_runner_,
85 base::TimeDelta()));
86 const int64 kOriginalLength = 150;
87 const int64 kReceivedLength = 100;
89 CreatePrefList(
90 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
91 CreatePrefList(
92 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
94 statistics_prefs_->SetInt64(
95 data_reduction_proxy::prefs::kHttpOriginalContentLength, kOriginalLength);
96 statistics_prefs_->SetInt64(
97 data_reduction_proxy::prefs::kHttpReceivedContentLength, kReceivedLength);
99 base::ListValue* original_daily_content_length_list =
100 statistics_prefs_->GetList(data_reduction_proxy::prefs::
101 kDailyHttpOriginalContentLength);
102 base::ListValue* received_daily_content_length_list =
103 statistics_prefs_->GetList(data_reduction_proxy::prefs::
104 kDailyHttpReceivedContentLength);
106 for (size_t i = 0; i < kNumDaysInHistory; ++i) {
107 original_daily_content_length_list->Set(
108 i, new base::StringValue(base::Int64ToString(i)));
111 received_daily_content_length_list->Clear();
112 for (size_t i = 0; i < kNumDaysInHistory/2; ++i) {
113 received_daily_content_length_list->Set(
114 i, new base::StringValue(base::Int64ToString(i)));
117 VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpOriginalContentLength);
118 VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpReceivedContentLength);
119 VerifyPrefListWasWritten(
120 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
121 VerifyPrefListWasWritten(
122 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
125 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefsDelayed) {
126 statistics_prefs_.reset(new DataReductionProxyStatisticsPrefs(
127 &simple_pref_service_,
128 task_runner_,
129 base::TimeDelta::FromMinutes(kWriteDelayMinutes)));
130 statistics_prefs_->Init();
132 CreatePrefList(
133 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
134 CreatePrefList(
135 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
137 const int64 kOriginalLength = 150;
138 const int64 kReceivedLength = 100;
140 statistics_prefs_->SetInt64(
141 data_reduction_proxy::prefs::kHttpOriginalContentLength, kOriginalLength);
142 statistics_prefs_->SetInt64(
143 data_reduction_proxy::prefs::kHttpReceivedContentLength, kReceivedLength);
145 base::ListValue* original_daily_content_length_list =
146 statistics_prefs_->GetList(data_reduction_proxy::prefs::
147 kDailyHttpOriginalContentLength);
148 base::ListValue* received_daily_content_length_list =
149 statistics_prefs_->GetList(data_reduction_proxy::prefs::
150 kDailyHttpReceivedContentLength);
152 for (size_t i = 0; i < kNumDaysInHistory; ++i) {
153 original_daily_content_length_list->Set(
154 i, new base::StringValue(base::Int64ToString(i)));
157 received_daily_content_length_list->Clear();
158 for (size_t i = 0; i < kNumDaysInHistory/2; ++i) {
159 received_daily_content_length_list->Set(
160 i, new base::StringValue(base::Int64ToString(i)));
163 task_runner_->RunPendingTasks();
165 VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpOriginalContentLength);
166 VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpReceivedContentLength);
167 VerifyPrefListWasWritten(
168 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
169 VerifyPrefListWasWritten(
170 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
173 } // namespace data_reduction_proxy