OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/mac/scoped_nsobject.h" | |
tapted
2017/02/20 23:14:21
nit: import
themblsha
2017/02/21 13:00:51
Done.
| |
8 #include "base/optional.h" | |
9 #include "ui/events/gesture_event_details.h" | |
10 #include "ui/views/test/widget_test.h" | |
11 #include "ui/views/view.h" | |
tapted
2017/02/20 23:14:21
nit: move this one all the way to the top (above C
themblsha
2017/02/21 13:00:51
Done.
| |
12 | |
13 // We can't create NSEventTypeSwipe using normal means, and rely on duck typing | |
14 // instead. | |
15 @interface FakeSwipeEvent : NSEvent | |
16 @property CGFloat deltaX; | |
17 @property CGFloat deltaY; | |
18 @property(assign) NSWindow* window; | |
19 @property NSPoint locationInWindow; | |
20 @property NSEventModifierFlags modifierFlags; | |
21 @property NSTimeInterval timestamp; | |
22 @end | |
23 | |
24 @implementation FakeSwipeEvent | |
25 @synthesize deltaX; | |
26 @synthesize deltaY; | |
27 @synthesize window; | |
28 @synthesize locationInWindow; | |
29 @synthesize modifierFlags; | |
30 @synthesize timestamp; | |
31 | |
32 - (NSEventType)type { | |
33 return NSEventTypeSwipe; | |
34 } | |
35 | |
36 - (NSEventSubtype)subtype { | |
37 return static_cast<NSEventSubtype>(0); | |
tapted
2017/02/20 23:14:21
0 is `NSEventSubtypeWindowExposed` - maybe `return
themblsha
2017/02/21 13:00:51
In my testing the Swipe events all had 0 as their
tapted
2017/02/22 00:04:44
Acknowledged.
| |
38 } | |
39 @end | |
40 | |
41 namespace views { | |
42 | |
tapted
2017/02/20 23:14:21
add anonymous
namespace {
themblsha
2017/02/21 13:00:51
Done.
| |
43 // Stores last received swipe gesture direction vector in | |
44 // |last_swipe_gesture()|. | |
45 class ThreeFingerSwipeView : public View { | |
46 public: | |
47 ThreeFingerSwipeView() {} | |
48 | |
49 // views::View: | |
tapted
2017/02/20 23:14:21
nit: remove `views::`
themblsha
2017/02/21 13:00:51
Done.
| |
50 void OnGestureEvent(ui::GestureEvent* event) override { | |
51 DCHECK_EQ(event->details().type(), ui::ET_GESTURE_SWIPE); | |
tapted
2017/02/20 23:14:21
nit: EXPECT_EQ(ui::ET_GESTURE_SWIPE, event->detail
themblsha
2017/02/21 13:00:52
Thanks! I fixed the EXPECTs at the bottom of the f
| |
52 | |
53 int dx = 0, dy = 0; | |
54 if (event->details().swipe_left()) | |
55 dx = -1; | |
56 | |
57 if (event->details().swipe_right()) { | |
58 DCHECK_EQ(dx, 0); | |
tapted
2017/02/20 23:14:21
EXPECT_EQ(0, dx)
themblsha
2017/02/21 13:00:51
Done.
| |
59 dx = 1; | |
60 } | |
61 | |
62 if (event->details().swipe_down()) | |
63 dy = 1; | |
64 | |
65 if (event->details().swipe_up()) { | |
66 DCHECK_EQ(dy, 0); | |
tapted
2017/02/20 23:14:21
EXPECT_EQ(0, dy);
themblsha
2017/02/21 13:00:51
Done.
| |
67 dy = -1; | |
68 } | |
69 | |
70 last_swipe_gesture_ = gfx::Point(dx, dy); | |
71 } | |
72 | |
73 base::Optional<gfx::Point> last_swipe_gesture() const { | |
74 return last_swipe_gesture_; | |
75 } | |
76 | |
77 private: | |
78 base::Optional<gfx::Point> last_swipe_gesture_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(ThreeFingerSwipeView); | |
81 }; | |
82 | |
tapted
2017/02/20 23:14:21
conventionally, the
} // namespace
would go her
themblsha
2017/02/21 13:00:51
Also works when placed at the end of the ViewMacTe
| |
83 class ViewMacTest : public test::WidgetTest { | |
84 public: | |
85 base::Optional<gfx::Point> SwipeGestureVector(int dx, int dy) { | |
86 base::scoped_nsobject<FakeSwipeEvent> swipe_event( | |
87 [[FakeSwipeEvent alloc] init]); | |
88 swipe_event.get().deltaX = dx; | |
tapted
2017/02/20 23:14:21
[swipe_event setDeltaX:dx]
same for all the stuff
themblsha
2017/02/21 13:00:51
Done.
| |
89 swipe_event.get().deltaY = dy; | |
90 swipe_event.get().window = widget_->GetNativeWindow(); | |
91 swipe_event.get().locationInWindow = NSMakePoint(50, 50); | |
92 swipe_event.get().timestamp = [[NSProcessInfo processInfo] systemUptime]; | |
93 | |
94 // BridgedContentView should create an appropriate ui::GestureEvent and pass | |
95 // it to the views::Widget. | |
96 [[widget_->GetNativeWindow() contentView] swipeWithEvent:swipe_event]; | |
97 return view_->last_swipe_gesture(); | |
98 } | |
99 | |
100 // testing::Test: | |
101 void SetUp() override { | |
102 WidgetTest::SetUp(); | |
103 | |
104 widget_ = CreateTopLevelPlatformWidget(); | |
105 const gfx::Rect bounds(0, 0, 100, 100); | |
106 widget_->SetBounds(bounds); | |
107 widget_->Show(); | |
108 | |
109 view_ = new ThreeFingerSwipeView; | |
110 view_->SetBoundsRect(bounds); | |
tapted
2017/02/20 23:14:21
This will make the view bigger than the window's c
themblsha
2017/02/21 13:00:51
There seems to be a lot of tests which set up the
| |
111 widget_->GetContentsView()->AddChildView(view_); | |
112 } | |
113 | |
114 void TearDown() override { | |
115 widget_->CloseNow(); | |
116 WidgetTest::TearDown(); | |
117 } | |
118 | |
119 private: | |
120 Widget* widget_ = nullptr; | |
121 ThreeFingerSwipeView* view_ = nullptr; | |
122 }; | |
tapted
2017/02/20 23:14:21
nit: DISALLOW_COPY_AND_ASSIGN(..) (you'll need to
themblsha
2017/02/21 13:00:51
Done.
| |
123 | |
124 // Three-finger swipes send immediate events and they cannot be tracked. | |
125 TEST_F(ViewMacTest, HandlesThreeFingerSwipeGestures) { | |
126 EXPECT_EQ(gfx::Point(1, 0), *SwipeGestureVector(-1, 0)); | |
tapted
2017/02/20 23:14:21
nit: a comment like "Note that positive delta is l
themblsha
2017/02/21 13:00:51
Done.
| |
127 EXPECT_EQ(gfx::Point(-1, 0), *SwipeGestureVector(1, 0)); | |
128 EXPECT_EQ(gfx::Point(0, 1), *SwipeGestureVector(0, -1)); | |
129 EXPECT_EQ(gfx::Point(0, -1), *SwipeGestureVector(0, 1)); | |
130 } | |
131 | |
132 } // namespace views | |
OLD | NEW |