system_gesture_event_filter_unittest.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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/wm/system_gesture_event_filter.h"
  5. #include <vector>
  6. #include "ash/accelerators/accelerator_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "ash/test/ash_test_base.h"
  9. #include "ash/wm/window_positioning_utils.h"
  10. #include "ash/wm/window_state.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. #include "ui/aura/env.h"
  14. #include "ui/aura/test/test_window_delegate.h"
  15. #include "ui/aura/test/test_windows.h"
  16. #include "ui/aura/window.h"
  17. #include "ui/aura/window_event_dispatcher.h"
  18. #include "ui/base/hit_test.h"
  19. #include "ui/display/manager/display_manager.h"
  20. #include "ui/display/screen.h"
  21. #include "ui/display/test/display_manager_test_api.h"
  22. #include "ui/events/event.h"
  23. #include "ui/events/event_handler.h"
  24. #include "ui/events/event_utils.h"
  25. #include "ui/events/gesture_detection/gesture_configuration.h"
  26. #include "ui/events/test/event_generator.h"
  27. #include "ui/events/test/test_event_handler.h"
  28. #include "ui/gfx/geometry/size.h"
  29. #include "ui/views/widget/widget.h"
  30. #include "ui/views/widget/widget_delegate.h"
  31. #include "ui/views/window/non_client_view.h"
  32. #include "ui/views/window/window_button_order_provider.h"
  33. namespace ash {
  34. namespace {
  35. class ResizableWidgetDelegate : public views::WidgetDelegateView {
  36. public:
  37. ResizableWidgetDelegate() {
  38. SetCanMaximize(true);
  39. SetCanMinimize(true);
  40. SetCanResize(true);
  41. }
  42. ResizableWidgetDelegate(const ResizableWidgetDelegate&) = delete;
  43. ResizableWidgetDelegate& operator=(const ResizableWidgetDelegate&) = delete;
  44. ~ResizableWidgetDelegate() override = default;
  45. };
  46. // Support class for testing windows with a maximum size.
  47. class MaxSizeNCFV : public views::NonClientFrameView {
  48. public:
  49. MaxSizeNCFV() = default;
  50. MaxSizeNCFV(const MaxSizeNCFV&) = delete;
  51. MaxSizeNCFV& operator=(const MaxSizeNCFV&) = delete;
  52. private:
  53. gfx::Size GetMaximumSize() const override { return gfx::Size(200, 200); }
  54. gfx::Rect GetBoundsForClientView() const override { return gfx::Rect(); }
  55. gfx::Rect GetWindowBoundsForClientBounds(
  56. const gfx::Rect& client_bounds) const override {
  57. return gfx::Rect();
  58. }
  59. // This function must ask the ClientView to do a hittest. We don't do this in
  60. // the parent NonClientView because that makes it more difficult to calculate
  61. // hittests for regions that are partially obscured by the ClientView, e.g.
  62. // HTSYSMENU.
  63. int NonClientHitTest(const gfx::Point& point) override { return HTNOWHERE; }
  64. void GetWindowMask(const gfx::Size& size, SkPath* window_mask) override {}
  65. void ResetWindowControls() override {}
  66. void UpdateWindowIcon() override {}
  67. void UpdateWindowTitle() override {}
  68. void SizeConstraintsChanged() override {}
  69. };
  70. class MaxSizeWidgetDelegate : public views::WidgetDelegateView {
  71. public:
  72. MaxSizeWidgetDelegate() {
  73. SetCanMinimize(true);
  74. SetCanResize(true);
  75. }
  76. MaxSizeWidgetDelegate(const MaxSizeWidgetDelegate&) = delete;
  77. MaxSizeWidgetDelegate& operator=(const MaxSizeWidgetDelegate&) = delete;
  78. ~MaxSizeWidgetDelegate() override = default;
  79. private:
  80. std::unique_ptr<views::NonClientFrameView> CreateNonClientFrameView(
  81. views::Widget* widget) override {
  82. return std::make_unique<MaxSizeNCFV>();
  83. }
  84. };
  85. } // namespace
  86. class SystemGestureEventFilterTest : public AshTestBase {
  87. public:
  88. SystemGestureEventFilterTest() : AshTestBase() {}
  89. SystemGestureEventFilterTest(const SystemGestureEventFilterTest&) = delete;
  90. SystemGestureEventFilterTest& operator=(const SystemGestureEventFilterTest&) =
  91. delete;
  92. ~SystemGestureEventFilterTest() override = default;
  93. // Overridden from AshTestBase:
  94. void SetUp() override {
  95. // TODO(jonross): TwoFingerDragDelayed() and ThreeFingerGestureStopsDrag()
  96. // both use hardcoded touch points, assuming that they target empty header
  97. // space. Window control order now reflects configuration files and can
  98. // change. The tests should be improved to dynamically decide touch points.
  99. // To address this we specify the originally expected window control
  100. // positions to be consistent across tests.
  101. std::vector<views::FrameButton> leading;
  102. std::vector<views::FrameButton> trailing;
  103. trailing.push_back(views::FrameButton::kMinimize);
  104. trailing.push_back(views::FrameButton::kMaximize);
  105. trailing.push_back(views::FrameButton::kClose);
  106. views::WindowButtonOrderProvider::GetInstance()->SetWindowButtonOrder(
  107. leading, trailing);
  108. AshTestBase::SetUp();
  109. // Enable brightness key.
  110. display::test::DisplayManagerTestApi(Shell::Get()->display_manager())
  111. .SetFirstDisplayAsInternalDisplay();
  112. }
  113. };
  114. ui::GestureEvent* CreateGesture(ui::EventType type,
  115. int x,
  116. int y,
  117. float delta_x,
  118. float delta_y,
  119. int touch_id) {
  120. return new ui::GestureEvent(x, y, 0, base::TimeTicks::Now(),
  121. ui::GestureEventDetails(type, delta_x, delta_y));
  122. }
  123. TEST_F(SystemGestureEventFilterTest, TwoFingerDrag) {
  124. gfx::Rect bounds(0, 0, 600, 600);
  125. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  126. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  127. new ResizableWidgetDelegate, root_window, bounds);
  128. toplevel->Show();
  129. const int kSteps = 15;
  130. const int kTouchPoints = 2;
  131. gfx::Point points[kTouchPoints] = {
  132. gfx::Point(250, 250), gfx::Point(350, 350),
  133. };
  134. ui::test::EventGenerator generator(root_window, toplevel->GetNativeWindow());
  135. WindowState* toplevel_state = WindowState::Get(toplevel->GetNativeWindow());
  136. // Swipe down to minimize.
  137. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 0, 150);
  138. EXPECT_TRUE(toplevel_state->IsMinimized());
  139. toplevel->Restore();
  140. toplevel->GetNativeWindow()->SetBounds(bounds);
  141. // Swipe up to maximize.
  142. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 0, -150);
  143. EXPECT_TRUE(toplevel_state->IsMaximized());
  144. toplevel->Restore();
  145. toplevel->GetNativeWindow()->SetBounds(bounds);
  146. // Swipe right to snap.
  147. gfx::Rect normal_bounds = toplevel->GetWindowBoundsInScreen();
  148. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 150, 0);
  149. gfx::Rect right_tile_bounds = toplevel->GetWindowBoundsInScreen();
  150. EXPECT_NE(normal_bounds.ToString(), right_tile_bounds.ToString());
  151. // Swipe left to snap.
  152. gfx::Point left_points[kTouchPoints];
  153. for (int i = 0; i < kTouchPoints; ++i) {
  154. left_points[i] = points[i];
  155. left_points[i].Offset(right_tile_bounds.x(), right_tile_bounds.y());
  156. }
  157. generator.GestureMultiFingerScroll(kTouchPoints, left_points, 15, kSteps,
  158. -150, 0);
  159. gfx::Rect left_tile_bounds = toplevel->GetWindowBoundsInScreen();
  160. EXPECT_NE(normal_bounds.ToString(), left_tile_bounds.ToString());
  161. EXPECT_NE(right_tile_bounds.ToString(), left_tile_bounds.ToString());
  162. // Swipe right again.
  163. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 150, 0);
  164. gfx::Rect current_bounds = toplevel->GetWindowBoundsInScreen();
  165. EXPECT_NE(current_bounds.ToString(), left_tile_bounds.ToString());
  166. EXPECT_EQ(current_bounds.ToString(), right_tile_bounds.ToString());
  167. }
  168. TEST_F(SystemGestureEventFilterTest, WindowsWithMaxSizeDontSnap) {
  169. gfx::Rect bounds(250, 150, 100, 100);
  170. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  171. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  172. new MaxSizeWidgetDelegate, root_window, bounds);
  173. toplevel->Show();
  174. const int kSteps = 15;
  175. const int kTouchPoints = 2;
  176. gfx::Point points[kTouchPoints] = {
  177. gfx::Point(bounds.x() + 10, bounds.y() + 30),
  178. gfx::Point(bounds.x() + 30, bounds.y() + 20),
  179. };
  180. ui::test::EventGenerator generator(root_window, toplevel->GetNativeWindow());
  181. // Swipe down to minimize.
  182. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 0, 150);
  183. WindowState* toplevel_state = WindowState::Get(toplevel->GetNativeWindow());
  184. EXPECT_TRUE(toplevel_state->IsMinimized());
  185. toplevel->Restore();
  186. toplevel->GetNativeWindow()->SetBounds(bounds);
  187. // Check that swiping up doesn't maximize.
  188. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 0, -150);
  189. EXPECT_FALSE(toplevel_state->IsMaximized());
  190. toplevel->Restore();
  191. toplevel->GetNativeWindow()->SetBounds(bounds);
  192. // Check that swiping right doesn't snap.
  193. gfx::Rect normal_bounds = toplevel->GetWindowBoundsInScreen();
  194. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 150, 0);
  195. normal_bounds.set_x(normal_bounds.x() + 150);
  196. EXPECT_EQ(normal_bounds.ToString(),
  197. toplevel->GetWindowBoundsInScreen().ToString());
  198. toplevel->GetNativeWindow()->SetBounds(bounds);
  199. // Check that swiping left doesn't snap.
  200. normal_bounds = toplevel->GetWindowBoundsInScreen();
  201. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, -150, 0);
  202. normal_bounds.set_x(normal_bounds.x() - 150);
  203. EXPECT_EQ(normal_bounds.ToString(),
  204. toplevel->GetWindowBoundsInScreen().ToString());
  205. toplevel->GetNativeWindow()->SetBounds(bounds);
  206. // Swipe right again, make sure the window still doesn't snap.
  207. normal_bounds = toplevel->GetWindowBoundsInScreen();
  208. normal_bounds.set_x(normal_bounds.x() + 150);
  209. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 150, 0);
  210. EXPECT_EQ(normal_bounds.ToString(),
  211. toplevel->GetWindowBoundsInScreen().ToString());
  212. }
  213. TEST_F(SystemGestureEventFilterTest, DISABLED_TwoFingerDragEdge) {
  214. gfx::Rect bounds(0, 0, 200, 100);
  215. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  216. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  217. new ResizableWidgetDelegate, root_window, bounds);
  218. toplevel->Show();
  219. const int kSteps = 15;
  220. const int kTouchPoints = 2;
  221. gfx::Point points[kTouchPoints] = {
  222. gfx::Point(30, 20), // Caption
  223. gfx::Point(0, 40), // Left edge
  224. };
  225. EXPECT_EQ(HTCAPTION,
  226. toplevel->GetNativeWindow()->delegate()->GetNonClientComponent(
  227. points[0]));
  228. EXPECT_EQ(HTLEFT,
  229. toplevel->GetNativeWindow()->delegate()->GetNonClientComponent(
  230. points[1]));
  231. ui::test::EventGenerator generator(root_window, toplevel->GetNativeWindow());
  232. bounds = toplevel->GetNativeWindow()->bounds();
  233. // Swipe down. Nothing should happen.
  234. generator.GestureMultiFingerScroll(kTouchPoints, points, 15, kSteps, 0, 150);
  235. EXPECT_EQ(bounds.ToString(),
  236. toplevel->GetNativeWindow()->bounds().ToString());
  237. }
  238. // We do not allow resizing a window via multiple edges simultaneously. Test
  239. // that the behavior is reasonable if a user attempts to resize a window via
  240. // several edges.
  241. TEST_F(SystemGestureEventFilterTest,
  242. TwoFingerAttemptResizeLeftAndRightEdgesSimultaneously) {
  243. gfx::Rect initial_bounds(0, 0, 400, 400);
  244. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  245. new ResizableWidgetDelegate, GetContext(), initial_bounds);
  246. toplevel->Show();
  247. const int kSteps = 15;
  248. const int kTouchPoints = 2;
  249. gfx::Point points[kTouchPoints] = {
  250. gfx::Point(0, 40), // Left edge
  251. gfx::Point(399, 40), // Right edge
  252. };
  253. int delays[kTouchPoints] = {0, 120};
  254. EXPECT_EQ(HTLEFT, toplevel->GetNonClientComponent(points[0]));
  255. EXPECT_EQ(HTRIGHT, toplevel->GetNonClientComponent(points[1]));
  256. GetEventGenerator()->GestureMultiFingerScrollWithDelays(
  257. kTouchPoints, points, delays, 15, kSteps, 0, 40);
  258. // The window bounds should not have changed because neither of the fingers
  259. // moved horizontally.
  260. EXPECT_EQ(initial_bounds.ToString(),
  261. toplevel->GetNativeWindow()->bounds().ToString());
  262. }
  263. TEST_F(SystemGestureEventFilterTest, TwoFingerDragDelayed) {
  264. gfx::Rect bounds(0, 0, 200, 100);
  265. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  266. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  267. new ResizableWidgetDelegate, root_window, bounds);
  268. toplevel->Show();
  269. const int kSteps = 15;
  270. const int kTouchPoints = 2;
  271. gfx::Point points[kTouchPoints] = {
  272. gfx::Point(30, 20), // Caption
  273. gfx::Point(34, 20), // Caption
  274. };
  275. int delays[kTouchPoints] = {0, 120};
  276. EXPECT_EQ(HTCAPTION,
  277. toplevel->GetNativeWindow()->delegate()->GetNonClientComponent(
  278. points[0]));
  279. EXPECT_EQ(HTCAPTION,
  280. toplevel->GetNativeWindow()->delegate()->GetNonClientComponent(
  281. points[1]));
  282. ui::test::EventGenerator generator(root_window, toplevel->GetNativeWindow());
  283. bounds = toplevel->GetNativeWindow()->bounds();
  284. // Swipe right and down starting with one finger.
  285. // Add another finger after 120ms and continue dragging.
  286. // The window should not move (see crbug.com/363625) and drag should be
  287. // determined by the delta of center point between the fingers.
  288. generator.GestureMultiFingerScrollWithDelays(kTouchPoints, points, delays, 15,
  289. kSteps, 150, 150);
  290. bounds += gfx::Vector2d(150, 150);
  291. EXPECT_EQ(bounds.ToString(),
  292. toplevel->GetNativeWindow()->bounds().ToString());
  293. }
  294. TEST_F(SystemGestureEventFilterTest, ThreeFingerGestureStopsDrag) {
  295. gfx::Rect bounds(0, 0, 200, 100);
  296. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  297. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  298. new ResizableWidgetDelegate, root_window, bounds);
  299. toplevel->Show();
  300. const int kSteps = 10;
  301. const int kTouchPoints = 3;
  302. gfx::Point points[kTouchPoints] = {
  303. gfx::Point(30, 20), // Caption
  304. gfx::Point(34, 20), // Caption
  305. gfx::Point(38, 20), // Caption
  306. };
  307. int delays[kTouchPoints] = {0, 0, 120};
  308. EXPECT_EQ(HTCAPTION,
  309. toplevel->GetNativeWindow()->delegate()->GetNonClientComponent(
  310. points[0]));
  311. EXPECT_EQ(HTCAPTION,
  312. toplevel->GetNativeWindow()->delegate()->GetNonClientComponent(
  313. points[1]));
  314. ui::test::EventGenerator generator(root_window, toplevel->GetNativeWindow());
  315. bounds = toplevel->GetNativeWindow()->bounds();
  316. // Swipe right and down starting with two fingers.
  317. // Add third finger after 120ms and continue dragging.
  318. // The window should start moving but stop when the 3rd finger touches down.
  319. const int kEventSeparation = 15;
  320. generator.GestureMultiFingerScrollWithDelays(
  321. kTouchPoints, points, delays, kEventSeparation, kSteps, 150, 150);
  322. int expected_drag = 150 / kSteps * 120 / kEventSeparation;
  323. bounds += gfx::Vector2d(expected_drag, expected_drag);
  324. EXPECT_EQ(bounds.ToString(),
  325. toplevel->GetNativeWindow()->bounds().ToString());
  326. }
  327. TEST_F(SystemGestureEventFilterTest, DragLeftNearEdgeSnaps) {
  328. gfx::Rect bounds(200, 150, 400, 100);
  329. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  330. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  331. new ResizableWidgetDelegate, root_window, bounds);
  332. toplevel->Show();
  333. const int kSteps = 15;
  334. const int kTouchPoints = 2;
  335. gfx::Point points[kTouchPoints] = {
  336. gfx::Point(bounds.x() + bounds.width() / 2, bounds.y() + 5),
  337. gfx::Point(bounds.x() + bounds.width() / 2, bounds.y() + 5),
  338. };
  339. aura::Window* toplevel_window = toplevel->GetNativeWindow();
  340. ui::test::EventGenerator generator(root_window, toplevel_window);
  341. // Check that dragging left snaps before reaching the screen edge.
  342. gfx::Rect work_area = display::Screen::GetScreen()
  343. ->GetDisplayNearestWindow(root_window)
  344. .work_area();
  345. int drag_x = work_area.x() + 20 - points[0].x();
  346. generator.GestureMultiFingerScroll(kTouchPoints, points, 120, kSteps, drag_x,
  347. 0);
  348. EXPECT_EQ(GetDefaultSnappedWindowBoundsInParent(toplevel_window,
  349. SnapViewType::kPrimary),
  350. toplevel_window->bounds());
  351. }
  352. TEST_F(SystemGestureEventFilterTest, DragRightNearEdgeSnaps) {
  353. gfx::Rect bounds(200, 150, 400, 100);
  354. aura::Window* root_window = Shell::GetPrimaryRootWindow();
  355. views::Widget* toplevel = views::Widget::CreateWindowWithContext(
  356. new ResizableWidgetDelegate, root_window, bounds);
  357. toplevel->Show();
  358. const int kSteps = 15;
  359. const int kTouchPoints = 2;
  360. gfx::Point points[kTouchPoints] = {
  361. gfx::Point(bounds.x() + bounds.width() / 2, bounds.y() + 5),
  362. gfx::Point(bounds.x() + bounds.width() / 2, bounds.y() + 5),
  363. };
  364. aura::Window* toplevel_window = toplevel->GetNativeWindow();
  365. ui::test::EventGenerator generator(root_window, toplevel_window);
  366. // Check that dragging right snaps before reaching the screen edge.
  367. gfx::Rect work_area = display::Screen::GetScreen()
  368. ->GetDisplayNearestWindow(root_window)
  369. .work_area();
  370. int drag_x = work_area.right() - 20 - points[0].x();
  371. generator.GestureMultiFingerScroll(kTouchPoints, points, 120, kSteps, drag_x,
  372. 0);
  373. EXPECT_EQ(GetDefaultSnappedWindowBoundsInParent(toplevel_window,
  374. SnapViewType::kSecondary),
  375. toplevel_window->bounds());
  376. }
  377. // Tests that the window manager does not consume gesture events targeted to
  378. // windows of type WINDOW_TYPE_CONTROL. This is important because the web
  379. // contents are often (but not always) of type WINDOW_TYPE_CONTROL.
  380. TEST_F(SystemGestureEventFilterTest,
  381. ControlWindowGetsMultiFingerGestureEvents) {
  382. std::unique_ptr<aura::Window> parent(
  383. CreateTestWindowInShellWithBounds(gfx::Rect(100, 100)));
  384. aura::test::EventCountDelegate delegate;
  385. delegate.set_window_component(HTCLIENT);
  386. std::unique_ptr<aura::Window> child = std::make_unique<aura::Window>(
  387. &delegate, aura::client::WINDOW_TYPE_CONTROL);
  388. child->Init(ui::LAYER_TEXTURED);
  389. parent->AddChild(child.get());
  390. child->SetBounds(gfx::Rect(100, 100));
  391. child->Show();
  392. ui::test::TestEventHandler event_handler;
  393. aura::Env::GetInstance()->AddPreTargetHandler(
  394. &event_handler, ui::EventTarget::Priority::kSystem);
  395. GetEventGenerator()->MoveMouseTo(0, 0);
  396. for (int i = 1; i <= 3; ++i)
  397. GetEventGenerator()->PressTouchId(i);
  398. for (int i = 1; i <= 3; ++i)
  399. GetEventGenerator()->ReleaseTouchId(i);
  400. EXPECT_EQ(event_handler.num_gesture_events(),
  401. delegate.GetGestureCountAndReset());
  402. aura::Env::GetInstance()->RemovePreTargetHandler(&event_handler);
  403. }
  404. } // namespace ash