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

Unified 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: Rebase. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/prefs/pref_metrics_service_unittest.cc
diff --git a/chrome/browser/prefs/pref_metrics_service_unittest.cc b/chrome/browser/prefs/pref_metrics_service_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..3ce9994345ea21f44a2ddcadfac5a90363542443
--- /dev/null
+++ b/chrome/browser/prefs/pref_metrics_service_unittest.cc
@@ -0,0 +1,225 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/memory/scoped_ptr.h"
+#include "base/metrics/histogram.h"
+#include "base/metrics/statistics_recorder.h"
+#include "base/prefs/testing_pref_service.h"
+#include "base/values.h"
+#include "chrome/browser/prefs/pref_metrics_service.h"
+#include "chrome/browser/prefs/scoped_user_pref_update.h"
+#include "chrome/test/base/testing_pref_service_syncable.h"
+#include "chrome/test/base/testing_profile.h"
+#include "components/user_prefs/pref_registry_syncable.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+// The name of the dictionary pref in local state where hashes are stored.
+const char kProfilePreferenceHashes[] = "profile.preference_hashes";
+
+static int preferences_checked_total;
+static int pref1_initialized_total;
+static int pref2_initialized_total;
+static int pref1_changed_total;
+static int pref2_changed_total;
+static int pref1_removed_total;
+static int pref2_removed_total;
+
+// TestingProfile may register some real preferences; to avoid interference,
+// define fake preferences for testing.
+const char* kTrackedPrefs[] = {
+ "pref_metrics_service_test.pref1",
+ "pref_metrics_service_test.pref2",
+};
+
+const int kTrackedPrefCount = arraysize(kTrackedPrefs);
+
+} // namespace
+
+class PrefMetricsServiceTest : public testing::Test {
+ protected:
+ virtual void SetUp() {
+ preferences_checked_ = 0;
+ pref1_initialized_ = 0;
+ pref2_initialized_ = 0;
+ pref1_changed_ = 0;
+ pref2_changed_ = 0;
+ pref1_removed_ = 0;
+ pref2_removed_ = 0;
+
+ base::StatisticsRecorder::Initialize();
+
+ prefs_ = profile_.GetTestingPrefService();
+
+ // Register our test-only tracked prefs as string values.
+ for (int i = 0; i < kTrackedPrefCount; ++i) {
+ prefs_->registry()->RegisterStringPref(
+ kTrackedPrefs[i],
+ "test_default_value",
+ user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
+ }
+
+ // Initialize pref in local state that holds hashed values.
+ PrefMetricsService::RegisterPrefs(local_state_.registry());
+ }
+
+ scoped_ptr<PrefMetricsService> CreatePrefMetricsService() {
+ return scoped_ptr<PrefMetricsService>(
+ new PrefMetricsService(&profile_,
+ &local_state_,
+ "test_device_id",
+ kTrackedPrefs,
+ kTrackedPrefCount));
+ }
+
+ void GetSamples(const char* histogram_name, int* bucket1, int* bucket2) {
+ base::HistogramBase* histogram =
+ base::StatisticsRecorder::FindHistogram(histogram_name);
+ if (!histogram) {
+ *bucket1 = 0;
+ *bucket2 = 0;
+ } else {
+ scoped_ptr<base::HistogramSamples> samples(histogram->SnapshotSamples());
+ *bucket1 = samples->GetCount(0);
+ *bucket2 = samples->GetCount(1);
+ }
+ }
+
+ void UpdateHistogramSamples() {
+ int checked1, checked2;
+ GetSamples("Settings.TrackedPreferencesChecked", &checked1, &checked2);
+ EXPECT_EQ(0, checked1);
+ preferences_checked_ = checked2 - preferences_checked_total;
+ preferences_checked_total = checked2;
+
+ int inited1, inited2;
+ GetSamples("Settings.TrackedPreferenceInitialized", &inited1, &inited2);
+ pref1_initialized_ = inited1 - pref1_initialized_total;
+ pref2_initialized_ = inited2 - pref2_initialized_total;
+ pref1_initialized_total = inited1;
+ pref2_initialized_total = inited2;
+
+ int changed1, changed2;
+ GetSamples("Settings.TrackedPreferenceChanged", &changed1, &changed2);
+ pref1_changed_ = changed1 - pref1_changed_total;
+ pref2_changed_ = changed2 - pref2_changed_total;
+ pref1_changed_total = changed1;
+ pref2_changed_total = changed2;
+
+ int removed1, removed2;
+ GetSamples("Settings.TrackedPreferenceRemoved", &removed1, &removed2);
+ pref1_removed_ = removed1 - pref1_removed_total;
+ pref2_removed_ = removed2 - pref2_removed_total;
+ pref1_removed_total = removed1;
+ pref2_removed_total = removed2;
+ }
+
+ TestingProfile profile_;
+ TestingPrefServiceSyncable* prefs_;
+ TestingPrefServiceSimple local_state_;
+
+ int preferences_checked_;
+ int pref1_initialized_;
+ int pref2_initialized_;
+ int pref1_changed_;
+ int pref2_changed_;
+ int pref1_removed_;
+ int pref2_removed_;
+};
+
+TEST_F(PrefMetricsServiceTest, StartupNoUserPref) {
+ // Local state is empty and no user prefs are set. We should record that we
+ // checked preferences once but since there are no user preference values, no
+ // other histogram data should be collected.
+ scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
+ UpdateHistogramSamples();
+ EXPECT_EQ(1, preferences_checked_);
+ EXPECT_EQ(0, pref1_initialized_);
+ EXPECT_EQ(0, pref2_initialized_);
+ EXPECT_EQ(0, pref1_changed_);
+ EXPECT_EQ(0, pref2_changed_);
+ EXPECT_EQ(0, pref1_removed_);
+ EXPECT_EQ(0, pref2_removed_);
+}
+
+TEST_F(PrefMetricsServiceTest, StartupUserPref) {
+ // Local state is empty. Set a value for one tracked pref. We should record
+ // that we checked preferences once and initialized a hash for the pref.
+ prefs_->SetString(kTrackedPrefs[0], "foo");
+ {
+ scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
+ UpdateHistogramSamples();
+ EXPECT_EQ(1, preferences_checked_);
+ EXPECT_EQ(1, pref1_initialized_);
+ EXPECT_EQ(0, pref2_initialized_);
+ EXPECT_EQ(0, pref1_changed_);
+ EXPECT_EQ(0, pref2_changed_);
+ EXPECT_EQ(0, pref1_removed_);
+ EXPECT_EQ(0, pref2_removed_);
+
+ // Change the pref. This should be observed by the PrefMetricsService, which
+ // will update the hash in local_state_ to stay in sync.
+ prefs_->SetString(kTrackedPrefs[0], "bar");
+ }
+ // The next startup should record no changes.
+ {
+ scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
+ UpdateHistogramSamples();
+ EXPECT_EQ(1, preferences_checked_);
+ EXPECT_EQ(0, pref1_initialized_);
+ EXPECT_EQ(0, pref2_initialized_);
+ EXPECT_EQ(0, pref1_changed_);
+ EXPECT_EQ(0, pref2_changed_);
+ EXPECT_EQ(0, pref1_removed_);
+ EXPECT_EQ(0, pref2_removed_);
+ }
+}
+
+TEST_F(PrefMetricsServiceTest, ChangedUserPref) {
+ // Local state is empty. Set a value for the tracked pref. We should record
+ // that we checked preferences once and initialized a hash for the pref.
+ prefs_->SetString(kTrackedPrefs[0], "foo");
+ {
+ scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
+ UpdateHistogramSamples();
+ EXPECT_EQ(1, preferences_checked_);
+ EXPECT_EQ(1, pref1_initialized_);
+ EXPECT_EQ(0, pref2_initialized_);
+ EXPECT_EQ(0, pref1_changed_);
+ EXPECT_EQ(0, pref2_changed_);
+ EXPECT_EQ(0, pref1_removed_);
+ EXPECT_EQ(0, pref2_removed_);
+ // Hashed prefs should now be stored in local state.
+ }
+ // Change the value of the tracked pref while there is no PrefMetricsService
+ // to update the hash. We should observe a pref value change.
+ prefs_->SetString(kTrackedPrefs[0], "bar");
+ {
+ scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
+ UpdateHistogramSamples();
+ EXPECT_EQ(1, preferences_checked_);
+ EXPECT_EQ(0, pref1_initialized_);
+ EXPECT_EQ(0, pref2_initialized_);
+ EXPECT_EQ(1, pref1_changed_);
+ EXPECT_EQ(0, pref2_changed_);
+ EXPECT_EQ(0, pref1_removed_);
+ EXPECT_EQ(0, pref2_removed_);
+ }
+ // Clear the value of the tracked pref while there is no PrefMetricsService
+ // to update the hash. We should observe a pref value removal.
+ prefs_->ClearPref(kTrackedPrefs[0]);
+ {
+ scoped_ptr<PrefMetricsService> test = CreatePrefMetricsService();
+ UpdateHistogramSamples();
+ EXPECT_EQ(1, preferences_checked_);
+ EXPECT_EQ(0, pref1_initialized_);
+ EXPECT_EQ(0, pref2_initialized_);
+ EXPECT_EQ(0, pref1_changed_);
+ EXPECT_EQ(0, pref2_changed_);
+ EXPECT_EQ(1, pref1_removed_);
+ EXPECT_EQ(0, pref2_removed_);
+ }
+}
+

Powered by Google App Engine
This is Rietveld 408576698