File: toast_controller_interactive_ui_test.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 (582 lines) | stat: -rw-r--r-- 24,684 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// 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 <memory>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/app/chrome_command_ids.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/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/omnibox/omnibox_tab_helper.h"
#include "chrome/browser/ui/toasts/api/toast_id.h"
#include "chrome/browser/ui/toasts/toast_controller.h"
#include "chrome/browser/ui/toasts/toast_features.h"
#include "chrome/browser/ui/toasts/toast_view.h"
#include "chrome/browser/ui/views/frame/app_menu_button.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/star_view.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 "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_view.h"
#include "components/plus_addresses/features.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/interactive_test.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/animation/animation_test_api.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/interaction/interactive_views_test.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"

#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif

namespace {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kFirstTab);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kSecondTab);

class OmniboxInputWaiter : public OmniboxTabHelper::Observer {
 public:
  explicit OmniboxInputWaiter(content::WebContents* web_contents) {
    omnibox_helper_observer_.Observe(
        OmniboxTabHelper::FromWebContents(web_contents));

    run_loop_ = std::make_unique<base::RunLoop>(
        base::RunLoop::Type::kNestableTasksAllowed);
  }
  ~OmniboxInputWaiter() override = default;

  void Wait() { run_loop_->Run(); }

  void OnOmniboxInputStateChanged() override {}

  void OnOmniboxInputInProgress(bool in_progress) override {
    run_loop_->Quit();
  }

  void OnOmniboxFocusChanged(OmniboxFocusState state,
                             OmniboxFocusChangeReason reason) override {}

  void OnOmniboxPopupVisibilityChanged(bool popup_is_open) override {}

 private:
  std::unique_ptr<base::RunLoop> run_loop_;
  base::ScopedObservation<OmniboxTabHelper, OmniboxTabHelper::Observer>
      omnibox_helper_observer_{this};
};

DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kSampleMenuItem);

// Simplified menu model with a single item that runs `closure` on selecting it.
class TestMenuModel : public ui::SimpleMenuModel,
                      ui::SimpleMenuModel::Delegate {
 public:
  explicit TestMenuModel(base::RepeatingClosure closure)
      : ui::SimpleMenuModel(/*delegate=*/this), closure_(std::move(closure)) {
    AddItem(kCommandId, u"Some entry");
    SetElementIdentifierAt(0, kSampleMenuItem);
  }

  // ui::SimpleMenuModel::Delegate:
  void ExecuteCommand(int command_id, int event_flags) override {
    ASSERT_EQ(command_id, kCommandId);
    closure_.Run();
  }

 private:
  static constexpr int kCommandId = 123;

  base::RepeatingClosure closure_;
};

}  // namespace

