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/prefs/pref_registry_simple.h" | |
6 #include "base/prefs/testing_pref_service.h" | |
7 #include "base/strings/string_number_conversions.h" | |
8 #include "base/test/test_simple_task_runner.h" | |
9 #include "components/data_reduction_proxy/browser/data_reduction_proxy_statistic s_prefs.h" | |
10 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names .h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace { | |
14 | |
15 const size_t kNumDaysInHistory = 60; | |
bengr
2014/08/27 04:53:26
Add TODO to make such constants accessible by othe
megjablon
2014/08/28 20:44:08
Done.
| |
16 | |
17 int64 GetListPrefInt64Value( | |
18 const base::ListValue& list_update, size_t index) { | |
19 std::string string_value; | |
20 EXPECT_TRUE(list_update.GetString(index, &string_value)); | |
21 | |
22 int64 value = 0; | |
23 EXPECT_TRUE(base::StringToInt64(string_value, &value)); | |
24 return value; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 namespace data_reduction_proxy { | |
30 class DataReductionProxyStatisticsPrefsTest : public testing::Test { | |
31 protected: | |
32 DataReductionProxyStatisticsPrefsTest() | |
33 : task_runner_(scoped_refptr<base::TestSimpleTaskRunner>( | |
34 new base::TestSimpleTaskRunner())), | |
35 pref_service_(new DataReductionProxyStatisticsPrefs( | |
36 &simple_pref_service_, | |
37 task_runner_, | |
38 commit_delay_)) {} | |
39 | |
40 virtual void SetUp() OVERRIDE { | |
41 PrefRegistrySimple* registry = simple_pref_service_.registry(); | |
42 registry->RegisterInt64Pref( | |
43 data_reduction_proxy::prefs::kHttpReceivedContentLength, 0); | |
44 registry->RegisterInt64Pref( | |
45 data_reduction_proxy::prefs::kHttpOriginalContentLength, 32); | |
46 | |
47 registry->RegisterListPref(data_reduction_proxy::prefs:: | |
48 kDailyHttpOriginalContentLength); | |
49 registry->RegisterListPref(data_reduction_proxy::prefs:: | |
50 kDailyHttpReceivedContentLength); | |
51 | |
52 #if !(defined(OS_ANDROID) || defined(OS_IOS)) | |
bengr
2014/08/27 04:53:26
Why is this here?
megjablon
2014/08/28 20:44:08
We only add the prefs to the pref map when we're n
| |
53 pref_service_->InitInt64Pref(data_reduction_proxy::prefs:: | |
54 kHttpReceivedContentLength); | |
55 pref_service_->InitInt64Pref(data_reduction_proxy::prefs:: | |
56 kHttpOriginalContentLength); | |
57 pref_service_->InitListPref(data_reduction_proxy::prefs:: | |
58 kDailyHttpOriginalContentLength); | |
59 pref_service_->InitListPref(data_reduction_proxy::prefs:: | |
60 kDailyHttpReceivedContentLength); | |
61 #endif | |
62 | |
63 CreatePrefList( | |
64 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); | |
65 CreatePrefList( | |
66 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); | |
67 } | |
68 | |
69 // Create daily pref list of |kNumDaysInHistory| zero values. | |
70 void CreatePrefList(const char* pref) { | |
71 base::ListValue* update = pref_service_->GetList(pref); | |
72 update->Clear(); | |
73 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | |
74 update->Insert(0, new base::StringValue(base::Int64ToString(0))); | |
75 } | |
76 } | |
77 | |
78 // Verify the pref list values in |pref_service_| are equal to those in | |
79 // |simple_pref_service| for |pref|. | |
80 void VerifyPrefListWasWritten(const char* pref) { | |
81 const base::ListValue* delayed_list = pref_service_->GetList(pref); | |
82 const base::ListValue* written_list = simple_pref_service_.GetList(pref); | |
83 ASSERT_EQ(delayed_list->GetSize(), written_list->GetSize()); | |
84 size_t count = delayed_list->GetSize(); | |
85 | |
86 for (size_t i = 0; i < count; ++i) { | |
87 EXPECT_EQ( | |
88 GetListPrefInt64Value(*delayed_list, i), | |
bengr
2014/08/27 04:53:26
Can this be moved up a line?
megjablon
2014/08/28 20:44:08
Done.
| |
89 GetListPrefInt64Value(*written_list, i)); | |
90 } | |
91 } | |
92 | |
93 // Verify the pref value in |pref_service_| are equal to that in | |
94 // |simple_pref_service|. | |
95 void VerifyPrefWasWritten(const char* pref) { | |
96 int64 delayed_pref = pref_service_->GetInt64(pref); | |
97 int64 written_pref = simple_pref_service_.GetInt64(pref); | |
98 EXPECT_EQ(delayed_pref, written_pref); | |
99 } | |
100 | |
101 #if defined(OS_ANDROID) || defined(OS_IOS) | |
102 base::TimeDelta commit_delay_ = base::TimeDelta(); | |
103 #else | |
104 base::TimeDelta commit_delay_ = base::TimeDelta::FromMinutes(60); | |
105 #endif | |
106 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
107 TestingPrefServiceSimple simple_pref_service_; | |
108 DataReductionProxyStatisticsPrefs* pref_service_; | |
109 }; | |
110 | |
111 TEST_F(DataReductionProxyStatisticsPrefsTest, WritePrefs) { | |
112 const int64 kOriginalLength = 150; | |
113 const int64 kReceivedLength = 100; | |
114 | |
115 pref_service_->SetInt64( | |
116 data_reduction_proxy::prefs::kHttpOriginalContentLength, kOriginalLength); | |
117 pref_service_->SetInt64( | |
118 data_reduction_proxy::prefs::kHttpReceivedContentLength, kReceivedLength); | |
119 | |
120 base::ListValue* original_daily_content_length_list = pref_service_->GetList( | |
121 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); | |
122 base::ListValue* received_daily_content_length_list = pref_service_->GetList( | |
123 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); | |
124 | |
125 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | |
126 original_daily_content_length_list->Set( | |
127 i, new base::StringValue(base::Int64ToString(i))); | |
128 } | |
129 | |
130 received_daily_content_length_list->Clear(); | |
131 for (size_t i = 0; i < kNumDaysInHistory/2; ++i) { | |
132 received_daily_content_length_list->Set( | |
133 i, new base::StringValue(base::Int64ToString(i))); | |
134 } | |
135 #if !(defined(OS_ANDROID) || defined(OS_IOS)) | |
136 task_runner_->RunPendingTasks(); | |
137 #endif | |
138 | |
139 VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpOriginalContentLength); | |
140 VerifyPrefWasWritten(data_reduction_proxy::prefs::kHttpReceivedContentLength); | |
141 VerifyPrefListWasWritten( | |
142 data_reduction_proxy::prefs::kDailyHttpOriginalContentLength); | |
143 VerifyPrefListWasWritten( | |
144 data_reduction_proxy::prefs::kDailyHttpReceivedContentLength); | |
145 } | |
146 | |
147 } // namespace data_reduction_proxy | |
OLD | NEW |