File: sticky_activation_manager_unittest.cc

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,605 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2025 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/variations/sticky_activation_manager.h"

#include "components/prefs/testing_pref_service.h"
#include "components/variations/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace variations {
namespace {

TEST(StickyActivationManagerTest, ShouldActivate) {
  TestingPrefServiceSimple local_state;
  StickyActivationManager::RegisterPrefs(*local_state.registry());

  local_state.SetString(prefs::kVariationsStickyStudies, "Foo/Bar/Foo2/Bar2");
  StickyActivationManager manager(&local_state,
                                  /*sticky_activation_enabled=*/true);

  EXPECT_TRUE(manager.ShouldActivate("Foo", "Bar"));
  EXPECT_TRUE(manager.ShouldActivate("Foo2", "Bar2"));
  EXPECT_FALSE(manager.ShouldActivate("Foo3", "Bar3"));
}

TEST(StickyActivationManagerTest, SavePrefs) {
  TestingPrefServiceSimple local_state;
  StickyActivationManager::RegisterPrefs(*local_state.registry());

  local_state.SetString(prefs::kVariationsStickyStudies, "Foo/Bar/Foo2/Bar2");
  StickyActivationManager manager(&local_state,
                                  /*sticky_activation_enabled=*/true);

  auto* foo_far_trial = base::FieldTrialList::CreateFieldTrial("Foo", "Far");
  auto* bla_bla_trial = base::FieldTrialList::CreateFieldTrial("Bla", "Bla");

  // Note: The ShouldActivate() calls mark the trials for monitoring.
  EXPECT_FALSE(manager.ShouldActivate("Foo", "Far"));
  EXPECT_TRUE(manager.ShouldActivate("Foo2", "Bar2"));
  EXPECT_FALSE(manager.ShouldActivate("Foo3", "Bar3"));
  manager.StartMonitoring();
  // Only Foo2/Bar2 should be written, as the other ones did not match.
  EXPECT_EQ("Foo2/Bar2",
            local_state.GetString(prefs::kVariationsStickyStudies));

  // Activate both "Foo" and "Bla" trials. Only "Foo" should be marked since
  // "Bla" hasn't been queried using ShouldActivate() and thus isn't sticky.
  foo_far_trial->group_name();
  bla_bla_trial->group_name();
  // The pref should have been updated with Foo added and Foo2 still there.
  EXPECT_EQ("Foo/Far/Foo2/Bar2",
            local_state.GetString(prefs::kVariationsStickyStudies));
}

TEST(StickyActivationManagerTest, NoCrashAfterManagerDestroyed) {
  TestingPrefServiceSimple local_state;
  StickyActivationManager::RegisterPrefs(*local_state.registry());

  auto* foo_trial = base::FieldTrialList::CreateFieldTrial("Foo", "Bar");
  auto* foo2_trial = base::FieldTrialList::CreateFieldTrial("Foo2", "Bar2");

  {
    StickyActivationManager manager(&local_state,
                                    /*sticky_activation_enabled=*/true);
    // Note: The ShouldActivate() calls mark the trials for monitoring.
    EXPECT_FALSE(manager.ShouldActivate("Foo", "Far"));
    EXPECT_FALSE(manager.ShouldActivate("Foo2", "Bar2"));
    manager.StartMonitoring();

    // Activate the first trial.
    foo_trial->group_name();
    // It should be saved to prefs.
    EXPECT_EQ("Foo/Bar",
              local_state.GetString(prefs::kVariationsStickyStudies));
  }

  // Activate the second trial after the manager is destroyed. This shouldn't
  // crash and can happen in practice during shutdown, as different objects get
  // destroyed at different times, and their destruction may do feature checks.
  foo2_trial->group_name();

  // The pref should not have changed, as the manager should have stopped
  // observing.
  EXPECT_EQ("Foo/Bar", local_state.GetString(prefs::kVariationsStickyStudies));
}

}  // namespace
}  // namespace variations