class ToastControllerInteractiveTest : public InteractiveBrowserTest {
 public:
  void SetUp() override {
    feature_list_.InitWithFeatures(
        {toast_features::kLinkCopiedToast, toast_features::kImageCopiedToast,
         toast_features::kReadingListToast,
         toast_features::kPinnedTabToastOnClose,
         plus_addresses::features::kPlusAddressesEnabled},
        {});
    InteractiveBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    InteractiveBrowserTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  GURL GetURL(std::string_view hostname = "example.com",
              std::string_view path = "/title1.html") {
    return embedded_test_server()->GetURL(hostname, path);
  }

  ToastController* GetToastController() {
    return browser()->browser_window_features()->toast_controller();
  }


  auto ShowToast(ToastParams params) {
    return Do(base::BindOnce(
        [](ToastController* toast_controller, ToastParams toast_params) {
          toast_controller->MaybeShowToast(std::move(toast_params));
        },
        GetToastController(), std::move(params)));
  }

  auto FireToastCloseTimer() {
    return Do([=, this]() {
      GetToastController()->GetToastCloseTimerForTesting()->FireNow();
    });
  }

  auto CheckShowingToastId(ToastId expected_id) {
    return CheckResult(
        [=, this]() {
          ToastController* const toast_controller = GetToastController();
          std::optional<ToastId> current_toast_id =
              toast_controller->GetCurrentToastId();
          return current_toast_id.value();
        },
        expected_id);
  }

  auto AdvanceKeyboardFocus(bool reverse) {
    return Do([this, reverse]() {
      ASSERT_TRUE(ui_test_utils::SendKeyPressSync(
          browser(), ui::VKEY_TAB, false, reverse, false, false));
    });
  }

  void RemoveOmniboxFocus() {
    ui_test_utils::ClickOnView(
        BrowserView::GetBrowserViewForBrowser(browser())->contents_web_view());
  }

 private:
  base::test::ScopedFeatureList feature_list_;
};

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, ShowToast) {
  RunTestSequence(
      ShowToast(ToastParams(ToastId::kLinkCopied)),
      WaitForShow(toasts::ToastView::kToastViewId),
      Check([=, this]() { return GetToastController()->IsShowingToast(); }));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, ShowSameToastTwice) {
  RunTestSequence(
      ShowToast(ToastParams(ToastId::kLinkCopied)),
      WaitForShow(toasts::ToastView::kToastViewId),
      Check([=, this]() { return GetToastController()->IsShowingToast(); }),
      ShowToast(ToastParams(ToastId::kLinkCopied)),
      WaitForShow(toasts::ToastView::kToastViewId),
      Check([=, this]() { return GetToastController()->IsShowingToast(); }));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, PreemptToast) {
  RunTestSequence(
      ShowToast(ToastParams(ToastId::kLinkCopied)),
      WaitForShow(toasts::ToastView::kToastViewId),
      Check([=, this]() { return GetToastController()->IsShowingToast(); }),
      ShowToast(ToastParams(ToastId::kImageCopied)));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, FocusNextPane) {
  ui::Accelerator next_pane;
  ASSERT_TRUE(BrowserView::GetBrowserViewForBrowser(browser())->GetAccelerator(
      IDC_FOCUS_NEXT_PANE, &next_pane));
  views::Widget* toast_widget = nullptr;
  RunTestSequence(
      ObserveState(views::test::kCurrentWidgetFocus),
      ShowToast(ToastParams(ToastId::kAddedToReadingList)),
      WaitForShow(toasts::ToastView::kToastViewId),
      WithView(
          toasts::ToastView::kToastViewId,
          [&](toasts::ToastView* toast) { toast_widget = toast->GetWidget(); }),
      CheckView(toasts::ToastView::kToastViewId,
                [](toasts::ToastView* toast) {
                  return !toast->GetFocusManager()->GetFocusedView();
                }),
      SendAccelerator(kBrowserViewElementId, next_pane),
      WaitForState(views::test::kCurrentWidgetFocus,
                   [&]() { return toast_widget->GetNativeView(); }),
      CheckView(toasts::ToastView::kToastViewId, [](toasts::ToastView* toast) {
        return toast->GetFocusManager()->GetFocusedView() ==
               toast->action_button_for_testing();
      }));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, ReverseFocusTraversal) {
  ui::Accelerator next_pane;
  ASSERT_TRUE(BrowserView::GetBrowserViewForBrowser(browser())->GetAccelerator(
      IDC_FOCUS_NEXT_PANE, &next_pane));
  RunTestSequence(
      ObserveState(views::test::kCurrentWidgetFocus),
      ShowToast(ToastParams(ToastId::kAddedToReadingList)),
      WaitForShow(toasts::ToastView::kToastViewId),
      ActivateSurface(toasts::ToastView::kToastViewId),
      SendAccelerator(kBrowserViewElementId, next_pane),
      CheckView(toasts::ToastView::kToastViewId,
                [](toasts::ToastView* toast) {
                  return toast->GetFocusManager()->GetFocusedView() ==
                         toast->action_button_for_testing();
                }),
      AdvanceKeyboardFocus(true),
#if BUILDFLAG(IS_MAC)
      // Mac focus traversal order is slightly different from other platforms
      CheckView(kToolbarAppMenuButtonElementId,
                [](AppMenuButton* button) { return button->HasFocus(); })
#else
        CheckView(kBookmarkStarViewElementId,
                [](StarView* star_view) { return star_view->HasFocus(); })
#endif
  );
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, ForwardFocusTraversal) {
  ui::Accelerator next_pane;
  ASSERT_TRUE(BrowserView::GetBrowserViewForBrowser(browser())->GetAccelerator(
      IDC_FOCUS_NEXT_PANE, &next_pane));
  RunTestSequence(
      ObserveState(views::test::kCurrentWidgetFocus),
      ShowToast(ToastParams(ToastId::kAddedToReadingList)),
      WaitForShow(toasts::ToastView::kToastViewId),
      ActivateSurface(toasts::ToastView::kToastViewId),
      SendAccelerator(kBrowserViewElementId, next_pane),
      // Advancing focus should move into the toast close button
      AdvanceKeyboardFocus(false),
      CheckView(toasts::ToastView::kToastViewId,
                [](toasts::ToastView* toast) {
                  return toast->close_button_for_testing()->HasFocus();
                }),
      // Advancing focus again should move out of the toast and into the WebView
      AdvanceKeyboardFocus(false),
      CheckView(toasts::ToastView::kToastViewId,
                [](toasts::ToastView* toast) {
                  return !toast->close_button_for_testing()->HasFocus();
                }),
      CheckView(kBrowserViewElementId, [](BrowserView* browser_view) {
        return browser_view->GetContentsWebView()->HasFocus();
      }));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       HideTabScopedToastOnTabChange) {
  RunTestSequence(InstrumentTab(kFirstTab),
                  AddInstrumentedTab(kSecondTab, GetURL()),
                  SelectTab(kTabStripElementId, 0), WaitForShow(kFirstTab),
                  ShowToast(ToastParams(ToastId::kLinkCopied)),
                  WaitForShow(toasts::ToastView::kToastViewId),
                  SelectTab(kTabStripElementId, 1),
                  WaitForHide(toasts::ToastView::kToastViewId));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       GlobalScopedToastStaysOnTabChange) {
  RunTestSequence(InstrumentTab(kFirstTab),
                  AddInstrumentedTab(kSecondTab, GetURL()),
                  SelectTab(kTabStripElementId, 0), WaitForShow(kFirstTab),
                  ShowToast(ToastParams(ToastId::kNonMilestoneUpdate)),
                  WaitForShow(toasts::ToastView::kToastViewId),
                  SelectTab(kTabStripElementId, 1),
                  EnsurePresent(toasts::ToastView::kToastViewId));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       HideTabScopedToastOnNavigation) {
  RunTestSequence(InstrumentTab(kFirstTab),
                  ShowToast(ToastParams(ToastId::kLinkCopied)),
                  WaitForShow(toasts::ToastView::kToastViewId),
                  NavigateWebContents(kFirstTab, GetURL()),
                  WaitForHide(toasts::ToastView::kToastViewId));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       GlobalScopedToastStaysOnNavigation) {
  RunTestSequence(InstrumentTab(kFirstTab),
                  ShowToast(ToastParams(ToastId::kNonMilestoneUpdate)),
                  WaitForShow(toasts::ToastView::kToastViewId),
                  NavigateWebContents(kFirstTab, GetURL()),
                  EnsurePresent(toasts::ToastView::kToastViewId));
}

