OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/common/wm_shell.h" | 5 #include "ash/common/wm_shell.h" |
6 #include "ash/test/ash_test_base.h" | 6 #include "ash/test/ash_test_base.h" |
7 #include "ui/events/base_event_utils.h" | 7 #include "ui/events/base_event_utils.h" |
8 #include "ui/events/event.h" | 8 #include "ui/events/event.h" |
9 #include "ui/events/test/event_generator.h" | 9 #include "ui/events/test/event_generator.h" |
10 #include "ui/views/pointer_watcher.h" | 10 #include "ui/views/pointer_watcher.h" |
11 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
12 | 12 |
13 namespace ash { | 13 namespace ash { |
14 | 14 |
15 using PointerWatcherAdapterTest = test::AshTestBase; | 15 using PointerWatcherAdapterTest = test::AshTestBase; |
16 | 16 |
17 // Records calls to OnPointerEventObserved() in |pointer_event_count_| and | 17 // Records calls to OnPointerEventObserved() in |pointer_event_count_| and |
18 // calls to OnMouseCaptureChanged() to |capture_changed_count_|. | 18 // calls to OnMouseCaptureChanged() to |capture_changed_count_|. |
19 class TestPointerWatcher : public views::PointerWatcher { | 19 class TestPointerWatcher : public views::PointerWatcher { |
20 public: | 20 public: |
21 explicit TestPointerWatcher(bool wants_moves) { | 21 explicit TestPointerWatcher(views::RequestedEvents events) { |
22 WmShell::Get()->AddPointerWatcher(this, wants_moves); | 22 WmShell::Get()->AddPointerWatcher(this, events); |
23 } | 23 } |
24 ~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); } | 24 ~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); } |
25 | 25 |
26 void ClearCounts() { pointer_event_count_ = capture_changed_count_ = 0; } | 26 void ClearCounts() { pointer_event_count_ = capture_changed_count_ = 0; } |
27 | 27 |
28 int pointer_event_count() const { return pointer_event_count_; } | 28 int pointer_event_count() const { return pointer_event_count_; } |
29 int capture_changed_count() const { return capture_changed_count_; } | 29 int capture_changed_count() const { return capture_changed_count_; } |
30 | 30 |
31 // views::PointerWatcher: | 31 // views::PointerWatcher: |
32 void OnPointerEventObserved(const ui::PointerEvent& event, | 32 void OnPointerEventObserved(const ui::PointerEvent& event, |
33 const gfx::Point& location_in_screen, | 33 const gfx::Point& location_in_screen, |
34 views::Widget* target) override { | 34 views::Widget* target) override { |
35 pointer_event_count_++; | 35 pointer_event_count_++; |
36 } | 36 } |
37 void OnMouseCaptureChanged() override { capture_changed_count_++; } | 37 void OnMouseCaptureChanged() override { capture_changed_count_++; } |
38 | 38 |
39 private: | 39 private: |
40 int pointer_event_count_ = 0; | 40 int pointer_event_count_ = 0; |
41 int capture_changed_count_ = 0; | 41 int capture_changed_count_ = 0; |
42 | 42 |
43 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); | 43 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); |
44 }; | 44 }; |
45 | 45 |
46 // Creates two TestPointerWatchers, one that wants moves and one that doesn't. | 46 // Creates three TestPointerWatchers, one that wants moves and one that wants |
47 // drags, and one that does not want either. | |
47 class TestHelper { | 48 class TestHelper { |
48 public: | 49 public: |
49 TestHelper() : non_move_watcher_(false), move_watcher_(true) {} | 50 TestHelper() |
51 : non_move_watcher_(views::RequestedEvents::BASIC), | |
52 move_watcher_(views::RequestedEvents::MOVES), | |
53 drag_watcher_(views::RequestedEvents::DRAGS) {} | |
50 ~TestHelper() {} | 54 ~TestHelper() {} |
51 | 55 |
52 // Used to verify call counts. | 56 // Used to verify call counts. |
53 void ExpectCallCount(int non_move_pointer_event_count, | 57 void ExpectCallCount(int non_move_pointer_event_count, |
54 int non_move_capture_changed_count, | 58 int non_move_capture_changed_count, |
55 int move_pointer_event_count, | 59 int move_pointer_event_count, |
56 int move_capture_changed_count) { | 60 int move_capture_changed_count, |
61 int drag_pointer_event_count, | |
62 int drag_capture_changed_count) { | |
57 EXPECT_EQ(non_move_pointer_event_count, | 63 EXPECT_EQ(non_move_pointer_event_count, |
58 non_move_watcher_.pointer_event_count()); | 64 non_move_watcher_.pointer_event_count()); |
59 EXPECT_EQ(non_move_capture_changed_count, | 65 EXPECT_EQ(non_move_capture_changed_count, |
60 non_move_watcher_.capture_changed_count()); | 66 non_move_watcher_.capture_changed_count()); |
61 EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count()); | 67 EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count()); |
62 EXPECT_EQ(move_capture_changed_count, | 68 EXPECT_EQ(move_capture_changed_count, |
63 move_watcher_.capture_changed_count()); | 69 move_watcher_.capture_changed_count()); |
70 EXPECT_EQ(drag_pointer_event_count, drag_watcher_.pointer_event_count()); | |
71 EXPECT_EQ(drag_capture_changed_count, | |
72 drag_watcher_.capture_changed_count()); | |
64 | 73 |
65 non_move_watcher_.ClearCounts(); | 74 non_move_watcher_.ClearCounts(); |
66 move_watcher_.ClearCounts(); | 75 move_watcher_.ClearCounts(); |
76 drag_watcher_.ClearCounts(); | |
67 } | 77 } |
68 | 78 |
69 private: | 79 private: |
70 TestPointerWatcher non_move_watcher_; | 80 TestPointerWatcher non_move_watcher_; |
71 TestPointerWatcher move_watcher_; | 81 TestPointerWatcher move_watcher_; |
82 TestPointerWatcher drag_watcher_; | |
72 | 83 |
73 DISALLOW_COPY_AND_ASSIGN(TestHelper); | 84 DISALLOW_COPY_AND_ASSIGN(TestHelper); |
74 }; | 85 }; |
75 | 86 |
76 TEST_F(PointerWatcherAdapterTest, MouseEvents) { | 87 TEST_F(PointerWatcherAdapterTest, MouseEvents) { |
77 TestHelper helper; | 88 TestHelper helper; |
78 | 89 |
79 // Move: only the move PointerWatcher should get the event. | 90 // Move: only the move and drag PointerWatcher should get the event. |
80 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); | 91 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); |
81 helper.ExpectCallCount(0, 0, 1, 0); | 92 helper.ExpectCallCount(0, 0, 1, 0, 1, 0); |
sky
2016/08/26 15:36:54
You can blame me for starting this, but this code
sammiequon
2016/08/26 18:34:20
Done.
| |
82 | 93 |
83 // Press: both. | 94 // Press: all. |
84 GetEventGenerator().PressLeftButton(); | 95 GetEventGenerator().PressLeftButton(); |
85 helper.ExpectCallCount(1, 0, 1, 0); | 96 helper.ExpectCallCount(1, 0, 1, 0, 1, 0); |
86 | 97 |
87 // Drag: none. | 98 // Drag: only drag PointerWatcher should get the event. |
88 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); | 99 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); |
89 helper.ExpectCallCount(0, 0, 0, 0); | 100 helper.ExpectCallCount(0, 0, 0, 0, 1, 0); |
90 | 101 |
91 // Release: both (aura generates a capture event here). | 102 // Release: all (aura generates a capture event here). |
92 GetEventGenerator().ReleaseLeftButton(); | 103 GetEventGenerator().ReleaseLeftButton(); |
93 helper.ExpectCallCount(1, 1, 1, 1); | 104 helper.ExpectCallCount(1, 1, 1, 1, 1, 1); |
94 | 105 |
95 // Exit: none. | 106 // Exit: none. |
96 GetEventGenerator().SendMouseExit(); | 107 GetEventGenerator().SendMouseExit(); |
97 helper.ExpectCallCount(0, 0, 0, 0); | 108 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
98 | 109 |
99 // Enter: none. | 110 // Enter: none. |
100 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), | 111 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), |
101 ui::EventTimeForNow(), 0, 0); | 112 ui::EventTimeForNow(), 0, 0); |
102 GetEventGenerator().Dispatch(&enter_event); | 113 GetEventGenerator().Dispatch(&enter_event); |
103 helper.ExpectCallCount(0, 0, 0, 0); | 114 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
104 | 115 |
105 // Wheel: none | 116 // Wheel: none |
106 GetEventGenerator().MoveMouseWheel(10, 11); | 117 GetEventGenerator().MoveMouseWheel(10, 11); |
107 helper.ExpectCallCount(0, 0, 0, 0); | 118 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); |
108 | 119 |
109 // Capture: both. | 120 // Capture: all. |
110 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), | 121 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), |
111 gfx::Point(), ui::EventTimeForNow(), 0, 0); | 122 gfx::Point(), ui::EventTimeForNow(), 0, 0); |
112 GetEventGenerator().Dispatch(&capture_event); | 123 GetEventGenerator().Dispatch(&capture_event); |
113 helper.ExpectCallCount(0, 1, 0, 1); | 124 helper.ExpectCallCount(0, 1, 0, 1, 0, 1); |
114 } | 125 } |
115 | 126 |
116 TEST_F(PointerWatcherAdapterTest, TouchEvents) { | 127 TEST_F(PointerWatcherAdapterTest, TouchEvents) { |
117 TestHelper helper; | 128 TestHelper helper; |
118 | 129 |
119 // Press: both. | 130 // Press: both. |
120 const int touch_id = 11; | 131 const int touch_id = 11; |
121 GetEventGenerator().PressTouchId(touch_id); | 132 GetEventGenerator().PressTouchId(touch_id); |
122 helper.ExpectCallCount(1, 0, 1, 0); | 133 helper.ExpectCallCount(1, 0, 1, 0, 1, 0); |
123 | 134 |
124 // Drag: none. | 135 // Drag: only drag. |
125 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); | 136 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); |
126 helper.ExpectCallCount(0, 0, 0, 0); | 137 helper.ExpectCallCount(0, 0, 0, 0, 1, 0); |
127 | 138 |
128 // Release: both (contrary to mouse above, touch does not implicitly generate | 139 // Release: both (contrary to mouse above, touch does not implicitly generate |
129 // capture). | 140 // capture). |
130 GetEventGenerator().ReleaseTouchId(touch_id); | 141 GetEventGenerator().ReleaseTouchId(touch_id); |
131 helper.ExpectCallCount(1, 0, 1, 0); | 142 helper.ExpectCallCount(1, 0, 1, 0, 1, 0); |
132 } | 143 } |
133 | 144 |
134 } // namespace ash | 145 } // namespace ash |
OLD | NEW |