Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: ash/aura/pointer_watcher_adapter_unittest.cc

Issue 2271393002: Wires up drags to pointer watcher adapter. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Fixed patch set 7 errors. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/aura/pointer_watcher_adapter.cc ('k') | ash/aura/wm_shell_aura.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 enum TestPointerCaptureEvents {
18 NONE = 0x01,
19 CAPTURE = 0x02,
20 WHEEL = 0x04,
21 PRESS_OR_RELEASE = 0x08,
22 MOVE = 0x10,
23 DRAG = 0x20
24 };
25
17 // Records calls to OnPointerEventObserved() in |mouse_wheel_event_count| for a 26 // Records calls to OnPointerEventObserved() in |mouse_wheel_event_count| for a
18 // mouse wheel event, in |capture_changed_count_| for a mouse capture change 27 // mouse wheel event, in |capture_changed_count_| for a mouse capture change
19 // event and in |pointer_event_count_| for all other pointer events. 28 // event and in |pointer_event_count_| for all other pointer events.
20 class TestPointerWatcher : public views::PointerWatcher { 29 class TestPointerWatcher : public views::PointerWatcher {
21 public: 30 public:
22 explicit TestPointerWatcher(bool wants_moves) { 31 explicit TestPointerWatcher(views::PointerWatcherEventTypes events) {
23 WmShell::Get()->AddPointerWatcher(this, wants_moves); 32 WmShell::Get()->AddPointerWatcher(this, events);
24 } 33 }
25 ~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); } 34 ~TestPointerWatcher() override { WmShell::Get()->RemovePointerWatcher(this); }
26 35
27 void ClearCounts() { 36 void ClearCounts() {
28 pointer_event_count_ = capture_changed_count_ = mouse_wheel_event_count_ = 37 pointer_event_count_ = capture_changed_count_ = mouse_wheel_event_count_ =
29 0; 38 move_changed_count_ = drag_changed_count_ = 0;
30 } 39 }
31 40
32 int pointer_event_count() const { return pointer_event_count_; } 41 int pointer_event_count() const { return pointer_event_count_; }
33 int capture_changed_count() const { return capture_changed_count_; } 42 int capture_changed_count() const { return capture_changed_count_; }
34 int mouse_wheel_event_count() const { return mouse_wheel_event_count_; } 43 int mouse_wheel_event_count() const { return mouse_wheel_event_count_; }
44 int move_changed_count() const { return move_changed_count_; }
45 int drag_changed_count() const { return drag_changed_count_; }
35 46
36 // views::PointerWatcher: 47 // views::PointerWatcher:
37 void OnPointerEventObserved(const ui::PointerEvent& event, 48 void OnPointerEventObserved(const ui::PointerEvent& event,
38 const gfx::Point& location_in_screen, 49 const gfx::Point& location_in_screen,
39 views::Widget* target) override { 50 views::Widget* target) override {
40 if (event.type() == ui::ET_POINTER_WHEEL_CHANGED) 51 if (event.type() == ui::ET_POINTER_WHEEL_CHANGED) {
41 mouse_wheel_event_count_++; 52 mouse_wheel_event_count_++;
42 else if (event.type() == ui::ET_POINTER_CAPTURE_CHANGED) 53 } else if (event.type() == ui::ET_POINTER_CAPTURE_CHANGED) {
43 capture_changed_count_++; 54 capture_changed_count_++;
44 else 55 } else if (event.type() == ui::ET_POINTER_MOVED) {
56 // Pointer moved events are drags if they are a touch event.
57 if (event.IsTouchPointerEvent()) {
58 drag_changed_count_++;
59 } else if (event.IsMousePointerEvent()) {
60 // If it is a mouse event, convert to mouse event to differentiate
61 // between a drag and move event.
62 ui::MouseEvent mouse_event(event);
sky 2016/09/01 17:17:04 You shouldn't need to create a new event here. You
sammiequon 2016/09/01 17:44:12 Done.
63 if (mouse_event.type() == ui::ET_MOUSE_DRAGGED)
64 drag_changed_count_++;
65 else
66 move_changed_count_++;
67 } else {
68 move_changed_count_++;
69 }
70 } else {
45 pointer_event_count_++; 71 pointer_event_count_++;
72 }
46 } 73 }
47 74
48 private: 75 private:
49 int pointer_event_count_ = 0; 76 int pointer_event_count_ = 0;
50 int capture_changed_count_ = 0; 77 int capture_changed_count_ = 0;
51 int mouse_wheel_event_count_ = 0; 78 int mouse_wheel_event_count_ = 0;
79 int move_changed_count_ = 0;
80 int drag_changed_count_ = 0;
52 81
53 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher); 82 DISALLOW_COPY_AND_ASSIGN(TestPointerWatcher);
54 }; 83 };
55 84
56 // Creates two TestPointerWatchers, one that wants moves and one that doesn't. 85 // Creates three TestPointerWatchers, one that wants moves and one that wants
86 // drags, and one that does not want either.
57 class TestHelper { 87 class TestHelper {
58 public: 88 public:
59 TestHelper() : non_move_watcher_(false), move_watcher_(true) {} 89 TestHelper()
90 : basic_watcher_(views::PointerWatcherEventTypes::BASIC),
91 move_watcher_(views::PointerWatcherEventTypes::MOVES),
92 drag_watcher_(views::PointerWatcherEventTypes::DRAGS) {}
60 ~TestHelper() {} 93 ~TestHelper() {}
61 94
62 // Used to verify call counts. 95 // Used to verify call counts. One ExpectCallCount call should be made after
63 void ExpectCallCount(int non_move_pointer_event_count, 96 // each generated mouse events. |basic_events_bitmask| defines which events
64 int non_move_capture_changed_count, 97 // the test basic watcher should receive and |move_events_bitamsk| defines
65 int non_move_mouse_wheel_event_count, 98 // which events the test move watcher should receive and |drag_events_bitmask|
66 int move_pointer_event_count, 99 // defines which events the test drag watcher should receive.
67 int move_capture_changed_count, 100 void ExpectCallCount(int basic_events_bitmask,
68 int move_mouse_wheel_event_count) { 101 int move_events_bitmask,
69 EXPECT_EQ(non_move_pointer_event_count, 102 int drag_events_bitmask) {
70 non_move_watcher_.pointer_event_count()); 103 // Compare the expected events in the |basic_events_bitmask| with the actual
71 EXPECT_EQ(non_move_capture_changed_count, 104 // counts. Basic watcher should never have any move or drag counts.
72 non_move_watcher_.capture_changed_count()); 105 EXPECT_EQ(0, basic_watcher_.move_changed_count());
73 EXPECT_EQ(non_move_mouse_wheel_event_count, 106 EXPECT_EQ(0, basic_watcher_.drag_changed_count());
74 non_move_watcher_.mouse_wheel_event_count()); 107 EXPECT_EQ(basic_events_bitmask & PRESS_OR_RELEASE ? 1 : 0,
75 EXPECT_EQ(move_pointer_event_count, move_watcher_.pointer_event_count()); 108 basic_watcher_.pointer_event_count());
76 EXPECT_EQ(move_capture_changed_count, 109 EXPECT_EQ(basic_events_bitmask & CAPTURE ? 1 : 0,
110 basic_watcher_.capture_changed_count());
111 EXPECT_EQ(basic_events_bitmask & WHEEL ? 1 : 0,
112 basic_watcher_.mouse_wheel_event_count());
113 // Compare the expected events in the |move_events_bitmask| with the actual
114 // counts. Move watcher should never have any drag counts.
115 EXPECT_EQ(0, move_watcher_.drag_changed_count());
116 EXPECT_EQ(move_events_bitmask & MOVE ? 1 : 0,
117 move_watcher_.move_changed_count());
118 EXPECT_EQ(move_events_bitmask & PRESS_OR_RELEASE ? 1 : 0,
119 move_watcher_.pointer_event_count());
120 EXPECT_EQ(move_events_bitmask & CAPTURE ? 1 : 0,
77 move_watcher_.capture_changed_count()); 121 move_watcher_.capture_changed_count());
78 EXPECT_EQ(move_mouse_wheel_event_count, 122 EXPECT_EQ(move_events_bitmask & WHEEL ? 1 : 0,
79 move_watcher_.mouse_wheel_event_count()); 123 move_watcher_.mouse_wheel_event_count());
124 // Compare the expected events in the |drag_events_bitmask| with the actual
125 // counts.
126 EXPECT_EQ(drag_events_bitmask & MOVE ? 1 : 0,
127 drag_watcher_.move_changed_count());
128 EXPECT_EQ(drag_events_bitmask & DRAG ? 1 : 0,
129 drag_watcher_.drag_changed_count());
130 EXPECT_EQ(drag_events_bitmask & PRESS_OR_RELEASE ? 1 : 0,
131 drag_watcher_.pointer_event_count());
132 EXPECT_EQ(drag_events_bitmask & CAPTURE ? 1 : 0,
133 drag_watcher_.capture_changed_count());
134 EXPECT_EQ(drag_events_bitmask & WHEEL ? 1 : 0,
135 drag_watcher_.mouse_wheel_event_count());
80 136
81 non_move_watcher_.ClearCounts(); 137 basic_watcher_.ClearCounts();
82 move_watcher_.ClearCounts(); 138 move_watcher_.ClearCounts();
139 drag_watcher_.ClearCounts();
83 } 140 }
84 141
85 private: 142 private:
86 TestPointerWatcher non_move_watcher_; 143 TestPointerWatcher basic_watcher_;
87 TestPointerWatcher move_watcher_; 144 TestPointerWatcher move_watcher_;
145 TestPointerWatcher drag_watcher_;
88 146
89 DISALLOW_COPY_AND_ASSIGN(TestHelper); 147 DISALLOW_COPY_AND_ASSIGN(TestHelper);
90 }; 148 };
91 149
92 TEST_F(PointerWatcherAdapterTest, MouseEvents) { 150 TEST_F(PointerWatcherAdapterTest, MouseEvents) {
93 TestHelper helper; 151 TestHelper helper;
94 152
95 // Move: only the move PointerWatcher should get the event. 153 // Move: only the move and drag PointerWatcher should get the event.
96 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10)); 154 GetEventGenerator().MoveMouseTo(gfx::Point(10, 10));
97 helper.ExpectCallCount(0, 0, 0, 1, 0, 0); 155 helper.ExpectCallCount(NONE, MOVE, MOVE);
98 156
99 // Press: both. 157 // Press: all.
100 GetEventGenerator().PressLeftButton(); 158 GetEventGenerator().PressLeftButton();
101 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); 159 helper.ExpectCallCount(PRESS_OR_RELEASE, PRESS_OR_RELEASE, PRESS_OR_RELEASE);
102 160
103 // Drag: none. 161 // Drag: only drag PointerWatcher should get the event.
104 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30)); 162 GetEventGenerator().MoveMouseTo(gfx::Point(20, 30));
105 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); 163 helper.ExpectCallCount(NONE, NONE, DRAG);
106 164
107 // Release: both (aura generates a capture event here). 165 // Release: all (aura generates a capture event here).
108 GetEventGenerator().ReleaseLeftButton(); 166 GetEventGenerator().ReleaseLeftButton();
109 helper.ExpectCallCount(1, 1, 0, 1, 1, 0); 167 helper.ExpectCallCount(CAPTURE | PRESS_OR_RELEASE, CAPTURE | PRESS_OR_RELEASE,
168 CAPTURE | PRESS_OR_RELEASE);
110 169
111 // Exit: none. 170 // Exit: none.
112 GetEventGenerator().SendMouseExit(); 171 GetEventGenerator().SendMouseExit();
113 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); 172 helper.ExpectCallCount(NONE, NONE, NONE);
114 173
115 // Enter: none. 174 // Enter: none.
116 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(), 175 ui::MouseEvent enter_event(ui::ET_MOUSE_ENTERED, gfx::Point(), gfx::Point(),
117 ui::EventTimeForNow(), 0, 0); 176 ui::EventTimeForNow(), 0, 0);
118 GetEventGenerator().Dispatch(&enter_event); 177 GetEventGenerator().Dispatch(&enter_event);
119 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); 178 helper.ExpectCallCount(NONE, NONE, NONE);
120 179
121 // Wheel: both 180 // Wheel: all
122 GetEventGenerator().MoveMouseWheel(10, 11); 181 GetEventGenerator().MoveMouseWheel(10, 11);
123 helper.ExpectCallCount(0, 0, 1, 0, 0, 1); 182 helper.ExpectCallCount(WHEEL, WHEEL, WHEEL);
124 183
125 // Capture: both. 184 // Capture: all.
126 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), 185 ui::MouseEvent capture_event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
127 gfx::Point(), ui::EventTimeForNow(), 0, 0); 186 gfx::Point(), ui::EventTimeForNow(), 0, 0);
128 GetEventGenerator().Dispatch(&capture_event); 187 GetEventGenerator().Dispatch(&capture_event);
129 helper.ExpectCallCount(0, 1, 0, 0, 1, 0); 188 helper.ExpectCallCount(CAPTURE, CAPTURE, CAPTURE);
130 } 189 }
131 190
132 TEST_F(PointerWatcherAdapterTest, TouchEvents) { 191 TEST_F(PointerWatcherAdapterTest, TouchEvents) {
133 TestHelper helper; 192 TestHelper helper;
134 193
135 // Press: both. 194 // Press: all.
136 const int touch_id = 11; 195 const int touch_id = 11;
137 GetEventGenerator().PressTouchId(touch_id); 196 GetEventGenerator().PressTouchId(touch_id);
138 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); 197 helper.ExpectCallCount(PRESS_OR_RELEASE, PRESS_OR_RELEASE, PRESS_OR_RELEASE);
139 198
140 // Drag: none. 199 // Drag: only drag.
141 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id); 200 GetEventGenerator().MoveTouchId(gfx::Point(20, 30), touch_id);
142 helper.ExpectCallCount(0, 0, 0, 0, 0, 0); 201 helper.ExpectCallCount(NONE, NONE, DRAG);
143 202
144 // Release: both (contrary to mouse above, touch does not implicitly generate 203 // Release: both (contrary to mouse above, touch does not implicitly generate
145 // capture). 204 // capture).
146 GetEventGenerator().ReleaseTouchId(touch_id); 205 GetEventGenerator().ReleaseTouchId(touch_id);
147 helper.ExpectCallCount(1, 0, 0, 1, 0, 0); 206 helper.ExpectCallCount(PRESS_OR_RELEASE, PRESS_OR_RELEASE, PRESS_OR_RELEASE);
148 } 207 }
149 208
150 } // namespace ash 209 } // namespace ash
OLDNEW
« no previous file with comments | « ash/aura/pointer_watcher_adapter.cc ('k') | ash/aura/wm_shell_aura.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698