File: browser_app_menu_button_interactive_uitest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (205 lines) | stat: -rw-r--r-- 8,056 bytes parent folder | download
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
// Copyright 2023 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/toolbar/app_menu_model.h"

#include "base/feature_list.h"
#include "base/logging.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/accelerator_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/toolbar/bookmark_sub_menu_model.h"
#include "chrome/browser/ui/views/toolbar/browser_app_menu_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/browser/user_education/user_education_service.h"
#include "chrome/browser/user_education/user_education_service_factory.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "chrome/test/interaction/tracked_element_webcontents.h"
#include "chrome/test/interaction/webcontents_interaction_test_util.h"
#include "components/feature_engagement/test/mock_tracker.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/interaction_sequence.h"

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

using user_education::FeaturePromoCloseReason;
using user_education::FeaturePromoRegistry;
using user_education::FeaturePromoSpecification;
using CloseReason = user_education::FeaturePromoStorageService::CloseReason;

namespace {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kPrimaryTabPageElementId);

BASE_FEATURE(kMenuPromoTestFeature,
             "MenuPromoTestFeature",
             base::FEATURE_ENABLED_BY_DEFAULT);
}  // namespace

class BrowserAppMenuButtonInteractiveTest : public InteractiveBrowserTest {
 public:
  BrowserAppMenuButtonInteractiveTest() {
    subscription_ = BrowserContextDependencyManager::GetInstance()
                        ->RegisterCreateServicesCallbackForTesting(
                            base::BindRepeating(RegisterMockTracker));
  }
  ~BrowserAppMenuButtonInteractiveTest() override = default;
  BrowserAppMenuButtonInteractiveTest(
      const BrowserAppMenuButtonInteractiveTest&) = delete;
  void operator=(const BrowserAppMenuButtonInteractiveTest&) = delete;

  void SetUpOnMainThread() override {
    InteractiveBrowserTest::SetUpOnMainThread();

    mock_tracker_ =
        static_cast<NiceMock<feature_engagement::test::MockTracker>*>(
            feature_engagement::TrackerFactory::GetForBrowserContext(
                browser()->profile()));
    ASSERT_TRUE(mock_tracker_);

    // Allow an unlimited number of calls to WouldTriggerHelpUI().
    EXPECT_CALL(*mock_tracker_, WouldTriggerHelpUI)
        .WillRepeatedly(Return(true));

    promo_controller_ = static_cast<BrowserFeaturePromoController*>(
        browser()->window()->GetFeaturePromoController());

    // Register test features.
    registry()->RegisterFeature(std::move(
        user_education::FeaturePromoSpecification::CreateForToastPromo(
            kMenuPromoTestFeature, kToolbarAppMenuButtonElementId,
            IDS_TUTORIAL_TAB_GROUP_EDIT_BUBBLE, IDS_TUTORIAL_TAB_GROUP_COLLAPSE,
            FeaturePromoSpecification::AcceleratorInfo())
            .SetHighlightedMenuItem(
                BookmarkSubMenuModel::kShowBookmarkSidePanelItem)));
  }

  auto AttemptIPH(const base::Feature& iph_feature,
                  user_education::FeaturePromoResult expected_result,
                  base::OnceClosure on_close = base::DoNothing()) {
    return Check([this, &iph_feature, expected_result,
                  callback = std::move(on_close)]() mutable {
      if (expected_result) {
        EXPECT_CALL(*mock_tracker_, ShouldTriggerHelpUI(Ref(iph_feature)))
            .WillOnce(Return(true));
      } else {
        EXPECT_CALL(*mock_tracker_, ShouldTriggerHelpUI(Ref(iph_feature)))
            .Times(0);
      }

      user_education::FeaturePromoParams params(iph_feature);
      params.close_callback = std::move(callback);
      if (expected_result !=
          promo_controller_->MaybeShowPromo(std::move(params))) {
        LOG(ERROR) << "MaybeShowPromo() didn't return expected result.";
        return false;
      }
      if (expected_result != promo_controller_->IsPromoActive(iph_feature)) {
        LOG(ERROR) << "IsPromoActive() didn't return expected result.";
        return false;
      }

      // If shown, Tracker::Dismissed should be called eventually.
      if (expected_result) {
        EXPECT_CALL(*mock_tracker_, Dismissed(Ref(iph_feature)));
      }
      return true;
    });
  }

  auto AbortIPH(const base::Feature& iph_feature) {
    return Steps(Do(base::BindLambdaForTesting([this, &iph_feature]() {
      EXPECT_TRUE(promo_controller_->IsPromoActive(iph_feature));
      promo_controller_->EndPromo(iph_feature,
                                  FeaturePromoCloseReason::kAbortPromo);

      EXPECT_FALSE(promo_controller_->IsPromoActive(iph_feature));
    })));
  }

  auto CheckAlertStatus(ui::ElementIdentifier element_id, bool is_alerted) {
    return Check([this, element_id, is_alerted]() mutable {
      auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
      auto* toolbar = browser_view->toolbar();
      auto* button = toolbar->app_menu_button();
      auto* model = button->app_menu_model();
      EXPECT_EQ(model->IsElementIdAlerted(element_id), is_alerted);
      return true;
    });
  }

  auto CloseMenu() {
    return Do([this]() mutable {
      auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
      auto* toolbar = browser_view->toolbar();
      auto* button = toolbar->app_menu_button();
      button->CloseMenu();
    });
  }

 private:
  static void RegisterMockTracker(content::BrowserContext* context) {
    feature_engagement::TrackerFactory::GetInstance()->SetTestingFactory(
        context, base::BindRepeating(CreateMockTracker));
  }

  static std::unique_ptr<KeyedService> CreateMockTracker(
      content::BrowserContext* context) {
    auto mock_tracker =
        std::make_unique<NiceMock<feature_engagement::test::MockTracker>>();

    // Allow any other IPH to call, but don't ever show them.
    EXPECT_CALL(*mock_tracker, ShouldTriggerHelpUI(_))
        .Times(AnyNumber())
        .WillRepeatedly(Return(false));

    return mock_tracker;
  }

  UserEducationService* factory() {
    return UserEducationServiceFactory::GetForBrowserContext(
        browser()->profile());
  }

  FeaturePromoRegistry* registry() {
    return &factory()->feature_promo_registry();
  }

  raw_ptr<NiceMock<feature_engagement::test::MockTracker>,
          AcrossTasksDanglingUntriaged>
      mock_tracker_;
  raw_ptr<BrowserFeaturePromoController, AcrossTasksDanglingUntriaged>
      promo_controller_;
  base::CallbackListSubscription subscription_;
};

IN_PROC_BROWSER_TEST_F(BrowserAppMenuButtonInteractiveTest,
                       HighlightedIdentifierPromo) {
  RunTestSequence(
      InstrumentTab(kPrimaryTabPageElementId),
      AttemptIPH(kMenuPromoTestFeature,
                 user_education::FeaturePromoResult::Success()),
      PressButton(kToolbarAppMenuButtonElementId),
      SelectMenuItem(AppMenuModel::kBookmarksMenuItem),
      CheckAlertStatus(BookmarkSubMenuModel::kShowBookmarkSidePanelItem, true),
      CloseMenu(), PressButton(kToolbarAppMenuButtonElementId),
      SelectMenuItem(AppMenuModel::kBookmarksMenuItem),
      CheckAlertStatus(BookmarkSubMenuModel::kShowBookmarkSidePanelItem,
                       false));
}