// Tests that setting a menu model in `ToastParams` adds a menu button to the
// toast that runs the menu model and that interacting with a menu element
// closes the toast.
IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       MenuButtonClickOpensMenu) {
  ToastParams params(ToastId::kPlusAddressOverride);
  int counter = 0;
  params.menu_model = std::make_unique<TestMenuModel>(
      base::BindLambdaForTesting([&counter]() { ++counter; }));
  RunTestSequence(ShowToast(std::move(params)),
                  WaitForShow(toasts::ToastView::kToastViewId),
                  EnsurePresent(toasts::ToastView::kToastMenuButton),
                  PressButton(toasts::ToastView::kToastMenuButton),
                  WaitForShow(kSampleMenuItem), SelectMenuItem(kSampleMenuItem),
                  WaitForHide(toasts::ToastView::kToastViewId),
                  Check([&]() { return counter == 1; }));
}

// Tests that attempting to close the `ToastView` does not succeed while the
// menu is open. If that happens, the `ToastView` is closed once the menu
// closes.

// TODO(crbug.com/398296825): Flaky on Windows builds.
#if BUILDFLAG(IS_WIN)
#define MAYBE_ToastDoesNotCloseWhileMenuIsOpen \
  DISABLED_ToastDoesNotCloseWhileMenuIsOpen
