File: browser_view_interactive_uitest.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (610 lines) | stat: -rw-r--r-- 25,072 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// Copyright 2015 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/strings/string_util.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_bubble_type.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/page_action/page_action_view.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/input/native_web_keyboard_event.h"
#include "components/tabs/public/split_tab_visual_data.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/ui_base_features.h"
#include "ui/ozone/public/ozone_platform.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/buildflags.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/widget_activation_waiter.h"
#include "ui/views/widget/widget_interactive_uitest_utils.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_commands_mac.h"
#include "chrome/test/base/interactive_test_utils.h"
#endif

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/wm/window_pin_util.h"
#endif

using views::FocusManager;

namespace {

class BrowserViewTest : public InProcessBrowserTest {
 public:
  BrowserViewTest() : ax_observer_(views::AXUpdateNotifier::Get()) {
    feature_list_.InitAndEnableFeature(features::kSideBySide);
  }
  ~BrowserViewTest() override = default;
  BrowserViewTest(const BrowserViewTest&) = delete;
  BrowserViewTest& operator=(const BrowserViewTest&) = delete;

  BrowserView* browser_view() {
    return BrowserView::GetBrowserViewForBrowser(browser());
  }

  void SetUpOnMainThread() override {
#if BUILDFLAG(IS_MAC)
    chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), true);
    // Ensure that the browser window is activated. BrowserView::Show calls
    // into BridgedNativeWidgetImpl::SetVisibilityState and makeKeyAndOrderFront
    // there somehow does not change the window's key status on bot.
    ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
#endif
  }

  void TearDownOnMainThread() override {
#if BUILDFLAG(IS_MAC)
    chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), true);
#endif
  }

 protected:
  views::test::AXEventCounter ax_observer_;
  base::test::ScopedFeatureList feature_list_;
};

}  // namespace

IN_PROC_BROWSER_TEST_F(BrowserViewTest, FullscreenClearsFocus) {
  BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());
  LocationBarView* location_bar_view = browser_view->GetLocationBarView();
  FocusManager* focus_manager = browser_view->GetFocusManager();

  // Focus starts in the location bar or one of its children.
  EXPECT_TRUE(location_bar_view->Contains(focus_manager->GetFocusedView()));

  // Enter into fullscreen mode.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_TRUE(browser_view->IsFullscreen());

  // Focus is released from the location bar.
  EXPECT_FALSE(location_bar_view->Contains(focus_manager->GetFocusedView()));
}

// Test whether the top view including toolbar and tab strip shows up or hides
// correctly in browser fullscreen mode.
IN_PROC_BROWSER_TEST_F(BrowserViewTest, BrowserFullscreenShowTopView) {
  BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());

  // The top view should always show up in regular mode.
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->GetTabStripVisible());

  // Enter into fullscreen mode.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_TRUE(browser_view->IsFullscreen());

  bool top_view_in_browser_fullscreen = false;
#if BUILDFLAG(IS_MAC)
  // The top view should show up by default.
  EXPECT_TRUE(browser_view->GetTabStripVisible());
  // The 'Always Show Bookmarks Bar' should be enabled.
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_SHOW_BOOKMARK_BAR));

  // Return back to normal mode and toggle to not show the top view in full
  // screen mode.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_FALSE(browser_view->IsFullscreen());
  // Disable 'Always Show Toolbar in Full Screen'.
  chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), false);

  // While back to fullscreen mode, the top view no longer shows up.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_TRUE(browser_view->IsFullscreen());
  EXPECT_FALSE(browser_view->GetTabStripVisible());
  // In non-immersive mode, the bookmark visibility cannot be changed because
  // the toolbar is invisible. In immersive mode, the bookmark visibility should
  // be able to change because the toolbar cannot be permanently hidden.
  EXPECT_EQ(chrome::IsCommandEnabled(browser(), IDC_SHOW_BOOKMARK_BAR),
            base::FeatureList::IsEnabled(features::kImmersiveFullscreen));

  if (browser_view->immersive_mode_controller()->IsEnabled()) {
    // Move mouse to the upper border of the browser window and the toolbar
    // should become visible.
    ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
        browser_view->GetBoundsInScreen().top_center()));
    views::test::PropertyWaiter(
        base::BindRepeating(&BrowserView::GetTabStripVisible,
                            base::Unretained(browser_view)),
        true)
        .Wait();
    EXPECT_TRUE(browser_view->GetTabStripVisible());
  }

  // Test toggling toolbar while being in fullscreen mode.
  chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), true);
  EXPECT_TRUE(browser_view->IsFullscreen());
  top_view_in_browser_fullscreen = true;
