File: webui_tab_strip_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 (356 lines) | stat: -rw-r--r-- 14,736 bytes parent folder | download | duplicates (6)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright 2019 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/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/immersive_mode_controller.h"
#include "chrome/browser/ui/views/frame/webui_tab_strip_container_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
#include "chrome/browser/ui/views/toolbar/reload_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/interaction/interaction_test_util_browser.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "chrome/test/interaction/webcontents_interaction_test_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/expect_call_in_scope.h"
#include "ui/base/interaction/interaction_sequence.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/pointer/touch_ui_controller.h"
#include "ui/base/test/ui_controls.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/controls/webview/webview.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "base/scoped_observation.h"
#include "chromeos/ui/frame/immersive/immersive_fullscreen_controller.h"
#include "chromeos/ui/frame/immersive/immersive_fullscreen_controller_test_api.h"
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/drag_drop_client_observer.h"
#include "ui/aura/window.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace {
class WebUITabStripTestHelper {
 public:
  WebUITabStripTestHelper() {
    feature_override_.InitAndEnableFeature(features::kWebUITabStrip);
  }

  ~WebUITabStripTestHelper() = default;

 private:
  base::test::ScopedFeatureList feature_override_;
  ui::TouchUiController::TouchUiScoperForTesting touch_ui_scoper_{true};
};
}  // namespace

class WebUITabStripInteractiveTest : public InProcessBrowserTest {
 public:
  WebUITabStripInteractiveTest() = default;
  ~WebUITabStripInteractiveTest() override = default;

 private:
  WebUITabStripTestHelper helper_;
};

// Regression test for crbug.com/1027375.
IN_PROC_BROWSER_TEST_F(WebUITabStripInteractiveTest,
                       CanTypeInOmniboxAfterTabStripClose) {
  BrowserView* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser());
  WebUITabStripContainerView* const container = browser_view->webui_tab_strip();
  ASSERT_NE(nullptr, container);

  ui_test_utils::FocusView(browser(), VIEW_ID_OMNIBOX);
  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));

  OmniboxViewViews* const omnibox =
      browser_view->toolbar()->location_bar()->omnibox_view();
  omnibox->SetUserText(u"");

  container->SetVisibleForTesting(true);
  RunScheduledLayouts();

  EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));

  // Make sure the tab strip's contents are fully loaded.
  views::WebView* const container_web_view = container->web_view_for_testing();
  ASSERT_TRUE(WaitForLoadStop(container_web_view->GetWebContents()));

  // Click in tab strip then in Omnibox.
  base::RunLoop click_loop_1;
  ui_test_utils::MoveMouseToCenterAndClick(
      container_web_view, ui_controls::LEFT,
      ui_controls::DOWN | ui_controls::UP, click_loop_1.QuitClosure());
  click_loop_1.Run();

  base::RunLoop click_loop_2;
  ui_test_utils::MoveMouseToCenterAndClick(omnibox, ui_controls::LEFT,
                                           ui_controls::DOWN | ui_controls::UP,
                                           click_loop_2.QuitClosure());
  click_loop_2.Run();

  // The omnibox should still be focused and should accept keyboard input.
  EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
  EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_A, false,
                                              false, false, false));
  EXPECT_EQ(u"a", omnibox->GetText());
}

IN_PROC_BROWSER_TEST_F(WebUITabStripInteractiveTest,
                       EventInTabContentClosesContainer) {
  BrowserView* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser());

  WebUITabStripContainerView* const container = browser_view->webui_tab_strip();
  ASSERT_NE(nullptr, container);

  // Open the tab strip
  container->SetVisibleForTesting(true);
  RunScheduledLayouts();

  base::RunLoop click_loop;
  ui_test_utils::MoveMouseToCenterAndClick(
      browser_view->contents_web_view(), ui_controls::LEFT,
      ui_controls::DOWN | ui_controls::UP, click_loop.QuitClosure());
  click_loop.Run();

  // Make sure it's closed (after the close animation).
  container->FinishAnimationForTesting();
  EXPECT_FALSE(container->GetVisible());
}

IN_PROC_BROWSER_TEST_F(WebUITabStripInteractiveTest,
                       EventInContainerDoesNotClose) {
  BrowserView* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser());

  WebUITabStripContainerView* const container = browser_view->webui_tab_strip();
  ASSERT_NE(nullptr, container);

  // Open the tab strip
  container->SetVisibleForTesting(true);
  RunScheduledLayouts();

  base::RunLoop click_loop;
  ui_test_utils::MoveMouseToCenterAndClick(container, ui_controls::LEFT,
                                           ui_controls::DOWN | ui_controls::UP,
                                           click_loop.QuitClosure());
  click_loop.Run();

  // Make sure it stays open. The FinishAnimationForTesting() call
  // should be a no-op.
  container->FinishAnimationForTesting();
  EXPECT_TRUE(container->GetVisible());
  EXPECT_FALSE(container->bounds().IsEmpty());
}

