1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/persistent_synthetic_trial_observer.h"
7 #include "base/test/task_environment.h"
8 #include "components/metrics/persistent_system_profile.h"
9 #include "components/variations/hashing.h"
10 #include "components/variations/synthetic_trials.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class PersistentSyntheticTrialObserverTest : public testing::Test {
17 constexpr static int32_t kAllocatorMemorySize = 1 << 20; // 1 MiB
19 PersistentSyntheticTrialObserverTest() = default;
20 ~PersistentSyntheticTrialObserverTest() override = default;
22 void SetUp() override {
25 memory_allocator_ = std::make_unique<base::LocalPersistentMemoryAllocator>(
26 kAllocatorMemorySize, 0, "");
27 GlobalPersistentSystemProfile::GetInstance()->RegisterPersistentAllocator(
28 memory_allocator_.get());
29 SystemProfileProto profile;
30 profile.set_client_uuid("id");
31 GlobalPersistentSystemProfile::GetInstance()->SetSystemProfile(profile,
35 void TearDown() override {
36 GlobalPersistentSystemProfile::GetInstance()->DeregisterPersistentAllocator(
37 memory_allocator_.get());
41 SystemProfileProto GetSystemProfile() {
42 SystemProfileProto profile;
43 GlobalPersistentSystemProfile::GetInstance()->GetSystemProfile(
44 *memory_allocator_, &profile);
49 base::test::TaskEnvironment task_env_;
50 std::unique_ptr<base::PersistentMemoryAllocator> memory_allocator_;
53 TEST_F(PersistentSyntheticTrialObserverTest, AddRemoveTrials) {
54 PersistentSyntheticTrialObserver observer;
55 const variations::SyntheticTrialGroup kGroup1(
57 variations::SyntheticTrialAnnotationMode::kCurrentLog);
58 const variations::SyntheticTrialGroup kGroup2(
59 "Trial2", "Group2", variations::SyntheticTrialAnnotationMode::kNextLog);
60 const variations::SyntheticTrialGroup kGroup3(
62 variations::SyntheticTrialAnnotationMode::kCurrentLog);
64 observer.OnSyntheticTrialsChanged({kGroup1, kGroup2}, {}, {kGroup1, kGroup2});
65 SystemProfileProto profile = GetSystemProfile();
66 ASSERT_EQ(1, profile.field_trial_size());
67 EXPECT_EQ(variations::HashName("Trial1"), profile.field_trial(0).name_id());
68 EXPECT_EQ(variations::HashName("Group1"), profile.field_trial(0).group_id());
70 observer.OnSyntheticTrialsChanged({kGroup3}, {kGroup1}, {kGroup3});
71 profile = GetSystemProfile();
72 ASSERT_EQ(1, profile.field_trial_size());
73 EXPECT_EQ(variations::HashName("Trial2"), profile.field_trial(0).name_id());
74 EXPECT_EQ(variations::HashName("Group3"), profile.field_trial(0).group_id());
77 } // namespace metrics