#else
  // In immersive fullscreen mode, the top view should show up; otherwise, it
  // always hides.
  if (browser_view->immersive_mode_controller()->IsEnabled()) {
    top_view_in_browser_fullscreen = true;
  }
#endif
  EXPECT_EQ(top_view_in_browser_fullscreen, browser_view->GetTabStripVisible());
  // The 'Always Show Bookmarks Bar' should be enabled if top view is shown.
  EXPECT_EQ(top_view_in_browser_fullscreen,
            chrome::IsCommandEnabled(browser(), IDC_SHOW_BOOKMARK_BAR));

  // Enter into tab fullscreen mode from browser fullscreen mode.
  FullscreenController* controller = browser()
                                         ->GetFeatures()
                                         .exclusive_access_manager()
                                         ->fullscreen_controller();
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  controller->EnterFullscreenModeForTab(web_contents->GetPrimaryMainFrame());
  EXPECT_TRUE(browser_view->IsFullscreen());
  bool top_view_in_tab_fullscreen =
      browser_view->immersive_mode_controller()->IsEnabled();
  EXPECT_EQ(top_view_in_tab_fullscreen, browser_view->GetTabStripVisible());
  // The 'Always Show Bookmarks Bar' should be disabled in tab fullscreen mode.
  EXPECT_EQ(top_view_in_tab_fullscreen,
            chrome::IsCommandEnabled(browser(), IDC_SHOW_BOOKMARK_BAR));

  // Return back to browser fullscreen mode.
  input::NativeWebKeyboardEvent event(
      blink::WebInputEvent::Type::kKeyDown, blink::WebInputEvent::kNoModifiers,
      blink::WebInputEvent::GetStaticTimeStampForTests());
  event.windows_key_code = ui::VKEY_ESCAPE;
  browser()->GetFeatures().exclusive_access_manager()->HandleUserKeyEvent(
      event);
  EXPECT_TRUE(browser_view->IsFullscreen());
  EXPECT_EQ(top_view_in_browser_fullscreen, browser_view->GetTabStripVisible());
  // This makes sure that the layout was updated accordingly.
  EXPECT_EQ(top_view_in_browser_fullscreen,
            browser_view->tabstrip()->GetVisible());
  EXPECT_EQ(top_view_in_browser_fullscreen,
            chrome::IsCommandEnabled(browser(), IDC_SHOW_BOOKMARK_BAR));

  // Return to regular mode.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->GetTabStripVisible());
}

// Test whether the top view including toolbar and tab strip appears or hides
// correctly in tab fullscreen mode.
IN_PROC_BROWSER_TEST_F(BrowserViewTest, TabFullscreenShowTopView) {
  BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());

  // The top view should always show up in regular mode.
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->GetTabStripVisible());

  // Enter into tab fullscreen mode.
  FullscreenController* controller = browser()
                                         ->GetFeatures()
                                         .exclusive_access_manager()
                                         ->fullscreen_controller();
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  controller->EnterFullscreenModeForTab(web_contents->GetPrimaryMainFrame());
  EXPECT_TRUE(browser_view->IsFullscreen());

  // The top view should not show up.
  EXPECT_TRUE(views::test::PropertyWaiter(
                  base::BindRepeating(&BrowserView::GetTabStripVisible,
                                      base::Unretained(browser_view)),
                  false)
                  .Wait());

  // After exiting the fullscreen mode, the top view should show up again.
  controller->ExitFullscreenModeForTab(web_contents);
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->GetTabStripVisible());
}