#if BUILDFLAG(IS_CHROMEOS)

// Regression test for crbug.com/1112028
IN_PROC_BROWSER_TEST_F(WebUITabStripInteractiveTest, CanUseInImmersiveMode) {
  BrowserView* const browser_view =
      BrowserView::GetBrowserViewForBrowser(browser());

  chromeos::ImmersiveFullscreenControllerTestApi immersive_test_api(
      chromeos::ImmersiveFullscreenController::Get(browser_view->GetWidget()));
  immersive_test_api.SetupForTest();

  ImmersiveModeController* const immersive_mode_controller =
      browser_view->immersive_mode_controller();
  immersive_mode_controller->SetEnabled(true);

  WebUITabStripContainerView* const container = browser_view->webui_tab_strip();
  ASSERT_NE(nullptr, container);

  EXPECT_FALSE(immersive_mode_controller->IsRevealed());

  // Try opening the tab strip.
  container->SetVisibleForTesting(true);
  RunScheduledLayouts();
  EXPECT_TRUE(container->GetVisible());
  EXPECT_FALSE(container->bounds().IsEmpty());
  EXPECT_TRUE(immersive_mode_controller->IsRevealed());

  // Tapping in the tab strip shouldn't hide the toolbar.
  base::RunLoop click_loop_1;
  ui_test_utils::MoveMouseToCenterAndClick(container, ui_controls::LEFT,
                                           ui_controls::DOWN | ui_controls::UP,
                                           click_loop_1.QuitClosure());
  click_loop_1.Run();

  // If the behavior is correct, this call will be a no-op.
  container->FinishAnimationForTesting();
  EXPECT_TRUE(container->GetVisible());
  EXPECT_FALSE(container->bounds().IsEmpty());
  EXPECT_TRUE(immersive_mode_controller->IsRevealed());

  // Interacting with the toolbar should also not close the container.
  base::RunLoop click_loop_2;
  ui_test_utils::MoveMouseToCenterAndClick(
      browser_view->toolbar()->reload_button(), ui_controls::LEFT,
      ui_controls::DOWN | ui_controls::UP, click_loop_2.QuitClosure());
  click_loop_2.Run();

  container->FinishAnimationForTesting();
  EXPECT_TRUE(container->GetVisible());
  EXPECT_FALSE(container->bounds().IsEmpty());
  EXPECT_TRUE(immersive_mode_controller->IsRevealed());
}

// Test fixture with additional logic for drag/drop.
class WebUITabStripDragInteractiveTest
    : public InteractiveBrowserTest,
      public testing::WithParamInterface<bool> {
 public:
  WebUITabStripDragInteractiveTest() = default;
  ~WebUITabStripDragInteractiveTest() override = default;

 private:
  WebUITabStripTestHelper helper_;
};

// Touch mode parameter, only supported by the test framework on Ash.
#if BUILDFLAG(IS_CHROMEOS)
INSTANTIATE_TEST_SUITE_P(/* no prefix */,
                         WebUITabStripDragInteractiveTest,
                         testing::Bool());
#else
INSTANTIATE_TEST_SUITE_P(/* no prefix */,
                         WebUITabStripDragInteractiveTest,
                         testing::Values(false));
#endif

// Regression test for crbug.com/1286203.
//
// The original bug was a UAF that happened when a tab closed itself (e.g. via
// javascript) during a drag from the WebUI tabstrip; not all references to the
// tab were properly cleaned up.
//
// There is already a proposed regression test for this bug using existing
// technology; see:
//   https://chromium-review.googlesource.com/c/chromium/src/+/3588859
//
// This is a proof-of-concept for regression testing using InteractionSequence,
// which demonstrates that:
//  - tests can be written without arbitrary (and often flaky) delays
//  - tests can be end-to-end interacting with both native and WebUI code
//  - tests can be written to reproduce very specific test cases
//
// This framework can be used to handle many similar types of bugs, for both
// WebUI and Views elements. These tests, while more verbose, can be made very
// specific and are declarative and event-driven. This particular test performs
// the following steps:
//  1. opens a second tab in the browser
//  2. clicks the tab counter button to open the WebUI tabstrip
//  3. drags the second tab out of the WebUI tabstrip
//  4. without finishing the drag, closes the tab via script
//  5. verifies the tab actually closed
//  6. completes the drag
//
// This sequence of events would crash without the associated bugfix. More
// detail is provided in the actual test sequence.

