chromium/components/metrics/structured/structured_metrics_recorder_unittest.cc

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/structured/structured_metrics_recorder.h"

#include <cstdint>
#include <memory>
#include <utility>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_refptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/threading/scoped_blocking_call.h"
#include "components/metrics/structured/event.h"
#include "components/metrics/structured/proto/event_storage.pb.h"
#include "components/metrics/structured/recorder.h"
#include "components/metrics/structured/structured_events.h"
#include "components/metrics/structured/structured_metrics_client.h"
#include "components/metrics/structured/structured_metrics_features.h"
#include "components/metrics/structured/test/test_event_storage.h"
#include "components/metrics/structured/test/test_key_data_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"

namespace metrics::structured {

namespace {

// These project, event, and metric names are used for testing.

// The name hash of "TestProjectOne".
constexpr uint64_t kProjectOneHash =16881314472396226433);
// The name hash of "TestProjectTwo".
constexpr uint64_t kProjectTwoHash =5876808001962504629);
// The name hash of "TestProjectThree".
constexpr uint64_t kProjectThreeHash =10860358748803291132);
// The name hash of "TestProjectFour".
constexpr uint64_t kProjectFourHash =6801665881746546626);
// The name hash of "TestProjectFive"
constexpr uint64_t kProjectFiveHash =3960582687892677139);
// The name hash of "TestProjectSix"
constexpr uint64_t kProjectSixHash =6972396123792667134);
// The name hash of "CrOSEvents"
constexpr uint64_t kCrOSEventsProjectHash =12657197978410187837);

// The name hash of "chrome::TestProjectOne::TestEventOne".
constexpr uint64_t kEventOneHash =13593049295042080097);
// The name hash of "chrome::TestProjectTwo::TestEventTwo".
constexpr uint64_t kEventTwoHash =8995967733561999410);
// The name hash of "chrome::TestProjectTwo::TestEventThree".
constexpr uint64_t kEventThreeHash =5848687377041124372);
// The name hash of "chrome::TestProjectFour::TestEventFive".
constexpr uint64_t kEventFiveHash =7045523601811399253);
// The name hash of "chrome::TestProjectFour::TestEventSix".
constexpr uint64_t kEventSixHash =2873337042686447043);
// The name hash of "chrome::TestProjectSix::TestEventSeven".
constexpr uint64_t kEventSevenHash =16749091071228286247);
// The name hash of "chrome::TestProjectSix::TestEnum".
constexpr uint64_t kEventEnumHash =14837072141472316574);
// The name hash of "chrome::CrOSEvents::NoMetricsEvent".
constexpr uint64_t kNoMetricsEventHash =5106854608989380457);
// The name has for "chrome::TestProjectSevent::TestEventEight".
const uint64_t kEventEightHash =16290206418240617738);

// The name hash of "TestMetricOne".
constexpr uint64_t kMetricOneHash =637929385654885975);
// The name hash of "TestMetricTwo".
constexpr uint64_t kMetricTwoHash =14083999144141567134);
// The name hash of "TestMetricThree".
constexpr uint64_t kMetricThreeHash =13469300759843809564);
// The name hash of "TestMetricFive".
constexpr uint64_t kMetricFiveHash =8665976921794972190);
// The name hash of "TestMetricSix".
constexpr uint64_t kMetricSixHash =3431522567539822144);
// The name hash of "TestMetricSeven".
constexpr uint64_t kMetricSevenHash =8395865158198697574);
// The name hash of "TestEnumMetric".
constexpr uint64_t kMetricEnumHash =16584986597633634829);

// The hex-encoded first 8 bytes of SHA256("aaa...a")
constexpr char kProjectOneId[] =;
// The hex-encoded first 8 bytes of SHA256("bbb...b")
constexpr char kProjectTwoId[] =;
// The hex-encoded first 8 bytes of SHA256("ddd...d")
constexpr char kProjectFourId[] =;

// Test values.
constexpr char kValueOne[] =;
constexpr char kValueTwo[] =;

std::string HashToHex(const uint64_t hash) {}

class TestRecorder : public StructuredMetricsClient::RecordingDelegate {};