// Test whether the split view is hidden when a tab is fullscreened.
IN_PROC_BROWSER_TEST_F(BrowserViewTest, TabFullscreenHideSplitView) {
  // Add a second tab and create a split
  chrome::AddTabAt(browser(), GURL(), -1, true);
  browser()->tab_strip_model()->ActivateTabAt(0);
  browser()->tab_strip_model()->AddToNewSplit({1},
                                              split_tabs::SplitTabVisualData());

  BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());

  // Split view should be open
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->IsInSplitView());

  // Enter into tab fullscreen mode.
  FullscreenController* controller = browser()
                                         ->GetFeatures()
                                         .exclusive_access_manager()
                                         ->fullscreen_controller();
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  controller->EnterFullscreenModeForTab(web_contents->GetPrimaryMainFrame());
  EXPECT_TRUE(browser_view->IsFullscreen());

  // The split view should be closed.
  EXPECT_FALSE(browser_view->IsInSplitView());
  EXPECT_TRUE(views::test::PropertyWaiter(
                  base::BindRepeating(&BrowserView::GetTabStripVisible,
                                      base::Unretained(browser_view)),
                  false)
                  .Wait());

  // After exiting the fullscreen mode, the top view should show up again.
  controller->ExitFullscreenModeForTab(web_contents);
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->IsInSplitView());
}

// Test whether bookmark bar shows up or hides correctly for fullscreen modes.
IN_PROC_BROWSER_TEST_F(BrowserViewTest, FullscreenShowBookmarkBar) {
  BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());

  // If the bookmark bar is not showing, enable showing it so that we can check
  // its state.
  if (!browser_view->IsBookmarkBarVisible()) {
    chrome::ToggleBookmarkBar(browser());
  }
#if BUILDFLAG(IS_MAC)
  // Disable showing toolbar in fullscreen mode to make its behavior similar to
  // other platforms.
  chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), false);
#endif
  ASSERT_TRUE(AddTabAtIndex(0, GURL("about:blank"), ui::PAGE_TRANSITION_TYPED));

  // Now the bookmark bar should show up in regular mode.
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->IsBookmarkBarVisible());

  // Enter into fullscreen mode.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_TRUE(browser_view->IsFullscreen());

  // Move to the center of the window so that the toolbar becomes hidden in
  // immersive mode.
  ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
      browser_view->GetBoundsInScreen().CenterPoint()));
  views::test::PropertyWaiter(
      base::BindRepeating(&BrowserView::IsBookmarkBarVisible,
                          base::Unretained(browser_view)),
      false)
      .Wait();
  EXPECT_FALSE(browser_view->GetTabStripVisible());
  EXPECT_FALSE(browser_view->IsBookmarkBarVisible());

#if BUILDFLAG(IS_MAC)
  // Test toggling toolbar state in fullscreen mode would also affect bookmark
  // bar state.
  chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), true);
  EXPECT_TRUE(browser_view->GetTabStripVisible());
  EXPECT_TRUE(browser_view->IsBookmarkBarVisible());

  chrome::SetAlwaysShowToolbarInFullscreenForTesting(browser(), false);
  EXPECT_FALSE(browser_view->GetTabStripVisible());
  EXPECT_FALSE(browser_view->IsBookmarkBarVisible());
#endif

  // Exit from fullscreen mode.
  ui_test_utils::ToggleFullscreenModeAndWait(browser());
  EXPECT_FALSE(browser_view->IsFullscreen());
  EXPECT_TRUE(browser_view->GetTabStripVisible());
  EXPECT_TRUE(browser_view->IsBookmarkBarVisible());
}

