File: immersive_mode_controller_chromeos_unittest.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 (311 lines) | stat: -rw-r--r-- 12,749 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
// Copyright 2013 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/views/frame/immersive_mode_controller_chromeos.h"

#include "ash/wm/window_pin_util.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/ui/ash/test_util.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view_chromeos.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/immersive_mode_tester.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "chrome/browser/ui/views/frame/top_container_view.h"
#include "chrome/browser/ui/views/frame/webui_tab_strip_container_view.h"
#include "chrome/browser/ui/views/fullscreen_control/fullscreen_control_host.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/ui/frame/immersive/immersive_fullscreen_controller_test_api.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "ui/aura/window.h"
#include "ui/base/pointer/touch_ui_controller.h"
#include "ui/events/event.h"
#include "ui/views/controls/native/native_view_host.h"
#include "ui/views/controls/webview/webview.h"

class ImmersiveModeControllerChromeosTest : public TestWithBrowserView {
 public:
  ImmersiveModeControllerChromeosTest()
      : TestWithBrowserView(Browser::TYPE_NORMAL) {}

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

  ~ImmersiveModeControllerChromeosTest() override = default;

  // TestWithBrowserView override:
  void SetUp() override {
    TestWithBrowserView::SetUp();

    browser()->window()->Show();

    controller_ = browser_view()->immersive_mode_controller();
    chromeos::ImmersiveFullscreenControllerTestApi(
        static_cast<ImmersiveModeControllerChromeos*>(controller_)
            ->controller())
        .SetupForTest();
  }

  // Returns the bounds of |view| in widget coordinates.
  gfx::Rect GetBoundsInWidget(views::View* view) {
    return view->ConvertRectToWidget(view->GetLocalBounds());
  }

  // Attempt revealing the top-of-window views.
  void AttemptReveal() {
    if (!revealed_lock_.get()) {
      revealed_lock_ = controller_->GetRevealedLock(
          ImmersiveModeControllerChromeos::ANIMATE_REVEAL_NO);
    }
  }

  // Attempt unrevealing the top-of-window views.
  void AttemptUnreveal() { revealed_lock_.reset(); }

  ImmersiveModeController* controller() { return controller_; }

 private:
  // Not owned.
  raw_ptr<ImmersiveModeController, DanglingUntriaged> controller_;

  std::unique_ptr<ImmersiveRevealedLock> revealed_lock_;
};

