File: feature_promo_dialog_interactive_uitest.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 (231 lines) | stat: -rw-r--r-- 9,288 bytes parent folder | download | duplicates (5)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <optional>
#include <string>
#include <vector>

#include "base/auto_reset.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/string_split.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_registry_cache_waiter.h"
#include "chrome/browser/banners/test_app_banner_manager_desktop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/test/test_browser_dialog.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/global_media_controls/media_toolbar_button_view.h"
#include "chrome/browser/ui/views/location_bar/intent_chip_button.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_controller.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/web_applications/link_capturing_features.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_callback_app_identity.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/common/buildflags.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/user_education/interactive_feature_promo_test.h"
#include "components/bookmarks/common/bookmark_pref_names.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/feature_engagement/public/feature_list.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/live_caption/caption_util.h"
#include "components/user_education/common/feature_promo/feature_promo_controller.h"
#include "components/user_education/common/feature_promo/feature_promo_result.h"
#include "components/user_education/common/feature_promo/feature_promo_specification.h"
#include "content/public/test/browser_test.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/interaction/expect_call_in_scope.h"
#include "ui/base/interaction/interaction_sequence_test_util.h"

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::NiceMock;
using ::testing::Ref;
using ::testing::Return;

namespace {

// Returns an appropriate set of string replacements; passing the wrong number
// of replacements for the body text of the IPH will cause a DCHECK.
user_education::FeaturePromoSpecification::FormatParameters
GetReplacementsForFeature(const base::Feature& feature) {
  if (&feature == &feature_engagement::kIPHDesktopPwaInstallFeature) {
    return u"Placeholder Text";
  }
  return user_education::FeaturePromoSpecification::NoSubstitution();
}

}  // namespace

using TestBase = InteractiveFeaturePromoTestT<DialogBrowserTest>;

class FeaturePromoDialogTest : public TestBase {
 public:
  FeaturePromoDialogTest()
      // Specifying features to enable is not important because a mock
      // FeatureEngagementTracker is used.
      : TestBase(UseMockTracker()),
        update_dialog_scope_(web_app::SetIdentityUpdateDialogActionForTesting(
            web_app::AppIdentityUpdate::kSkipped)) {
    feature_ = GetFeatureForTest();
    scoped_feature_list_.InitWithFeatures(
        /* enabled_features =*/{*feature_},
        /* disabled_features =*/
        {feature_engagement::kIPHLiveCaptionFeature});

    // TODO(crbug.com/40727458): fix cause of bubbles overflowing the
    // screen and remove this.
    set_should_verify_dialog_bounds(false);
  }
  void SetUp() override {
    webapps::TestAppBannerManagerDesktop::SetUp();
    TestBase::SetUp();
  }
  void SetUpOnMainThread() override {
    TestBase::SetUpOnMainThread();
    browser()->window()->Activate();
    ui_test_utils::BrowserActivationWaiter(browser()).WaitForActivation();
  }

  void TearDownOnMainThread() override {
    Profile* const profile = browser()->profile();
    web_app::WebAppRegistrar& registrar =
        web_app::WebAppProvider::GetForTest(profile)->registrar_unsafe();
    for (const auto& app_id : registrar.GetAppIds()) {
      apps::AppReadinessWaiter app_readiness_waiter(
          profile, app_id, apps::Readiness::kUninstalledByUser);
      web_app::test::UninstallWebApp(profile, app_id);
      app_readiness_waiter.Await();
    }

    TestBase::TearDownOnMainThread();
  }

  ~FeaturePromoDialogTest() override = default;

