File: browser_user_education_service_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (324 lines) | stat: -rw-r--r-- 14,033 bytes parent folder | download | duplicates (3)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// 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 "chrome/browser/ui/views/user_education/browser_user_education_service.h"

#include <iterator>
#include <string>
#include <vector>

#include "base/strings/string_util.h"
#include "base/test/metrics/action_suffix_reader.h"
#include "base/test/metrics/histogram_variants_reader.h"
#include "base/threading/thread_restrictions.h"
#include "build/build_config.h"
#include "components/feature_engagement/public/configuration.h"
#include "components/feature_engagement/public/feature_configurations.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/user_education/common/feature_promo/feature_promo_registry.h"
#include "components/user_education/common/tutorial/tutorial_registry.h"
#include "components/user_education/common/user_education_metadata.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Checks metadata and returns a list of errors.
std::vector<std::string> CheckMetadata(
    const user_education::Metadata& metadata) {
  std::vector<std::string> errors;
  if (!metadata.launch_milestone) {
    errors.emplace_back("launch milestone");
  }
  if (metadata.owners.empty()) {
    errors.emplace_back("owners list");
  }
  if (metadata.additional_description.empty()) {
    errors.emplace_back("description");
  }
  return errors;
}

template <size_t N>
bool Contains(const char* const (&values)[N], const std::string& value) {
  return std::find(std::begin(values), std::end(values), value) !=
         std::end(values);
}

}  // namespace

TEST(BrowserUserEducationServiceTest, CheckFeaturePromoHistograms) {
  std::optional<base::HistogramVariantsEntryMap> iph_features;
  std::vector<std::string> missing_features;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    iph_features =
        base::ReadVariantsFromHistogramsXml("IPHFeature", "feature_engagement");
    ASSERT_TRUE(iph_features.has_value());
  }

  user_education::FeaturePromoRegistry registry;
  MaybeRegisterChromeFeaturePromos(registry);
  const auto& iph_specifications = registry.feature_data();
  for (const auto& [feature, spec] : iph_specifications) {
    if (!base::Contains(*iph_features, feature->name)) {
      missing_features.emplace_back(feature->name);
    }
  }
  ASSERT_TRUE(missing_features.empty())
      << "IPH Features:\n"
      << base::JoinString(missing_features, ", ")
      << "\nconfigured in browser_user_education_service.cc but no "
         "corresponding variants were added to IPHFeature variants in "
         "//tools/metrics/histograms/metadata/feature_engagement/"
         "histograms.xml";
}

TEST(BrowserUserEducationServiceTest, CheckFeaturePromoActions) {
  std::vector<base::ActionSuffixEntryMap> iph_suffixes;
  std::vector<std::string> missing_features;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    iph_suffixes =
        base::ReadActionSuffixesForAction("UserEducation.MessageShown.IPH");
    ASSERT_EQ(1U, iph_suffixes.size());
  }

  user_education::FeaturePromoRegistry registry;
  MaybeRegisterChromeFeaturePromos(registry);
  const auto& iph_specifications = registry.feature_data();
  for (const auto& [feature, spec] : iph_specifications) {
    std::string feature_name = feature->name;
    if (feature_name.starts_with("IPH_")) {
      feature_name = feature_name.substr(4);
    }
    if (!base::Contains(iph_suffixes[0], feature_name)) {
      missing_features.emplace_back(feature->name);
    }
  }
  ASSERT_TRUE(missing_features.empty())
      << "IPH Features:\n"
      << base::JoinString(missing_features, ", ")
      << "\nconfigured in browser_user_education_service.cc but no "
         "corresponding action suffixes were added in "
         "//tools/metrics/actions/actions.xml";
}

TEST(BrowserUserEducationServiceTest, CheckNewBadgeHistograms) {
  std::optional<base::HistogramVariantsEntryMap> new_badge_features;
  std::vector<std::string> missing_features;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    new_badge_features = base::ReadVariantsFromHistogramsXml("NewBadgeFeature",
                                                             "user_education");
    ASSERT_TRUE(new_badge_features.has_value());
  }
  user_education::NewBadgeRegistry registry;
  MaybeRegisterChromeNewBadges(registry);
  const auto& new_badge_specifications = registry.feature_data();
  for (const auto& [feature, spec] : new_badge_specifications) {
    if (!base::Contains(*new_badge_features, feature->name)) {
      missing_features.emplace_back(feature->name);
    }
  }
  ASSERT_TRUE(missing_features.empty())
      << "New Badge Features:\n"
      << base::JoinString(missing_features, ", ")
      << "\nconfigured in browser_user_education_service.cc but no "
         "corresponding variants were added to NewBadgeFeature variants in "
         "//tools/metrics/histograms/metadata/user_education/histograms.xml";
}