class TestSMRecorder : public StructuredMetricsRecorder {};

}  // namespace

class StructuredMetricsRecorderTest : public testing::Test {};

// Simple test to ensure initialization works correctly in the case of a
// first-time run.
TEST_F(StructuredMetricsRecorderTest, RecorderInitializesFromBlankSlate) {}

// Ensure a call to OnRecordingDisabled prevents reporting.
TEST_F(StructuredMetricsRecorderTest, EventsNotReportedWhenRecordingDisabled) {}

// Ensure that disabling the structured metrics feature flag prevents all
// structured metrics reporting.
TEST_F(StructuredMetricsRecorderTest, EventsNotReportedWhenFeatureDisabled) {}

// Ensure that, if recording is disabled part-way through initialization, the
// initialization still completes correctly, but recording is correctly set to
// disabled.
TEST_F(StructuredMetricsRecorderTest, RecordingDisabledDuringInitialization) {}

// Ensure that recording is disabled until explicitly enabled with a call to
// OnRecordingEnabled.
TEST_F(StructuredMetricsRecorderTest, RecordingDisabledByDefault) {}

TEST_F(StructuredMetricsRecorderTest, RecordedEventAppearsInReport) {}

TEST_F(StructuredMetricsRecorderTest, EventMetricsReportedCorrectly) {}

// Ensure that events containing raw string metrics are reported correctly.
TEST_F(StructuredMetricsRecorderTest, RawStringMetricsReportedCorrectly) {}

TEST_F(StructuredMetricsRecorderTest, FloatMetricsReportedCorrectly) {}

// TODO: Test copied in AshStructuredMetricsRecorder unit tests, remove this one
// once the test key provider is simplified.
TEST_F(StructuredMetricsRecorderTest, DeviceKeysUsedForDeviceScopedProjects) {}

// Check that a full int64 can be recorded, and is not truncated to an int32.
TEST_F(StructuredMetricsRecorderTest, Int64MetricsNotTruncated) {}

TEST_F(StructuredMetricsRecorderTest, EventsWithinProjectReportedWithSameID) {}

TEST_F(StructuredMetricsRecorderTest, EventWithoutMetricsReportCorrectly) {}

// Test that events reported before recording is enabled are ignored.
TEST_F(StructuredMetricsRecorderTest, EventsNotRecordedBeforeRecordingEnabled) {}

// Test that events reported after recording is enabled but before the keys are
// loaded are hashed and stored after keys are loaded.
TEST_F(StructuredMetricsRecorderTest, EventsRecordedBeforeKeysInitialized) {}

// Ensure a call to OnRecordingDisabled not only prevents the reporting of new
// events, but also clears the cache of any existing events that haven't yet
// been reported.
TEST_F(StructuredMetricsRecorderTest,
       ExistingEventsClearedWhenRecordingDisabled) {}

// Ensure that recording and reporting is re-enabled after recording is disabled
// and then enabled again.
TEST_F(StructuredMetricsRecorderTest, ReportingResumesWhenEnabled) {}

// Ensure that a call to ProvideCurrentSessionData before initialization
// completes returns no events.
TEST_F(StructuredMetricsRecorderTest,
       ReportsNothingBeforeInitializationComplete) {}

TEST_F(StructuredMetricsRecorderTest, EventsClone) {}

TEST_F(StructuredMetricsRecorderTest, DisallowedProjectAreDropped) {}

class TestProcessor : public EventsProcessorInterface {};

TEST_F(StructuredMetricsRecorderTest, AppliesProcessorCorrectly) {}

TEST_F(StructuredMetricsRecorderTest, ForceRecordedEvents) {}

TEST_F(StructuredMetricsRecorderTest, EventMetadataLookupCorrectly) {}

class TestWatcher : public StructuredMetricsRecorder::Observer {};

TEST_F(StructuredMetricsRecorderTest, WatcherTest) {}

TEST_F(StructuredMetricsRecorderTest, EnumRecordedCorrectly) {}

TEST_F(StructuredMetricsRecorderTest, MultipleReports) {}

}  // namespace metrics::structured