// Test the layout and visibility of the tabstrip, toolbar and TopContainerView
// in immersive fullscreen.
TEST_F(ImmersiveModeControllerChromeosTest, Layout) {
  AddTab(browser(), GURL("about:blank"));

  TabStrip* tabstrip = browser_view()->tabstrip();
  ToolbarView* toolbar = browser_view()->toolbar();
  views::WebView* contents_web_view = browser_view()->contents_web_view();

  // Immersive fullscreen starts out disabled.
  ASSERT_FALSE(browser_view()->GetWidget()->IsFullscreen());
  ASSERT_FALSE(controller()->IsEnabled());

  // By default, the tabstrip and toolbar should be visible.
  EXPECT_TRUE(tabstrip->GetVisible());
  EXPECT_TRUE(toolbar->GetVisible());
  EXPECT_EQ(
      0, browser_view()->contents_web_view()->holder()->GetHitTestTopInset());

  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  EXPECT_TRUE(browser_view()->GetWidget()->IsFullscreen());
  EXPECT_TRUE(controller()->IsEnabled());
  EXPECT_FALSE(controller()->IsRevealed());
  EXPECT_FALSE(toolbar->GetVisible());
  // The browser's top chrome is completely offscreen with tapstrip visible.
  EXPECT_TRUE(tabstrip->GetVisible());
  // Tabstrip and top container view should be completely offscreen.
  EXPECT_EQ(0, GetBoundsInWidget(tabstrip).bottom());
  EXPECT_EQ(0, GetBoundsInWidget(browser_view()->top_container()).bottom());
  EXPECT_EQ(
      0, browser_view()->contents_web_view()->holder()->GetHitTestTopInset());

  // Since the tab strip and tool bar are both hidden in immersive fullscreen
  // mode, the web contents should extend to the edge of screen.
  EXPECT_EQ(0, GetBoundsInWidget(contents_web_view).y());

  // Revealing the top-of-window views should set the tab strip back to the
  // normal style and show the toolbar.
  AttemptReveal();
  EXPECT_TRUE(controller()->IsRevealed());
  EXPECT_TRUE(tabstrip->GetVisible());
  EXPECT_TRUE(toolbar->GetVisible());
  EXPECT_NE(
      0, browser_view()->contents_web_view()->holder()->GetHitTestTopInset());

  // The TopContainerView should be flush with the top edge of the widget. If
  // it is not flush with the top edge the immersive reveal animation looks
  // wonky.
  EXPECT_EQ(0, GetBoundsInWidget(browser_view()->top_container()).y());

  // The web contents should be at the same y position as they were when the
  // top-of-window views were hidden.
  EXPECT_EQ(0, GetBoundsInWidget(contents_web_view).y());

  // Repeat the test for when in both immersive fullscreen and tab fullscreen.
  ChromeOSBrowserUITest::EnterTabFullscreenMode(
      browser(), browser_view()->contents_web_view()->GetWebContents());
  // Hide and reveal the top-of-window views so that they get relain out.
  AttemptUnreveal();
  AttemptReveal();

  // The tab strip and toolbar should still be visible and the TopContainerView
  // should still be flush with the top edge of the widget.
  EXPECT_TRUE(controller()->IsRevealed());
  EXPECT_TRUE(tabstrip->GetVisible());
  EXPECT_TRUE(toolbar->GetVisible());
  EXPECT_EQ(0, GetBoundsInWidget(browser_view()->top_container()).y());

  // The web contents should be flush with the top edge of the widget when in
  // both immersive and tab fullscreen.
  EXPECT_EQ(0, GetBoundsInWidget(contents_web_view).y());

  // Hide the top-of-window views. Tabstrip is still considered as visible.
  AttemptUnreveal();
  EXPECT_FALSE(controller()->IsRevealed());
  EXPECT_FALSE(toolbar->GetVisible());
  EXPECT_TRUE(tabstrip->GetVisible());

  // The web contents should still be flush with the edge of the widget.
  EXPECT_EQ(0, GetBoundsInWidget(contents_web_view).y());

  // Exiting both immersive and tab fullscreen should show the tab strip and
  // toolbar.
  ChromeOSBrowserUITest::ExitImmersiveFullscreenMode(browser());
  EXPECT_EQ(
      0, browser_view()->contents_web_view()->holder()->GetHitTestTopInset());
  EXPECT_FALSE(browser_view()->GetWidget()->IsFullscreen());
  EXPECT_FALSE(controller()->IsEnabled());
  EXPECT_FALSE(controller()->IsRevealed());
  EXPECT_TRUE(tabstrip->GetVisible());
  EXPECT_TRUE(toolbar->GetVisible());
}

// Verifies that transitioning from fullscreen to trusted pinned disables the
// immersive controls.
TEST_F(ImmersiveModeControllerChromeosTest, FullscreenToLockedTransition) {
  AddTab(browser(), GURL("about:blank"));
  // Start in fullscreen.
  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  // ImmersiveController is enabled in fullscreen.
  EXPECT_TRUE(controller()->IsEnabled());

  // Transition to locked fullscreen.
  ChromeOSBrowserUITest::PinWindow(
      browser_view()->GetWidget()->GetNativeWindow(), /*trusted=*/true);
  // ImmersiveController is disabled in TrustedPinned so that it cannot be
  // exited.
  EXPECT_FALSE(controller()->IsEnabled());
}

// Verifies that transitioning from fullscreen to trusted pinned keeps immersive
// controls when the webapp is locked for OnTask. Only relevant for non-web
// browser scenarios.
TEST_F(ImmersiveModeControllerChromeosTest,
       FullscreenToLockedTransitionWhenLockedForOnTask) {
  browser()->SetLockedForOnTask(true);
  AddTab(browser(), GURL("about:blank"));
  // Start in fullscreen and verify ImmersiveController is enabled.
  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  EXPECT_TRUE(controller()->IsEnabled());

  // Transition to locked fullscreen and verify ImmersiveController remains
  // enabled.
  ChromeOSBrowserUITest::PinWindow(
      browser_view()->GetWidget()->GetNativeWindow(), /*trusted=*/true);
  EXPECT_TRUE(controller()->IsEnabled());
}

