#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 {
constexpr uint64_t kProjectOneHash = …16881314472396226433);
constexpr uint64_t kProjectFiveHash = …3960582687892677139);
constexpr uint64_t kProjectSevenHash = …10319251808101486833);
constexpr uint64_t kEventOneHash = …13593049295042080097);
constexpr uint64_t kEventSixHash = …2873337042686447043);
constexpr uint64_t kEventEightHash = …16290206418240617738);
structured::StructuredMetricsService* GetSMService() { … }
}
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,
DISABLED_InMemoryPurgeOnConsentRevoke) { … }
#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();
structured_metrics_mixin_.UpdateRecordingState(true);
WaitForConsentChanges();
EXPECT_TRUE(sm_service->recording_enabled());
EXPECT_TRUE(sm_service->reporting_active());
WaitUntilKeysReady();
structured::StructuredMetricsClient::Record(
structured::events::v2::test_project_one::TestEventOne()
.SetTestMetricOne("metric one")
.SetTestMetricTwo(10));
structured_metrics_mixin_.WaitUntilEventRecorded(kProjectOneHash,
kEventOneHash);
sm_service->Flush(metrics::MetricsLogsEventManager::CreateReason::kUnknown);
std::unique_ptr<ChromeUserMetricsExtension> uma_proto = GetStagedLog();
ASSERT_NE(uma_proto.get(), nullptr);
const SystemProfileProto& system_profile = uma_proto->system_profile();
EXPECT_EQ(system_profile.app_version(),
GetSMService()->GetMetricsServiceClient()->GetVersionString());
}
#endif
class TestStructuredMetricsServiceDisabled
: public StructuredMetricsServiceTestBase { … };
IN_PROC_BROWSER_TEST_F(TestStructuredMetricsServiceDisabled,
ValidStateWhenDisabled) { … }
IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService,
DISABLED_PurgeForceRecordedEvents) { … }
IN_PROC_BROWSER_TEST_F(TestStructuredMetricsService, CreateLogs) { … }
}