Index: chrome/browser/metrics/chrome_setup_metrics_provider_unittest.cc |
diff --git a/chrome/browser/metrics/chrome_setup_metrics_provider_unittest.cc b/chrome/browser/metrics/chrome_setup_metrics_provider_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..28012d5ed87c875d9602266bf2971e23732979b4 |
--- /dev/null |
+++ b/chrome/browser/metrics/chrome_setup_metrics_provider_unittest.cc |
@@ -0,0 +1,87 @@ |
+// Copyright 2015 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 "chrome/browser/metrics/chrome_setup_metrics_provider.h" |
+ |
+#include "base/basictypes.h" |
+#include "base/files/file_util.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/metrics/histogram.h" |
+#include "base/metrics/histogram_persistence.h" |
+#include "base/metrics/persistent_memory_allocator.h" |
+#include "base/prefs/pref_registry_simple.h" |
+#include "base/prefs/testing_pref_service.h" |
+#include "chrome/installer/util/google_update_constants.h" |
+#include "components/metrics/metrics_pref_names.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using namespace base; |
+ |
+class ChromeSetupMetricsProviderTest : public testing::Test { |
+ protected: |
+ ChromeSetupMetricsProviderTest() : prefs_(new TestingPrefServiceSimple) { |
+ EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
+ prefs_->registry()->RegisterInt64Pref(metrics::prefs::kSetupMetricsLastSeen, |
+ 0); |
+ } |
+ |
+ TestingPrefServiceSimple* prefs() { return prefs_.get(); } |
+ FilePath temp_dir() { return temp_dir_.path(); } |
+ FilePath metrics_file() { |
+ return temp_dir_.path().AppendASCII( |
+ google_update::kSetupHistogramAllocatorName); |
+ } |
+ |
+ ChromeSetupMetricsProvider* provider() { |
+ if (!provider_) { |
+ provider_.reset(new ChromeSetupMetricsProvider(nullptr, prefs())); |
+ provider_->SetProgramDirectory(temp_dir()); |
+ } |
+ return provider_.get(); |
+ } |
+ |
+ private: |
+ ScopedTempDir temp_dir_; |
+ scoped_ptr<TestingPrefServiceSimple> prefs_; |
+ scoped_ptr<ChromeSetupMetricsProvider> provider_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChromeSetupMetricsProviderTest); |
+}; |
+ |
+ |
+TEST_F(ChromeSetupMetricsProviderTest, AccessMetrics) { |
+ ASSERT_FALSE(PathExists(metrics_file())); |
+ |
+ { |
+ SetPersistentHistogramMemoryAllocator( |
+ new LocalPersistentMemoryAllocator( |
+ 64 << 10, 0, google_update::kSetupHistogramAllocatorName)); |
+ HistogramBase* foo = Histogram::FactoryGet("foo", 1, 100, 10, 0); |
+ HistogramBase* bar = Histogram::FactoryGet("bar", 1, 100, 10, 0); |
+ foo->Add(42); |
+ bar->Add(84); |
+ |
+ scoped_ptr<PersistentMemoryAllocator> allocator( |
+ ReleasePersistentHistogramMemoryAllocator()); |
+ File writer(metrics_file(), File::FLAG_CREATE | File::FLAG_WRITE); |
+ EXPECT_TRUE(writer.IsValid()); |
+ writer.Write(0, (const char*)allocator->data(), allocator->used()); |
+ } |
+ |
+ ASSERT_TRUE(PathExists(metrics_file())); |
+ EXPECT_FALSE(provider()->metrics_found_.load()); |
+ EXPECT_FALSE(provider()->metrics_file_); |
+ provider()->CheckForSetupMetrics(); |
+ EXPECT_TRUE(provider()->metrics_found_.load()); |
+ EXPECT_TRUE(provider()->metrics_file_); |
+ |
+ EXPECT_FALSE(provider()->metrics_allocator_); |
+ provider()->OnDidCreateMetricsLog(); |
+ EXPECT_TRUE(provider()->metrics_allocator_); |
+ EXPECT_TRUE(provider()->metrics_found_.load()); |
+ EXPECT_FALSE(provider()->metrics_file_); |
+ provider()->OnDidCreateMetricsLog(); |
+ EXPECT_FALSE(provider()->metrics_allocator_); |
+ EXPECT_FALSE(provider()->metrics_found_.load()); |
+} |