TEST(BrowserUserEducationServiceTest, CheckTutorialHistograms) {
  std::optional<base::HistogramVariantsEntryMap> tutorial_features;
  std::map<std::string, std::string> known_histograms;
  std::vector<std::tuple<std::string, std::string, std::string>>
      histogram_collisions;
  std::vector<std::string> missing_features;
  {
    base::ScopedAllowBlockingForTesting allow_blocking;
    tutorial_features =
        base::ReadVariantsFromHistogramsXml("TutorialID", "user_education");
    ASSERT_TRUE(tutorial_features.has_value());
  }
  user_education::TutorialRegistry registry;
  MaybeRegisterChromeTutorials(registry);
  const auto& tutorial_identifiers = registry.GetTutorialIdentifiers();
  for (const auto& identifier : tutorial_identifiers) {
    const auto* tutorial = registry.GetTutorialDescription(identifier);
    ASSERT_NE(nullptr, tutorial->histograms)
        << "Tutorials must be created with a histogram prefix";
    const auto variant_name = tutorial->histograms->GetTutorialPrefix();
    if (!known_histograms.emplace(variant_name, identifier).second) {
      histogram_collisions.emplace_back(
          identifier, known_histograms[identifier], variant_name);
    }
    if (!base::Contains(*tutorial_features, variant_name)) {
      missing_features.emplace_back(variant_name);
    }
  }
  EXPECT_TRUE(missing_features.empty())
      << "Tutorial Features:\n"
      << base::JoinString(missing_features, ", ")
      << "\nconfigured in browser_user_education_service.cc but no "
         "corresponding variants were added to TutorialID variants in "
         "//tools/metrics/histograms/metadata/user_education/histograms.xml";
  for (const auto& [t1, t2, histogram] : histogram_collisions) {
    ADD_FAILURE() << "Error: tutorial " << t1 << " and " << t2
                  << " share histogram " << histogram;
  }
  EXPECT_TRUE(histogram_collisions.empty());
}

TEST(BrowserUserEducationServiceTest, PreventNewHardCodedConfigurations) {
  const base::Feature* const kAllowedConfigurations[] = {
      // To be triaged:
      //
      // (These are listed because they were present prior to this test being
      // written; in the future as many as possible should be eliminated, and
      // the rest moved down to the "explicitly allowed" list below.)
      //
      // DO NOT ADD ENTRIES TO THIS LIST, EVER.
      &feature_engagement::kIPHBatterySaverModeFeature,
      &feature_engagement::kIPHCompanionSidePanelFeature,
      &feature_engagement::kIPHComposeMSBBSettingsFeature,
      &feature_engagement::kIPHDesktopSharedHighlightingFeature,
      &feature_engagement::kIPHDiscardRingFeature,
      &feature_engagement::kIPHDownloadEsbPromoFeature,
      &feature_engagement::kIPHExtensionsMenuFeature,
      &feature_engagement::kIPHExtensionsRequestAccessButtonFeature,
      &feature_engagement::kIPHGMCCastStartStopFeature,
      &feature_engagement::kIPHGMCLocalMediaCastingFeature,
      &feature_engagement::kIPHMemorySaverModeFeature,
      &feature_engagement::kIPHPasswordsManagementBubbleAfterSaveFeature,
      &feature_engagement::kIPHPasswordsManagementBubbleDuringSigninFeature,
      &feature_engagement::kIPHPasswordsWebAppProfileSwitchFeature,
      &feature_engagement::kIPHPasswordManagerShortcutFeature,
      &feature_engagement::kIPHPasswordSharingFeature,
      &feature_engagement::kIPHPowerBookmarksSidePanelFeature,
      &feature_engagement::kIPHReadingListInSidePanelFeature,
      &feature_engagement::kIPHReadingModeSidePanelFeature,
      &feature_engagement::kIPHSidePanelGenericPinnableFeature,
      &feature_engagement::kIPHSignoutWebInterceptFeature,
      &feature_engagement::kIPHTabOrganizationSuccessFeature,
      &feature_engagement::kIPHProfileSwitchFeature,
      &feature_engagement::kIPHPriceTrackingInSidePanelFeature,
      &feature_engagement::kIPHBackNavigationMenuFeature,
      &feature_engagement::kIPHAutofillCreditCardBenefitFeature,
      &feature_engagement::kIPHAutofillExternalAccountProfileSuggestionFeature,
      &feature_engagement::kIPHAutofillVirtualCardCVCSuggestionFeature,
      &feature_engagement::kIPHAutofillVirtualCardSuggestionFeature,
      &feature_engagement::kIPHCookieControlsFeature,
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
      &feature_engagement::kIPHDesktopPWAsLinkCapturingLaunch
#endif
      // Explicitly allowed:
      //
      // (These have been cleared by Frizzle Team as requiring their own
      // specific configuration.)
      //
      // DO NOT ADD ENTRIES TO THIS LIST WITHOUT APPROVAL FROM
      // components/user_education/OWNERS
  };

  std::vector<std::string> invalid_configs;

  user_education::FeaturePromoRegistry registry;
  MaybeRegisterChromeFeaturePromos(registry);
  const auto& iph_specifications = registry.feature_data();
  for (const auto& [feature, spec] : iph_specifications) {
    const auto config = feature_engagement::GetClientSideFeatureConfig(feature);
    if (config && !Contains(kAllowedConfigurations, feature)) {
      invalid_configs.emplace_back(feature->name);
    }
  }

  ASSERT_TRUE(invalid_configs.empty())
      << "Disallowed desktop Chrome entries in feature_configurations.cc:\n"
      << base::JoinString(invalid_configs, "\n")
      << "\nPlease note that configurations are automatically generated for"
         " Desktop IPH. In nearly all cases, this auto-configuration (plus"
         " additional configuration options in FeaturePromoSpecification)"
         " should be sufficient, and adding to feature_configurations.cc"
         " should be avoided.";
}

