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

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: Add TODO for Device ID API use. Created 7 years, 4 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 preferences_checked_ = 0;
34 pref1_initialized_ = 0;
35 pref2_initialized_ = 0;
36 pref1_changed_ = 0;
37 pref2_changed_ = 0;
38 pref1_removed_ = 0;
39 pref2_removed_ = 0;
40
41 base::StatisticsRecorder::Initialize();
42
43 prefs_ = profile_.GetTestingPrefService();
44
45 // Register our test-only tracked prefs as string values.
46 for (int i = 0; i < kTrackedPrefCount; ++i) {
47 prefs_->registry()->RegisterStringPref(
48 kTrackedPrefs[i],
49 "test_default_value",
50 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
51 }
52
53 // Initialize pref in local state that holds hashed values.
54 PrefMetricsService::RegisterPrefs(local_state_.registry());
55
56 // Update global counts in case another test left stray samples.
57 UpdateHistogramSamples();
58 }
59
60 scoped_ptr<PrefMetricsService> CreatePrefMetricsService() {
61 return scoped_ptr<PrefMetricsService>(
62 new PrefMetricsService(&profile_,
63 &local_state_,
64 "test_device_id",
65 kTrackedPrefs,
66 kTrackedPrefCount));
67 }
68
69 void GetSamples(const char* histogram_name, int* bucket1, int* bucket2) {
70 base::HistogramBase* histogram =
71 base::StatisticsRecorder::FindHistogram(histogram_name);
72 if (!histogram) {
73 *bucket1 = 0;
74 *bucket2 = 0;
75 } else {
76 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
77 *bucket1 = samples->GetCount(0);
78 *bucket2 = samples->GetCount(1);
79 }
80 }
81
82 void UpdateHistogramSamples() {
83 int checked1, checked2;
84 GetSamples("Settings.TrackedPreferencesChecked", &checked1, &checked2);
85 EXPECT_EQ(0, checked1);
86 preferences_checked_ = checked2 - preferences_checked_total;
87 preferences_checked_total = checked2;
88
89 int inited1, inited2;
90 GetSamples("Settings.TrackedPreferenceInitialized", &inited1, &inited2);
91 pref1_initialized_ = inited1 - pref1_initialized_total;
92 pref2_initialized_ = inited2 - pref2_initialized_total;
93 pref1_initialized_total = inited1;
94 pref2_initialized_total = inited2;
95
96 int changed1, changed2;
97 GetSamples("Settings.TrackedPreferenceChanged", &changed1, &changed2);
98 pref1_changed_ = changed1 - pref1_changed_total;
99 pref2_changed_ = changed2 - pref2_changed_total;
100 pref1_changed_total = changed1;
101 pref2_changed_total = changed2;
102
103 int removed1, removed2;
104 GetSamples("Settings.TrackedPreferenceRemoved", &removed1, &removed2);
105 pref1_removed_ = removed1 - pref1_removed_total;
106 pref2_removed_ = removed2 - pref2_removed_total;
107 pref1_removed_total = removed1;
108 pref2_removed_total = removed2;
109 }
110
111 TestingProfile profile_;
112 TestingPrefServiceSyncable* prefs_;
113 TestingPrefServiceSimple local_state_;
114
115 // Since histogram samples are recorded by a global StatisticsRecorder, we
116 // need to maintain total counts so we can compute deltas for individual
117 // tests.
118 static int preferences_checked_total;
119 static int pref1_initialized_total;
120 static int pref2_initialized_total;
121 static int pref1_changed_total;
122 static int pref2_changed_total;
123 static int pref1_removed_total;
124 static int pref2_removed_total;
125
126 // Counts of samples recorded since UpdateHistogramSamples was last called.
127 int preferences_checked_;
128 int pref1_initialized_;
129 int pref2_initialized_;
130 int pref1_changed_;
131 int pref2_changed_;
132 int pref1_removed_;
133 int pref2_removed_;
134 };
135
136 int PrefMetricsServiceTest::preferences_checked_total;
137 int PrefMetricsServiceTest::pref1_initialized_total;
138 int PrefMetricsServiceTest::pref2_initialized_total;
139 int PrefMetricsServiceTest::pref1_changed_total;
140 int PrefMetricsServiceTest::pref2_changed_total;
141 int PrefMetricsServiceTest::pref1_removed_total;
142 int PrefMetricsServiceTest::pref2_removed_total;
143
144 TEST_F(PrefMetricsServiceTest, StartupNoUserPref) {
145 // Local state is empty and no user prefs are set. We should record that we
146 // checked preferences once but since there are no user preference values, no
147 // other histogram data should be collected.
148 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
149 UpdateHistogramSamples();
150 EXPECT_EQ(1, preferences_checked_);
151 EXPECT_EQ(0, pref1_initialized_);
152 EXPECT_EQ(0, pref2_initialized_);
153 EXPECT_EQ(0, pref1_changed_);
154 EXPECT_EQ(0, pref2_changed_);
155 EXPECT_EQ(0, pref1_removed_);
156 EXPECT_EQ(0, pref2_removed_);
157 }
158
159 TEST_F(PrefMetricsServiceTest, StartupUserPref) {
160 // Local state is empty. Set a value for one tracked pref. We should record
161 // that we checked preferences once and initialized a hash for the pref.
162 prefs_->SetString(kTrackedPrefs[0], "foo");
163 {
164 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
165 UpdateHistogramSamples();
166 EXPECT_EQ(1, preferences_checked_);
167 EXPECT_EQ(1, pref1_initialized_);
168 EXPECT_EQ(0, pref2_initialized_);
169 EXPECT_EQ(0, pref1_changed_);
170 EXPECT_EQ(0, pref2_changed_);
171 EXPECT_EQ(0, pref1_removed_);
172 EXPECT_EQ(0, pref2_removed_);
173
174 // Change the pref. This should be observed by the PrefMetricsService, which
175 // will update the hash in local_state_ to stay in sync.
176 prefs_->SetString(kTrackedPrefs[0], "bar");
177 }
178 // The next startup should record no changes.
179 {
180 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
181 UpdateHistogramSamples();
182 EXPECT_EQ(1, preferences_checked_);
183 EXPECT_EQ(0, pref1_initialized_);
184 EXPECT_EQ(0, pref2_initialized_);
185 EXPECT_EQ(0, pref1_changed_);
186 EXPECT_EQ(0, pref2_changed_);
187 EXPECT_EQ(0, pref1_removed_);
188 EXPECT_EQ(0, pref2_removed_);
189 }
190 }
191
192 TEST_F(PrefMetricsServiceTest, ChangedUserPref) {
193 // Local state is empty. Set a value for the tracked pref. We should record
194 // that we checked preferences once and initialized a hash for the pref.
195 prefs_->SetString(kTrackedPrefs[0], "foo");
196 {
197 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
198 UpdateHistogramSamples();
199 EXPECT_EQ(1, preferences_checked_);
200 EXPECT_EQ(1, pref1_initialized_);
201 EXPECT_EQ(0, pref2_initialized_);
202 EXPECT_EQ(0, pref1_changed_);
203 EXPECT_EQ(0, pref2_changed_);
204 EXPECT_EQ(0, pref1_removed_);
205 EXPECT_EQ(0, pref2_removed_);
206 // Hashed prefs should now be stored in local state.
207 }
208 // Change the value of the tracked pref while there is no PrefMetricsService
209 // to update the hash. We should observe a pref value change.
210 prefs_->SetString(kTrackedPrefs[0], "bar");
211 {
212 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
213 UpdateHistogramSamples();
214 EXPECT_EQ(1, preferences_checked_);
215 EXPECT_EQ(0, pref1_initialized_);
216 EXPECT_EQ(0, pref2_initialized_);
217 EXPECT_EQ(1, pref1_changed_);
218 EXPECT_EQ(0, pref2_changed_);
219 EXPECT_EQ(0, pref1_removed_);
220 EXPECT_EQ(0, pref2_removed_);
221 }
222 // Clear the value of the tracked pref while there is no PrefMetricsService
223 // to update the hash. We should observe a pref value removal.
224 prefs_->ClearPref(kTrackedPrefs[0]);
225 {
226 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
227 UpdateHistogramSamples();
228 EXPECT_EQ(1, preferences_checked_);
229 EXPECT_EQ(0, pref1_initialized_);
230 EXPECT_EQ(0, pref2_initialized_);
231 EXPECT_EQ(0, pref1_changed_);
232 EXPECT_EQ(0, pref2_changed_);
233 EXPECT_EQ(1, pref1_removed_);
234 EXPECT_EQ(0, pref2_removed_);
235 }
236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698