Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/prefs/pref_metrics_service_unittest.cc

Issue 22676002: Add UMA to report Preferences File Corruption (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix Windows compile. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013 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/memory/scoped_ptr.h"
6 #include "base/metrics/histogram.h"
7 #include "base/metrics/statistics_recorder.h"
8 #include "base/prefs/testing_pref_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/prefs/pref_metrics_service.h"
11 #include "chrome/browser/prefs/scoped_user_pref_update.h"
12 #include "chrome/test/base/testing_pref_service_syncable.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/user_prefs/pref_registry_syncable.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace {
18
19 // TestingProfile may register some real preferences; to avoid interference,
20 // define fake preferences for testing.
21 const char* kTrackedPrefs[] = {
22 "pref_metrics_service_test.pref1",
23 "pref_metrics_service_test.pref2",
24 };
25
26 const int kTrackedPrefCount = arraysize(kTrackedPrefs);
27
28 } // namespace
29
30 class PrefMetricsServiceTest : public testing::Test {
31 protected:
32 virtual void SetUp() {
33 pref1_changed_ = 0;
34 pref2_changed_ = 0;
35 pref1_cleared_ = 0;
36 pref2_cleared_ = 0;
37 pref1_initialized_ = 0;
38 pref2_initialized_ = 0;
39 pref1_unchanged_ = 0;
40 pref2_unchanged_ = 0;
41
42 base::StatisticsRecorder::Initialize();
43
44 prefs_ = profile_.GetTestingPrefService();
45
46 // Register our test-only tracked prefs as string values.
47 for (int i = 0; i < kTrackedPrefCount; ++i) {
48 prefs_->registry()->RegisterStringPref(
49 kTrackedPrefs[i],
50 "test_default_value",
51 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
52 }
53
54 // Initialize pref in local state that holds hashed values.
55 PrefMetricsService::RegisterPrefs(local_state_.registry());
56
57 // Update global counts in case another test left stray samples.
58 UpdateHistogramSamples();
59 }
60
61 scoped_ptr<PrefMetricsService> CreatePrefMetricsService() {
62 return scoped_ptr<PrefMetricsService>(
63 new PrefMetricsService(&profile_,
64 &local_state_,
65 "test_device_id",
66 kTrackedPrefs,
67 kTrackedPrefCount));
68 }
69
70 std::string GetHashedPrefValue(PrefMetricsService* service,
71 const char* path, base::Value* value) {
72 return service->GetHashedPrefValue(path, value);
73 }
74
75 void GetSamples(const char* histogram_name, int* bucket1, int* bucket2) {
76 base::HistogramBase* histogram =
77 base::StatisticsRecorder::FindHistogram(histogram_name);
78 if (!histogram) {
79 *bucket1 = 0;
80 *bucket2 = 0;
81 } else {
82 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
83 *bucket1 = samples->GetCount(0);
84 *bucket2 = samples->GetCount(1);
85 }
86 }
87
88 void UpdateHistogramSamples() {
89 int changed1, changed2;
90 GetSamples("Settings.TrackedPreferenceChanged", &changed1, &changed2);
91 pref1_changed_ = changed1 - pref1_changed_total;
92 pref2_changed_ = changed2 - pref2_changed_total;
93 pref1_changed_total = changed1;
94 pref2_changed_total = changed2;
95
96 int cleared1, cleared2;
97 GetSamples("Settings.TrackedPreferenceCleared", &cleared1, &cleared2);
98 pref1_cleared_ = cleared1 - pref1_cleared_total;
99 pref2_cleared_ = cleared2 - pref2_cleared_total;
100 pref1_cleared_total = cleared1;
101 pref2_cleared_total = cleared2;
102
103 int inited1, inited2;
104 GetSamples("Settings.TrackedPreferenceInitialized", &inited1, &inited2);
105 pref1_initialized_ = inited1 - pref1_initialized_total;
106 pref2_initialized_ = inited2 - pref2_initialized_total;
107 pref1_initialized_total = inited1;
108 pref2_initialized_total = inited2;
109
110 int unchanged1, unchanged2;
111 GetSamples("Settings.TrackedPreferenceUnchanged", &unchanged1, &unchanged2);
112 pref1_unchanged_ = unchanged1 - pref1_unchanged_total;
113 pref2_unchanged_ = unchanged2 - pref2_unchanged_total;
114 pref1_unchanged_total = unchanged1;
115 pref2_unchanged_total = unchanged2;
116 }
117
118 TestingProfile profile_;
119 TestingPrefServiceSyncable* prefs_;
120 TestingPrefServiceSimple local_state_;
121
122 // Since histogram samples are recorded by a global StatisticsRecorder, we
123 // need to maintain total counts so we can compute deltas for individual
124 // tests.
125 static int pref1_changed_total;
126 static int pref2_changed_total;
127 static int pref1_cleared_total;
128 static int pref2_cleared_total;
129 static int pref1_initialized_total;
130 static int pref2_initialized_total;
131 static int pref1_unchanged_total;
132 static int pref2_unchanged_total;
133
134 // Counts of samples recorded since UpdateHistogramSamples was last called.
135 int pref1_changed_;
136 int pref2_changed_;
137 int pref1_cleared_;
138 int pref2_cleared_;
139 int pref1_initialized_;
140 int pref2_initialized_;
141 int pref1_unchanged_;
142 int pref2_unchanged_;
143 };
144
145 int PrefMetricsServiceTest::pref1_changed_total;
146 int PrefMetricsServiceTest::pref2_changed_total;
147 int PrefMetricsServiceTest::pref1_cleared_total;
148 int PrefMetricsServiceTest::pref2_cleared_total;
149 int PrefMetricsServiceTest::pref1_initialized_total;
150 int PrefMetricsServiceTest::pref2_initialized_total;
151 int PrefMetricsServiceTest::pref1_unchanged_total;
152 int PrefMetricsServiceTest::pref2_unchanged_total;
153
154 TEST_F(PrefMetricsServiceTest, StartupNoUserPref) {
155 // Local state is empty and no user prefs are set. We should record that we
156 // checked preferences once but since there are no user preference values, no
157 // other histogram data should be collected.
158 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
159 UpdateHistogramSamples();
160 EXPECT_EQ(0, pref1_changed_);
161 EXPECT_EQ(0, pref2_changed_);
162 EXPECT_EQ(0, pref1_cleared_);
163 EXPECT_EQ(0, pref2_cleared_);
164 EXPECT_EQ(0, pref1_initialized_);
165 EXPECT_EQ(0, pref2_initialized_);
166 EXPECT_EQ(1, pref1_unchanged_);
167 EXPECT_EQ(1, pref2_unchanged_);
168 }
169
170 TEST_F(PrefMetricsServiceTest, StartupUserPref) {
171 // Local state is empty. Set a value for one tracked pref. We should record
172 // that we checked preferences once and initialized a hash for the pref.
173 prefs_->SetString(kTrackedPrefs[0], "foo");
174 {
175 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
176 UpdateHistogramSamples();
177 EXPECT_EQ(0, pref1_changed_);
178 EXPECT_EQ(0, pref2_changed_);
179 EXPECT_EQ(0, pref1_cleared_);
180 EXPECT_EQ(0, pref2_cleared_);
181 EXPECT_EQ(1, pref1_initialized_);
182 EXPECT_EQ(0, pref2_initialized_);
183 EXPECT_EQ(0, pref1_unchanged_);
184 EXPECT_EQ(1, pref2_unchanged_);
185
186 // Change the pref. This should be observed by the PrefMetricsService, which
187 // will update the hash in local_state_ to stay in sync.
188 prefs_->SetString(kTrackedPrefs[0], "bar");
189 }
190 // The next startup should record no changes.
191 {
192 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
193 UpdateHistogramSamples();
194 EXPECT_EQ(0, pref1_changed_);
195 EXPECT_EQ(0, pref2_changed_);
196 EXPECT_EQ(0, pref1_cleared_);
197 EXPECT_EQ(0, pref2_cleared_);
198 EXPECT_EQ(0, pref1_initialized_);
199 EXPECT_EQ(0, pref2_initialized_);
200 EXPECT_EQ(1, pref1_unchanged_);
201 EXPECT_EQ(1, pref2_unchanged_);
202 }
203 }
204
205 TEST_F(PrefMetricsServiceTest, ChangedUserPref) {
206 // Local state is empty. Set a value for the tracked pref. We should record
207 // that we checked preferences once and initialized a hash for the pref.
208 prefs_->SetString(kTrackedPrefs[0], "foo");
209 {
210 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
211 UpdateHistogramSamples();
212 EXPECT_EQ(0, pref1_changed_);
213 EXPECT_EQ(0, pref2_changed_);
214 EXPECT_EQ(0, pref1_cleared_);
215 EXPECT_EQ(0, pref2_cleared_);
216 EXPECT_EQ(1, pref1_initialized_);
217 EXPECT_EQ(0, pref2_initialized_);
218 EXPECT_EQ(0, pref1_unchanged_);
219 EXPECT_EQ(1, pref2_unchanged_);
220 // Hashed prefs should now be stored in local state.
221 }
222 // Change the value of the tracked pref while there is no PrefMetricsService
223 // to update the hash. We should observe a pref value change.
224 prefs_->SetString(kTrackedPrefs[0], "bar");
225 {
226 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
227 UpdateHistogramSamples();
228 EXPECT_EQ(1, pref1_changed_);
229 EXPECT_EQ(0, pref2_changed_);
230 EXPECT_EQ(0, pref1_cleared_);
231 EXPECT_EQ(0, pref2_cleared_);
232 EXPECT_EQ(0, pref1_initialized_);
233 EXPECT_EQ(0, pref2_initialized_);
234 EXPECT_EQ(0, pref1_unchanged_);
235 EXPECT_EQ(1, pref2_unchanged_);
236 }
237 // Clear the value of the tracked pref while there is no PrefMetricsService
238 // to update the hash. We should observe a pref value removal.
239 prefs_->ClearPref(kTrackedPrefs[0]);
240 {
241 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
242 UpdateHistogramSamples();
243 EXPECT_EQ(0, pref1_changed_);
244 EXPECT_EQ(0, pref2_changed_);
245 EXPECT_EQ(1, pref1_cleared_);
246 EXPECT_EQ(0, pref2_cleared_);
247 EXPECT_EQ(0, pref1_initialized_);
248 EXPECT_EQ(0, pref2_initialized_);
249 EXPECT_EQ(0, pref1_unchanged_);
250 EXPECT_EQ(1, pref2_unchanged_);
251 }
252 }
253
254 // Tests that serialization of dictionary values is stable. If the order of
255 // the entries or any whitespace changes, it would cause a spike in pref change
256 // UMA events as every hash would change.
257 TEST_F(PrefMetricsServiceTest, PrefHashStability) {
258 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
259 DictionaryValue dict;
260 dict.Set("a", new StringValue("foo"));
261 dict.Set("b", new StringValue("bar"));
262 dict.Set("c", new StringValue("baz"));
263 dict.Set("d", new StringValue("bad"));
battre 2013/09/06 11:35:33 Please move this line 3 lines up to confirm that t
bbudge 2013/09/06 13:31:34 Done.
264 std::string hash = GetHashedPrefValue(test.get(), "pref.path", &dict);
265 EXPECT_EQ("C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2",
266 hash);
267 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698