  // DialogBrowserTest:
  void ShowUi(const std::string& name) override {
    auto* const promo_controller =
        BrowserView::GetBrowserViewForBrowser(browser())
            ->GetFeaturePromoControllerForTesting();
    ASSERT_TRUE(promo_controller);

    // The browser may have already queued a promo for startup. Since the test
    // uses a mock, cancel that and just show it directly.
    const auto status = promo_controller->GetPromoStatus(*feature_);
    if (status == user_education::FeaturePromoStatus::kQueued) {
      promo_controller->EndPromo(
          *feature_, user_education::EndFeaturePromoReason::kAbortPromo);
    }

    // Set up mock tracker to allow the IPH, then attempt to show it.
    EXPECT_CALL(*GetMockTrackerFor(browser()),
                ShouldTriggerHelpUI(Ref(*feature_)))
        .Times(1)
        .WillOnce(Return(true));
    user_education::FeaturePromoParams params(*feature_);
    params.body_params = GetReplacementsForFeature(*feature_);
    UNCALLED_MOCK_CALLBACK(
        user_education::FeaturePromoController::ShowPromoResultCallback,
        show_callback);
    params.show_promo_result_callback = show_callback.Get();
    EXPECT_ASYNC_CALL_IN_SCOPE(
        show_callback, Run(user_education::FeaturePromoResult::Success()),
        promo_controller->MaybeShowPromo(std::move(params)));
  }

 private:
  // Looks up the IPH name from the test name and returns the corresponding
  // base::Feature.
  const base::Feature* GetFeatureForTest() const {
    const std::string full_name =
        testing::UnitTest::GetInstance()->current_test_info()->name();
    const std::string name = full_name.substr(full_name.find('_') + 1);
    std::vector<const base::Feature*> iph_features =
        feature_engagement::GetAllFeatures();
    auto feature_it =
        std::ranges::find(iph_features, name, &base::Feature::name);
    CHECK(feature_it != iph_features.end());
    return *feature_it;
  }

  raw_ptr<const base::Feature> feature_ = nullptr;
  base::test::ScopedFeatureList scoped_feature_list_;
  base::AutoReset<std::optional<web_app::AppIdentityUpdate>>
      update_dialog_scope_;
};

// Adding new tests for your promo
//
// When you add a new IPH, add a test for your promo. In most cases you
// can follow these steps:
//
// 1. Get the feature name for your IPH. This will be the of the form
//    IPH_<name>. It should be the same as defined in
//    //components/feature_engagement/public/feature_constants.cc.
//
// 2. Define a new test below with the name InvokeUi_IPH_<name>. Place
//    it in alphabetical order.
//
// 3. Call set_baseline("<cl-number>"). This enables Skia Gold pixel
//    tests for your IPH.
//
// 4. Call ShowAndVerifyUi().
//
// For running your test reference the docs in
// //chrome/browser/ui/test/test_browser_dialog.h

IN_PROC_BROWSER_TEST_F(FeaturePromoDialogTest, InvokeUi_IPH_DesktopPwaInstall) {
  set_baseline("2936082");
  // Navigate to an installable site so PWA install icon shows up.
  ASSERT_TRUE(embedded_test_server()->Start());
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser(),
      embedded_test_server()->GetURL("/banners/manifest_test_page.html")));
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  auto* app_banner_manager =
      webapps::TestAppBannerManagerDesktop::FromWebContents(web_contents);
  app_banner_manager->WaitForInstallableCheck();
  EXPECT_TRUE(BrowserView::GetBrowserViewForBrowser(browser())
                  ->toolbar()
                  ->location_bar()
                  ->page_action_icon_controller()
                  ->GetIconView(PageActionIconType::kPwaInstall)
                  ->GetVisible());
  browser()->window()->Activate();
  ui_test_utils::BrowserActivationWaiter(browser()).WaitForActivation();

  ShowAndVerifyUi();
}

#if !BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(FeaturePromoDialogTest, InvokeUi_IPH_ProfileSwitch) {
  set_baseline("3710120");
  ShowAndVerifyUi();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_F(FeaturePromoDialogTest, InvokeUi_IPH_TabSearch) {
  set_baseline("2991858");
  ShowAndVerifyUi();
}

IN_PROC_BROWSER_TEST_F(FeaturePromoDialogTest,
                       InvokeUi_IPH_DesktopSharedHighlighting) {
  set_baseline("3253618");
  ShowAndVerifyUi();
}