#else
#define MAYBE_ToastDoesNotCloseWhileMenuIsOpen ToastDoesNotCloseWhileMenuIsOpen
#endif
IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       MAYBE_ToastDoesNotCloseWhileMenuIsOpen) {
#if BUILDFLAG(IS_OZONE)
  if (ui::OzonePlatform::GetPlatformNameForTest() == "wayland") {
    GTEST_SKIP() << "Flaky in Wayland due to way events are routed and bounds "
                    "are reported";
  }
#endif
  ToastParams params(ToastId::kPlusAddressOverride);
  params.menu_model = std::make_unique<TestMenuModel>(base::DoNothing());
  RunTestSequence(ShowToast(std::move(params)),
                  WaitForShow(toasts::ToastView::kToastViewId),
                  EnsurePresent(toasts::ToastView::kToastMenuButton),
                  PressButton(toasts::ToastView::kToastMenuButton),
                  WaitForShow(kSampleMenuItem),
                  EnsurePresent(toasts::ToastView::kToastViewId),
                  FireToastCloseTimer(),
                  EnsurePresent(toasts::ToastView::kToastViewId),
                  MoveMouseTo(toasts::ToastView::kToastMenuButton),
                  ClickMouse(), WaitForHide(toasts::ToastView::kToastViewId));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       ToastReactToOmniboxFocus) {
  LocationBar* const location_bar = browser()->window()->GetLocationBar();
  ASSERT_TRUE(location_bar);
  OmniboxView* const omnibox_view = location_bar->GetOmniboxView();
  ASSERT_TRUE(omnibox_view);
  browser()->window()->SetFocusToLocationBar(true);
  ASSERT_FALSE(omnibox_view->model()->PopupIsOpen());

  // Even though the omnibox is focused, the toast should still show because
  // the omnibox doesn't have a popup and the user isn't interacting with the
  // omnibox.
  ToastController* const toast_controller = GetToastController();
  EXPECT_TRUE(
      toast_controller->MaybeShowToast(ToastParams(ToastId::kLinkCopied)));
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_TRUE(toast_controller->GetToastWidgetForTesting()->IsVisible());

  // Omnibox should still show even when focus is removed from the omnibox.
  RemoveOmniboxFocus();
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_TRUE(toast_controller->GetToastWidgetForTesting()->IsVisible());

  // Focus the omnibox again should cause the toast to no longer be visible
  // because we are focusing after the toast is already shown.
  browser()->window()->SetFocusToLocationBar(true);
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_FALSE(toast_controller->GetToastWidgetForTesting()->IsVisible());
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       HidesWhenOmniboxPopupShows) {
  // Even though the omnibox is focused, the toast should still show because
  // the omnibox doesn't have a popup and the user isn't interacting with the
  // omnibox.
  ToastController* const toast_controller = GetToastController();
  EXPECT_TRUE(
      toast_controller->MaybeShowToast(ToastParams(ToastId::kLinkCopied)));
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_TRUE(toast_controller->GetToastWidgetForTesting()->IsVisible());

  // Trigger the omnibox popup to show.
  LocationBar* const location_bar = browser()->window()->GetLocationBar();
  ASSERT_TRUE(location_bar);
  OmniboxView* const omnibox_view = location_bar->GetOmniboxView();
  ASSERT_TRUE(omnibox_view);
  ASSERT_FALSE(omnibox_view->model()->PopupIsOpen());
  omnibox_view->OnBeforePossibleChange();
  omnibox_view->SetUserText(u"hello world");
  omnibox_view->OnAfterPossibleChange(true);

  ASSERT_TRUE(omnibox_view->model()->PopupIsOpen());

  // The toast widget should no longer be visible because there is a popup.
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_FALSE(toast_controller->GetToastWidgetForTesting()->IsVisible());

  // Toast widget is visible again after the omnibox is no longer focused.
  RemoveOmniboxFocus();
  ASSERT_FALSE(omnibox_view->model()->PopupIsOpen());
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_TRUE(toast_controller->GetToastWidgetForTesting()->IsVisible());
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       HidesWhenTypingInOmnibox) {
  browser()->window()->SetFocusToLocationBar(true);

  // Even though the omnibox is focused, the toast should still show because
  // the omnibox doesn't have a popup and the user isn't interacting with the
  // omnibox.
  ToastController* const toast_controller = GetToastController();
  EXPECT_TRUE(
      toast_controller->MaybeShowToast(ToastParams(ToastId::kLinkCopied)));
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_TRUE(toast_controller->GetToastWidgetForTesting()->IsVisible());

  // Start typing in the omnibox.
  auto omnibox_input_waiter = std::make_unique<OmniboxInputWaiter>(
      browser()->tab_strip_model()->GetActiveWebContents());
  ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_A, false,
                                              false, false, false));
  omnibox_input_waiter->Wait();

  // The toast widget should no longer be visible because we are typing.
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_FALSE(toast_controller->GetToastWidgetForTesting()->IsVisible());

  // Toast widget is visible again after the omnibox is no longer focused.
  RemoveOmniboxFocus();
  EXPECT_TRUE(toast_controller->IsShowingToast());
  EXPECT_TRUE(toast_controller->GetToastWidgetForTesting()->IsVisible());
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest, RecordToastShows) {
  base::HistogramTester histogram_tester;
  RunTestSequence(
      InstrumentTab(kFirstTab), AddInstrumentedTab(kSecondTab, GetURL()),
      SelectTab(kTabStripElementId, 0), WaitForShow(kFirstTab), Do([&]() {
        histogram_tester.ExpectBucketCount("Toast.TriggeredToShow",
                                           ToastId::kLinkCopied, 0);
      }),
      ShowToast(ToastParams(ToastId::kLinkCopied)),
      WaitForShow(toasts::ToastView::kToastViewId), Do([&]() {
        histogram_tester.ExpectBucketCount("Toast.TriggeredToShow",
                                           ToastId::kLinkCopied, 1);
      }));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       RecordToastDismissReason) {
  base::HistogramTester histogram_tester;
  RunTestSequence(
      InstrumentTab(kFirstTab), AddInstrumentedTab(kSecondTab, GetURL()),
      SelectTab(kTabStripElementId, 0), WaitForShow(kFirstTab), Do([&]() {
        histogram_tester.ExpectBucketCount("Toast.LinkCopied.Dismissed",
                                           toasts::ToastCloseReason::kAbort, 0);
      }),
      ShowToast(ToastParams(ToastId::kLinkCopied)),
      WaitForShow(toasts::ToastView::kToastViewId),
      SelectTab(kTabStripElementId, 1),
      WaitForHide(toasts::ToastView::kToastViewId), Do([&]() {
        histogram_tester.ExpectBucketCount("Toast.LinkCopied.Dismissed",
                                           toasts::ToastCloseReason::kAbort, 1);
      }));
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       ToastRendersOverWebContents) {
#if BUILDFLAG(IS_MAC)
  FullscreenController* const fullscreen_controller =
      browser()
          ->GetFeatures()
          .exclusive_access_manager()
          ->fullscreen_controller();
  fullscreen_controller->set_is_tab_fullscreen_for_testing(true);
#else
  ui_test_utils::FullscreenWaiter waiter(browser(), {.tab_fullscreen = true});
  content::WebContents* const active_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  active_contents->GetDelegate()->EnterFullscreenModeForTab(
      active_contents->GetPrimaryMainFrame(), {});
  waiter.Wait();
#endif

  ToastController* const toast_controller = GetToastController();
  EXPECT_TRUE(
      toast_controller->MaybeShowToast(ToastParams(ToastId::kLinkCopied)));
  const gfx::Rect toast_bounds =
      toast_controller->GetToastViewForTesting()->GetBoundsInScreen();
  const gfx::Rect web_view_bounds =
      BrowserView::GetBrowserViewForBrowser(browser())
          ->GetContentsWebView()
          ->GetBoundsInScreen();
  EXPECT_TRUE(web_view_bounds.Contains(toast_bounds));
}