// TODO(crbug.com/40598906): Only Aura platforms use the WindowActivated
// accessibility event. We need to harmonize the firing of accessibility events
// between platforms.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
IN_PROC_BROWSER_TEST_F(BrowserViewTest, WindowActivatedAccessibleEvent) {
  // Wait for window activated event from the first browser window.
  // This event is asynchronous, it is emitted as a response to a system window
  // event. It is possible that we haven't received it yet when we run this test
  // and we need to explicitly wait for it.
  if (ax_observer_.GetCount(ax::mojom::Event::kWindowActivated) == 0) {
    ax_observer_.WaitForEvent(ax::mojom::Event::kWindowActivated);
  }
  ASSERT_EQ(1, ax_observer_.GetCount(ax::mojom::Event::kWindowActivated));

  // Create a new browser window and wait for event again.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
  if (ax_observer_.GetCount(ax::mojom::Event::kWindowActivated) == 1) {
    ax_observer_.WaitForEvent(ax::mojom::Event::kWindowActivated);
  }
  ASSERT_EQ(2, ax_observer_.GetCount(ax::mojom::Event::kWindowActivated));
}
#endif

#if BUILDFLAG(IS_MAC)
// TODO(crbug.com/40568702) NativeWidgetMac::Deactivate is not implemented on
// Mac.
#define MAYBE_FocusInactivePopupForAccessibility \
  DISABLED_FocusInactivePopupForAccessibility
#else
#define MAYBE_FocusInactivePopupForAccessibility \
  FocusInactivePopupForAccessibility
#endif
IN_PROC_BROWSER_TEST_F(BrowserViewTest,
                       MAYBE_FocusInactivePopupForAccessibility) {
  std::unique_ptr<ui::DialogModel> dialog_model =
      ui::DialogModel::Builder()
          .SetTitle(u"test")
          .SetIsAlertDialog()
          .AddOkButton(base::DoNothing())
          .Build();
  views::View* anchor = browser_view()->GetLocationBarView();
  auto bubble = std::make_unique<views::BubbleDialogModelHost>(
      std::move(dialog_model), anchor, views::BubbleBorder::TOP_RIGHT);
  bubble->set_close_on_deactivate(false);
  views::Widget* widget =
      views::BubbleDialogDelegate::CreateBubble(std::move(bubble));

  widget->Show();
  views::test::WaitForWidgetActive(widget, true);

  widget->Deactivate();
  widget->ShowInactive();
  EXPECT_TRUE(widget->IsVisible());
  EXPECT_FALSE(widget->IsActive());

  browser_view()->FocusInactivePopupForAccessibility();
  views::test::WaitForWidgetActive(widget, true);

  // Ensure the bubble's widget refreshed appropriately.
  EXPECT_TRUE(widget->IsVisible());
  EXPECT_TRUE(widget->IsActive());
}

