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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.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 "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace data_reduction_proxy {
17 class DataReductionProxyPrefsTest : public testing::Test {
19 void SetUp() override {
20 RegisterPrefs(local_state_prefs_.registry());
21 PrefRegistrySimple* profile_registry = profile_prefs_.registry();
22 RegisterPrefs(profile_registry);
23 profile_registry->RegisterBooleanPref(
24 prefs::kStatisticsPrefsMigrated, false);
27 PrefService* local_state_prefs() {
28 return &local_state_prefs_;
31 PrefService* profile_prefs() {
32 return &profile_prefs_;
35 // Initializes a list with ten string representations of successive int64
36 // values, starting with |starting_value|.
37 void InitializeList(const char* pref_name,
39 PrefService* pref_service) {
40 ListPrefUpdate list(local_state_prefs(), pref_name);
41 for (int64 i = 0; i < 10L; ++i) {
42 list->Set(i, new base::StringValue(
43 base::Int64ToString(i + starting_value)));
47 // Verifies that ten string repreentations of successive int64 values starting
48 // with |starting_value| are found in the |ListValue| with the associated
50 void VerifyList(const char* pref_name,
52 PrefService* pref_service) {
53 const base::ListValue* list_value = pref_service->GetList(pref_name);
54 for (int64 i = 0; i < 10L; ++i) {
55 std::string string_value;
57 list_value->GetString(i, &string_value);
58 base::StringToInt64(string_value, &value);
59 EXPECT_EQ(i + starting_value, value);
64 void RegisterPrefs(PrefRegistrySimple* registry) {
65 registry->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0);
66 registry->RegisterInt64Pref(prefs::kHttpOriginalContentLength, 0);
68 registry->RegisterListPref(prefs::kDailyHttpOriginalContentLength);
69 registry->RegisterListPref(prefs::kDailyHttpReceivedContentLength);
70 registry->RegisterListPref(
71 prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled);
72 registry->RegisterListPref(
73 prefs::kDailyContentLengthWithDataReductionProxyEnabled);
74 registry->RegisterListPref(
75 prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled);
76 registry->RegisterListPref(
77 prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled);
78 registry->RegisterListPref(
79 prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled);
80 registry->RegisterListPref(
81 prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled);
82 registry->RegisterListPref(
83 prefs::kDailyOriginalContentLengthViaDataReductionProxy);
84 registry->RegisterListPref(prefs::kDailyContentLengthViaDataReductionProxy);
85 registry->RegisterInt64Pref(
86 prefs::kDailyHttpContentLengthLastUpdateDate, 0L);
89 TestingPrefServiceSimple local_state_prefs_;
90 TestingPrefServiceSimple profile_prefs_;
93 TEST_F(DataReductionProxyPrefsTest, TestMigration) {
94 local_state_prefs()->SetInt64(prefs::kHttpReceivedContentLength, 123L);
95 local_state_prefs()->SetInt64(prefs::kHttpOriginalContentLength, 234L);
96 local_state_prefs()->SetInt64(
97 prefs::kDailyHttpContentLengthLastUpdateDate, 345L);
99 prefs::kDailyHttpOriginalContentLength, 0L, local_state_prefs());
101 prefs::kDailyHttpReceivedContentLength, 1L, local_state_prefs());
103 prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled,
105 local_state_prefs());
107 prefs::kDailyContentLengthWithDataReductionProxyEnabled,
109 local_state_prefs());
111 prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled,
113 local_state_prefs());
115 prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled,
117 local_state_prefs());
119 prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled,
121 local_state_prefs());
123 prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled,
125 local_state_prefs());
127 prefs::kDailyOriginalContentLengthViaDataReductionProxy,
129 local_state_prefs());
131 prefs::kDailyContentLengthViaDataReductionProxy,
133 local_state_prefs());
135 EXPECT_EQ(0L, profile_prefs()->GetInt64(prefs::kHttpReceivedContentLength));
136 EXPECT_EQ(0L, profile_prefs()->GetInt64(prefs::kHttpOriginalContentLength));
137 EXPECT_EQ(0u, profile_prefs()->GetList(
138 prefs::kDailyHttpOriginalContentLength)->GetSize());
139 EXPECT_EQ(0u, profile_prefs()->GetList(
140 prefs::kDailyHttpReceivedContentLength)->GetSize());
141 EXPECT_EQ(0u, profile_prefs()->GetList(
142 prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled)->
144 EXPECT_EQ(0u, profile_prefs()->GetList(
145 prefs::kDailyContentLengthWithDataReductionProxyEnabled)->GetSize());
146 EXPECT_EQ(0u, profile_prefs()->GetList(
147 prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled)->GetSize());
148 EXPECT_EQ(0u, profile_prefs()->GetList(
149 prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled)->
151 EXPECT_EQ(0u, profile_prefs()->GetList(
152 prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled)->
154 EXPECT_EQ(0u, profile_prefs()->GetList(
155 prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled)->
157 EXPECT_EQ(0u, profile_prefs()->GetList(
158 prefs::kDailyOriginalContentLengthViaDataReductionProxy)->GetSize());
159 EXPECT_EQ(0u, profile_prefs()->GetList(
160 prefs::kDailyContentLengthViaDataReductionProxy)->GetSize());
161 EXPECT_EQ(0L, profile_prefs()->GetInt64(
162 prefs::kDailyHttpContentLengthLastUpdateDate));
163 EXPECT_FALSE(profile_prefs()->GetBoolean(prefs::kStatisticsPrefsMigrated));
165 data_reduction_proxy::MigrateStatisticsPrefs(local_state_prefs(),
168 EXPECT_EQ(123L, profile_prefs()->GetInt64(prefs::kHttpReceivedContentLength));
169 EXPECT_EQ(234L, profile_prefs()->GetInt64(prefs::kHttpOriginalContentLength));
170 VerifyList(prefs::kDailyHttpOriginalContentLength, 0L, profile_prefs());
171 VerifyList(prefs::kDailyHttpReceivedContentLength, 1L, profile_prefs());
173 prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled,
177 prefs::kDailyContentLengthWithDataReductionProxyEnabled,
181 prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled,
185 prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled,
189 prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled,
193 prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled,
197 prefs::kDailyOriginalContentLengthViaDataReductionProxy,
201 prefs::kDailyContentLengthViaDataReductionProxy,
204 EXPECT_EQ(345L, profile_prefs()->GetInt64(
205 prefs::kDailyHttpContentLengthLastUpdateDate));
206 EXPECT_TRUE(profile_prefs()->GetBoolean(prefs::kStatisticsPrefsMigrated));
208 // Migration should only happen once.
209 local_state_prefs()->SetInt64(prefs::kHttpReceivedContentLength, 456L);
210 data_reduction_proxy::MigrateStatisticsPrefs(local_state_prefs(),
212 EXPECT_EQ(123L, profile_prefs()->GetInt64(prefs::kHttpReceivedContentLength));
215 } // namespace data_reduction_proxy