#if BUILDFLAG(IS_CHROMEOS)
// TODO(crbug.com/40883259): Flaky on linux-chromeos-chrome. Reenable
// this test when the flakiness will be resolved.
#define MAYBE_CloseTabDuringDragDoesNotCrash \
  DISABLED_CloseTabDuringDragDoesNotCrash
#else
#define MAYBE_CloseTabDuringDragDoesNotCrash CloseTabDuringDragDoesNotCrash
#endif  // BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_P(WebUITabStripDragInteractiveTest,
                       MAYBE_CloseTabDuringDragDoesNotCrash) {
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kSecondTabElementId);
  DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebUiTabStripElementId);

  auto* const browser_view = BrowserView::GetBrowserViewForBrowser(browser());

  // This is the DeepQuery path to the second tab element in the WebUI tabstrip.
  // modified to reflect a new page structure.
  const DeepQuery kSecondTabQuery{"tabstrip-tab-list",
                                  "tabstrip-tab + tabstrip-tab"};

  // It takes a while for tab data to be filled out in the tabstrip. Before it
  // is fully loaded the tabs have zero visible size, so wait until they are the
  // expected size.
  DEFINE_LOCAL_CUSTOM_ELEMENT_EVENT_TYPE(kTabPopulatedCustomEvent);
  StateChange tab_populated_change;
  tab_populated_change.event = kTabPopulatedCustomEvent;
  tab_populated_change.where = kSecondTabQuery;
  tab_populated_change.type = StateChange::Type::kExistsAndConditionTrue;
  tab_populated_change.test_function =
      "el => (el.getBoundingClientRect().width > 0)";

  // Provide a way to get a reasonable target for a tab drag that is guaranteed
  // to be outside the tabstrip.
  auto get_point_not_in_tabstrip = base::BindLambdaForTesting([browser_view]() {
    return browser_view->contents_web_view()->bounds().CenterPoint();
  });

  // Close a tab from within its own javascript.
  auto close_tab = base::BindRepeating([](ui::TrackedElement* tab) {
    AsInstrumentedWebContents(tab)->Execute("() => window.close()");
  });

  auto get_tab_count = base::BindRepeating(
      [](Browser* browser) { return browser->tab_strip_model()->count(); },
      base::Unretained(browser()));

  auto get_tabstrip_webview =
      base::BindLambdaForTesting([browser_view]() -> views::View* {
        // The WebUI tabstrip can be created dynamically, so wait until the
        // browser is re-laid-out to bind the associated WebUI.
        browser_view->GetWidget()->LayoutRootViewIfNecessary();
        return browser_view->webui_tab_strip()->web_view_for_testing();
      });

  RunTestSequence(
      // Toggle touch mode to send either mouse or touch events.
      Check([this]() { return mouse_util().SetTouchMode(GetParam()); }),
      AddInstrumentedTab(kSecondTabElementId, GURL("about:blank")),
      // Click the counter button and then wait for the WebUI tabstrip to
      // appear.
      PressButton(kToolbarTabCounterButtonElementId),
      InstrumentNonTabWebView(kWebUiTabStripElementId, get_tabstrip_webview),
      // Verify there are two tabs.
      CheckResult(get_tab_count, 2),
      // Wait for the WebUI tabstrip contents to populate.
      WaitForStateChange(kWebUiTabStripElementId, tab_populated_change),
      // Now that the tab is properly rendered, drag it out of the tabstrip.
      MoveMouseTo(kWebUiTabStripElementId, kSecondTabQuery),
      // Drag to the center of the main web contents pane, which should be
      // sufficiently outside the tabstrip. Do not release the drag.
      DragMouseTo(get_point_not_in_tabstrip, /* release =*/false),
      // The tab is not removed from the tabstrip until the drag completes.
      // Verify the count and close the tab.
      CheckResult(get_tab_count, 2),
      WithElement(kSecondTabElementId, close_tab),
      // Wait for the dragged tab to be closed, and verify the tab count is
      // updated.
      //
      // Transition only on event means the test will fail if the tab goes
      // away before this step is queued; it will only succeed if the tab
      // disappears specifically in response to the previous step.
      WaitForHide(kSecondTabElementId, /* transition_only_on_event =*/true),
      CheckResult(get_tab_count, 1),
      // Finish the drag to clean up.
      ReleaseMouse());
}

#endif  // BUILDFLAG(IS_CHROMEOS)