| Index: components/metrics/file_metrics_provider_unittest.cc
|
| diff --git a/components/metrics/file_metrics_provider_unittest.cc b/components/metrics/file_metrics_provider_unittest.cc
|
| index 9781cea05a36e2e50059c50b022237c5c7dce404..2b6fbdbacb60e18aed6551767567adc9d5fb9eea 100644
|
| --- a/components/metrics/file_metrics_provider_unittest.cc
|
| +++ b/components/metrics/file_metrics_provider_unittest.cc
|
| @@ -4,6 +4,8 @@
|
|
|
| #include "components/metrics/file_metrics_provider.h"
|
|
|
| +#include <functional>
|
| +
|
| #include "base/files/file_util.h"
|
| #include "base/files/memory_mapped_file.h"
|
| #include "base/files/scoped_temp_dir.h"
|
| @@ -20,6 +22,8 @@
|
| #include "base/threading/thread_task_runner_handle.h"
|
| #include "base/time/time.h"
|
| #include "components/metrics/metrics_pref_names.h"
|
| +#include "components/metrics/persistent_system_profile.h"
|
| +#include "components/metrics/proto/system_profile.pb.h"
|
| #include "components/prefs/pref_registry_simple.h"
|
| #include "components/prefs/testing_pref_service.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -118,6 +122,13 @@ class FileMetricsProviderTest : public testing::TestWithParam<bool> {
|
| provider()->MergeHistogramDeltas();
|
| }
|
|
|
| + bool ProvideIndependentMetrics(
|
| + SystemProfileProto* profile_proto,
|
| + base::HistogramSnapshotManager* snapshot_manager) {
|
| + return provider()->ProvideIndependentMetrics(profile_proto,
|
| + snapshot_manager);
|
| + }
|
| +
|
| void RecordInitialHistogramSnapshots(
|
| base::HistogramSnapshotManager* snapshot_manager) {
|
| provider()->RecordInitialHistogramSnapshots(snapshot_manager);
|
| @@ -176,7 +187,10 @@ class FileMetricsProviderTest : public testing::TestWithParam<bool> {
|
| }
|
|
|
| std::unique_ptr<base::PersistentHistogramAllocator>
|
| - CreateMetricsFileWithHistograms(int histogram_count) {
|
| + CreateMetricsFileWithHistograms(
|
| + int histogram_count,
|
| + const std::function<void(base::PersistentHistogramAllocator*)>&
|
| + callback) {
|
| // Get this first so it isn't created inside the persistent allocator.
|
| base::GlobalHistogramAllocator::GetCreateHistogramResultHistogram();
|
|
|
| @@ -188,10 +202,18 @@ class FileMetricsProviderTest : public testing::TestWithParam<bool> {
|
|
|
| std::unique_ptr<base::PersistentHistogramAllocator> histogram_allocator =
|
| base::GlobalHistogramAllocator::ReleaseForTesting();
|
| + callback(histogram_allocator.get());
|
| +
|
| WriteMetricsFile(metrics_file(), histogram_allocator.get());
|
| return histogram_allocator;
|
| }
|
|
|
| + std::unique_ptr<base::PersistentHistogramAllocator>
|
| + CreateMetricsFileWithHistograms(int histogram_count) {
|
| + return CreateMetricsFileWithHistograms(
|
| + histogram_count, [](base::PersistentHistogramAllocator* allocator) {});
|
| + }
|
| +
|
| base::HistogramBase* GetCreatedHistogram(int index) {
|
| DCHECK_GT(kMaxCreateHistograms, index);
|
| return created_histograms_[index];
|
| @@ -411,7 +433,7 @@ TEST_P(FileMetricsProviderTest, AccessInitialMetrics) {
|
| kMetricsName);
|
|
|
| // Record embedded snapshots via snapshot-manager.
|
| - HasInitialStabilityMetrics();
|
| + ASSERT_TRUE(HasInitialStabilityMetrics());
|
| RunTasks();
|
| {
|
| HistogramFlattenerDeltaRecorder flattener;
|
| @@ -435,4 +457,144 @@ TEST_P(FileMetricsProviderTest, AccessInitialMetrics) {
|
| EXPECT_TRUE(base::PathExists(metrics_file()));
|
| }
|
|
|
| +TEST_P(FileMetricsProviderTest, AccessEmbeddedProfileMetricsWithoutProfile) {
|
| + ASSERT_FALSE(PathExists(metrics_file()));
|
| + CreateMetricsFileWithHistograms(2);
|
| +
|
| + // Register the file and allow the "checker" task to run.
|
| + ASSERT_TRUE(PathExists(metrics_file()));
|
| + provider()->RegisterSource(
|
| + metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE,
|
| + FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE, kMetricsName);
|
| +
|
| + // Record embedded snapshots via snapshot-manager.
|
| + OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + SystemProfileProto profile;
|
| +
|
| + // A read of metrics with internal profiles should return nothing.
|
| + EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager));
|
| + }
|
| + EXPECT_TRUE(base::PathExists(metrics_file()));
|
| + OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + EXPECT_FALSE(base::PathExists(metrics_file()));
|
| +}
|
| +
|
| +TEST_P(FileMetricsProviderTest, AccessEmbeddedProfileMetricsWithProfile) {
|
| + ASSERT_FALSE(PathExists(metrics_file()));
|
| + CreateMetricsFileWithHistograms(
|
| + 2, [](base::PersistentHistogramAllocator* allocator) {
|
| + SystemProfileProto profile_proto;
|
| + SystemProfileProto::FieldTrial* trial = profile_proto.add_field_trial();
|
| + trial->set_name_id(123);
|
| + trial->set_group_id(456);
|
| +
|
| + PersistentSystemProfile persistent_profile;
|
| + persistent_profile.RegisterPersistentAllocator(
|
| + allocator->memory_allocator());
|
| + persistent_profile.SetSystemProfile(profile_proto);
|
| + });
|
| +
|
| + // Register the file and allow the "checker" task to run.
|
| + ASSERT_TRUE(PathExists(metrics_file()));
|
| + provider()->RegisterSource(
|
| + metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE,
|
| + FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE, kMetricsName);
|
| +
|
| + // Record embedded snapshots via snapshot-manager.
|
| + OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + RecordInitialHistogramSnapshots(&snapshot_manager);
|
| + EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size());
|
| +
|
| + // A read of metrics with internal profiles should return one result.
|
| + SystemProfileProto profile;
|
| + EXPECT_TRUE(ProvideIndependentMetrics(&profile, &snapshot_manager));
|
| + EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager));
|
| + }
|
| + EXPECT_TRUE(base::PathExists(metrics_file()));
|
| + OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + EXPECT_FALSE(base::PathExists(metrics_file()));
|
| +}
|
| +
|
| +TEST_P(FileMetricsProviderTest, AccessEmbeddedFallbackMetricsWithoutProfile) {
|
| + ASSERT_FALSE(PathExists(metrics_file()));
|
| + CreateMetricsFileWithHistograms(2);
|
| +
|
| + // Register the file and allow the "checker" task to run.
|
| + ASSERT_TRUE(PathExists(metrics_file()));
|
| + provider()->RegisterSource(
|
| + metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE,
|
| + FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE_OR_PREVIOUS_RUN,
|
| + kMetricsName);
|
| +
|
| + // Record embedded snapshots via snapshot-manager.
|
| + ASSERT_TRUE(HasInitialStabilityMetrics());
|
| + RunTasks();
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + RecordInitialHistogramSnapshots(&snapshot_manager);
|
| + EXPECT_EQ(2U, flattener.GetRecordedDeltaHistogramNames().size());
|
| +
|
| + // A read of metrics with internal profiles should return nothing.
|
| + SystemProfileProto profile;
|
| + EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager));
|
| + }
|
| + EXPECT_TRUE(base::PathExists(metrics_file()));
|
| + OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + EXPECT_FALSE(base::PathExists(metrics_file()));
|
| +}
|
| +
|
| +TEST_P(FileMetricsProviderTest, AccessEmbeddedFallbackMetricsWithProfile) {
|
| + ASSERT_FALSE(PathExists(metrics_file()));
|
| + CreateMetricsFileWithHistograms(
|
| + 2, [](base::PersistentHistogramAllocator* allocator) {
|
| + SystemProfileProto profile_proto;
|
| + SystemProfileProto::FieldTrial* trial = profile_proto.add_field_trial();
|
| + trial->set_name_id(123);
|
| + trial->set_group_id(456);
|
| +
|
| + PersistentSystemProfile persistent_profile;
|
| + persistent_profile.RegisterPersistentAllocator(
|
| + allocator->memory_allocator());
|
| + persistent_profile.SetSystemProfile(profile_proto);
|
| + });
|
| +
|
| + // Register the file and allow the "checker" task to run.
|
| + ASSERT_TRUE(PathExists(metrics_file()));
|
| + provider()->RegisterSource(
|
| + metrics_file(), FileMetricsProvider::SOURCE_HISTOGRAMS_ATOMIC_FILE,
|
| + FileMetricsProvider::ASSOCIATE_INTERNAL_PROFILE_OR_PREVIOUS_RUN,
|
| + kMetricsName);
|
| +
|
| + // Record embedded snapshots via snapshot-manager.
|
| + EXPECT_FALSE(HasInitialStabilityMetrics());
|
| + RunTasks();
|
| + {
|
| + HistogramFlattenerDeltaRecorder flattener;
|
| + base::HistogramSnapshotManager snapshot_manager(&flattener);
|
| + RecordInitialHistogramSnapshots(&snapshot_manager);
|
| + EXPECT_EQ(0U, flattener.GetRecordedDeltaHistogramNames().size());
|
| +
|
| + // A read of metrics with internal profiles should return one result.
|
| + SystemProfileProto profile;
|
| + EXPECT_TRUE(ProvideIndependentMetrics(&profile, &snapshot_manager));
|
| + EXPECT_FALSE(ProvideIndependentMetrics(&profile, &snapshot_manager));
|
| + }
|
| + EXPECT_TRUE(base::PathExists(metrics_file()));
|
| + OnDidCreateMetricsLog();
|
| + RunTasks();
|
| + EXPECT_FALSE(base::PathExists(metrics_file()));
|
| +}
|
| +
|
| } // namespace metrics
|
|
|