class BrowserViewFullscreenTest : public BrowserViewTest {
 public:
  BrowserViewFullscreenTest() {
    feature_list_.InitAndEnableFeature(features::kAsyncFullscreenWindowState);
  }

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

// Disabled on platforms where async fullscreen state transition is not
// yet supported.
// TODO(b/40276379): Apply this to all remaining desktop platforms.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_Fullscreen Fullscreen
#else
#define MAYBE_Fullscreen DISABLED_Fullscreen
#endif
IN_PROC_BROWSER_TEST_F(BrowserViewFullscreenTest, MAYBE_Fullscreen) {
#if BUILDFLAG(IS_LINUX)
  // Skip non wayland cases, now, such as X11, where the fullscreen
  // call async support is not yet completed.
  if (ui::OzonePlatform::GetPlatformNameForTest() != "wayland") {
    GTEST_SKIP();
  }
#endif

  BrowserView* browser_view = static_cast<BrowserView*>(browser()->window());

  // The top view should always show up in regular mode.
  EXPECT_FALSE(browser_view->IsFullscreen());

  // Enter into fullscreen mode.
  {
    ui_test_utils::FullscreenWaiter waiter(browser(),
                                           {.browser_fullscreen = true});
    chrome::ToggleFullscreenMode(browser());
    // The state won't change immediately.
    EXPECT_FALSE(browser_view->IsFullscreen());
    waiter.Wait();
    EXPECT_TRUE(browser_view->IsFullscreen());
  }

  // Exit from fullscreen mode.
  {
    ui_test_utils::FullscreenWaiter waiter(browser(),
                                           {.browser_fullscreen = false});
    chrome::ToggleFullscreenMode(browser());
    // The state won't change immediately.
    EXPECT_TRUE(browser_view->IsFullscreen());
    waiter.Wait();
    EXPECT_FALSE(browser_view->IsFullscreen());
  }
}

// TODO(b/342017720): Re-enable on Mac
#if BUILDFLAG(IS_MAC)
#define MAYBE_LoadingAnimationChangeOnMinimizeAndRestore \
  DISABLED_LoadingAnimationChangeOnMinimizeAndRestore
#else
#define MAYBE_LoadingAnimationChangeOnMinimizeAndRestore \
  LoadingAnimationChangeOnMinimizeAndRestore
#endif  // BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(BrowserViewTest,
                       MAYBE_LoadingAnimationChangeOnMinimizeAndRestore) {
  auto* contents = browser()->tab_strip_model()->GetActiveWebContents();
  content::TestNavigationObserver navigation_watcher(
      contents, 1, content::MessageLoopRunner::QuitMode::DEFERRED);

  // Navigate without blocking.
  const GURL test_url = ui_test_utils::GetTestUrl(
      base::FilePath(base::FilePath::kCurrentDirectory),
      base::FilePath(FILE_PATH_LITERAL("title2.html")));
  contents->GetController().LoadURL(test_url, content::Referrer(),
                                    ui::PAGE_TRANSITION_LINK, std::string());
  {
    base::RunLoop run_loop;
    browser_view()->SetLoadingAnimationStateChangeClosureForTesting(
        run_loop.QuitClosure());

    // Loading animation is not rendered when browser view is minimized.
    browser_view()->Minimize();
    run_loop.Run();
  }

  EXPECT_TRUE(browser()->tab_strip_model()->TabsNeedLoadingUI());
  EXPECT_FALSE(browser_view()->IsLoadingAnimationRunning());

  {
    base::RunLoop run_loop;
    browser_view()->SetLoadingAnimationStateChangeClosureForTesting(
        run_loop.QuitClosure());

    // Loading animation is rendered when browser view is restored.
    browser_view()->Restore();
    run_loop.Run();
  }

  EXPECT_TRUE(browser()->tab_strip_model()->TabsNeedLoadingUI());
  EXPECT_TRUE(browser_view()->IsLoadingAnimationRunning());

  // Now block for the navigation to complete.
  navigation_watcher.Wait();
  EXPECT_FALSE(browser()->tab_strip_model()->TabsNeedLoadingUI());
}

// On Mac, voiceover treats tab modal dialogs as native windows, so setting an
// accessible title for tab-modal dialogs is not necessary.
#if !BUILDFLAG(IS_MAC)
namespace {

class TestTabModalConfirmDialogDelegate : public TabModalConfirmDialogDelegate {
 public:
  explicit TestTabModalConfirmDialogDelegate(content::WebContents* contents)
      : TabModalConfirmDialogDelegate(contents) {}

  TestTabModalConfirmDialogDelegate(const TestTabModalConfirmDialogDelegate&) =
      delete;
  TestTabModalConfirmDialogDelegate& operator=(
      const TestTabModalConfirmDialogDelegate&) = delete;

  std::u16string GetTitle() override { return std::u16string(u"Dialog Title"); }
  std::u16string GetDialogMessage() override { return std::u16string(); }
};

}  // namespace

