File: whats_new_registry_feature_param_unittest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 6,495 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2024 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 "base/feature_list.h"
#include "base/test/scoped_feature_list.h"
#include "components/user_education/common/user_education_features.h"
#include "components/user_education/webui/mock_whats_new_storage_service.h"
#include "components/user_education/webui/whats_new_registry.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/interaction/expect_call_in_scope.h"

using testing::Return;
using whats_new::WhatsNewEdition;
using whats_new::WhatsNewModule;
using whats_new::WhatsNewRegistry;
using whats_new::WhatsNewStorageService;

namespace user_education {

namespace {

using BrowserCommand = browser_command::mojom::Command;

// Modules
// Enabled through feature list.
BASE_FEATURE(kTestModuleEnabled1,
             "TestModuleEnabled1",
             base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kTestModuleEnabled2,
             "TestModuleEnabled2",
             base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kTestEditionEnabled,
             "TestEditionEnabled",
             base::FEATURE_DISABLED_BY_DEFAULT);
// Enabled by default
BASE_FEATURE(kTestModuleEnabled3,
             "TestModuleEnabled3",
             base::FEATURE_ENABLED_BY_DEFAULT);

}  // namespace

class WhatsNewRegistryFeatureParamTest : public testing::Test {
 public:
  WhatsNewRegistryFeatureParamTest() = default;
  ~WhatsNewRegistryFeatureParamTest() override = default;

  void SetUp() override {
    testing::Test::SetUp();
    feature_list_.Reset();
  }

  void CreateRegistry() {
    auto mock_storage_service = std::make_unique<MockWhatsNewStorageService>();
    EXPECT_CALL(*mock_storage_service, SetModuleEnabled(testing::_)).Times(3);
    EXPECT_CALL(*mock_storage_service, ReadModuleData())
        .WillRepeatedly(testing::ReturnRef(stored_enabled_modules_));
    whats_new_registry_ =
        std::make_unique<WhatsNewRegistry>(std::move(mock_storage_service));

    // Modules
    whats_new_registry_->RegisterModule(
        WhatsNewModule(kTestModuleEnabled1, ""));
    stored_enabled_modules_.Append(kTestModuleEnabled1.name);
    whats_new_registry_->RegisterModule(
        WhatsNewModule(kTestModuleEnabled2, ""));
    stored_enabled_modules_.Append(kTestModuleEnabled2.name);
    whats_new_registry_->RegisterModule(
        WhatsNewModule(kTestModuleEnabled3, ""));
    stored_enabled_modules_.Append(kTestModuleEnabled3.name);

    // Editions
    whats_new_registry_->RegisterEdition(
        WhatsNewEdition(kTestEditionEnabled, ""));
  }

  void TearDown() override {
    stored_enabled_modules_.clear();
    whats_new_registry_.reset();
    testing::Test::TearDown();
  }

 protected:
  std::unique_ptr<WhatsNewRegistry> whats_new_registry_;
  base::Value::List stored_enabled_modules_;
  base::test::ScopedFeatureList feature_list_;
};

TEST_F(WhatsNewRegistryFeatureParamTest, FeatureWithoutCustomization) {
  feature_list_.InitWithFeatures({kTestModuleEnabled1, kTestModuleEnabled2},
                                 {});
  CreateRegistry();

  std::string customization1 = base::GetFieldTrialParamValueByFeature(
      kTestModuleEnabled1, whats_new::kCustomizationParam);
  EXPECT_EQ("", customization1);

  std::string customization2 = base::GetFieldTrialParamValueByFeature(
      kTestModuleEnabled2, whats_new::kCustomizationParam);
  EXPECT_EQ("", customization2);

  auto active_customizations = whats_new_registry_->GetCustomizations();
  EXPECT_EQ(static_cast<size_t>(0), active_customizations.size());
}

TEST_F(WhatsNewRegistryFeatureParamTest, FeatureWithCustomization) {
  feature_list_.InitWithFeaturesAndParameters(
      {{kTestModuleEnabled1, {{whats_new::kCustomizationParam, "hello"}}},
       {kTestModuleEnabled2, {{}}}},
      {});
  CreateRegistry();

  std::string customization1 = base::GetFieldTrialParamValueByFeature(
      kTestModuleEnabled1, whats_new::kCustomizationParam);
  EXPECT_EQ("hello", customization1);

  std::string customization2 = base::GetFieldTrialParamValueByFeature(
      kTestModuleEnabled2, whats_new::kCustomizationParam);
  EXPECT_EQ("", customization2);

  auto active_customizations = whats_new_registry_->GetCustomizations();
  EXPECT_EQ(static_cast<size_t>(1), active_customizations.size());
  EXPECT_EQ("hello", active_customizations.at(0));
}

TEST_F(WhatsNewRegistryFeatureParamTest, MultipleFeaturesWithCustomizations) {
  feature_list_.InitWithFeaturesAndParameters(
      {{kTestModuleEnabled1, {{whats_new::kCustomizationParam, "hello"}}},
       {kTestModuleEnabled2, {{whats_new::kCustomizationParam, "goodbye"}}}},
      {});
  CreateRegistry();

  std::string customization1 = base::GetFieldTrialParamValueByFeature(
      kTestModuleEnabled1, whats_new::kCustomizationParam);
  EXPECT_EQ("hello", customization1);

  std::string customization2 = base::GetFieldTrialParamValueByFeature(
      kTestModuleEnabled2, whats_new::kCustomizationParam);
  EXPECT_EQ("goodbye", customization2);

  auto active_customizations = whats_new_registry_->GetCustomizations();
  EXPECT_EQ(static_cast<size_t>(2), active_customizations.size());
  EXPECT_EQ("hello", active_customizations.at(0));
  EXPECT_EQ("goodbye", active_customizations.at(1));
}

TEST_F(WhatsNewRegistryFeatureParamTest, FeatureWithoutSurvey) {
  feature_list_.InitWithFeatures({kTestModuleEnabled1, kTestModuleEnabled2},
                                 {});
  CreateRegistry();

  std::string survey_param = base::GetFieldTrialParamValueByFeature(
      kTestEditionEnabled, whats_new::kSurveyParam);
  EXPECT_EQ("", survey_param);

  auto survey_from_registry = whats_new_registry_->GetActiveEditionSurvey();
  EXPECT_FALSE(survey_from_registry.has_value());
}

TEST_F(WhatsNewRegistryFeatureParamTest, FeatureWithSurvey) {
  feature_list_.InitWithFeaturesAndParameters(
      {{kTestModuleEnabled1, {{}}},
       {kTestModuleEnabled2, {{}}},
       {kTestEditionEnabled, {{whats_new::kSurveyParam, "hello"}}}},
      {});
  CreateRegistry();

  std::string survey_param = base::GetFieldTrialParamValueByFeature(
      kTestEditionEnabled, whats_new::kSurveyParam);
  EXPECT_EQ("hello", survey_param);

  auto survey_from_registry = whats_new_registry_->GetActiveEditionSurvey();
  EXPECT_TRUE(survey_from_registry.has_value());
  EXPECT_EQ("hello", survey_from_registry.value());
}

}  // namespace user_education