File: footer_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (342 lines) | stat: -rw-r--r-- 13,507 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// 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 "base/test/scoped_feature_list.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/extensions/install_verifier.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/extensions/settings_api_bubble_helpers.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/new_tab_footer/footer_web_view.h"
#include "chrome/browser/ui/webui/new_tab_footer/footer_context_menu.h"
#include "chrome/browser/ui/webui/new_tab_footer/new_tab_footer_handler.h"
#include "chrome/browser/ui/webui/test_support/webui_interactive_test_mixin.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/policy/core/common/management/scoped_management_service_override_for_testing.h"
#include "components/search/ntp_features.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "extensions/test/test_extension_dir.h"

namespace {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kNewTabElementId);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kLocalFooterElementId);

using DeepQuery = WebContentsInteractionTestUtil::DeepQuery;
const DeepQuery kCustomizeChromeButton{
    "new-tab-footer-app", "ntp-customize-buttons", "#customizeButton"};

}  // namespace

class FooterInteractiveTest
    : public WebUiInteractiveTestMixin<InteractiveBrowserTest> {
 public:
  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{ntp_features::kNtpFooter},
        /*disabled_features=*/{features::kSideBySide});
    InteractiveBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    WebUiInteractiveTestMixin::SetUpOnMainThread();
    browser()->GetProfile()->GetPrefs()->SetBoolean(prefs::kNtpFooterVisible,
                                                    true);
  }

  void LoadNtpOverridingExtension() {
    extensions::TestExtensionDir extension_dir;
    extension_dir.WriteFile(FILE_PATH_LITERAL("ext.html"),
                            "<body>Extension-overridden NTP</body>");

    const char extension_manifest[] = R"(
       {
           "chrome_url_overrides": {
               "newtab": "ext.html"
           },
           "name": "Extension-overridden NTP",
           "manifest_version": 3,
           "version": "0.1"
         })";

    extension_dir.WriteManifest(extension_manifest);

    extensions::ChromeTestExtensionLoader extension_loader(
        browser()->profile());
    extension_loader.set_ignore_manifest_warnings(true);
    const extensions::Extension* extension =
        extension_loader.LoadExtension(extension_dir.Pack()).get();
    ASSERT_TRUE(extension);
  }

  InteractiveTestApi::MultiStep OpenCustomizeChromeSidePanel(
      const ui::ElementIdentifier& contents_id) {
    return Steps(Do(base::BindLambdaForTesting([=, this]() {
                   chrome::ExecuteCommand(browser(),
                                          IDC_SHOW_CUSTOMIZE_CHROME_SIDE_PANEL);
                 })),
                 InstrumentNonTabWebView(
                     contents_id, kCustomizeChromeSidePanelWebViewElementId));
  }

  InteractiveTestApi::MultiStep OpenSidePanel(
      const ui::ElementIdentifier& contents_id) {
    return Steps(
        EnsureNotPresent(kSidePanelElementId),
        ExecuteJsAt(contents_id, kCustomizeChromeButton, "el => el.click()"),
        WaitForShow(kSidePanelElementId));
  }

  InteractiveTestApi::MultiStep CloseSidePanel(
      const ui::ElementIdentifier& contents_id) {
    return Steps(
        EnsurePresent(kSidePanelElementId),
        ExecuteJsAt(contents_id, kCustomizeChromeButton, "el => el.click()"),
        WaitForHide(kSidePanelElementId));
  }

  InteractiveTestApi::MultiStep OpenContextMenuAndSelect(
      const ui::ElementIdentifier& menu_item_id) {
    // Disable the "NTP overridden" dialog as it can interfere with this
    // test.
    extensions::SetNtpPostInstallUiEnabledForTesting(false);
    const DeepQuery kFooterContainer = {"new-tab-footer-app", "#container"};
    return Steps(InstrumentNonTabWebView(kLocalFooterElementId, kNtpFooterId),
                 MoveMouseTo(kLocalFooterElementId, kFooterContainer),
                 ClickMouse(ui_controls::RIGHT), WaitForShow(menu_item_id),
                 SelectMenuItem(menu_item_id, InputType::kMouse));
  }

  InteractiveTestApi::MultiStep WaitForElementExists(
      const ui::ElementIdentifier& contents_id,
      const DeepQuery& element) {
    DEFINE_LOCAL_CUSTOM_ELEMENT_EVENT_TYPE(kElementExists);
    StateChange element_exists;
    element_exists.type = StateChange::Type::kExists;
    element_exists.where = element;
    element_exists.event = kElementExists;
    return WaitForStateChange(contents_id, element_exists);
  }

  new_tab_footer::NewTabFooterWebView* GetFooterView() {
    return browser()->GetBrowserView().new_tab_footer_web_view();
  }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
  extensions::ScopedInstallVerifierBypassForTest install_verifier_bypass_;
};