TEST(BrowserUserEducationServiceTest, CheckFeaturePromoMetadata) {
  // These promos get a pass because they are old and never had metadata
  // associated with them. All new promos should have metadata.
  //
  // DO NOT ADD ENTRIES TO THIS LIST, EVER.
  const base::Feature* const kExistingPromosWithoutMetadata[] = {
      &feature_engagement::kIPHComposeMSBBSettingsFeature,
      &feature_engagement::kIPHDesktopSharedHighlightingFeature,
      &feature_engagement::kIPHDesktopCustomizeChromeFeature,
      &feature_engagement::kIPHExplicitBrowserSigninPreferenceRememberedFeature,
      &feature_engagement::kIPHGMCCastStartStopFeature,
      &feature_engagement::kIPHGMCLocalMediaCastingFeature,
      &feature_engagement::kIPHLiveCaptionFeature,
      &feature_engagement::kIPHTabAudioMutingFeature,
      &feature_engagement::kIPHPasswordsManagementBubbleDuringSigninFeature,
      &feature_engagement::kIPHPasswordsWebAppProfileSwitchFeature,
      &feature_engagement::kIPHPasswordManagerShortcutFeature,
      &feature_engagement::kIPHPasswordSharingFeature,
      &feature_engagement::kIPHReadingListDiscoveryFeature,
      &feature_engagement::kIPHReadingListEntryPointFeature,
      &feature_engagement::kIPHReadingListInSidePanelFeature,
      &feature_engagement::kIPHSignoutWebInterceptFeature,
      &feature_engagement::kIPHProfileSwitchFeature,
      &feature_engagement::kIPHBackNavigationMenuFeature,
      &feature_engagement::kIPHCookieControlsFeature};

  user_education::FeaturePromoRegistry registry;
  MaybeRegisterChromeFeaturePromos(registry);
  const auto& iph_specifications = registry.feature_data();
  std::ostringstream oss;
  bool failed = false;
  for (const auto& [feature, spec] : iph_specifications) {
    const auto errors = CheckMetadata(spec.metadata());
    if (!errors.empty() && !Contains(kExistingPromosWithoutMetadata, feature)) {
      failed = true;
      oss << "\n"
          << feature->name
          << " is missing metadata: " << base::JoinString(errors, ", ");
    }
  }
  EXPECT_FALSE(failed) << "Feature promos missing metadata:" << oss.str();
}

TEST(BrowserUserEducationServiceTest, CheckTutorialPromoMetadata) {
  user_education::TutorialRegistry registry;
  MaybeRegisterChromeTutorials(registry);
  const auto& tutorial_identifiers = registry.GetTutorialIdentifiers();
  std::ostringstream oss;
  bool failed = false;
  for (const auto& identifier : tutorial_identifiers) {
    const auto* tutorial = registry.GetTutorialDescription(identifier);
    const auto errors = CheckMetadata(tutorial->metadata);
    if (!errors.empty()) {
      failed = true;
      oss << "\n"
          << identifier
          << " is missing metadata: " << base::JoinString(errors, ", ");
    }
  }
  EXPECT_FALSE(failed) << "Tutorials missing metadata:" << oss.str();
}

TEST(BrowserUserEducationServiceTest, CheckNewBadgeMetadata) {
  user_education::NewBadgeRegistry registry;
  MaybeRegisterChromeNewBadges(registry);
  const auto& new_badge_specifications = registry.feature_data();
  std::ostringstream oss;
  bool failed = false;
  for (const auto& [feature, spec] : new_badge_specifications) {
    const auto errors = CheckMetadata(spec.metadata);
    if (!errors.empty()) {
      failed = true;
      oss << "\n"
          << feature->name
          << " is missing metadata: " << base::JoinString(errors, ", ");
    }
  }
  EXPECT_FALSE(failed) << "\"New\" Badges missing metadata:" << oss.str();
}