chromium/chrome/browser/metrics/structured_metrics_service_browsertest.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 <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/structured/test/structured_metrics_mixin.h"
#include "chrome/browser/metrics/testing/sync_metrics_test_utils.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/unified_consent/unified_consent_service_factory.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "components/metrics/log_decoder.h"
#include "components/metrics/metrics_service_client.h"
#include "components/metrics/structured/structured_events.h"
#include "components/metrics/structured/structured_metrics_features.h"
#include "components/metrics/structured/structured_metrics_service.h"
#include "components/metrics/unsent_log_store.h"
#include "components/metrics_services_manager/metrics_services_manager.h"
#include "components/sync/test/fake_server_network_resources.h"
#include "components/unified_consent/unified_consent_service.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"

namespace metrics {
namespace {

// The name hash of "TestProjectOne".
constexpr uint64_t kProjectOneHash =16881314472396226433);

// The name hash of "TestProjectFive".
constexpr uint64_t kProjectFiveHash =3960582687892677139);

// The name hash of "TestProjectSeven".
constexpr uint64_t kProjectSevenHash =10319251808101486833);

// The name hash of "chrome::TestProjectOne::TestEventOne".
constexpr uint64_t kEventOneHash =13593049295042080097);

// The name hash of "chrome::TestProjectFive::TestEventSix".
constexpr uint64_t kEventSixHash =2873337042686447043);

// The name hash of "chrome::TestProjectSeven::TestEventEight".
constexpr uint64_t kEventEightHash =16290206418240617738);

structured::StructuredMetricsService* GetSMService() {}

}  // namespace

class StructuredMetricsServiceTestBase : public MixinBasedInProcessBrowserTest {};

class TestStructuredMetricsService : public StructuredMetricsServiceTestBase {};

IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService, EnabledWithConsent) {}

IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService, DisabledWhenRevoked) {}

IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService,
                       // TODO(crbug.com/40931527): Re-enable this test
                       DISABLED_InMemoryPurgeOnConsentRevoke) {}

// TODO(crbug.com/40931189): Re-enable this test
// Only flaky on chromeos-rel.
#if BUILDFLAG(IS_CHROMEOS) && defined(NDEBUG) && !defined(ADDRESS_SANITIZER)
#define MAYBE_StagedLogPurgeOnConsentRevoke
#else
#define MAYBE_StagedLogPurgeOnConsentRevoke
#endif
IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService,
                       MAYBE_StagedLogPurgeOnConsentRevoke) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService, SystemProfilePopulated) {
  auto* sm_service = GetSMService();

  // Enable consent for profile.
  structured_metrics_mixin_.UpdateRecordingState(true);

  // Wait for the consent to propagate.
  WaitForConsentChanges();

  // Verify that recording and reporting are enabled.
  EXPECT_TRUE(sm_service->recording_enabled());
  EXPECT_TRUE(sm_service->reporting_active());

  WaitUntilKeysReady();

  // Record an event inorder to build a log.
  structured::StructuredMetricsClient::Record(
      structured::events::v2::test_project_one::TestEventOne()
          .SetTestMetricOne("metric one")
          .SetTestMetricTwo(10));

  // This will timeout and fail the test if events have not been recorded
  // successfully.
  structured_metrics_mixin_.WaitUntilEventRecorded(kProjectOneHash,
                                                   kEventOneHash);

  // Flush the in-memory events to a staged log.
  sm_service->Flush(metrics::MetricsLogsEventManager::CreateReason::kUnknown);

  std::unique_ptr<ChromeUserMetricsExtension> uma_proto = GetStagedLog();
  ASSERT_NE(uma_proto.get(), nullptr);

  // Verify that the SystemProfile has been set appropriately.
  const SystemProfileProto& system_profile = uma_proto->system_profile();
  EXPECT_EQ(system_profile.app_version(),
            GetSMService()->GetMetricsServiceClient()->GetVersionString());
}
#endif  //  BUILDFLAG(IS_CHROMEOS_ASH)

class TestStructuredMetricsServiceDisabled
    : public StructuredMetricsServiceTestBase {};

IN_PROC_BROWSER_TEST_F(TestStructuredMetricsServiceDisabled,
                       ValidStateWhenDisabled) {}

// TODO(crbug.com/41485716): Flaky on linux-chromeos-rel.
IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService,
                       DISABLED_PurgeForceRecordedEvents) {}

IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService, CreateLogs) {}

}  // namespace metrics