root_window_controller_unittest.cc 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. // Copyright (c) 2012 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 "ash/root_window_controller.h"
  5. #include <memory>
  6. #include "ash/keyboard/ui/keyboard_ui.h"
  7. #include "ash/keyboard/ui/keyboard_ui_controller.h"
  8. #include "ash/keyboard/ui/keyboard_util.h"
  9. #include "ash/keyboard/ui/test/keyboard_test_util.h"
  10. #include "ash/public/cpp/keyboard/keyboard_switches.h"
  11. #include "ash/public/cpp/shelf_config.h"
  12. #include "ash/public/cpp/shell_window_ids.h"
  13. #include "ash/session/session_controller_impl.h"
  14. #include "ash/session/test_session_controller_client.h"
  15. #include "ash/shell.h"
  16. #include "ash/test/ash_test_base.h"
  17. #include "ash/wm/system_modal_container_layout_manager.h"
  18. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  19. #include "ash/wm/window_properties.h"
  20. #include "ash/wm/window_state.h"
  21. #include "ash/wm/window_util.h"
  22. #include "base/run_loop.h"
  23. #include "base/strings/stringprintf.h"
  24. #include "ui/aura/client/focus_change_observer.h"
  25. #include "ui/aura/client/focus_client.h"
  26. #include "ui/aura/client/window_parenting_client.h"
  27. #include "ui/aura/env.h"
  28. #include "ui/aura/test/test_window_delegate.h"
  29. #include "ui/aura/test/test_windows.h"
  30. #include "ui/aura/window.h"
  31. #include "ui/aura/window_event_dispatcher.h"
  32. #include "ui/aura/window_tracker.h"
  33. #include "ui/base/ime/dummy_text_input_client.h"
  34. #include "ui/base/ime/input_method.h"
  35. #include "ui/base/ime/text_input_client.h"
  36. #include "ui/base/ui_base_features.h"
  37. #include "ui/display/manager/display_manager.h"
  38. #include "ui/display/test/display_manager_test_api.h"
  39. #include "ui/events/test/event_generator.h"
  40. #include "ui/events/test/test_event_handler.h"
  41. #include "ui/views/controls/menu/menu_controller.h"
  42. #include "ui/views/widget/widget.h"
  43. #include "ui/views/widget/widget_delegate.h"
  44. using aura::Window;
  45. using views::Widget;
  46. namespace ash {
  47. namespace {
  48. class DeleteOnBlurDelegate : public aura::test::TestWindowDelegate,
  49. public aura::client::FocusChangeObserver {
  50. public:
  51. DeleteOnBlurDelegate() : window_(nullptr) {}
  52. DeleteOnBlurDelegate(const DeleteOnBlurDelegate&) = delete;
  53. DeleteOnBlurDelegate& operator=(const DeleteOnBlurDelegate&) = delete;
  54. ~DeleteOnBlurDelegate() override = default;
  55. void SetWindow(aura::Window* window) {
  56. window_ = window;
  57. aura::client::SetFocusChangeObserver(window_, this);
  58. }
  59. private:
  60. // aura::test::TestWindowDelegate overrides:
  61. bool CanFocus() override { return true; }
  62. // aura::client::FocusChangeObserver implementation:
  63. void OnWindowFocused(aura::Window* gained_focus,
  64. aura::Window* lost_focus) override {
  65. if (window_ == lost_focus)
  66. delete window_;
  67. }
  68. aura::Window* window_;
  69. };
  70. aura::LayoutManager* GetLayoutManager(RootWindowController* controller,
  71. int id) {
  72. return controller->GetContainer(id)->layout_manager();
  73. }
  74. } // namespace
  75. class RootWindowControllerTest : public AshTestBase {
  76. public:
  77. views::Widget* CreateTestWidget(const gfx::Rect& bounds) {
  78. views::Widget* widget =
  79. views::Widget::CreateWindowWithContext(nullptr, GetContext());
  80. // Any initial bounds are constrained to the screen work area or the parent.
  81. // See Widget::InitialBounds() & Widget::SetBoundsConstrained(). Explicitly
  82. // setting the bounds here will allow the view to be positioned such that it
  83. // can extend outside the screen work area.
  84. widget->SetBounds(bounds);
  85. widget->Show();
  86. return widget;
  87. }
  88. views::WidgetDelegate* CreateModalWidgetDelegate() {
  89. auto delegate = std::make_unique<views::WidgetDelegateView>();
  90. delegate->SetModalType(ui::MODAL_TYPE_SYSTEM);
  91. return delegate.release();
  92. }
  93. views::Widget* CreateModalWidget(const gfx::Rect& bounds) {
  94. views::Widget* widget = views::Widget::CreateWindowWithContext(
  95. CreateModalWidgetDelegate(), GetContext());
  96. // See the above comment.
  97. widget->SetBounds(bounds);
  98. widget->Show();
  99. return widget;
  100. }
  101. views::Widget* CreateModalWidgetWithParent(const gfx::Rect& bounds,
  102. aura::Window* parent) {
  103. views::Widget* widget = views::Widget::CreateWindowWithParent(
  104. CreateModalWidgetDelegate(), parent);
  105. // See the above comment.
  106. widget->SetBounds(bounds);
  107. widget->Show();
  108. return widget;
  109. }
  110. aura::Window* GetModalContainer(aura::Window* root_window) {
  111. return Shell::GetContainer(root_window,
  112. kShellWindowId_SystemModalContainer);
  113. }
  114. };
  115. TEST_F(RootWindowControllerTest, MoveWindows_Basic) {
  116. // Windows origin should be doubled when moved to the 1st display.
  117. UpdateDisplay("600x500,300x250");
  118. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  119. int bottom_inset = 250 - ShelfConfig::Get()->shelf_size();
  120. views::Widget* normal = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
  121. EXPECT_EQ(root_windows[1], normal->GetNativeView()->GetRootWindow());
  122. EXPECT_EQ("650,10 100x100", normal->GetWindowBoundsInScreen().ToString());
  123. EXPECT_EQ("50,10 100x100",
  124. normal->GetNativeView()->GetBoundsInRootWindow().ToString());
  125. views::Widget* maximized = CreateTestWidget(gfx::Rect(700, 10, 100, 100));
  126. maximized->Maximize();
  127. EXPECT_EQ(root_windows[1], maximized->GetNativeView()->GetRootWindow());
  128. EXPECT_EQ(gfx::Rect(600, 0, 300, bottom_inset).ToString(),
  129. maximized->GetWindowBoundsInScreen().ToString());
  130. EXPECT_EQ(gfx::Rect(0, 0, 300, bottom_inset).ToString(),
  131. maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
  132. views::Widget* minimized = CreateTestWidget(gfx::Rect(550, 10, 200, 200));
  133. minimized->Minimize();
  134. EXPECT_EQ(root_windows[1], minimized->GetNativeView()->GetRootWindow());
  135. EXPECT_EQ("550,10 200x200", minimized->GetWindowBoundsInScreen().ToString());
  136. views::Widget* fullscreen = CreateTestWidget(gfx::Rect(850, 10, 200, 200));
  137. display::Display secondary_display = GetSecondaryDisplay();
  138. gfx::Rect orig_bounds = fullscreen->GetWindowBoundsInScreen();
  139. EXPECT_TRUE(secondary_display.work_area().Intersects(orig_bounds));
  140. EXPECT_FALSE(secondary_display.work_area().Contains(orig_bounds));
  141. fullscreen->SetFullscreen(true);
  142. EXPECT_EQ(root_windows[1], fullscreen->GetNativeView()->GetRootWindow());
  143. EXPECT_EQ("600,0 300x250", fullscreen->GetWindowBoundsInScreen().ToString());
  144. EXPECT_EQ("0,0 300x250",
  145. fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
  146. views::Widget* unparented_control = new Widget;
  147. Widget::InitParams params;
  148. params.bounds = gfx::Rect(650, 10, 100, 100);
  149. params.type = Widget::InitParams::TYPE_CONTROL;
  150. params.context = GetContext();
  151. unparented_control->Init(std::move(params));
  152. EXPECT_EQ(root_windows[1],
  153. unparented_control->GetNativeView()->GetRootWindow());
  154. EXPECT_EQ(kShellWindowId_UnparentedContainer,
  155. unparented_control->GetNativeView()->parent()->GetId());
  156. // Make sure a window that will delete itself when losing focus
  157. // will not crash.
  158. aura::WindowTracker tracker;
  159. DeleteOnBlurDelegate delete_on_blur_delegate;
  160. aura::Window* d2 = CreateTestWindowInShellWithDelegate(
  161. &delete_on_blur_delegate, 0, gfx::Rect(50, 50, 100, 100));
  162. delete_on_blur_delegate.SetWindow(d2);
  163. aura::client::GetFocusClient(root_windows[0])->FocusWindow(d2);
  164. tracker.Add(d2);
  165. UpdateDisplay("600x500");
  166. // d2 must have been deleted.
  167. EXPECT_FALSE(tracker.Contains(d2));
  168. EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
  169. EXPECT_EQ("100,20 100x100", normal->GetWindowBoundsInScreen().ToString());
  170. EXPECT_EQ("100,20 100x100",
  171. normal->GetNativeView()->GetBoundsInRootWindow().ToString());
  172. bottom_inset = 500 - ShelfConfig::Get()->shelf_size();
  173. // First clear fullscreen status, since both fullscreen and maximized windows
  174. // share the same desktop workspace, which cancels the shelf status.
  175. fullscreen->SetFullscreen(false);
  176. EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
  177. EXPECT_EQ(gfx::Rect(0, 0, 600, bottom_inset).ToString(),
  178. maximized->GetWindowBoundsInScreen().ToString());
  179. EXPECT_EQ(gfx::Rect(0, 0, 600, bottom_inset).ToString(),
  180. maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
  181. // Set fullscreen to true, but maximized window's size won't change because
  182. // it's not visible. see crbug.com/504299.
  183. fullscreen->SetFullscreen(true);
  184. EXPECT_EQ(root_windows[0], maximized->GetNativeView()->GetRootWindow());
  185. EXPECT_EQ(gfx::Rect(0, 0, 600, bottom_inset).ToString(),
  186. maximized->GetWindowBoundsInScreen().ToString());
  187. EXPECT_EQ(gfx::Rect(0, 0, 600, bottom_inset).ToString(),
  188. maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
  189. EXPECT_EQ(root_windows[0], minimized->GetNativeView()->GetRootWindow());
  190. EXPECT_EQ("0,20 200x200", minimized->GetWindowBoundsInScreen().ToString());
  191. EXPECT_EQ(root_windows[0], fullscreen->GetNativeView()->GetRootWindow());
  192. EXPECT_TRUE(fullscreen->IsFullscreen());
  193. EXPECT_EQ("0,0 600x500", fullscreen->GetWindowBoundsInScreen().ToString());
  194. EXPECT_EQ("0,0 600x500",
  195. fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
  196. // Test if the restore bounds are correctly updated.
  197. WindowState::Get(maximized->GetNativeView())->Restore();
  198. EXPECT_EQ("200,20 100x100", maximized->GetWindowBoundsInScreen().ToString());
  199. EXPECT_EQ("200,20 100x100",
  200. maximized->GetNativeView()->GetBoundsInRootWindow().ToString());
  201. fullscreen->SetFullscreen(false);
  202. EXPECT_EQ("400,20 200x200", fullscreen->GetWindowBoundsInScreen().ToString());
  203. EXPECT_EQ("400,20 200x200",
  204. fullscreen->GetNativeView()->GetBoundsInRootWindow().ToString());
  205. // Test if the unparented widget has moved.
  206. EXPECT_EQ(root_windows[0],
  207. unparented_control->GetNativeView()->GetRootWindow());
  208. EXPECT_EQ(kShellWindowId_UnparentedContainer,
  209. unparented_control->GetNativeView()->parent()->GetId());
  210. }
  211. TEST_F(RootWindowControllerTest, MoveWindows_Modal) {
  212. UpdateDisplay("500x400,500x600");
  213. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  214. // Emulate virtual screen coordinate system.
  215. root_windows[0]->SetBounds(gfx::Rect(0, 0, 500, 400));
  216. root_windows[1]->SetBounds(gfx::Rect(500, 0, 500, 600));
  217. views::Widget* normal = CreateTestWidget(gfx::Rect(300, 10, 100, 100));
  218. EXPECT_EQ(root_windows[0], normal->GetNativeView()->GetRootWindow());
  219. EXPECT_TRUE(wm::IsActiveWindow(normal->GetNativeView()));
  220. views::Widget* modal = CreateModalWidget(gfx::Rect(650, 10, 100, 100));
  221. EXPECT_EQ(root_windows[1], modal->GetNativeView()->GetRootWindow());
  222. EXPECT_TRUE(
  223. GetModalContainer(root_windows[1])->Contains(modal->GetNativeView()));
  224. EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
  225. ui::test::EventGenerator generator_1st(root_windows[0]);
  226. generator_1st.ClickLeftButton();
  227. EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
  228. UpdateDisplay("500x400");
  229. EXPECT_EQ(root_windows[0], modal->GetNativeView()->GetRootWindow());
  230. EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
  231. generator_1st.ClickLeftButton();
  232. EXPECT_TRUE(wm::IsActiveWindow(modal->GetNativeView()));
  233. }
  234. // Make sure lock related windows moves.
  235. TEST_F(RootWindowControllerTest, MoveWindows_LockWindowsInUnified) {
  236. display_manager()->SetUnifiedDesktopEnabled(true);
  237. UpdateDisplay("500x400");
  238. const int kLockScreenWindowId = 1000;
  239. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  240. aura::Window* lock_container =
  241. controller->GetContainer(kShellWindowId_LockScreenContainer);
  242. views::Widget* lock_screen =
  243. CreateModalWidgetWithParent(gfx::Rect(10, 10, 100, 100), lock_container);
  244. lock_screen->GetNativeWindow()->SetId(kLockScreenWindowId);
  245. lock_screen->SetFullscreen(true);
  246. ASSERT_EQ(lock_screen->GetNativeWindow(),
  247. controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
  248. EXPECT_EQ("0,0 500x400", lock_screen->GetNativeWindow()->bounds().ToString());
  249. // Switch to unified.
  250. UpdateDisplay("500x400,500x400");
  251. // In unified mode, RWC is created
  252. controller = Shell::GetPrimaryRootWindowController();
  253. ASSERT_EQ(lock_screen->GetNativeWindow(),
  254. controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
  255. EXPECT_EQ("0,0 500x400", lock_screen->GetNativeWindow()->bounds().ToString());
  256. // Switch to mirror.
  257. display_manager()->SetMirrorMode(display::MirrorMode::kNormal, absl::nullopt);
  258. EXPECT_TRUE(display_manager()->IsInMirrorMode());
  259. controller = Shell::GetPrimaryRootWindowController();
  260. ASSERT_EQ(lock_screen->GetNativeWindow(),
  261. controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
  262. EXPECT_EQ("0,0 500x400", lock_screen->GetNativeWindow()->bounds().ToString());
  263. // Switch to unified.
  264. display_manager()->SetMirrorMode(display::MirrorMode::kOff, absl::nullopt);
  265. EXPECT_TRUE(display_manager()->IsInUnifiedMode());
  266. controller = Shell::GetPrimaryRootWindowController();
  267. ASSERT_EQ(lock_screen->GetNativeWindow(),
  268. controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
  269. EXPECT_EQ("0,0 500x400", lock_screen->GetNativeWindow()->bounds().ToString());
  270. // Switch to single display.
  271. UpdateDisplay("600x500");
  272. EXPECT_FALSE(display_manager()->IsInUnifiedMode());
  273. EXPECT_FALSE(display_manager()->IsInMirrorMode());
  274. controller = Shell::GetPrimaryRootWindowController();
  275. ASSERT_EQ(lock_screen->GetNativeWindow(),
  276. controller->GetRootWindow()->GetChildById(kLockScreenWindowId));
  277. EXPECT_EQ("0,0 600x500", lock_screen->GetNativeWindow()->bounds().ToString());
  278. }
  279. // Tests that the moved windows maintain MRU ordering.
  280. TEST_F(RootWindowControllerTest, MoveWindows_MaintainMRUordering) {
  281. UpdateDisplay("600x500,300x250");
  282. display::Screen* screen = display::Screen::GetScreen();
  283. const display::Display primary_display = screen->GetPrimaryDisplay();
  284. const display::Display secondary_display = GetSecondaryDisplay();
  285. views::Widget* existing1 = CreateTestWidget(gfx::Rect(0, 10, 100, 100));
  286. ASSERT_EQ(primary_display.id(),
  287. screen->GetDisplayNearestWindow(existing1->GetNativeWindow()).id());
  288. views::Widget* moved = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
  289. ASSERT_EQ(secondary_display.id(),
  290. screen->GetDisplayNearestWindow(moved->GetNativeWindow()).id());
  291. views::Widget* existing2 = CreateTestWidget(gfx::Rect(0, 10, 100, 100));
  292. ASSERT_EQ(primary_display.id(),
  293. screen->GetDisplayNearestWindow(existing2->GetNativeWindow()).id());
  294. views::Widget* active = CreateTestWidget(gfx::Rect(650, 10, 100, 100));
  295. ASSERT_TRUE(active->IsActive());
  296. ASSERT_EQ(secondary_display.id(),
  297. screen->GetDisplayNearestWindow(active->GetNativeWindow()).id());
  298. // Switch to single display.
  299. UpdateDisplay("600x500");
  300. // |active| is still active.
  301. ASSERT_TRUE(active->IsActive());
  302. // |moved| should be put between |existing2| and |existing1| to maintain MRU
  303. // ordering.
  304. aura::Window* parent = moved->GetNativeWindow()->parent();
  305. ASSERT_EQ(parent, existing1->GetNativeWindow()->parent());
  306. const std::vector<aura::Window*> expected_order = {
  307. existing1->GetNativeWindow(), moved->GetNativeWindow(),
  308. existing2->GetNativeWindow(), active->GetNativeWindow()};
  309. EXPECT_EQ(expected_order, parent->children());
  310. }
  311. TEST_F(RootWindowControllerTest, ModalContainer) {
  312. UpdateDisplay("600x500");
  313. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  314. EXPECT_TRUE(Shell::Get()->session_controller()->IsActiveUserSessionStarted());
  315. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  316. controller->GetSystemModalLayoutManager(nullptr));
  317. views::Widget* session_modal_widget =
  318. CreateModalWidget(gfx::Rect(300, 10, 100, 100));
  319. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  320. controller->GetSystemModalLayoutManager(
  321. session_modal_widget->GetNativeWindow()));
  322. GetSessionControllerClient()->LockScreen();
  323. EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
  324. EXPECT_EQ(
  325. GetLayoutManager(controller, kShellWindowId_LockSystemModalContainer),
  326. controller->GetSystemModalLayoutManager(nullptr));
  327. aura::Window* lock_container =
  328. controller->GetContainer(kShellWindowId_LockScreenContainer);
  329. views::Widget* lock_modal_widget =
  330. CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
  331. EXPECT_EQ(
  332. GetLayoutManager(controller, kShellWindowId_LockSystemModalContainer),
  333. controller->GetSystemModalLayoutManager(
  334. lock_modal_widget->GetNativeWindow()));
  335. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  336. controller->GetSystemModalLayoutManager(
  337. session_modal_widget->GetNativeWindow()));
  338. GetSessionControllerClient()->UnlockScreen();
  339. }
  340. TEST_F(RootWindowControllerTest, ModalContainerNotLoggedInLoggedIn) {
  341. UpdateDisplay("600x500");
  342. // Configure login screen environment.
  343. SessionControllerImpl* session_controller =
  344. Shell::Get()->session_controller();
  345. ClearLogin();
  346. EXPECT_EQ(0, session_controller->NumberOfLoggedInUsers());
  347. EXPECT_FALSE(session_controller->IsActiveUserSessionStarted());
  348. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  349. EXPECT_EQ(
  350. GetLayoutManager(controller, kShellWindowId_LockSystemModalContainer),
  351. controller->GetSystemModalLayoutManager(nullptr));
  352. aura::Window* lock_container =
  353. controller->GetContainer(kShellWindowId_LockScreenContainer);
  354. views::Widget* login_modal_widget =
  355. CreateModalWidgetWithParent(gfx::Rect(300, 10, 100, 100), lock_container);
  356. EXPECT_EQ(
  357. GetLayoutManager(controller, kShellWindowId_LockSystemModalContainer),
  358. controller->GetSystemModalLayoutManager(
  359. login_modal_widget->GetNativeWindow()));
  360. login_modal_widget->Close();
  361. // Configure user session environment.
  362. CreateUserSessions(1);
  363. EXPECT_EQ(1, session_controller->NumberOfLoggedInUsers());
  364. EXPECT_TRUE(session_controller->IsActiveUserSessionStarted());
  365. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  366. controller->GetSystemModalLayoutManager(nullptr));
  367. views::Widget* session_modal_widget =
  368. CreateModalWidget(gfx::Rect(300, 10, 100, 100));
  369. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  370. controller->GetSystemModalLayoutManager(
  371. session_modal_widget->GetNativeWindow()));
  372. }
  373. TEST_F(RootWindowControllerTest, ModalContainerBlockedSession) {
  374. UpdateDisplay("600x500");
  375. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  376. aura::Window* lock_container =
  377. controller->GetContainer(kShellWindowId_LockScreenContainer);
  378. for (int block_reason = FIRST_BLOCK_REASON;
  379. block_reason < NUMBER_OF_BLOCK_REASONS; ++block_reason) {
  380. views::Widget* session_modal_widget =
  381. CreateModalWidget(gfx::Rect(300, 10, 100, 100));
  382. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  383. controller->GetSystemModalLayoutManager(
  384. session_modal_widget->GetNativeWindow()));
  385. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  386. controller->GetSystemModalLayoutManager(nullptr));
  387. session_modal_widget->Close();
  388. BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
  389. EXPECT_EQ(
  390. GetLayoutManager(controller, kShellWindowId_LockSystemModalContainer),
  391. controller->GetSystemModalLayoutManager(nullptr));
  392. views::Widget* lock_modal_widget = CreateModalWidgetWithParent(
  393. gfx::Rect(300, 10, 100, 100), lock_container);
  394. EXPECT_EQ(
  395. GetLayoutManager(controller, kShellWindowId_LockSystemModalContainer),
  396. controller->GetSystemModalLayoutManager(
  397. lock_modal_widget->GetNativeWindow()));
  398. session_modal_widget = CreateModalWidget(gfx::Rect(300, 10, 100, 100));
  399. EXPECT_EQ(GetLayoutManager(controller, kShellWindowId_SystemModalContainer),
  400. controller->GetSystemModalLayoutManager(
  401. session_modal_widget->GetNativeWindow()));
  402. session_modal_widget->Close();
  403. lock_modal_widget->Close();
  404. UnblockUserSession();
  405. }
  406. }
  407. TEST_F(RootWindowControllerTest, GetWindowForFullscreenMode) {
  408. UpdateDisplay("600x500");
  409. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  410. Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
  411. w1->Maximize();
  412. Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
  413. w2->SetFullscreen(true);
  414. // |w3| is a transient child of |w2|.
  415. Widget* w3 = Widget::CreateWindowWithParent(nullptr, w2->GetNativeWindow(),
  416. gfx::Rect(0, 0, 100, 100));
  417. // Test that GetWindowForFullscreenMode() finds the fullscreen window when one
  418. // of its transient children is active.
  419. w3->Activate();
  420. EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
  421. // If the topmost window is not fullscreen, it returns nullptr.
  422. w1->Activate();
  423. EXPECT_EQ(nullptr, controller->GetWindowForFullscreenMode());
  424. w1->Close();
  425. w3->Close();
  426. // Only w2 remains, if minimized GetWindowForFullscreenMode should return
  427. // nullptr.
  428. w2->Activate();
  429. EXPECT_EQ(w2->GetNativeWindow(), controller->GetWindowForFullscreenMode());
  430. w2->Minimize();
  431. EXPECT_EQ(nullptr, controller->GetWindowForFullscreenMode());
  432. }
  433. TEST_F(RootWindowControllerTest, MultipleDisplaysGetWindowForFullscreenMode) {
  434. UpdateDisplay("600x500,600x500");
  435. Shell::RootWindowControllerList controllers =
  436. Shell::Get()->GetAllRootWindowControllers();
  437. Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
  438. w1->Maximize();
  439. Widget* w2 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
  440. w2->SetFullscreen(true);
  441. Widget* w3 = CreateTestWidget(gfx::Rect(600, 0, 100, 100));
  442. EXPECT_EQ(w1->GetNativeWindow()->GetRootWindow(),
  443. controllers[0]->GetRootWindow());
  444. EXPECT_EQ(w2->GetNativeWindow()->GetRootWindow(),
  445. controllers[0]->GetRootWindow());
  446. EXPECT_EQ(w3->GetNativeWindow()->GetRootWindow(),
  447. controllers[1]->GetRootWindow());
  448. w1->Activate();
  449. EXPECT_EQ(nullptr, controllers[0]->GetWindowForFullscreenMode());
  450. EXPECT_EQ(nullptr, controllers[1]->GetWindowForFullscreenMode());
  451. w2->Activate();
  452. EXPECT_EQ(w2->GetNativeWindow(),
  453. controllers[0]->GetWindowForFullscreenMode());
  454. EXPECT_EQ(nullptr, controllers[1]->GetWindowForFullscreenMode());
  455. // Verify that the first root window controller remains in fullscreen mode
  456. // when a window on the other display is activated.
  457. w3->Activate();
  458. EXPECT_EQ(w2->GetNativeWindow(),
  459. controllers[0]->GetWindowForFullscreenMode());
  460. EXPECT_EQ(nullptr, controllers[1]->GetWindowForFullscreenMode());
  461. }
  462. // Test that ForWindow() works with multiple displays and child widgets.
  463. TEST_F(RootWindowControllerTest, ForWindow) {
  464. UpdateDisplay("600x500,600x500");
  465. Shell::RootWindowControllerList controllers =
  466. Shell::Get()->GetAllRootWindowControllers();
  467. ASSERT_EQ(2u, controllers.size());
  468. // Test a root window.
  469. EXPECT_EQ(controllers[0],
  470. RootWindowController::ForWindow(Shell::GetPrimaryRootWindow()));
  471. // Test a widget on the first display.
  472. Widget* w1 = CreateTestWidget(gfx::Rect(0, 0, 100, 100));
  473. EXPECT_EQ(controllers[0],
  474. RootWindowController::ForWindow(w1->GetNativeWindow()));
  475. // Test a child widget.
  476. Widget* w2 = Widget::CreateWindowWithParent(nullptr, w1->GetNativeWindow(),
  477. gfx::Rect(0, 0, 100, 100));
  478. EXPECT_EQ(controllers[0],
  479. RootWindowController::ForWindow(w2->GetNativeWindow()));
  480. // Test a widget on the second display.
  481. Widget* w3 = CreateTestWidget(gfx::Rect(600, 0, 100, 100));
  482. EXPECT_EQ(controllers[1],
  483. RootWindowController::ForWindow(w3->GetNativeWindow()));
  484. }
  485. // Test that user session window can't be focused if user session blocked by
  486. // some overlapping UI.
  487. TEST_F(RootWindowControllerTest, FocusBlockedWindow) {
  488. UpdateDisplay("600x500");
  489. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  490. aura::Window* lock_container =
  491. controller->GetContainer(kShellWindowId_LockScreenContainer);
  492. aura::Window* lock_window =
  493. Widget::CreateWindowWithParent(nullptr, lock_container,
  494. gfx::Rect(0, 0, 100, 100))
  495. ->GetNativeView();
  496. lock_window->Show();
  497. aura::Window* session_window =
  498. CreateTestWidget(gfx::Rect(0, 0, 100, 100))->GetNativeView();
  499. session_window->Show();
  500. for (int block_reason = FIRST_BLOCK_REASON;
  501. block_reason < NUMBER_OF_BLOCK_REASONS; ++block_reason) {
  502. BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
  503. lock_window->Focus();
  504. EXPECT_TRUE(lock_window->HasFocus());
  505. session_window->Focus();
  506. EXPECT_FALSE(session_window->HasFocus());
  507. UnblockUserSession();
  508. }
  509. }
  510. // Tracks whether OnWindowDestroying() has been invoked.
  511. class DestroyedWindowObserver : public aura::WindowObserver {
  512. public:
  513. DestroyedWindowObserver() : destroyed_(false), window_(nullptr) {}
  514. DestroyedWindowObserver(const DestroyedWindowObserver&) = delete;
  515. DestroyedWindowObserver& operator=(const DestroyedWindowObserver&) = delete;
  516. ~DestroyedWindowObserver() override { Shutdown(); }
  517. void SetWindow(Window* window) {
  518. window_ = window;
  519. window->AddObserver(this);
  520. }
  521. bool destroyed() const { return destroyed_; }
  522. // WindowObserver overrides:
  523. void OnWindowDestroying(Window* window) override {
  524. destroyed_ = true;
  525. Shutdown();
  526. }
  527. private:
  528. void Shutdown() {
  529. if (!window_)
  530. return;
  531. window_->RemoveObserver(this);
  532. window_ = nullptr;
  533. }
  534. bool destroyed_;
  535. Window* window_;
  536. };
  537. // Verifies shutdown doesn't delete windows that are not owned by the parent.
  538. TEST_F(RootWindowControllerTest, DontDeleteWindowsNotOwnedByParent) {
  539. DestroyedWindowObserver observer1;
  540. aura::test::TestWindowDelegate delegate1;
  541. std::unique_ptr<aura::Window> window1 = std::make_unique<aura::Window>(
  542. &delegate1, aura::client::WINDOW_TYPE_CONTROL);
  543. window1->set_owned_by_parent(false);
  544. observer1.SetWindow(window1.get());
  545. window1->Init(ui::LAYER_NOT_DRAWN);
  546. aura::client::ParentWindowWithContext(
  547. window1.get(), Shell::GetPrimaryRootWindow(), gfx::Rect());
  548. DestroyedWindowObserver observer2;
  549. std::unique_ptr<aura::Window> window2 =
  550. std::make_unique<aura::Window>(nullptr);
  551. window2->set_owned_by_parent(false);
  552. observer2.SetWindow(window2.get());
  553. window2->Init(ui::LAYER_NOT_DRAWN);
  554. Shell::GetPrimaryRootWindow()->AddChild(window2.get());
  555. Shell::GetPrimaryRootWindowController()->CloseChildWindows();
  556. ASSERT_FALSE(observer1.destroyed());
  557. window1.reset();
  558. ASSERT_FALSE(observer2.destroyed());
  559. window2.reset();
  560. }
  561. // Verify that the context menu gets hidden when entering or exiting tablet
  562. // mode.
  563. TEST_F(RootWindowControllerTest, ContextMenuDisappearsInTabletMode) {
  564. RootWindowController* controller = Shell::GetPrimaryRootWindowController();
  565. // Open context menu.
  566. ui::test::EventGenerator generator(controller->GetRootWindow());
  567. generator.PressRightButton();
  568. generator.ReleaseRightButton();
  569. EXPECT_TRUE(controller->root_window_menu_model_adapter_);
  570. // Verify menu closes on entering tablet mode.
  571. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(true);
  572. EXPECT_FALSE(controller->root_window_menu_model_adapter_);
  573. // Open context menu.
  574. generator.PressRightButton();
  575. generator.ReleaseRightButton();
  576. EXPECT_TRUE(controller->root_window_menu_model_adapter_);
  577. // Verify menu closes on exiting tablet mode.
  578. Shell::Get()->tablet_mode_controller()->SetEnabledForTest(false);
  579. EXPECT_FALSE(controller->root_window_menu_model_adapter_);
  580. }
  581. class VirtualKeyboardRootWindowControllerTest
  582. : public RootWindowControllerTest {
  583. public:
  584. VirtualKeyboardRootWindowControllerTest() = default;
  585. VirtualKeyboardRootWindowControllerTest(
  586. const VirtualKeyboardRootWindowControllerTest&) = delete;
  587. VirtualKeyboardRootWindowControllerTest& operator=(
  588. const VirtualKeyboardRootWindowControllerTest&) = delete;
  589. ~VirtualKeyboardRootWindowControllerTest() override = default;
  590. void SetUp() override {
  591. AshTestBase::SetUp();
  592. SetVirtualKeyboardEnabled(true);
  593. }
  594. void TearDown() override {
  595. SetVirtualKeyboardEnabled(false);
  596. AshTestBase::TearDown();
  597. }
  598. void EnsureCaretInWorkArea(const gfx::Rect& occluded_bounds) {
  599. keyboard::KeyboardUIController::Get()->EnsureCaretInWorkAreaForTest(
  600. occluded_bounds);
  601. }
  602. };
  603. class MockTextInputClient : public ui::DummyTextInputClient {
  604. public:
  605. MockTextInputClient() : ui::DummyTextInputClient(ui::TEXT_INPUT_TYPE_TEXT) {}
  606. MockTextInputClient(const MockTextInputClient&) = delete;
  607. MockTextInputClient& operator=(const MockTextInputClient&) = delete;
  608. void EnsureCaretNotInRect(const gfx::Rect& rect) override {
  609. caret_exclude_rect_ = rect;
  610. }
  611. const gfx::Rect& caret_exclude_rect() const { return caret_exclude_rect_; }
  612. private:
  613. gfx::Rect caret_exclude_rect_;
  614. };
  615. class TargetHitTestEventHandler : public ui::test::TestEventHandler {
  616. public:
  617. TargetHitTestEventHandler() = default;
  618. TargetHitTestEventHandler(const TargetHitTestEventHandler&) = delete;
  619. TargetHitTestEventHandler& operator=(const TargetHitTestEventHandler&) =
  620. delete;
  621. // ui::test::TestEventHandler overrides.
  622. void OnMouseEvent(ui::MouseEvent* event) override {
  623. if (event->type() == ui::ET_MOUSE_PRESSED)
  624. ui::test::TestEventHandler::OnMouseEvent(event);
  625. event->StopPropagation();
  626. }
  627. };
  628. // Test for http://crbug.com/263599. Virtual keyboard should be able to receive
  629. // events at blocked user session.
  630. TEST_F(VirtualKeyboardRootWindowControllerTest,
  631. ClickVirtualKeyboardInBlockedWindow) {
  632. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  633. aura::Window* keyboard_container =
  634. Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
  635. ASSERT_TRUE(keyboard_container);
  636. keyboard_container->Show();
  637. aura::Window* contents_window =
  638. keyboard::KeyboardUIController::Get()->GetKeyboardWindow();
  639. contents_window->Show();
  640. keyboard::KeyboardUIController::Get()->ShowKeyboard(false);
  641. // Make sure no pending mouse events in the queue.
  642. base::RunLoop().RunUntilIdle();
  643. // TODO(oshima|yhanada): This simply make sure that targeting logic works, but
  644. // doesn't mean it'll deliver the event to the target. Fix this to make this
  645. // more reliable.
  646. ui::test::TestEventHandler handler;
  647. root_window->AddPreTargetHandler(&handler);
  648. ui::test::EventGenerator event_generator(root_window, contents_window);
  649. event_generator.ClickLeftButton();
  650. EXPECT_EQ(2, handler.num_mouse_events());
  651. for (int block_reason = FIRST_BLOCK_REASON;
  652. block_reason < NUMBER_OF_BLOCK_REASONS; ++block_reason) {
  653. SCOPED_TRACE(base::StringPrintf("Reason: %d", block_reason));
  654. BlockUserSession(static_cast<UserSessionBlockReason>(block_reason));
  655. handler.Reset();
  656. event_generator.ClickLeftButton();
  657. // Click may generate CAPTURE_CHANGED event so make sure it's more than
  658. // 2 (press,release);
  659. EXPECT_LE(2, handler.num_mouse_events());
  660. UnblockUserSession();
  661. }
  662. root_window->RemovePreTargetHandler(&handler);
  663. }
  664. // Test for crbug.com/342524. After user login, the work space should restore to
  665. // full screen.
  666. TEST_F(VirtualKeyboardRootWindowControllerTest, RestoreWorkspaceAfterLogin) {
  667. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  668. aura::Window* keyboard_container =
  669. Shell::GetContainer(root_window, kShellWindowId_VirtualKeyboardContainer);
  670. keyboard_container->Show();
  671. auto* controller = keyboard::KeyboardUIController::Get();
  672. aura::Window* contents_window = controller->GetKeyboardWindow();
  673. contents_window->SetBounds(
  674. keyboard::KeyboardBoundsFromRootBounds(root_window->bounds(), 100));
  675. contents_window->Show();
  676. gfx::Rect before =
  677. display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  678. if (!controller->IsKeyboardOverscrollEnabled()) {
  679. gfx::Rect after =
  680. display::Screen::GetScreen()->GetPrimaryDisplay().work_area();
  681. EXPECT_LT(after, before);
  682. }
  683. // Mock a login user profile change to reinitialize the keyboard.
  684. SessionInfo info;
  685. info.state = session_manager::SessionState::ACTIVE;
  686. Shell::Get()->session_controller()->SetSessionInfo(info);
  687. EXPECT_EQ(display::Screen::GetScreen()->GetPrimaryDisplay().work_area(),
  688. before);
  689. }
  690. // Ensure that system modal dialogs do not block events targeted at the virtual
  691. // keyboard.
  692. TEST_F(VirtualKeyboardRootWindowControllerTest, ClickWithActiveModalDialog) {
  693. auto* controller = keyboard::KeyboardUIController::Get();
  694. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  695. ASSERT_EQ(root_window, controller->GetRootWindow());
  696. controller->ShowKeyboard(false /* locked */);
  697. ASSERT_TRUE(keyboard::WaitUntilShown());
  698. ui::test::TestEventHandler handler;
  699. root_window->AddPreTargetHandler(&handler);
  700. ui::test::EventGenerator root_window_event_generator(root_window);
  701. ui::test::EventGenerator keyboard_event_generator(
  702. root_window, controller->GetKeyboardWindow());
  703. views::Widget* modal_widget = CreateModalWidget(gfx::Rect(300, 10, 100, 100));
  704. // Verify that mouse events to the root window are block with a visble modal
  705. // dialog.
  706. root_window_event_generator.ClickLeftButton();
  707. EXPECT_EQ(0, handler.num_mouse_events());
  708. // Verify that event dispatch to the virtual keyboard is unblocked.
  709. keyboard_event_generator.ClickLeftButton();
  710. EXPECT_EQ(1, handler.num_mouse_events() / 2);
  711. modal_widget->Close();
  712. // Verify that mouse events are now unblocked to the root window.
  713. root_window_event_generator.ClickLeftButton();
  714. EXPECT_EQ(2, handler.num_mouse_events() / 2);
  715. root_window->RemovePreTargetHandler(&handler);
  716. }
  717. // Ensure that the visible area for scrolling the text caret excludes the
  718. // region occluded by the on-screen keyboard.
  719. TEST_F(VirtualKeyboardRootWindowControllerTest, EnsureCaretInWorkArea) {
  720. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  721. MockTextInputClient text_input_client;
  722. ui::InputMethod* input_method = keyboard_controller->GetInputMethodForTest();
  723. ASSERT_TRUE(input_method);
  724. input_method->SetFocusedTextInputClient(&text_input_client);
  725. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  726. const int keyboard_height = 100;
  727. aura::Window* contents_window = keyboard_controller->GetKeyboardWindow();
  728. contents_window->SetBounds(keyboard::KeyboardBoundsFromRootBounds(
  729. root_window->bounds(), keyboard_height));
  730. contents_window->Show();
  731. EnsureCaretInWorkArea(contents_window->GetBoundsInScreen());
  732. ASSERT_EQ(root_window->bounds().width(),
  733. text_input_client.caret_exclude_rect().width());
  734. ASSERT_EQ(keyboard_height, text_input_client.caret_exclude_rect().height());
  735. input_method->SetFocusedTextInputClient(nullptr);
  736. }
  737. TEST_F(VirtualKeyboardRootWindowControllerTest,
  738. EnsureCaretInWorkAreaWithMultipleDisplays) {
  739. UpdateDisplay("600x500,600x500");
  740. const int64_t primary_display_id =
  741. display::Screen::GetScreen()->GetPrimaryDisplay().id();
  742. const int64_t secondary_display_id = GetSecondaryDisplay().id();
  743. ASSERT_NE(primary_display_id, secondary_display_id);
  744. aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  745. ASSERT_EQ(static_cast<size_t>(2), root_windows.size());
  746. aura::Window* primary_root_window = root_windows[0];
  747. aura::Window* secondary_root_window = root_windows[1];
  748. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  749. MockTextInputClient text_input_client;
  750. ui::InputMethod* input_method = keyboard_controller->GetInputMethodForTest();
  751. ASSERT_TRUE(input_method);
  752. input_method->SetFocusedTextInputClient(&text_input_client);
  753. const int keyboard_height = 100;
  754. // Check that the keyboard on the primary screen doesn't cover the window on
  755. // the secondary screen.
  756. aura::Window* contents_window = keyboard_controller->GetKeyboardWindow();
  757. contents_window->SetBounds(keyboard::KeyboardBoundsFromRootBounds(
  758. primary_root_window->bounds(), keyboard_height));
  759. contents_window->Show();
  760. EnsureCaretInWorkArea(contents_window->GetBoundsInScreen());
  761. EXPECT_TRUE(primary_root_window->GetBoundsInScreen().Contains(
  762. text_input_client.caret_exclude_rect()));
  763. EXPECT_EQ(primary_root_window->GetBoundsInScreen().width(),
  764. text_input_client.caret_exclude_rect().width());
  765. EXPECT_FALSE(secondary_root_window->GetBoundsInScreen().Contains(
  766. text_input_client.caret_exclude_rect()));
  767. // Move the keyboard into the secondary display and check that the keyboard
  768. // doesn't cover the window on the primary screen.
  769. keyboard_controller->ShowKeyboardInDisplay(GetSecondaryDisplay());
  770. contents_window->SetBounds(keyboard::KeyboardBoundsFromRootBounds(
  771. secondary_root_window->bounds(), keyboard_height));
  772. EnsureCaretInWorkArea(contents_window->GetBoundsInScreen());
  773. EXPECT_FALSE(primary_root_window->GetBoundsInScreen().Contains(
  774. text_input_client.caret_exclude_rect()));
  775. EXPECT_TRUE(secondary_root_window->GetBoundsInScreen().Contains(
  776. text_input_client.caret_exclude_rect()));
  777. EXPECT_EQ(secondary_root_window->GetBoundsInScreen().width(),
  778. text_input_client.caret_exclude_rect().width());
  779. input_method->SetFocusedTextInputClient(nullptr);
  780. }
  781. // Tests that the virtual keyboard does not block context menus. The virtual
  782. // keyboard should appear in front of most content, but not context menus. See
  783. // crbug/377180.
  784. TEST_F(VirtualKeyboardRootWindowControllerTest, ZOrderTest) {
  785. UpdateDisplay("800x600");
  786. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  787. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  788. const int keyboard_height = 200;
  789. aura::Window* contents_window = keyboard_controller->GetKeyboardWindow();
  790. gfx::Rect keyboard_bounds = keyboard::KeyboardBoundsFromRootBounds(
  791. root_window->bounds(), keyboard_height);
  792. contents_window->SetBounds(keyboard_bounds);
  793. contents_window->Show();
  794. ui::test::EventGenerator generator(root_window);
  795. // Cover the screen with two windows: a normal window on the left side and a
  796. // context menu on the right side. When the virtual keyboard is displayed it
  797. // partially occludes the normal window, but not the context menu. Compute
  798. // positions for generating synthetic click events to perform hit tests,
  799. // ensuring the correct window layering. 'top' is above the VK, whereas
  800. // 'bottom' lies within the VK. 'left' is centered in the normal window, and
  801. // 'right' is centered in the context menu.
  802. int window_height = keyboard_bounds.bottom();
  803. int window_width = keyboard_bounds.width() / 2;
  804. int left = window_width / 2;
  805. int right = 3 * window_width / 2;
  806. int top = keyboard_bounds.y() / 2;
  807. int bottom = window_height - keyboard_height / 2;
  808. // Normal window is partially occluded by the virtual keyboard.
  809. aura::test::TestWindowDelegate delegate;
  810. std::unique_ptr<aura::Window> normal(
  811. CreateTestWindowInShellWithDelegateAndType(
  812. &delegate, aura::client::WINDOW_TYPE_NORMAL, 0,
  813. gfx::Rect(0, 0, window_width, window_height)));
  814. normal->set_owned_by_parent(false);
  815. normal->Show();
  816. TargetHitTestEventHandler normal_handler;
  817. normal->AddPreTargetHandler(&normal_handler);
  818. // Test that only the click on the top portion of the window is picked up. The
  819. // click on the bottom hits the virtual keyboard instead.
  820. generator.MoveMouseTo(left, top);
  821. generator.ClickLeftButton();
  822. EXPECT_EQ(1, normal_handler.num_mouse_events());
  823. generator.MoveMouseTo(left, bottom);
  824. generator.ClickLeftButton();
  825. EXPECT_EQ(1, normal_handler.num_mouse_events());
  826. // Menu overlaps virtual keyboard.
  827. aura::test::TestWindowDelegate delegate2;
  828. std::unique_ptr<aura::Window> menu(CreateTestWindowInShellWithDelegateAndType(
  829. &delegate2, aura::client::WINDOW_TYPE_MENU, 0,
  830. gfx::Rect(window_width, 0, window_width, window_height)));
  831. menu->set_owned_by_parent(false);
  832. menu->Show();
  833. TargetHitTestEventHandler menu_handler;
  834. menu->AddPreTargetHandler(&menu_handler);
  835. // Test that both clicks register.
  836. generator.MoveMouseTo(right, top);
  837. generator.ClickLeftButton();
  838. EXPECT_EQ(1, menu_handler.num_mouse_events());
  839. generator.MoveMouseTo(right, bottom);
  840. generator.ClickLeftButton();
  841. EXPECT_EQ(2, menu_handler.num_mouse_events());
  842. // Cleanup to ensure that the test windows are destroyed before their
  843. // delegates.
  844. normal.reset();
  845. menu.reset();
  846. }
  847. // Tests that the virtual keyboard correctly resizes with a change to display
  848. // orientation. See crbug/417612.
  849. TEST_F(VirtualKeyboardRootWindowControllerTest, DisplayRotation) {
  850. UpdateDisplay("800x600");
  851. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  852. keyboard_controller->ShowKeyboard(false);
  853. aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
  854. keyboard_window->SetBounds(gfx::Rect(0, 400, 800, 200));
  855. EXPECT_EQ("0,400 800x200", keyboard_window->bounds().ToString());
  856. UpdateDisplay("600x800");
  857. EXPECT_EQ("0,600 600x200", keyboard_window->bounds().ToString());
  858. }
  859. // Keeps a count of all the events a window receives.
  860. class EventObserver : public ui::EventHandler {
  861. public:
  862. EventObserver() = default;
  863. EventObserver(const EventObserver&) = delete;
  864. EventObserver& operator=(const EventObserver&) = delete;
  865. ~EventObserver() override = default;
  866. int GetEventCount(ui::EventType type) { return event_counts_[type]; }
  867. void ResetAllEventCounts() { event_counts_.clear(); }
  868. private:
  869. // Overridden from ui::EventHandler:
  870. void OnEvent(ui::Event* event) override {
  871. ui::EventHandler::OnEvent(event);
  872. event_counts_[event->type()]++;
  873. }
  874. std::map<ui::EventType, int> event_counts_;
  875. };
  876. // Tests that tapping/clicking inside the keyboard does not give it focus.
  877. TEST_F(VirtualKeyboardRootWindowControllerTest, ClickDoesNotFocusKeyboard) {
  878. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  879. // Create a test window in the background with the same size as the screen.
  880. aura::test::EventCountDelegate delegate;
  881. std::unique_ptr<aura::Window> background_window(
  882. CreateTestWindowInShellWithDelegate(&delegate, 0, root_window->bounds()));
  883. background_window->Focus();
  884. EXPECT_TRUE(background_window->IsVisible());
  885. EXPECT_TRUE(background_window->HasFocus());
  886. auto* keyboard_controller = keyboard::KeyboardUIController::Get();
  887. keyboard_controller->ShowKeyboard(false);
  888. ASSERT_TRUE(keyboard::WaitUntilShown());
  889. aura::Window* keyboard_window = keyboard_controller->GetKeyboardWindow();
  890. EXPECT_FALSE(keyboard_window->HasFocus());
  891. // Click on the keyboard. Make sure the keyboard receives the event, but does
  892. // not get focus.
  893. EventObserver observer;
  894. keyboard_window->AddPreTargetHandler(&observer);
  895. ui::test::EventGenerator generator(root_window);
  896. generator.MoveMouseTo(keyboard_window->bounds().CenterPoint());
  897. generator.ClickLeftButton();
  898. EXPECT_TRUE(background_window->HasFocus());
  899. EXPECT_FALSE(keyboard_window->HasFocus());
  900. EXPECT_EQ("0 0", delegate.GetMouseButtonCountsAndReset());
  901. EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_PRESSED));
  902. EXPECT_EQ(1, observer.GetEventCount(ui::ET_MOUSE_RELEASED));
  903. // Click outside of the keyboard. It should reach the window behind.
  904. observer.ResetAllEventCounts();
  905. generator.MoveMouseTo(gfx::Point());
  906. generator.ClickLeftButton();
  907. EXPECT_EQ("1 1", delegate.GetMouseButtonCountsAndReset());
  908. EXPECT_EQ(0, observer.GetEventCount(ui::ET_MOUSE_PRESSED));
  909. EXPECT_EQ(0, observer.GetEventCount(ui::ET_MOUSE_RELEASED));
  910. keyboard_window->RemovePreTargetHandler(&observer);
  911. }
  912. } // namespace ash