Chromium Code Reviews| 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 // The name of the dictionary pref in local state where hashes are stored. | |
| 20 const char kProfilePreferenceHashes[] = "profile.preference_hashes"; | |
|
Mattias Nissler (ping if slow)
2013/08/20 12:53:54
Use the constants defined in pref_names.{h,cc} ins
bbudge
2013/08/20 18:17:50
Done.
| |
| 21 | |
| 22 static int preferences_checked_total; | |
|
Mattias Nissler (ping if slow)
2013/08/20 12:53:54
These should probably have a comment indicating th
bbudge
2013/08/20 18:17:50
Added a comment, made them member variables, and a
| |
| 23 static int pref1_initialized_total; | |
| 24 static int pref2_initialized_total; | |
| 25 static int pref1_changed_total; | |
| 26 static int pref2_changed_total; | |
| 27 static int pref1_removed_total; | |
| 28 static int pref2_removed_total; | |
| 29 | |
| 30 // TestingProfile may register some real preferences; to avoid interference, | |
| 31 // define fake preferences for testing. | |
| 32 const char* kTrackedPrefs[] = { | |
| 33 "pref_metrics_service_test.pref1", | |
| 34 "pref_metrics_service_test.pref2", | |
| 35 }; | |
| 36 | |
| 37 const int kTrackedPrefCount = arraysize(kTrackedPrefs); | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 class PrefMetricsServiceTest : public testing::Test { | |
| 42 protected: | |
| 43 virtual void SetUp() { | |
| 44 preferences_checked_ = 0; | |
| 45 pref1_initialized_ = 0; | |
| 46 pref2_initialized_ = 0; | |
| 47 pref1_changed_ = 0; | |
| 48 pref2_changed_ = 0; | |
| 49 pref1_removed_ = 0; | |
| 50 pref2_removed_ = 0; | |
| 51 | |
| 52 base::StatisticsRecorder::Initialize(); | |
| 53 | |
| 54 prefs_ = profile_.GetTestingPrefService(); | |
| 55 | |
| 56 // Register our test-only tracked prefs as string values. | |
| 57 for (int i = 0; i < kTrackedPrefCount; ++i) { | |
| 58 prefs_->registry()->RegisterStringPref( | |
| 59 kTrackedPrefs[i], | |
| 60 "test_default_value", | |
| 61 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 62 } | |
| 63 | |
| 64 // Initialize pref in local state that holds hashed values. | |
| 65 PrefMetricsService::RegisterPrefs(local_state_.registry()); | |
| 66 } | |
| 67 | |
| 68 scoped_ptr<PrefMetricsService> CreatePrefMetricsService() { | |
| 69 return scoped_ptr<PrefMetricsService>( | |
| 70 new PrefMetricsService(&profile_, | |
| 71 &local_state_, | |
| 72 "test_device_id", | |
| 73 kTrackedPrefs, | |
| 74 kTrackedPrefCount)); | |
| 75 } | |
| 76 | |
| 77 void GetSamples(const char* histogram_name, int* bucket1, int* bucket2) { | |
| 78 base::HistogramBase* histogram = | |
| 79 base::StatisticsRecorder::FindHistogram(histogram_name); | |
| 80 if (!histogram) { | |
| 81 *bucket1 = 0; | |
| 82 *bucket2 = 0; | |
| 83 } else { | |
| 84 scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples()); | |
| 85 *bucket1 = samples->GetCount(0); | |
| 86 *bucket2 = samples->GetCount(1); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void UpdateHistogramSamples() { | |
| 91 int checked1, checked2; | |
| 92 GetSamples("Settings.TrackedPreferencesChecked", &checked1, &checked2); | |
| 93 EXPECT_EQ(0, checked1); | |
| 94 preferences_checked_ = checked2 - preferences_checked_total; | |
| 95 preferences_checked_total = checked2; | |
| 96 | |
| 97 int inited1, inited2; | |
| 98 GetSamples("Settings.TrackedPreferenceInitialized", &inited1, &inited2); | |
| 99 pref1_initialized_ = inited1 - pref1_initialized_total; | |
| 100 pref2_initialized_ = inited2 - pref2_initialized_total; | |
| 101 pref1_initialized_total = inited1; | |
| 102 pref2_initialized_total = inited2; | |
| 103 | |
| 104 int changed1, changed2; | |
| 105 GetSamples("Settings.TrackedPreferenceChanged", &changed1, &changed2); | |
| 106 pref1_changed_ = changed1 - pref1_changed_total; | |
| 107 pref2_changed_ = changed2 - pref2_changed_total; | |
| 108 pref1_changed_total = changed1; | |
| 109 pref2_changed_total = changed2; | |
| 110 | |
| 111 int removed1, removed2; | |
| 112 GetSamples("Settings.TrackedPreferenceRemoved", &removed1, &removed2); | |
| 113 pref1_removed_ = removed1 - pref1_removed_total; | |
| 114 pref2_removed_ = removed2 - pref2_removed_total; | |
| 115 pref1_removed_total = removed1; | |
| 116 pref2_removed_total = removed2; | |
| 117 } | |
| 118 | |
| 119 TestingProfile profile_; | |
| 120 TestingPrefServiceSyncable* prefs_; | |
| 121 TestingPrefServiceSimple local_state_; | |
| 122 | |
| 123 int preferences_checked_; | |
| 124 int pref1_initialized_; | |
| 125 int pref2_initialized_; | |
| 126 int pref1_changed_; | |
| 127 int pref2_changed_; | |
| 128 int pref1_removed_; | |
| 129 int pref2_removed_; | |
| 130 }; | |
| 131 | |
| 132 TEST_F(PrefMetricsServiceTest, StartupNoUserPref) { | |
| 133 // Local state is empty and no user prefs are set. We should record that we | |
| 134 // checked preferences once but since there are no user preference values, no | |
| 135 // other histogram data should be collected. | |
| 136 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); | |
| 137 UpdateHistogramSamples(); | |
| 138 EXPECT_EQ(1, preferences_checked_); | |
| 139 EXPECT_EQ(0, pref1_initialized_); | |
| 140 EXPECT_EQ(0, pref2_initialized_); | |
| 141 EXPECT_EQ(0, pref1_changed_); | |
| 142 EXPECT_EQ(0, pref2_changed_); | |
| 143 EXPECT_EQ(0, pref1_removed_); | |
| 144 EXPECT_EQ(0, pref2_removed_); | |
| 145 } | |
| 146 | |
| 147 TEST_F(PrefMetricsServiceTest, StartupUserPref) { | |
| 148 // Local state is empty. Set a value for one tracked pref. We should record | |
| 149 // that we checked preferences once and initialized a hash for the pref. | |
| 150 prefs_->SetString(kTrackedPrefs[0], "foo"); | |
| 151 { | |
| 152 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); | |
| 153 UpdateHistogramSamples(); | |
| 154 EXPECT_EQ(1, preferences_checked_); | |
| 155 EXPECT_EQ(1, pref1_initialized_); | |
| 156 EXPECT_EQ(0, pref2_initialized_); | |
| 157 EXPECT_EQ(0, pref1_changed_); | |
| 158 EXPECT_EQ(0, pref2_changed_); | |
| 159 EXPECT_EQ(0, pref1_removed_); | |
| 160 EXPECT_EQ(0, pref2_removed_); | |
| 161 | |
| 162 // Change the pref. This should be observed by the PrefMetricsService, which | |
| 163 // will update the hash in local_state_ to stay in sync. | |
| 164 prefs_->SetString(kTrackedPrefs[0], "bar"); | |
| 165 } | |
| 166 // The next startup should record no changes. | |
| 167 { | |
| 168 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); | |
| 169 UpdateHistogramSamples(); | |
| 170 EXPECT_EQ(1, preferences_checked_); | |
| 171 EXPECT_EQ(0, pref1_initialized_); | |
| 172 EXPECT_EQ(0, pref2_initialized_); | |
| 173 EXPECT_EQ(0, pref1_changed_); | |
| 174 EXPECT_EQ(0, pref2_changed_); | |
| 175 EXPECT_EQ(0, pref1_removed_); | |
| 176 EXPECT_EQ(0, pref2_removed_); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 TEST_F(PrefMetricsServiceTest, ChangedUserPref) { | |
| 181 // Local state is empty. Set a value for the tracked pref. We should record | |
| 182 // that we checked preferences once and initialized a hash for the pref. | |
| 183 prefs_->SetString(kTrackedPrefs[0], "foo"); | |
| 184 { | |
| 185 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); | |
| 186 UpdateHistogramSamples(); | |
| 187 EXPECT_EQ(1, preferences_checked_); | |
| 188 EXPECT_EQ(1, pref1_initialized_); | |
| 189 EXPECT_EQ(0, pref2_initialized_); | |
| 190 EXPECT_EQ(0, pref1_changed_); | |
| 191 EXPECT_EQ(0, pref2_changed_); | |
| 192 EXPECT_EQ(0, pref1_removed_); | |
| 193 EXPECT_EQ(0, pref2_removed_); | |
| 194 // Hashed prefs should now be stored in local state. | |
| 195 } | |
| 196 // Change the value of the tracked pref while there is no PrefMetricsService | |
| 197 // to update the hash. We should observe a pref value change. | |
| 198 prefs_->SetString(kTrackedPrefs[0], "bar"); | |
| 199 { | |
| 200 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); | |
| 201 UpdateHistogramSamples(); | |
| 202 EXPECT_EQ(1, preferences_checked_); | |
| 203 EXPECT_EQ(0, pref1_initialized_); | |
| 204 EXPECT_EQ(0, pref2_initialized_); | |
| 205 EXPECT_EQ(1, pref1_changed_); | |
| 206 EXPECT_EQ(0, pref2_changed_); | |
| 207 EXPECT_EQ(0, pref1_removed_); | |
| 208 EXPECT_EQ(0, pref2_removed_); | |
| 209 } | |
| 210 // Clear the value of the tracked pref while there is no PrefMetricsService | |
| 211 // to update the hash. We should observe a pref value removal. | |
| 212 prefs_->ClearPref(kTrackedPrefs[0]); | |
| 213 { | |
| 214 scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService(); | |
| 215 UpdateHistogramSamples(); | |
| 216 EXPECT_EQ(1, preferences_checked_); | |
| 217 EXPECT_EQ(0, pref1_initialized_); | |
| 218 EXPECT_EQ(0, pref2_initialized_); | |
| 219 EXPECT_EQ(0, pref1_changed_); | |
| 220 EXPECT_EQ(0, pref2_changed_); | |
| 221 EXPECT_EQ(1, pref1_removed_); | |
| 222 EXPECT_EQ(0, pref2_removed_); | |
| 223 } | |
| 224 } | |
| 225 | |
|
Mattias Nissler (ping if slow)
2013/08/20 12:53:54
nit: remove trailing newline.
bbudge
2013/08/20 18:17:50
Done.
| |
| OLD | NEW |