IN_PROC_BROWSER_TEST_F(FooterInteractiveTest, FooterShowsOnExtensionNtp) {
  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open extension NTP.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Ensure footer and footer separator are visible.
      Steps(WaitForShow(kNtpFooterId),
            EnsurePresent(kFooterWebViewSeparatorElementId)));
}

IN_PROC_BROWSER_TEST_F(FooterInteractiveTest, FooterHiddenOnNonExtensionNtp) {
  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open extension NTP.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Ensure footer shows.
      WaitForShow(kNtpFooterId),
      // Navigate to non-extension NTP.
      NavigateWebContents(kNewTabElementId, GURL("https://google.com")),
      // Ensure footer hides.
      WaitForHide(kNtpFooterId));
}

IN_PROC_BROWSER_TEST_F(FooterInteractiveTest,
                       ExtensionAttributionTogglesVisibility) {
  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open extension NTP.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Ensure footer shows.
      WaitForShow(kNtpFooterId),
      // Disable extension attribution policy.
      Do([=, this]() {
        browser()->profile()->GetPrefs()->SetBoolean(
            prefs::kNTPFooterExtensionAttributionEnabled, false);
      }),
      // Ensure footer hides.
      WaitForHide(kNtpFooterId));
}

IN_PROC_BROWSER_TEST_F(FooterInteractiveTest, OpenAndCloseCustomizeChrome) {
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kTabElementId1);
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kTabElementId2);
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kFooterElementId1);
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kFooterElementId2);

  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open the first tab.
      Steps(
          AddInstrumentedTab(kTabElementId1, GURL(chrome::kChromeUINewTabURL)),
          InstrumentNonTabWebView(kFooterElementId1, kNtpFooterId)),
      // Open the side panel in the first tab.
      OpenSidePanel(kFooterElementId1),
      // Open the second tab.
      Steps(
          AddInstrumentedTab(kTabElementId2, GURL(chrome::kChromeUINewTabURL)),
          InstrumentNonTabWebView(kFooterElementId2, kNtpFooterId)),
      // Open the side panel in the second tab.
      OpenSidePanel(kFooterElementId2),
      // Close the side panel in the second tab.
      CloseSidePanel(kFooterElementId2),
      // Switch to the first tab.
      SelectTab(kTabStripElementId, 1),
      // Close the side panel in the first tab.
      CloseSidePanel(kFooterElementId1));
}

// Context menu tests flaky on Mac, possibly due to the Mac handling of context
// menus.
#if !BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(FooterInteractiveTest, ContextMenuHidesFooter) {
  // Override the ntp with an extension.
  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open extension ntp.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Open context menu and select "hide footer" option.
      OpenContextMenuAndSelect(FooterContextMenu::kHideFooterIdForTesting),
      // Ensure footer hides.
      WaitForHide(kLocalFooterElementId));
}