// Open a tab-modal dialog and check that the accessible window title is the
// title of the dialog. The accessible window title is based on the focused
// dialog and this dependency on focus is why this is an interactive ui test.
IN_PROC_BROWSER_TEST_F(BrowserViewTest, GetAccessibleTabModalDialogTitle) {
  std::u16string window_title =
      u"about:blank - " + l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
  EXPECT_TRUE(base::StartsWith(browser_view()->GetAccessibleWindowTitle(),
                               window_title, base::CompareCase::SENSITIVE));

  content::WebContents* contents = browser_view()->GetActiveWebContents();
  auto delegate = std::make_unique<TestTabModalConfirmDialogDelegate>(contents);
  TestTabModalConfirmDialogDelegate* delegate_observer = delegate.get();
  TabModalConfirmDialog::Create(std::move(delegate), contents);
  EXPECT_EQ(browser_view()->GetAccessibleWindowTitle(),
            delegate_observer->GetTitle());

  delegate_observer->Close();

  EXPECT_TRUE(base::StartsWith(browser_view()->GetAccessibleWindowTitle(),
                               window_title, base::CompareCase::SENSITIVE));
}
#endif  // !BUILDFLAG(IS_MAC)

#if BUILDFLAG(IS_CHROMEOS)
using BrowserViewLockedFullscreenTestChromeOS = BrowserViewTest;

IN_PROC_BROWSER_TEST_F(BrowserViewLockedFullscreenTestChromeOS,
                       ShowExclusiveAccessBubbleWhenNotLocked) {
  PinWindow(browser()->window()->GetNativeWindow(), /*trusted=*/false);
  browser()
      ->GetFeatures()
      .exclusive_access_manager()
      ->context()
      ->UpdateExclusiveAccessBubble(
          {
              .origin = url::Origin::Create(
                  GURL("http://www.example.com")),  // Should be non-empty to
                                                    // show bubble
              .type = ExclusiveAccessBubbleType::
                  EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION,
              .force_update = true,
          },
          base::NullCallback());
  EXPECT_TRUE(browser_view()->IsExclusiveAccessBubbleDisplayed());
}

IN_PROC_BROWSER_TEST_F(BrowserViewLockedFullscreenTestChromeOS,
                       HideExclusiveAccessBubbleWhenLocked) {
  PinWindow(browser()->window()->GetNativeWindow(), /*trusted=*/true);
  browser()
      ->GetFeatures()
      .exclusive_access_manager()
      ->context()
      ->UpdateExclusiveAccessBubble(
          {.origin = url::Origin::Create(
               GURL("http://www.example.com")),  // Should be non-empty to show
                                                 // bubble
           .type = ExclusiveAccessBubbleType::
               EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_EXIT_INSTRUCTION,
           .force_update = true},
          base::NullCallback());
  EXPECT_FALSE(browser_view()->IsExclusiveAccessBubbleDisplayed());
}

IN_PROC_BROWSER_TEST_F(BrowserViewLockedFullscreenTestChromeOS,
                       EnableImmersiveModeWhenNotTrustedPinned) {
  PinWindow(browser()->window()->GetNativeWindow(), /*trusted=*/false);
  EXPECT_TRUE(browser_view()->immersive_mode_controller()->IsEnabled());
}

IN_PROC_BROWSER_TEST_F(BrowserViewLockedFullscreenTestChromeOS,
                       DisableImmersiveModeWhenNotLockedForOnTask) {
  browser()->SetLockedForOnTask(false);
  PinWindow(browser()->window()->GetNativeWindow(), /*trusted=*/true);
  EXPECT_FALSE(browser_view()->immersive_mode_controller()->IsEnabled());
}

IN_PROC_BROWSER_TEST_F(BrowserViewLockedFullscreenTestChromeOS,
                       EnableImmersiveModeWhenLockedForOnTask) {
  browser()->SetLockedForOnTask(true);
  PinWindow(browser()->window()->GetNativeWindow(), /*trusted=*/true);
  EXPECT_TRUE(browser_view()->immersive_mode_controller()->IsEnabled());
}
#endif  // BUILDFLAG(IS_CHROMEOS)