// Test that the browser commands which are usually disabled in fullscreen are
// are enabled in immersive fullscreen.
TEST_F(ImmersiveModeControllerChromeosTest, EnabledCommands) {
  ASSERT_FALSE(controller()->IsEnabled());
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPEN_CURRENT_URL));
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ABOUT));
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_LOCATION));

  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  EXPECT_TRUE(controller()->IsEnabled());
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_OPEN_CURRENT_URL));
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_ABOUT));
  EXPECT_TRUE(chrome::IsCommandEnabled(browser(), IDC_FOCUS_LOCATION));
}

// Test that restoring a window properly exits immersive fullscreen.
TEST_F(ImmersiveModeControllerChromeosTest, ExitUponRestore) {
  ASSERT_FALSE(controller()->IsEnabled());
  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  AttemptReveal();
  ASSERT_TRUE(controller()->IsEnabled());
  ASSERT_TRUE(controller()->IsRevealed());
  ASSERT_TRUE(browser_view()->GetWidget()->IsFullscreen());

  browser_view()->GetWidget()->Restore();
  ImmersiveModeTester(browser()).WaitForFullscreenToExit();
}

// Ensure the circular tab-loading throbbers are not painted as layers in
// immersive fullscreen, since the tab strip may animate in or out without
// moving the layers.
TEST_F(ImmersiveModeControllerChromeosTest, LayeredSpinners) {
  AddTab(browser(), GURL("about:blank"));

  TabStrip* tabstrip = browser_view()->tabstrip();

  // Immersive fullscreen starts out disabled; layers are OK.
  EXPECT_FALSE(browser_view()->GetWidget()->IsFullscreen());
  EXPECT_FALSE(controller()->IsEnabled());
  EXPECT_TRUE(tabstrip->CanPaintThrobberToLayer());

  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  EXPECT_TRUE(browser_view()->GetWidget()->IsFullscreen());
  EXPECT_TRUE(controller()->IsEnabled());
  EXPECT_FALSE(tabstrip->CanPaintThrobberToLayer());

  ChromeOSBrowserUITest::ExitImmersiveFullscreenMode(browser());
  EXPECT_TRUE(tabstrip->CanPaintThrobberToLayer());
}

// Ensure SetEnable is called when needed even when the previous request is
// passed from different client.
TEST_F(ImmersiveModeControllerChromeosTest, CallEnableForWidgetWhenNeeded) {
  ASSERT_FALSE(controller()->IsEnabled());
  chromeos::ImmersiveFullscreenController::EnableForWidget(
      browser_view()->frame(), /*enabled=*/true);
  ASSERT_TRUE(controller()->IsEnabled());
  controller()->SetEnabled(/*enabled=*/false);
  ASSERT_FALSE(controller()->IsEnabled());
}

class ImmersiveModeControllerChromeosWebUITabStripTest
    : public ImmersiveModeControllerChromeosTest {
 public:
  ImmersiveModeControllerChromeosWebUITabStripTest() {
    scoped_feature_list_.InitAndEnableFeature(features::kWebUITabStrip);
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Ensures the WebUI tab strip can be opened during immersive reveal.
// Regression test for crbug.com/1096569 where it couldn't be opened.
TEST_F(ImmersiveModeControllerChromeosWebUITabStripTest, CanOpen) {
  AddTab(browser(), GURL("about:blank"));

  // The WebUI tab strip is only used in touch mode.
  ui::TouchUiController::TouchUiScoperForTesting touch_mode_override(true);

  WebUITabStripContainerView* const webui_tab_strip =
      browser_view()->webui_tab_strip();
  ASSERT_TRUE(webui_tab_strip);
  EXPECT_FALSE(webui_tab_strip->GetVisible());

  ChromeOSBrowserUITest::EnterImmersiveFullscreenMode(browser());
  EXPECT_FALSE(webui_tab_strip->GetVisible());

  AttemptReveal();
  EXPECT_FALSE(webui_tab_strip->GetVisible());

  webui_tab_strip->SetVisibleForTesting(true);

  // The WebUITabStrip should be layed out.
  browser_view()->GetWidget()->LayoutRootViewIfNecessary();
  EXPECT_TRUE(webui_tab_strip->GetVisible());
  EXPECT_FALSE(webui_tab_strip->size().IsEmpty());
}