IN_PROC_BROWSER_TEST_F(FooterInteractiveTest, ContextMenuOpensCustomizeChrome) {
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kLocalCustomizeChromeElementId);
  const DeepQuery kFooterSection = {"customize-chrome-app", "#footer",
                                    "customize-chrome-footer",
                                    "#showToggleContainer"};

  // Override the ntp with an extension.
  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open extension ntp.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Open context menu and select "customize chrome" option.
      OpenContextMenuAndSelect(
          FooterContextMenu::kShowCustomizeChromeIdForTesting),
      // Ensure customize chrome opens to footer section.
      Steps(
          InstrumentNonTabWebView(kLocalCustomizeChromeElementId,
                                  kCustomizeChromeSidePanelWebViewElementId,
                                  false),
          WaitForElementExists(kLocalCustomizeChromeElementId, kFooterSection),
          WaitForElementToRender(kLocalCustomizeChromeElementId,
                                 kFooterSection)));
}
#endif  // !BUILDFLAG(IS_MAC)

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
// Tests in this class will have a managed browser, unless the test disables it
// manually.
class FooterEnterpriseInteractiveTest : public FooterInteractiveTest {
 public:
  void SetUp() override {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{ntp_features::kNtpFooter,
                              features::kEnterpriseBadgingForNtpFooter},
        /*disabled_features=*/{features::kSideBySide});
    InteractiveBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    scoped_browser_management_ =
        std::make_unique<policy::ScopedManagementServiceOverrideForTesting>(
            policy::ManagementServiceFactory::GetForProfile(
                browser()->profile()),
            policy::EnterpriseManagementAuthority::DOMAIN_LOCAL);
    FooterInteractiveTest::SetUpOnMainThread();
  }

  void TearDownOnMainThread() override {
    scoped_browser_management_.reset();
    FooterInteractiveTest::TearDownOnMainThread();
  }

 private:
  std::unique_ptr<policy::ScopedManagementServiceOverrideForTesting>
      scoped_browser_management_;
};

IN_PROC_BROWSER_TEST_F(FooterEnterpriseInteractiveTest, FooterShowsOnNtpOnly) {
  LoadNtpOverridingExtension();
  RunTestSequence(
      // Open extension NTP.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Ensure footer shows.
      WaitForShow(kNtpFooterId),
      // Navigate to non-NTP.
      NavigateWebContents(kNewTabElementId, GURL("https://google.com")),
      // Ensure footer hides.
      WaitForHide(kNtpFooterId),
      // Navigate to 1P WebUI NTP.
      NavigateWebContents(kNewTabElementId,
                          GURL(chrome::kChromeUINewTabPageURL)),
      // Ensure footer shows.
      WaitForShow(kNtpFooterId));
}

IN_PROC_BROWSER_TEST_F(FooterEnterpriseInteractiveTest,
                       ManagementNoticePolicyTogglesVisibility) {
  RunTestSequence(
      // Open NTP.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Ensure footer shows.
      WaitForShow(kNtpFooterId),
      // Disable management notice policy.
      Do([=]() {
        g_browser_process->local_state()->SetBoolean(
            prefs::kNTPFooterManagementNoticeEnabled, false);
      }),
      // Ensure footer hides.
      WaitForHide(kNtpFooterId));
}

IN_PROC_BROWSER_TEST_F(FooterEnterpriseInteractiveTest,
                       CustomizationTogglesVisibility) {
  RunTestSequence(
      // Open NTP.
      AddInstrumentedTab(kNewTabElementId, GURL(chrome::kChromeUINewTabURL)),
      // Ensure footer shows.
      WaitForShow(kNtpFooterId),
      // Toggle off visibility.
      Do([=, this]() {
        browser()->GetProfile()->GetPrefs()->SetBoolean(
            prefs::kNtpFooterVisible, false);
      }),
      // Ensure footer hides.
      WaitForHide(kNtpFooterId),
      // Set a custom label policy.
      Do([=]() {
        g_browser_process->local_state()->SetString(
            prefs::kEnterpriseCustomLabelForBrowser, "Custom Label");
      }),
      // Ensure footer shows
      WaitForShow(kNtpFooterId),
      // Unset the custom label policy.
      Do([=]() {
        g_browser_process->local_state()->SetString(
            prefs::kEnterpriseCustomLabelForBrowser, "");
      }),
      // Ensure footer hides.
      WaitForHide(kNtpFooterId));
}
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)