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