// Regression test for http://crbug.com/383898425
IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       HandlesReducedAnimation) {
  ToastController* const toast_controller = GetToastController();
  {
    // Show toast while rich animations are disabled.
    const gfx::AnimationTestApi::RenderModeResetter disable_rich_animations =
        gfx::AnimationTestApi::SetRichAnimationRenderMode(
            gfx::Animation::RichAnimationRenderMode::FORCE_DISABLED);
    gfx::Animation::SetPrefersReducedMotionForTesting(true);
    EXPECT_TRUE(gfx::Animation::PrefersReducedMotion());
    EXPECT_TRUE(
        toast_controller->MaybeShowToast(ToastParams(ToastId::kLinkCopied)));
  }
  {
    // Animate out toast (due to new toast showing) after enabling animations.
    const gfx::AnimationTestApi::RenderModeResetter disable_rich_animations =
        gfx::AnimationTestApi::SetRichAnimationRenderMode(
            gfx::Animation::RichAnimationRenderMode::FORCE_ENABLED);
    EXPECT_TRUE(
        toast_controller->MaybeShowToast(ToastParams(ToastId::kLinkCopied)));
  }
}

IN_PROC_BROWSER_TEST_F(ToastControllerInteractiveTest,
                       ShowPinnedTabToastOnTabCloseViaKeyboardShortcut) {
  ui::Accelerator close_tab_accelerator;
  ASSERT_TRUE(BrowserView::GetBrowserViewForBrowser(browser())->GetAccelerator(
      IDC_CLOSE_TAB, &close_tab_accelerator));

  RunTestSequence(
      // Add a pinned tab.
      InstrumentTab(kFirstTab), WaitForShow(kFirstTab),
      AddInstrumentedTab(kSecondTab, GetURL()),
      SelectTab(kTabStripElementId, 0),
      Do([&]() { browser()->tab_strip_model()->SetTabPinned(0, true); }),
      // Expect that closing the tab with an accelerator will show a toast.
      SendAccelerator(kBrowserViewElementId, close_tab_accelerator),
      WaitForShow(toasts::ToastView::kToastViewId),
      CheckResult([&]() { return browser()->tab_strip_model()->count(); }, 2),
      // Expect that we can close the tab by pressing the accelerator again.
      SendAccelerator(kBrowserViewElementId, close_tab_accelerator),
      WaitForHide(toasts::ToastView::kToastViewId),
      CheckResult([&]() { return browser()->tab_strip_model()->count(); }, 1));
}