OLD | NEW |
(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 void GetSamples(const char* histogram_name, int* bucket1, int* bucket2) { |
| 71 base::HistogramBase* histogram = |
| 72 base::StatisticsRecorder::FindHistogram(histogram_name); |
| 73 if (!histogram) { |
| 74 *bucket1 = 0; |
| 75 *bucket2 = 0; |
| 76 } else { |
| 77 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); |
| 78 *bucket1 = samples->GetCount(0); |
| 79 *bucket2 = samples->GetCount(1); |
| 80 } |
| 81 } |
| 82 |
| 83 void UpdateHistogramSamples() { |
| 84 int changed1, changed2; |
| 85 GetSamples("Settings.TrackedPreferenceChanged", &changed1, &changed2); |
| 86 pref1_changed_ = changed1 - pref1_changed_total; |
| 87 pref2_changed_ = changed2 - pref2_changed_total; |
| 88 pref1_changed_total = changed1; |
| 89 pref2_changed_total = changed2; |
| 90 |
| 91 int cleared1, cleared2; |
| 92 GetSamples("Settings.TrackedPreferenceCleared", &cleared1, &cleared2); |
| 93 pref1_cleared_ = cleared1 - pref1_cleared_total; |
| 94 pref2_cleared_ = cleared2 - pref2_cleared_total; |
| 95 pref1_cleared_total = cleared1; |
| 96 pref2_cleared_total = cleared2; |
| 97 |
| 98 int inited1, inited2; |
| 99 GetSamples("Settings.TrackedPreferenceInitialized", &inited1, &inited2); |
| 100 pref1_initialized_ = inited1 - pref1_initialized_total; |
| 101 pref2_initialized_ = inited2 - pref2_initialized_total; |
| 102 pref1_initialized_total = inited1; |
| 103 pref2_initialized_total = inited2; |
| 104 |
| 105 int unchanged1, unchanged2; |
| 106 GetSamples("Settings.TrackedPreferenceUnchanged", &unchanged1, &unchanged2); |
| 107 pref1_unchanged_ = unchanged1 - pref1_unchanged_total; |
| 108 pref2_unchanged_ = unchanged2 - pref2_unchanged_total; |
| 109 pref1_unchanged_total = unchanged1; |
| 110 pref2_unchanged_total = unchanged2; |
| 111 } |
| 112 |
| 113 TestingProfile profile_; |
| 114 TestingPrefServiceSyncable* prefs_; |
| 115 TestingPrefServiceSimple local_state_; |
| 116 |
| 117 // Since histogram samples are recorded by a global StatisticsRecorder, we |
| 118 // need to maintain total counts so we can compute deltas for individual |
| 119 // tests. |
| 120 static int pref1_changed_total; |
| 121 static int pref2_changed_total; |
| 122 static int pref1_cleared_total; |
| 123 static int pref2_cleared_total; |
| 124 static int pref1_initialized_total; |
| 125 static int pref2_initialized_total; |
| 126 static int pref1_unchanged_total; |
| 127 static int pref2_unchanged_total; |
| 128 |
| 129 // Counts of samples recorded since UpdateHistogramSamples was last called. |
| 130 int pref1_changed_; |
| 131 int pref2_changed_; |
| 132 int pref1_cleared_; |
| 133 int pref2_cleared_; |
| 134 int pref1_initialized_; |
| 135 int pref2_initialized_; |
| 136 int pref1_unchanged_; |
| 137 int pref2_unchanged_; |
| 138 }; |
| 139 |
| 140 int PrefMetricsServiceTest::pref1_changed_total; |
| 141 int PrefMetricsServiceTest::pref2_changed_total; |
| 142 int PrefMetricsServiceTest::pref1_cleared_total; |
| 143 int PrefMetricsServiceTest::pref2_cleared_total; |
| 144 int PrefMetricsServiceTest::pref1_initialized_total; |
| 145 int PrefMetricsServiceTest::pref2_initialized_total; |
| 146 int PrefMetricsServiceTest::pref1_unchanged_total; |
| 147 int PrefMetricsServiceTest::pref2_unchanged_total; |
| 148 |
| 149 TEST_F(PrefMetricsServiceTest, StartupNoUserPref) { |
| 150 // Local state is empty and no user prefs are set. We should record that we |
| 151 // checked preferences once but since there are no user preference values, no |
| 152 // other histogram data should be collected. |
| 153 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); |
| 154 UpdateHistogramSamples(); |
| 155 EXPECT_EQ(0, pref1_changed_); |
| 156 EXPECT_EQ(0, pref2_changed_); |
| 157 EXPECT_EQ(0, pref1_cleared_); |
| 158 EXPECT_EQ(0, pref2_cleared_); |
| 159 EXPECT_EQ(0, pref1_initialized_); |
| 160 EXPECT_EQ(0, pref2_initialized_); |
| 161 EXPECT_EQ(1, pref1_unchanged_); |
| 162 EXPECT_EQ(1, pref2_unchanged_); |
| 163 } |
| 164 |
| 165 TEST_F(PrefMetricsServiceTest, StartupUserPref) { |
| 166 // Local state is empty. Set a value for one tracked pref. We should record |
| 167 // that we checked preferences once and initialized a hash for the pref. |
| 168 prefs_->SetString(kTrackedPrefs[0], "foo"); |
| 169 { |
| 170 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); |
| 171 UpdateHistogramSamples(); |
| 172 EXPECT_EQ(0, pref1_changed_); |
| 173 EXPECT_EQ(0, pref2_changed_); |
| 174 EXPECT_EQ(0, pref1_cleared_); |
| 175 EXPECT_EQ(0, pref2_cleared_); |
| 176 EXPECT_EQ(1, pref1_initialized_); |
| 177 EXPECT_EQ(0, pref2_initialized_); |
| 178 EXPECT_EQ(0, pref1_unchanged_); |
| 179 EXPECT_EQ(1, pref2_unchanged_); |
| 180 |
| 181 // Change the pref. This should be observed by the PrefMetricsService, which |
| 182 // will update the hash in local_state_ to stay in sync. |
| 183 prefs_->SetString(kTrackedPrefs[0], "bar"); |
| 184 } |
| 185 // The next startup should record no changes. |
| 186 { |
| 187 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); |
| 188 UpdateHistogramSamples(); |
| 189 EXPECT_EQ(0, pref1_changed_); |
| 190 EXPECT_EQ(0, pref2_changed_); |
| 191 EXPECT_EQ(0, pref1_cleared_); |
| 192 EXPECT_EQ(0, pref2_cleared_); |
| 193 EXPECT_EQ(0, pref1_initialized_); |
| 194 EXPECT_EQ(0, pref2_initialized_); |
| 195 EXPECT_EQ(1, pref1_unchanged_); |
| 196 EXPECT_EQ(1, pref2_unchanged_); |
| 197 } |
| 198 } |
| 199 |
| 200 TEST_F(PrefMetricsServiceTest, ChangedUserPref) { |
| 201 // Local state is empty. Set a value for the tracked pref. We should record |
| 202 // that we checked preferences once and initialized a hash for the pref. |
| 203 prefs_->SetString(kTrackedPrefs[0], "foo"); |
| 204 { |
| 205 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); |
| 206 UpdateHistogramSamples(); |
| 207 EXPECT_EQ(0, pref1_changed_); |
| 208 EXPECT_EQ(0, pref2_changed_); |
| 209 EXPECT_EQ(0, pref1_cleared_); |
| 210 EXPECT_EQ(0, pref2_cleared_); |
| 211 EXPECT_EQ(1, pref1_initialized_); |
| 212 EXPECT_EQ(0, pref2_initialized_); |
| 213 EXPECT_EQ(0, pref1_unchanged_); |
| 214 EXPECT_EQ(1, pref2_unchanged_); |
| 215 // Hashed prefs should now be stored in local state. |
| 216 } |
| 217 // Change the value of the tracked pref while there is no PrefMetricsService |
| 218 // to update the hash. We should observe a pref value change. |
| 219 prefs_->SetString(kTrackedPrefs[0], "bar"); |
| 220 { |
| 221 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); |
| 222 UpdateHistogramSamples(); |
| 223 EXPECT_EQ(1, pref1_changed_); |
| 224 EXPECT_EQ(0, pref2_changed_); |
| 225 EXPECT_EQ(0, pref1_cleared_); |
| 226 EXPECT_EQ(0, pref2_cleared_); |
| 227 EXPECT_EQ(0, pref1_initialized_); |
| 228 EXPECT_EQ(0, pref2_initialized_); |
| 229 EXPECT_EQ(0, pref1_unchanged_); |
| 230 EXPECT_EQ(1, pref2_unchanged_); |
| 231 } |
| 232 // Clear the value of the tracked pref while there is no PrefMetricsService |
| 233 // to update the hash. We should observe a pref value removal. |
| 234 prefs_->ClearPref(kTrackedPrefs[0]); |
| 235 { |
| 236 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); |
| 237 UpdateHistogramSamples(); |
| 238 EXPECT_EQ(0, pref1_changed_); |
| 239 EXPECT_EQ(0, pref2_changed_); |
| 240 EXPECT_EQ(1, pref1_cleared_); |
| 241 EXPECT_EQ(0, pref2_cleared_); |
| 242 EXPECT_EQ(0, pref1_initialized_); |
| 243 EXPECT_EQ(0, pref2_initialized_); |
| 244 EXPECT_EQ(0, pref1_unchanged_); |
| 245 EXPECT_EQ(1, pref2_unchanged_); |
| 246 } |
| 247 } |
OLD | NEW |