screen_position_controller_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/display/screen_position_controller.h"
  5. #include <memory>
  6. #include "ash/public/cpp/shell_window_ids.h"
  7. #include "ash/public/cpp/test/shell_test_api.h"
  8. #include "ash/screen_util.h"
  9. #include "ash/shell.h"
  10. #include "ash/test/ash_test_base.h"
  11. #include "base/run_loop.h"
  12. #include "ui/aura/env.h"
  13. #include "ui/aura/test/test_window_delegate.h"
  14. #include "ui/aura/window.h"
  15. #include "ui/aura/window_tracker.h"
  16. #include "ui/aura/window_tree_host.h"
  17. #include "ui/base/layout.h"
  18. #include "ui/display/display_layout.h"
  19. #include "ui/display/display_switches.h"
  20. #include "ui/display/manager/display_manager.h"
  21. #include "ui/display/screen.h"
  22. #include "ui/display/test/display_manager_test_api.h"
  23. #include "ui/events/test/event_generator.h"
  24. namespace ash {
  25. namespace {
  26. ScreenPositionController* GetScreenPositionController() {
  27. return ShellTestApi().screen_position_controller();
  28. }
  29. class ScreenPositionControllerTest : public AshTestBase {
  30. public:
  31. ScreenPositionControllerTest() = default;
  32. ScreenPositionControllerTest(const ScreenPositionControllerTest&) = delete;
  33. ScreenPositionControllerTest& operator=(const ScreenPositionControllerTest&) =
  34. delete;
  35. ~ScreenPositionControllerTest() override = default;
  36. void SetUp() override {
  37. AshTestBase::SetUp();
  38. window_ = std::make_unique<aura::Window>(&window_delegate_);
  39. window_->SetType(aura::client::WINDOW_TYPE_NORMAL);
  40. window_->Init(ui::LAYER_NOT_DRAWN);
  41. ParentWindowInPrimaryRootWindow(window_.get());
  42. window_->SetId(1);
  43. }
  44. void TearDown() override {
  45. window_.reset();
  46. AshTestBase::TearDown();
  47. }
  48. // Converts a point (x, y) in host window's coordinate to screen and
  49. // returns its string representation.
  50. std::string ConvertHostPointToScreen(int x, int y) const {
  51. gfx::Point point(x, y);
  52. GetScreenPositionController()->ConvertHostPointToScreen(
  53. window_->GetRootWindow(), &point);
  54. return point.ToString();
  55. }
  56. void SetSecondaryDisplayLayout(display::DisplayPlacement::Position position) {
  57. std::unique_ptr<display::DisplayLayout> layout(
  58. display_manager()->GetCurrentDisplayLayout().Copy());
  59. layout->placement_list[0].position = position;
  60. display_manager()->SetLayoutForCurrentDisplays(std::move(layout));
  61. }
  62. protected:
  63. std::unique_ptr<aura::Window> window_;
  64. aura::test::TestWindowDelegate window_delegate_;
  65. };
  66. } // namespace
  67. TEST_F(ScreenPositionControllerTest, ConvertHostPointToScreen) {
  68. // Make sure that the point is in host coordinates. (crbug.com/521919)
  69. UpdateDisplay("100+100-200x190,100+300-200x190");
  70. // The point 150,210 should be in host coords, and detected as outside.
  71. EXPECT_EQ("350,10", ConvertHostPointToScreen(150, 210));
  72. UpdateDisplay("100+100-200x190,100+500-200x190");
  73. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  74. EXPECT_EQ(
  75. "100,100",
  76. root_windows[0]->GetHost()->GetBoundsInPixels().origin().ToString());
  77. EXPECT_EQ("200x190",
  78. root_windows[0]->GetHost()->GetBoundsInPixels().size().ToString());
  79. EXPECT_EQ(
  80. "100,500",
  81. root_windows[1]->GetHost()->GetBoundsInPixels().origin().ToString());
  82. EXPECT_EQ("200x190",
  83. root_windows[1]->GetHost()->GetBoundsInPixels().size().ToString());
  84. const gfx::Point window_pos(100, 100);
  85. window_->SetBoundsInScreen(
  86. gfx::Rect(window_pos, gfx::Size(100, 100)),
  87. display::Screen::GetScreen()->GetDisplayNearestPoint(window_pos));
  88. SetSecondaryDisplayLayout(display::DisplayPlacement::RIGHT);
  89. // The point is on the primary root window.
  90. EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
  91. // The point is out of the all root windows.
  92. EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
  93. // The point is on the secondary display.
  94. EXPECT_EQ("250,0", ConvertHostPointToScreen(50, 400));
  95. SetSecondaryDisplayLayout(display::DisplayPlacement::BOTTOM);
  96. // The point is on the primary root window.
  97. EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
  98. // The point is out of the all root windows.
  99. EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
  100. // The point is on the secondary display.
  101. EXPECT_EQ("50,190", ConvertHostPointToScreen(50, 400));
  102. SetSecondaryDisplayLayout(display::DisplayPlacement::LEFT);
  103. // The point is on the primary root window.
  104. EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
  105. // The point is out of the all root windows.
  106. EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
  107. // The point is on the secondary display.
  108. EXPECT_EQ("-150,0", ConvertHostPointToScreen(50, 400));
  109. SetSecondaryDisplayLayout(display::DisplayPlacement::TOP);
  110. // The point is on the primary root window.
  111. EXPECT_EQ("50,50", ConvertHostPointToScreen(50, 50));
  112. // The point is out of the all root windows.
  113. EXPECT_EQ("250,250", ConvertHostPointToScreen(250, 250));
  114. // The point is on the secondary display.
  115. EXPECT_EQ("50,-190", ConvertHostPointToScreen(50, 400));
  116. SetSecondaryDisplayLayout(display::DisplayPlacement::RIGHT);
  117. const gfx::Point window_pos2(300, 100);
  118. window_->SetBoundsInScreen(
  119. gfx::Rect(window_pos2, gfx::Size(100, 100)),
  120. display::Screen::GetScreen()->GetDisplayNearestPoint(window_pos2));
  121. // The point is on the secondary display.
  122. EXPECT_EQ("250,50", ConvertHostPointToScreen(50, 50));
  123. // The point is out of the all root windows.
  124. EXPECT_EQ("450,250", ConvertHostPointToScreen(250, 250));
  125. // The point is on the primary root window.
  126. EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
  127. SetSecondaryDisplayLayout(display::DisplayPlacement::BOTTOM);
  128. // The point is on the secondary display.
  129. EXPECT_EQ("50,240", ConvertHostPointToScreen(50, 50));
  130. // The point is out of the all root windows.
  131. EXPECT_EQ("250,440", ConvertHostPointToScreen(250, 250));
  132. // The point is on the primary root window.
  133. EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
  134. SetSecondaryDisplayLayout(display::DisplayPlacement::LEFT);
  135. // The point is on the secondary display.
  136. EXPECT_EQ("-150,50", ConvertHostPointToScreen(50, 50));
  137. // The point is out of the all root windows.
  138. EXPECT_EQ("50,250", ConvertHostPointToScreen(250, 250));
  139. // The point is on the primary root window.
  140. EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
  141. SetSecondaryDisplayLayout(display::DisplayPlacement::TOP);
  142. // The point is on the secondary display.
  143. EXPECT_EQ("50,-140", ConvertHostPointToScreen(50, 50));
  144. // The point is out of the all root windows.
  145. EXPECT_EQ("250,60", ConvertHostPointToScreen(250, 250));
  146. // The point is on the primary root window.
  147. EXPECT_EQ("50,0", ConvertHostPointToScreen(50, -400));
  148. }
  149. TEST_F(ScreenPositionControllerTest, ConvertHostPointToScreenHiDPI) {
  150. UpdateDisplay("50+50-200x190*2,50+300-300x290");
  151. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  152. EXPECT_EQ("50,50 200x190",
  153. root_windows[0]->GetHost()->GetBoundsInPixels().ToString());
  154. EXPECT_EQ("50,300 300x290",
  155. root_windows[1]->GetHost()->GetBoundsInPixels().ToString());
  156. // Put |window_| to the primary 2x display.
  157. window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
  158. display::Screen::GetScreen()->GetPrimaryDisplay());
  159. // (30, 30) means the host coordinate, so the point is still on the primary
  160. // root window. Since it's 2x, the specified native point was halved.
  161. EXPECT_EQ("15,15", ConvertHostPointToScreen(30, 30));
  162. // Similar to above but the point is out of the all root windows.
  163. EXPECT_EQ("200,200", ConvertHostPointToScreen(400, 400));
  164. // Similar to above but the point is on the first display.
  165. EXPECT_EQ("100,15", ConvertHostPointToScreen(200, 30));
  166. // On secondary display. The position on the 2nd host window is (100,95)
  167. // and the relative position is (150, 100).
  168. EXPECT_EQ("250,195", ConvertHostPointToScreen(150, 450));
  169. // At the edge but still in the primary display. Remaining of the primary
  170. // display is (50, 50) but adding ~100 since it's 2x-display.
  171. EXPECT_EQ("79,79", ConvertHostPointToScreen(158, 158));
  172. // At the edge of the secondary display.
  173. EXPECT_EQ("80,80", ConvertHostPointToScreen(160, 160));
  174. }
  175. TEST_F(ScreenPositionControllerTest, ConvertHostPointToScreenRotate) {
  176. // 1st display is rotated 90 clockise, and 2nd display is rotated
  177. // 270 clockwise.
  178. UpdateDisplay("100+100-200x190/r,100+500-200x190/l");
  179. // Put |window_| to the 1st.
  180. window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
  181. display::Screen::GetScreen()->GetPrimaryDisplay());
  182. // The point is on the 1st host.
  183. EXPECT_EQ("70,150", ConvertHostPointToScreen(50, 70));
  184. // The point is out of the host windows.
  185. EXPECT_EQ("250,-50", ConvertHostPointToScreen(250, 250));
  186. // The point is on the 2nd host. Point on 2nd host (30,150) -
  187. // rotate 270 clockwise -> (149, 30) - layout [+(200,0)] -> (349,30).
  188. EXPECT_EQ("330,30", ConvertHostPointToScreen(30, 450));
  189. // Move |window_| to the 2nd.
  190. window_->SetBoundsInScreen(
  191. gfx::Rect(300, 20, 50, 50),
  192. display::test::DisplayManagerTestApi(display_manager())
  193. .GetSecondaryDisplay());
  194. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  195. EXPECT_EQ(root_windows[1], window_->GetRootWindow());
  196. // The point is on the 2nd host. (50,70) on 2n host -
  197. // roatate 270 clockwise -> (129,50) -layout [+(200,0)] -> (329,50)
  198. EXPECT_EQ("310,50", ConvertHostPointToScreen(50, 70));
  199. // The point is out of the host windows.
  200. EXPECT_EQ("430,50", ConvertHostPointToScreen(50, -50));
  201. // The point is on the 2nd host. Point on 2nd host (50,50) -
  202. // rotate 90 clockwise -> (50, 149)
  203. EXPECT_EQ("50,150", ConvertHostPointToScreen(50, -350));
  204. }
  205. TEST_F(ScreenPositionControllerTest, ConvertHostPointToScreenZoomScale) {
  206. // 1st display is 2x density with 1.5 UI scale.
  207. UpdateDisplay("100+100-200x190*2@0.8,100+500-200x190");
  208. // Put |window_| to the 1st.
  209. window_->SetBoundsInScreen(gfx::Rect(20, 20, 50, 50),
  210. display::Screen::GetScreen()->GetPrimaryDisplay());
  211. // The point is on the 1st host.
  212. EXPECT_EQ("37,37", ConvertHostPointToScreen(60, 60));
  213. // The point is out of the host windows.
  214. EXPECT_EQ("37,187", ConvertHostPointToScreen(60, 300));
  215. // The point is on the 2nd host. Point on 2nd host (60,150) -
  216. // - screen [+(150,0)]
  217. EXPECT_EQ("185,50", ConvertHostPointToScreen(60, 450));
  218. // Move |window_| to the 2nd.
  219. window_->SetBoundsInScreen(
  220. gfx::Rect(300, 20, 50, 50),
  221. display::test::DisplayManagerTestApi(display_manager())
  222. .GetSecondaryDisplay());
  223. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  224. EXPECT_EQ(root_windows[1], window_->GetRootWindow());
  225. // The point is on the 2nd host. (50,70) - ro
  226. EXPECT_EQ("185,70", ConvertHostPointToScreen(60, 70));
  227. // The point is out of the host windows.
  228. EXPECT_EQ("185,-50", ConvertHostPointToScreen(60, -50));
  229. // The point is on the 2nd host. Point on 1nd host (60, 60)
  230. // 1/2 * 1 / 0.8 = (45,45)
  231. EXPECT_EQ("37,37", ConvertHostPointToScreen(60, -340));
  232. }
  233. namespace {
  234. // EventHandler which tracks whether it got any MouseEvents whose location could
  235. // not be converted to screen coordinates.
  236. class ConvertToScreenEventHandler : public ui::EventHandler {
  237. public:
  238. ConvertToScreenEventHandler() : could_convert_to_screen_(true) {
  239. aura::Env::GetInstance()->AddPreTargetHandler(this);
  240. }
  241. ConvertToScreenEventHandler(const ConvertToScreenEventHandler&) = delete;
  242. ConvertToScreenEventHandler& operator=(const ConvertToScreenEventHandler&) =
  243. delete;
  244. ~ConvertToScreenEventHandler() override {
  245. aura::Env::GetInstance()->RemovePreTargetHandler(this);
  246. }
  247. bool could_convert_to_screen() const { return could_convert_to_screen_; }
  248. private:
  249. void OnMouseEvent(ui::MouseEvent* event) override {
  250. if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED)
  251. return;
  252. aura::Window* root =
  253. static_cast<aura::Window*>(event->target())->GetRootWindow();
  254. if (!aura::client::GetScreenPositionClient(root))
  255. could_convert_to_screen_ = false;
  256. }
  257. bool could_convert_to_screen_;
  258. };
  259. } // namespace
  260. // Test that events are only dispatched when a ScreenPositionClient is available
  261. // to convert the event to screen coordinates. The ScreenPositionClient is
  262. // detached from the root window prior to the root window being destroyed. Test
  263. // that no events are dispatched at this time.
  264. TEST_F(ScreenPositionControllerTest,
  265. ConvertToScreenWhileRemovingSecondaryDisplay) {
  266. UpdateDisplay("600x500,600x500");
  267. base::RunLoop().RunUntilIdle();
  268. // Create a window on the secondary display.
  269. window_->SetBoundsInScreen(
  270. gfx::Rect(600, 0, 400, 400),
  271. display::test::DisplayManagerTestApi(display_manager())
  272. .GetSecondaryDisplay());
  273. // Move the mouse cursor over |window_|. Synthetic mouse moves are dispatched
  274. // asynchronously when a window which contains the mouse cursor is destroyed.
  275. // We want to check that none of these synthetic events are dispatched after
  276. // ScreenPositionClient has been detached from the root window.
  277. GetEventGenerator()->MoveMouseTo(800, 200);
  278. EXPECT_TRUE(window_->GetBoundsInScreen().Contains(
  279. aura::Env::GetInstance()->last_mouse_location()));
  280. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  281. aura::WindowTracker tracker;
  282. tracker.Add(root_windows[1]);
  283. std::unique_ptr<ConvertToScreenEventHandler> event_handler(
  284. new ConvertToScreenEventHandler);
  285. // Remove the secondary monitor.
  286. UpdateDisplay("600x500");
  287. // The secondary root window is not immediately destroyed.
  288. EXPECT_TRUE(tracker.Contains(root_windows[1]));
  289. base::RunLoop().RunUntilIdle();
  290. // Check that we waited long enough and that the secondary root window was
  291. // destroyed.
  292. EXPECT_FALSE(tracker.Contains(root_windows[1]));
  293. // Check that we could convert all of the mouse events we got to screen
  294. // coordinates.
  295. EXPECT_TRUE(event_handler->could_convert_to_screen());
  296. }
  297. } // namespace ash