ui_lock_controller_unittest.cc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/exo/ui_lock_controller.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/shell.h"
  7. #include "ash/wm/window_state.h"
  8. #include "base/feature_list.h"
  9. #include "base/test/power_monitor_test.h"
  10. #include "base/test/scoped_feature_list.h"
  11. #include "chromeos/dbus/power/fake_power_manager_client.h"
  12. #include "chromeos/dbus/power/power_manager_client.h"
  13. #include "chromeos/dbus/power_manager/backlight.pb.h"
  14. #include "chromeos/ui/base/window_properties.h"
  15. #include "components/exo/buffer.h"
  16. #include "components/exo/display.h"
  17. #include "components/exo/pointer.h"
  18. #include "components/exo/pointer_constraint_delegate.h"
  19. #include "components/exo/pointer_delegate.h"
  20. #include "components/exo/shell_surface.h"
  21. #include "components/exo/surface.h"
  22. #include "components/exo/test/exo_test_base.h"
  23. #include "components/exo/test/exo_test_helper.h"
  24. #include "components/exo/test/shell_surface_builder.h"
  25. #include "components/exo/wm_helper.h"
  26. #include "components/fullscreen_control/fullscreen_control_popup.h"
  27. #include "testing/gmock/include/gmock/gmock.h"
  28. #include "testing/gtest/include/gtest/gtest.h"
  29. #include "ui/base/class_property.h"
  30. #include "ui/gfx/animation/animation_test_api.h"
  31. #include "ui/wm/core/window_util.h"
  32. namespace exo {
  33. namespace {
  34. constexpr char kNoEscHoldAppId[] = "no-esc-hold";
  35. constexpr char kOverviewToExitAppId[] = "overview-to-exit";
  36. aura::Window* GetTopLevelWindow(
  37. const std::unique_ptr<ShellSurface>& shell_surface) {
  38. auto* top_level_widget = views::Widget::GetTopLevelWidgetForNativeView(
  39. shell_surface->host_window());
  40. assert(top_level_widget);
  41. return top_level_widget->GetNativeWindow();
  42. }
  43. ash::WindowState* GetTopLevelWindowState(
  44. const std::unique_ptr<ShellSurface>& shell_surface) {
  45. return ash::WindowState::Get(GetTopLevelWindow(shell_surface));
  46. }
  47. class MockPointerDelegate : public PointerDelegate {
  48. public:
  49. MockPointerDelegate() {
  50. EXPECT_CALL(*this, CanAcceptPointerEventsForSurface(testing::_))
  51. .WillRepeatedly(testing::Return(true));
  52. }
  53. // Overridden from PointerDelegate:
  54. MOCK_METHOD1(OnPointerDestroying, void(Pointer*));
  55. MOCK_CONST_METHOD1(CanAcceptPointerEventsForSurface, bool(Surface*));
  56. MOCK_METHOD3(OnPointerEnter, void(Surface*, const gfx::PointF&, int));
  57. MOCK_METHOD1(OnPointerLeave, void(Surface*));
  58. MOCK_METHOD2(OnPointerMotion, void(base::TimeTicks, const gfx::PointF&));
  59. MOCK_METHOD3(OnPointerButton, void(base::TimeTicks, int, bool));
  60. MOCK_METHOD3(OnPointerScroll,
  61. void(base::TimeTicks, const gfx::Vector2dF&, bool));
  62. MOCK_METHOD1(OnPointerScrollStop, void(base::TimeTicks));
  63. MOCK_METHOD0(OnPointerFrame, void());
  64. };
  65. class MockPointerConstraintDelegate : public PointerConstraintDelegate {
  66. public:
  67. MockPointerConstraintDelegate(Pointer* pointer, Surface* surface)
  68. : pointer_(pointer) {
  69. EXPECT_CALL(*this, GetConstrainedSurface())
  70. .WillRepeatedly(testing::Return(surface));
  71. ON_CALL(*this, OnConstraintActivated).WillByDefault([this]() {
  72. activated_count++;
  73. });
  74. ON_CALL(*this, OnConstraintBroken).WillByDefault([this]() {
  75. broken_count++;
  76. });
  77. }
  78. ~MockPointerConstraintDelegate() {
  79. // Notifying destruction here removes some boilerplate from tests.
  80. pointer_->OnPointerConstraintDelegateDestroying(this);
  81. }
  82. // Overridden from PointerConstraintDelegate:
  83. MOCK_METHOD0(OnConstraintActivated, void());
  84. MOCK_METHOD0(OnAlreadyConstrained, void());
  85. MOCK_METHOD0(OnConstraintBroken, void());
  86. MOCK_METHOD0(IsPersistent, bool());
  87. MOCK_METHOD0(GetConstrainedSurface, Surface*());
  88. MOCK_METHOD0(OnDefunct, void());
  89. raw_ptr<Pointer> pointer_;
  90. int activated_count = 0;
  91. int broken_count = 0;
  92. };
  93. class UILockControllerTest : public test::ExoTestBase {
  94. public:
  95. UILockControllerTest()
  96. : test::ExoTestBase(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  97. ~UILockControllerTest() override = default;
  98. UILockControllerTest(const UILockControllerTest&) = delete;
  99. UILockControllerTest& operator=(const UILockControllerTest&) = delete;
  100. protected:
  101. class TestPropertyResolver : public exo::WMHelper::AppPropertyResolver {
  102. public:
  103. TestPropertyResolver() = default;
  104. ~TestPropertyResolver() override = default;
  105. void PopulateProperties(
  106. const Params& params,
  107. ui::PropertyHandler& out_properties_container) override {
  108. out_properties_container.SetProperty(
  109. chromeos::kEscHoldToExitFullscreen,
  110. params.app_id != kNoEscHoldAppId &&
  111. params.app_id != kOverviewToExitAppId);
  112. out_properties_container.SetProperty(
  113. chromeos::kUseOverviewToExitFullscreen,
  114. params.app_id == kOverviewToExitAppId);
  115. out_properties_container.SetProperty(
  116. chromeos::kUseOverviewToExitPointerLock,
  117. params.app_id == kOverviewToExitAppId);
  118. }
  119. };
  120. // test::ExoTestBase:
  121. void SetUp() override {
  122. test::ExoTestBase::SetUp();
  123. seat_ = std::make_unique<Seat>();
  124. scoped_feature_list_.InitWithFeatures(
  125. {chromeos::features::kExoLockNotification}, {});
  126. WMHelper::GetInstance()->RegisterAppPropertyResolver(
  127. std::make_unique<TestPropertyResolver>());
  128. }
  129. void TearDown() override {
  130. seat_.reset();
  131. test::ExoTestBase::TearDown();
  132. }
  133. std::unique_ptr<ShellSurface> BuildSurface(gfx::Point origin, int w, int h) {
  134. test::ShellSurfaceBuilder builder({w, h});
  135. builder.SetOrigin(origin);
  136. return builder.BuildShellSurface();
  137. }
  138. std::unique_ptr<ShellSurface> BuildSurface(int w, int h) {
  139. return BuildSurface(gfx::Point(0, 0), w, h);
  140. }
  141. views::Widget* GetEscNotification(
  142. const std::unique_ptr<ShellSurface>& surface) {
  143. return seat_->GetUILockControllerForTesting()->GetEscNotificationForTesting(
  144. GetTopLevelWindow(surface));
  145. }
  146. views::Widget* GetPointerCaptureNotification(
  147. const std::unique_ptr<ShellSurface>& surface) {
  148. return seat_->GetUILockControllerForTesting()
  149. ->GetPointerCaptureNotificationForTesting(GetTopLevelWindow(surface));
  150. }
  151. bool IsExitPopupVisible(aura::Window* window) {
  152. FullscreenControlPopup* popup =
  153. seat_->GetUILockControllerForTesting()->GetExitPopupForTesting(window);
  154. if (popup && popup->IsAnimating()) {
  155. gfx::AnimationTestApi animation_api(popup->GetAnimationForTesting());
  156. base::TimeTicks now = base::TimeTicks::Now();
  157. animation_api.SetStartTime(now);
  158. animation_api.Step(now + base::Milliseconds(500));
  159. }
  160. return popup && popup->IsVisible();
  161. }
  162. std::unique_ptr<Seat> seat_;
  163. base::test::ScopedFeatureList scoped_feature_list_;
  164. };
  165. TEST_F(UILockControllerTest, HoldingEscapeExitsFullscreen) {
  166. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  167. test_surface->SetUseImmersiveForFullscreen(false);
  168. test_surface->SetFullscreen(true);
  169. test_surface->surface_for_testing()->Commit();
  170. auto* window_state = GetTopLevelWindowState(test_surface);
  171. EXPECT_TRUE(window_state->IsFullscreen());
  172. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  173. task_environment()->FastForwardBy(base::Seconds(1));
  174. EXPECT_TRUE(window_state->IsFullscreen()); // no change yet
  175. task_environment()->FastForwardBy(base::Seconds(1));
  176. EXPECT_FALSE(window_state->IsFullscreen());
  177. EXPECT_TRUE(window_state->IsNormalStateType());
  178. }
  179. TEST_F(UILockControllerTest, HoldingCtrlEscapeDoesNotExitFullscreen) {
  180. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  181. test_surface->SetUseImmersiveForFullscreen(false);
  182. test_surface->SetFullscreen(true);
  183. test_surface->surface_for_testing()->Commit();
  184. auto* window_state = GetTopLevelWindowState(test_surface);
  185. EXPECT_TRUE(window_state->IsFullscreen());
  186. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_CONTROL_DOWN);
  187. task_environment()->FastForwardBy(base::Seconds(2));
  188. EXPECT_TRUE(window_state->IsFullscreen());
  189. }
  190. TEST_F(UILockControllerTest,
  191. HoldingEscapeOnlyExitsFullscreenIfWindowPropertySet) {
  192. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  193. // Do not set chromeos::kEscHoldToExitFullscreen on TopLevelWindow.
  194. test_surface->SetApplicationId(kNoEscHoldAppId);
  195. test_surface->SetUseImmersiveForFullscreen(false);
  196. test_surface->SetFullscreen(true);
  197. test_surface->surface_for_testing()->Commit();
  198. auto* window_state = GetTopLevelWindowState(test_surface);
  199. EXPECT_TRUE(window_state->IsFullscreen());
  200. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  201. task_environment()->FastForwardBy(base::Seconds(2));
  202. EXPECT_TRUE(window_state->IsFullscreen());
  203. }
  204. TEST_F(UILockControllerTest, HoldingEscapeOnlyExitsFocusedFullscreen) {
  205. std::unique_ptr<ShellSurface> test_surface1 = BuildSurface(1024, 768);
  206. test_surface1->SetUseImmersiveForFullscreen(false);
  207. test_surface1->SetFullscreen(true);
  208. test_surface1->surface_for_testing()->Commit();
  209. std::unique_ptr<ShellSurface> test_surface2 = BuildSurface(1024, 768);
  210. test_surface2->SetUseImmersiveForFullscreen(false);
  211. test_surface2->SetFullscreen(true);
  212. test_surface2->surface_for_testing()->Commit();
  213. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  214. task_environment()->FastForwardBy(base::Seconds(2));
  215. EXPECT_TRUE(GetTopLevelWindowState(test_surface1)->IsFullscreen());
  216. EXPECT_FALSE(GetTopLevelWindowState(test_surface2)->IsFullscreen());
  217. }
  218. TEST_F(UILockControllerTest, DestroyingWindowCancels) {
  219. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  220. test_surface->SetUseImmersiveForFullscreen(false);
  221. test_surface->SetFullscreen(true);
  222. test_surface->surface_for_testing()->Commit();
  223. auto* window_state = GetTopLevelWindowState(test_surface);
  224. EXPECT_TRUE(window_state->IsFullscreen());
  225. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  226. task_environment()->FastForwardBy(base::Seconds(1));
  227. test_surface.reset(); // Destroying the Surface destroys the Window
  228. task_environment()->FastForwardBy(base::Seconds(3));
  229. // The implicit assertion is that the code doesn't crash.
  230. }
  231. TEST_F(UILockControllerTest, FocusChangeCancels) {
  232. // Arrange: two windows, one is fullscreen and focused
  233. std::unique_ptr<ShellSurface> other_surface = BuildSurface(1024, 768);
  234. other_surface->surface_for_testing()->Commit();
  235. std::unique_ptr<ShellSurface> fullscreen_surface = BuildSurface(1024, 768);
  236. fullscreen_surface->SetUseImmersiveForFullscreen(false);
  237. fullscreen_surface->SetFullscreen(true);
  238. fullscreen_surface->surface_for_testing()->Commit();
  239. EXPECT_EQ(fullscreen_surface->surface_for_testing(),
  240. seat_->GetFocusedSurface());
  241. EXPECT_FALSE(GetTopLevelWindowState(fullscreen_surface)->IsMinimized());
  242. // Act: Press escape, then toggle focus back and forth
  243. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  244. task_environment()->FastForwardBy(base::Seconds(1));
  245. wm::ActivateWindow(other_surface->surface_for_testing()->window());
  246. wm::ActivateWindow(fullscreen_surface->surface_for_testing()->window());
  247. task_environment()->FastForwardBy(base::Seconds(2));
  248. // Assert: Fullscreen window was not minimized, despite regaining focus.
  249. EXPECT_FALSE(GetTopLevelWindowState(fullscreen_surface)->IsMinimized());
  250. EXPECT_EQ(fullscreen_surface->surface_for_testing(),
  251. seat_->GetFocusedSurface());
  252. }
  253. TEST_F(UILockControllerTest, ShortHoldEscapeDoesNotExitFullscreen) {
  254. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  255. test_surface->SetUseImmersiveForFullscreen(false);
  256. test_surface->SetFullscreen(true);
  257. test_surface->surface_for_testing()->Commit();
  258. auto* window_state = GetTopLevelWindowState(test_surface);
  259. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  260. task_environment()->FastForwardBy(base::Seconds(1));
  261. GetEventGenerator()->ReleaseKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  262. task_environment()->FastForwardBy(base::Seconds(2));
  263. EXPECT_TRUE(window_state->IsFullscreen());
  264. }
  265. TEST_F(UILockControllerTest, FullScreenShowsEscNotification) {
  266. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  267. test_surface->SetUseImmersiveForFullscreen(false);
  268. test_surface->SetFullscreen(true);
  269. test_surface->surface_for_testing()->Commit();
  270. EXPECT_TRUE(GetTopLevelWindowState(test_surface)->IsFullscreen());
  271. EXPECT_TRUE(GetEscNotification(test_surface));
  272. }
  273. TEST_F(UILockControllerTest, EscNotificationClosesAfterDuration) {
  274. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  275. test_surface->SetUseImmersiveForFullscreen(false);
  276. test_surface->SetFullscreen(true);
  277. test_surface->surface_for_testing()->Commit();
  278. EXPECT_TRUE(GetEscNotification(test_surface));
  279. task_environment()->FastForwardBy(base::Seconds(5));
  280. EXPECT_FALSE(GetEscNotification(test_surface));
  281. }
  282. TEST_F(UILockControllerTest, HoldingEscapeHidesNotification) {
  283. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  284. test_surface->SetUseImmersiveForFullscreen(false);
  285. test_surface->SetFullscreen(true);
  286. test_surface->surface_for_testing()->Commit();
  287. EXPECT_TRUE(GetTopLevelWindowState(test_surface)->IsFullscreen());
  288. EXPECT_TRUE(GetEscNotification(test_surface));
  289. GetEventGenerator()->PressKey(ui::VKEY_ESCAPE, ui::EF_NONE);
  290. task_environment()->FastForwardBy(base::Seconds(3));
  291. EXPECT_FALSE(GetTopLevelWindowState(test_surface)->IsFullscreen());
  292. EXPECT_FALSE(GetEscNotification(test_surface));
  293. }
  294. TEST_F(UILockControllerTest, LosingFullscreenHidesNotification) {
  295. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  296. test_surface->SetUseImmersiveForFullscreen(false);
  297. test_surface->SetFullscreen(true);
  298. test_surface->surface_for_testing()->Commit();
  299. EXPECT_TRUE(GetTopLevelWindowState(test_surface)->IsFullscreen());
  300. EXPECT_TRUE(GetEscNotification(test_surface));
  301. // Have surface loose fullscreen, notification should now be hidden.
  302. test_surface->Minimize();
  303. test_surface->SetFullscreen(false);
  304. test_surface->surface_for_testing()->Commit();
  305. EXPECT_FALSE(GetTopLevelWindowState(test_surface)->IsFullscreen());
  306. EXPECT_FALSE(
  307. seat_->GetUILockControllerForTesting()->GetEscNotificationForTesting(
  308. GetTopLevelWindow(test_surface)));
  309. }
  310. TEST_F(UILockControllerTest, EscNotificationIsReshownIfInterrupted) {
  311. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  312. test_surface->SetUseImmersiveForFullscreen(false);
  313. test_surface->SetFullscreen(true);
  314. test_surface->surface_for_testing()->Commit();
  315. EXPECT_TRUE(GetEscNotification(test_surface));
  316. // Stop fullscreen.
  317. test_surface->SetFullscreen(false);
  318. EXPECT_FALSE(
  319. seat_->GetUILockControllerForTesting()->GetEscNotificationForTesting(
  320. GetTopLevelWindow(test_surface)));
  321. // Fullscreen should show notification since it did not stay visible for
  322. // duration.
  323. test_surface->SetFullscreen(true);
  324. EXPECT_TRUE(GetEscNotification(test_surface));
  325. // After duration, notification should be removed.
  326. task_environment()->FastForwardBy(base::Seconds(5));
  327. EXPECT_FALSE(GetEscNotification(test_surface));
  328. // Notification is shown after fullscreen toggle.
  329. test_surface->SetFullscreen(false);
  330. test_surface->SetFullscreen(true);
  331. EXPECT_TRUE(GetEscNotification(test_surface));
  332. }
  333. TEST_F(UILockControllerTest, EscNotificationIsReshownAfterUnlock) {
  334. // Arrange: Go fullscreen and time out the notification.
  335. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  336. test_surface->SetUseImmersiveForFullscreen(false);
  337. test_surface->SetFullscreen(true);
  338. test_surface->surface_for_testing()->Commit();
  339. task_environment()->FastForwardBy(base::Seconds(10));
  340. // Ensure the notification did time out; if not, we can't trust the test
  341. // result.
  342. EXPECT_FALSE(GetEscNotification(test_surface));
  343. // Act: Simulate locking and unlocking.
  344. GetSessionControllerClient()->LockScreen();
  345. GetSessionControllerClient()->UnlockScreen();
  346. // Assert: Notification shown again.
  347. EXPECT_TRUE(GetEscNotification(test_surface));
  348. }
  349. TEST_F(UILockControllerTest, EscNotificationReshownWhenScreenTurnedOn) {
  350. // Arrange: Set up a pointer capture notification, then let it expire.
  351. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  352. test_surface->SetUseImmersiveForFullscreen(false);
  353. test_surface->SetFullscreen(true);
  354. test_surface->surface_for_testing()->Commit();
  355. task_environment()->FastForwardBy(base::Seconds(10));
  356. // Ensure the notification did time out; if not, we can't trust the test
  357. // result.
  358. EXPECT_FALSE(GetEscNotification(test_surface));
  359. // Act: Simulate turning the backlight off and on again.
  360. power_manager::SetBacklightBrightnessRequest request;
  361. request.set_percent(0);
  362. chromeos::FakePowerManagerClient::Get()->SetScreenBrightness(request);
  363. base::RunLoop().RunUntilIdle();
  364. request.set_percent(100);
  365. chromeos::FakePowerManagerClient::Get()->SetScreenBrightness(request);
  366. base::RunLoop().RunUntilIdle();
  367. // Assert: Notification shown again.
  368. EXPECT_TRUE(GetEscNotification(test_surface));
  369. }
  370. TEST_F(UILockControllerTest, EscNotificationReshownWhenLidReopened) {
  371. // Arrange: Set up a pointer capture notification, then let it expire.
  372. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  373. test_surface->SetUseImmersiveForFullscreen(false);
  374. test_surface->SetFullscreen(true);
  375. test_surface->surface_for_testing()->Commit();
  376. task_environment()->FastForwardBy(base::Seconds(10));
  377. // Ensure the notification did time out; if not, we can't trust the test
  378. // result.
  379. EXPECT_FALSE(GetEscNotification(test_surface));
  380. // Act: Simulate closing and reopening the lid.
  381. chromeos::FakePowerManagerClient::Get()->SetLidState(
  382. chromeos::PowerManagerClient::LidState::CLOSED, base::TimeTicks::Now());
  383. chromeos::FakePowerManagerClient::Get()->SetLidState(
  384. chromeos::PowerManagerClient::LidState::OPEN, base::TimeTicks::Now());
  385. // Assert: Notification shown again.
  386. EXPECT_TRUE(GetEscNotification(test_surface));
  387. }
  388. TEST_F(UILockControllerTest, EscNotificationShowsOnSecondaryDisplay) {
  389. // Create surface on secondary display.
  390. UpdateDisplay("900x800,70x600");
  391. std::unique_ptr<ShellSurface> test_surface =
  392. BuildSurface(gfx::Point(900, 100), 200, 200);
  393. test_surface->SetUseImmersiveForFullscreen(false);
  394. test_surface->SetFullscreen(true);
  395. test_surface->surface_for_testing()->Commit();
  396. // Esc notification should be in secondary display.
  397. views::Widget* esc_notification = GetEscNotification(test_surface);
  398. EXPECT_TRUE(GetSecondaryDisplay().bounds().Contains(
  399. esc_notification->GetWindowBoundsInScreen()));
  400. }
  401. TEST_F(UILockControllerTest, PointerLockShowsNotification) {
  402. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  403. test_surface->SetApplicationId(kOverviewToExitAppId);
  404. test_surface->surface_for_testing()->Commit();
  405. testing::NiceMock<MockPointerDelegate> delegate;
  406. Pointer pointer(&delegate, seat_.get());
  407. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  408. &pointer, test_surface->surface_for_testing());
  409. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  410. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  411. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  412. }
  413. TEST_F(UILockControllerTest, PointerLockNotificationObeysCooldown) {
  414. // Arrange: Set up a pointer capture notification.
  415. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  416. test_surface->SetApplicationId(kOverviewToExitAppId);
  417. test_surface->surface_for_testing()->Commit();
  418. testing::NiceMock<MockPointerDelegate> delegate;
  419. Pointer pointer(&delegate, seat_.get());
  420. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  421. &pointer, test_surface->surface_for_testing());
  422. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  423. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  424. // Act: Wait for the notification to timeout.
  425. task_environment()->FastForwardBy(base::Seconds(5));
  426. // Assert: Notification has disappeared.
  427. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  428. // Act: Remove and re-apply the constraint.
  429. pointer.OnPointerConstraintDelegateDestroying(&constraint);
  430. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  431. // Assert: Notification not shown due to the cooldown.
  432. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  433. // Act: Wait for the cooldown, then re-apply again
  434. pointer.OnPointerConstraintDelegateDestroying(&constraint);
  435. task_environment()->FastForwardBy(base::Minutes(5));
  436. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  437. // Assert: Cooldown has expired so notification is shown.
  438. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  439. }
  440. TEST_F(UILockControllerTest, PointerLockNotificationReshownOnLidOpen) {
  441. // Arrange: Set up a pointer capture notification, then let it expire.
  442. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  443. test_surface->SetApplicationId(kOverviewToExitAppId);
  444. test_surface->surface_for_testing()->Commit();
  445. testing::NiceMock<MockPointerDelegate> delegate;
  446. Pointer pointer(&delegate, seat_.get());
  447. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  448. &pointer, test_surface->surface_for_testing());
  449. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  450. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  451. task_environment()->FastForwardBy(base::Seconds(5));
  452. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  453. // Act: Simulate closing and reopening the lid.
  454. chromeos::FakePowerManagerClient::Get()->SetLidState(
  455. chromeos::PowerManagerClient::LidState::CLOSED, base::TimeTicks::Now());
  456. chromeos::FakePowerManagerClient::Get()->SetLidState(
  457. chromeos::PowerManagerClient::LidState::OPEN, base::TimeTicks::Now());
  458. // Assert: Notification shown again.
  459. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  460. }
  461. TEST_F(UILockControllerTest, PointerLockNotificationReshownWhenScreenTurnedOn) {
  462. // Arrange: Set up a pointer capture notification, then let it expire.
  463. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  464. test_surface->SetApplicationId(kOverviewToExitAppId);
  465. test_surface->surface_for_testing()->Commit();
  466. testing::NiceMock<MockPointerDelegate> delegate;
  467. Pointer pointer(&delegate, seat_.get());
  468. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  469. &pointer, test_surface->surface_for_testing());
  470. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  471. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  472. task_environment()->FastForwardBy(base::Seconds(5));
  473. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  474. // Act: Simulate turning the backlight off and on again.
  475. power_manager::SetBacklightBrightnessRequest request;
  476. request.set_percent(0);
  477. chromeos::FakePowerManagerClient::Get()->SetScreenBrightness(request);
  478. base::RunLoop().RunUntilIdle();
  479. request.set_percent(100);
  480. chromeos::FakePowerManagerClient::Get()->SetScreenBrightness(request);
  481. base::RunLoop().RunUntilIdle();
  482. // Assert: Notification shown again.
  483. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  484. }
  485. TEST_F(UILockControllerTest, PointerLockNotificationReshownOnUnlock) {
  486. // Arrange: Set up a pointer capture notification, then let it expire.
  487. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  488. test_surface->SetApplicationId(kOverviewToExitAppId);
  489. test_surface->surface_for_testing()->Commit();
  490. testing::NiceMock<MockPointerDelegate> delegate;
  491. Pointer pointer(&delegate, seat_.get());
  492. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  493. &pointer, test_surface->surface_for_testing());
  494. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  495. task_environment()->FastForwardBy(base::Seconds(5));
  496. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  497. // Act: Simulate locking and unlocking.
  498. GetSessionControllerClient()->LockScreen();
  499. GetSessionControllerClient()->UnlockScreen();
  500. // Assert: Notification shown again.
  501. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  502. }
  503. TEST_F(UILockControllerTest, PointerLockNotificationReshownAfterSuspend) {
  504. // Arrange: Set up a pointer capture notification, then let it expire.
  505. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  506. test_surface->SetApplicationId(kOverviewToExitAppId);
  507. test_surface->surface_for_testing()->Commit();
  508. testing::NiceMock<MockPointerDelegate> delegate;
  509. Pointer pointer(&delegate, seat_.get());
  510. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  511. &pointer, test_surface->surface_for_testing());
  512. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  513. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  514. task_environment()->FastForwardBy(base::Seconds(5));
  515. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  516. // Act: Simulate suspend and resume
  517. chromeos::FakePowerManagerClient::Get()->SendSuspendImminent(
  518. power_manager::SuspendImminent_Reason_IDLE);
  519. task_environment()->FastForwardBy(base::Minutes(1));
  520. chromeos::FakePowerManagerClient::Get()->SendSuspendDone(base::Minutes(1));
  521. // Assert: Notification shown again.
  522. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  523. }
  524. TEST_F(UILockControllerTest, PointerLockNotificationReshownAfterIdle) {
  525. // Arrange: Set up a pointer capture notification, then let it expire.
  526. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  527. test_surface->SetApplicationId(kOverviewToExitAppId);
  528. test_surface->surface_for_testing()->Commit();
  529. testing::NiceMock<MockPointerDelegate> delegate;
  530. Pointer pointer(&delegate, seat_.get());
  531. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  532. &pointer, test_surface->surface_for_testing());
  533. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  534. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  535. task_environment()->FastForwardBy(base::Seconds(5));
  536. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  537. // Act: Simulate activity, then go idle.
  538. seat_->GetUILockControllerForTesting()->OnUserActivity(/*event=*/nullptr);
  539. task_environment()->FastForwardBy(base::Minutes(10));
  540. // Assert: Notification not yet shown again.
  541. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  542. // Act: Simulate activity after being idle.
  543. seat_->GetUILockControllerForTesting()->OnUserActivity(/*event=*/nullptr);
  544. // Assert: Notification shown again.
  545. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  546. }
  547. TEST_F(UILockControllerTest, PointerLockCooldownResetForAllWindows) {
  548. // Arrange: Create two surfaces, one with a pointer lock notification.
  549. std::unique_ptr<ShellSurface> other_surface = BuildSurface(1024, 768);
  550. other_surface->SetApplicationId(kOverviewToExitAppId);
  551. other_surface->surface_for_testing()->Commit();
  552. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  553. test_surface->SetApplicationId(kOverviewToExitAppId);
  554. test_surface->surface_for_testing()->Commit();
  555. testing::NiceMock<MockPointerDelegate> delegate;
  556. Pointer pointer(&delegate, seat_.get());
  557. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  558. &pointer, test_surface->surface_for_testing());
  559. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  560. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  561. // Act: Focus the other window, then lock and unlock.
  562. wm::ActivateWindow(other_surface->surface_for_testing()->window());
  563. GetSessionControllerClient()->LockScreen();
  564. GetSessionControllerClient()->UnlockScreen();
  565. // Assert: Notification shown again.
  566. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  567. }
  568. TEST_F(UILockControllerTest, FullscreenNotificationHasPriority) {
  569. // Arrange: Set up a pointer capture notification.
  570. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  571. test_surface->SetApplicationId(kOverviewToExitAppId);
  572. test_surface->surface_for_testing()->Commit();
  573. testing::NiceMock<MockPointerDelegate> delegate;
  574. Pointer pointer(&delegate, seat_.get());
  575. testing::NiceMock<MockPointerConstraintDelegate> constraint(
  576. &pointer, test_surface->surface_for_testing());
  577. EXPECT_TRUE(pointer.ConstrainPointer(&constraint));
  578. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  579. // Act: Go fullscreen.
  580. test_surface->SetUseImmersiveForFullscreen(false);
  581. test_surface->SetFullscreen(true);
  582. test_surface->surface_for_testing()->Commit();
  583. // Assert: Fullscreen notification overrides pointer notification.
  584. EXPECT_FALSE(GetPointerCaptureNotification(test_surface));
  585. EXPECT_TRUE(GetEscNotification(test_surface));
  586. // Act: Exit fullscreen.
  587. test_surface->SetFullscreen(false);
  588. test_surface->surface_for_testing()->Commit();
  589. // Assert: Pointer notification returns, since it was interrupted.
  590. EXPECT_TRUE(GetPointerCaptureNotification(test_surface));
  591. EXPECT_FALSE(GetEscNotification(test_surface));
  592. }
  593. TEST_F(UILockControllerTest, ExitPopup) {
  594. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  595. test_surface->SetUseImmersiveForFullscreen(false);
  596. test_surface->SetFullscreen(true);
  597. test_surface->surface_for_testing()->Commit();
  598. auto* window_state = GetTopLevelWindowState(test_surface);
  599. EXPECT_TRUE(window_state->IsFullscreen());
  600. aura::Window* window = GetTopLevelWindow(test_surface);
  601. EXPECT_FALSE(IsExitPopupVisible(window));
  602. EXPECT_TRUE(GetEscNotification(test_surface));
  603. // Move mouse above y=3 should not show exit popup while notification is
  604. // visible.
  605. GetEventGenerator()->MoveMouseTo(0, 2);
  606. EXPECT_FALSE(IsExitPopupVisible(window));
  607. // Wait for notification to close, now exit popup should show.
  608. task_environment()->FastForwardBy(base::Seconds(5));
  609. EXPECT_FALSE(GetEscNotification(test_surface));
  610. GetEventGenerator()->MoveMouseTo(1, 2);
  611. EXPECT_TRUE(IsExitPopupVisible(window));
  612. // Move mouse below y=150 should hide exit popup.
  613. GetEventGenerator()->MoveMouseTo(0, 160);
  614. EXPECT_FALSE(IsExitPopupVisible(window));
  615. // Move mouse back above y=3 should show exit popup.
  616. GetEventGenerator()->MoveMouseTo(0, 2);
  617. EXPECT_TRUE(IsExitPopupVisible(window));
  618. // Popup should hide after 3s.
  619. task_environment()->FastForwardBy(base::Seconds(5));
  620. EXPECT_FALSE(IsExitPopupVisible(window));
  621. // Moving mouse to y=100, then above y=3 should still have popup hidden.
  622. GetEventGenerator()->MoveMouseTo(0, 100);
  623. GetEventGenerator()->MoveMouseTo(0, 2);
  624. EXPECT_FALSE(IsExitPopupVisible(window));
  625. // Moving mouse below y=150, then above y=3 should show exit popup.
  626. GetEventGenerator()->MoveMouseTo(0, 160);
  627. GetEventGenerator()->MoveMouseTo(0, 2);
  628. EXPECT_TRUE(IsExitPopupVisible(window));
  629. // Clicking exit popup should exit fullscreen.
  630. FullscreenControlPopup* popup =
  631. seat_->GetUILockControllerForTesting()->GetExitPopupForTesting(window);
  632. GetEventGenerator()->MoveMouseTo(
  633. popup->GetPopupWidget()->GetWindowBoundsInScreen().CenterPoint());
  634. GetEventGenerator()->ClickLeftButton();
  635. EXPECT_FALSE(window_state->IsFullscreen());
  636. EXPECT_FALSE(IsExitPopupVisible(window));
  637. }
  638. TEST_F(UILockControllerTest, ExitPopupNotShownForOverviewCase) {
  639. std::unique_ptr<ShellSurface> test_surface = BuildSurface(1024, 768);
  640. // Set chromeos::kUseOverviewToExitFullscreen on TopLevelWindow.
  641. test_surface->SetApplicationId(kOverviewToExitAppId);
  642. test_surface->SetUseImmersiveForFullscreen(false);
  643. test_surface->SetFullscreen(true);
  644. test_surface->surface_for_testing()->Commit();
  645. EXPECT_FALSE(IsExitPopupVisible(GetTopLevelWindow(test_surface)));
  646. // Move mouse above y=3 should not show exit popup.
  647. GetEventGenerator()->MoveMouseTo(0, 2);
  648. EXPECT_FALSE(IsExitPopupVisible(GetTopLevelWindow(test_surface)));
  649. }
  650. TEST_F(UILockControllerTest, OnlyShowWhenActive) {
  651. std::unique_ptr<ShellSurface> test_surface1 = BuildSurface(1024, 768);
  652. test_surface1->surface_for_testing()->Commit();
  653. std::unique_ptr<ShellSurface> test_surface2 =
  654. BuildSurface(gfx::Point(100, 100), 200, 200);
  655. test_surface2->surface_for_testing()->Commit();
  656. // Surface2 is active when we make Surface1 fullscreen.
  657. // Esc notification, and exit popup should not be shown.
  658. test_surface1->SetFullscreen(true);
  659. EXPECT_FALSE(GetEscNotification(test_surface1));
  660. GetEventGenerator()->MoveMouseTo(0, 2);
  661. EXPECT_FALSE(IsExitPopupVisible(GetTopLevelWindow(test_surface1)));
  662. }
  663. } // namespace
  664. } // namespace exo