Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / core / browser / data_reduction_proxy_prefs_unittest.cc
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 "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
6
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"
14
15 namespace data_reduction_proxy {
16
17 class DataReductionProxyPrefsTest : public testing::Test {
18  public:
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);
25   }
26
27   PrefService* local_state_prefs() {
28     return &local_state_prefs_;
29   }
30
31   PrefService* profile_prefs() {
32     return &profile_prefs_;
33   }
34
35   // Initializes a list with ten string representations of successive int64
36   // values, starting with |starting_value|.
37   void InitializeList(const char* pref_name,
38                       int64 starting_value,
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)));
44     }
45   }
46
47   // Verifies that ten string repreentations of successive int64 values starting
48   // with |starting_value| are found in the |ListValue| with the associated
49   // |pref_name|.
50   void VerifyList(const char* pref_name,
51                   int64 starting_value,
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;
56       int64 value;
57       list_value->GetString(i, &string_value);
58       base::StringToInt64(string_value, &value);
59       EXPECT_EQ(i + starting_value, value);
60     }
61   }
62
63  private:
64   void RegisterPrefs(PrefRegistrySimple* registry) {
65     registry->RegisterInt64Pref(prefs::kHttpReceivedContentLength, 0);
66     registry->RegisterInt64Pref(prefs::kHttpOriginalContentLength, 0);
67
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);
87   }
88
89   TestingPrefServiceSimple local_state_prefs_;
90   TestingPrefServiceSimple profile_prefs_;
91 };
92
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);
98   InitializeList(
99       prefs::kDailyHttpOriginalContentLength, 0L, local_state_prefs());
100   InitializeList(
101       prefs::kDailyHttpReceivedContentLength, 1L, local_state_prefs());
102   InitializeList(
103       prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled,
104       2L,
105       local_state_prefs());
106   InitializeList(
107       prefs::kDailyContentLengthWithDataReductionProxyEnabled,
108       3L,
109       local_state_prefs());
110   InitializeList(
111       prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled,
112       4L,
113       local_state_prefs());
114   InitializeList(
115       prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled,
116       5L,
117       local_state_prefs());
118   InitializeList(
119       prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled,
120       6L,
121       local_state_prefs());
122   InitializeList(
123       prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled,
124       7L,
125       local_state_prefs());
126   InitializeList(
127       prefs::kDailyOriginalContentLengthViaDataReductionProxy,
128       8L,
129       local_state_prefs());
130   InitializeList(
131       prefs::kDailyContentLengthViaDataReductionProxy,
132       9L,
133       local_state_prefs());
134
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)->
143           GetSize());
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)->
150           GetSize());
151   EXPECT_EQ(0u, profile_prefs()->GetList(
152       prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled)->
153           GetSize());
154   EXPECT_EQ(0u, profile_prefs()->GetList(
155       prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled)->
156           GetSize());
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));
164
165   data_reduction_proxy::MigrateStatisticsPrefs(local_state_prefs(),
166                                                profile_prefs());
167
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());
172   VerifyList(
173       prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled,
174       2L,
175       profile_prefs());
176   VerifyList(
177       prefs::kDailyContentLengthWithDataReductionProxyEnabled,
178       3L,
179       profile_prefs());
180   VerifyList(
181       prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled,
182       4L,
183       profile_prefs());
184   VerifyList(
185       prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled,
186       5L,
187       profile_prefs());
188   VerifyList(
189       prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled,
190       6L,
191       profile_prefs());
192   VerifyList(
193       prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled,
194       7L,
195       profile_prefs());
196   VerifyList(
197       prefs::kDailyOriginalContentLengthViaDataReductionProxy,
198       8L,
199       profile_prefs());
200   VerifyList(
201       prefs::kDailyContentLengthViaDataReductionProxy,
202       9L,
203       profile_prefs());
204   EXPECT_EQ(345L, profile_prefs()->GetInt64(
205       prefs::kDailyHttpContentLengthLastUpdateDate));
206   EXPECT_TRUE(profile_prefs()->GetBoolean(prefs::kStatisticsPrefsMigrated));
207
208   // Migration should only happen once.
209   local_state_prefs()->SetInt64(prefs::kHttpReceivedContentLength, 456L);
210   data_reduction_proxy::MigrateStatisticsPrefs(local_state_prefs(),
211                                                profile_prefs());
212   EXPECT_EQ(123L, profile_prefs()->GetInt64(prefs::kHttpReceivedContentLength));
213 }
214
215 }  // namespace data_reduction_proxy