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

Side by Side Diff: ui/views/view_unittest_mac.mm

Issue 2690573002: MacViews: Handle three-finger swipe gestures for navigation. (Closed)
Patch Set: Fix compilation on Windows. Created 3 years, 10 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
OLDNEW
(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 #include "ui/views/view.h"
6
7 #import <Cocoa/Cocoa.h>
8
9 #import "base/mac/scoped_nsobject.h"
10 #include "base/optional.h"
11 #include "ui/events/gesture_event_details.h"
12 #include "ui/views/test/widget_test.h"
13
14 // We can't create NSEventTypeSwipe using normal means, and rely on duck typing
15 // instead.
16 @interface FakeSwipeEvent : NSEvent
17 @property CGFloat deltaX;
18 @property CGFloat deltaY;
19 @property(assign) NSWindow* window;
20 @property NSPoint locationInWindow;
21 @property NSEventModifierFlags modifierFlags;
22 @property NSTimeInterval timestamp;
23 @end
24
25 @implementation FakeSwipeEvent
26 @synthesize deltaX;
27 @synthesize deltaY;
28 @synthesize window;
29 @synthesize locationInWindow;
30 @synthesize modifierFlags;
31 @synthesize timestamp;
32
33 - (NSEventType)type {
34 return NSEventTypeSwipe;
35 }
36
37 - (NSEventSubtype)subtype {
38 // themblsha: In my testing, all native three-finger NSEventTypeSwipe events
39 // all had 0 as their subtype.
40 return static_cast<NSEventSubtype>(0);
41 }
42 @end
43
44 namespace views {
45
46 namespace {
47
48 // Stores last received swipe gesture direction vector in
49 // |last_swipe_gesture()|.
50 class ThreeFingerSwipeView : public View {
51 public:
52 ThreeFingerSwipeView() {}
53
54 // View:
55 void OnGestureEvent(ui::GestureEvent* event) override {
56 EXPECT_EQ(ui::ET_GESTURE_SWIPE, event->details().type());
57
58 int dx = 0, dy = 0;
59 if (event->details().swipe_left())
60 dx = -1;
61
62 if (event->details().swipe_right()) {
63 EXPECT_EQ(0, dx);
64 dx = 1;
65 }
66
67 if (event->details().swipe_down())
68 dy = 1;
69
70 if (event->details().swipe_up()) {
71 EXPECT_EQ(0, dy);
72 dy = -1;
73 }
74
75 last_swipe_gesture_ = gfx::Point(dx, dy);
76 }
77
78 base::Optional<gfx::Point> last_swipe_gesture() const {
79 return last_swipe_gesture_;
80 }
81
82 private:
83 base::Optional<gfx::Point> last_swipe_gesture_;
84
85 DISALLOW_COPY_AND_ASSIGN(ThreeFingerSwipeView);
86 };
87
88 } // namespace
89
90 class ViewMacTest : public test::WidgetTest {
91 public:
92 ViewMacTest() {}
93
94 base::Optional<gfx::Point> SwipeGestureVector(int dx, int dy) {
95 base::scoped_nsobject<FakeSwipeEvent> swipe_event(
96 [[FakeSwipeEvent alloc] init]);
97 [swipe_event setDeltaX:dx];
98 [swipe_event setDeltaY:dy];
99 [swipe_event setWindow:widget_->GetNativeWindow()];
100 [swipe_event setLocationInWindow:NSMakePoint(50, 50)];
101 [swipe_event setTimestamp:[[NSProcessInfo processInfo] systemUptime]];
102
103 // BridgedContentView should create an appropriate ui::GestureEvent and pass
104 // it to the Widget.
105 [[widget_->GetNativeWindow() contentView] swipeWithEvent:swipe_event];
106 return view_->last_swipe_gesture();
107 }
108
109 // testing::Test:
110 void SetUp() override {
111 WidgetTest::SetUp();
112
113 widget_ = CreateTopLevelPlatformWidget();
114 widget_->SetBounds(gfx::Rect(0, 0, 100, 100));
115 widget_->Show();
116
117 view_ = new ThreeFingerSwipeView;
118 view_->SetSize(widget_->GetClientAreaBoundsInScreen().size());
119 widget_->GetContentsView()->AddChildView(view_);
120 }
121
122 void TearDown() override {
123 widget_->CloseNow();
124 WidgetTest::TearDown();
125 }
126
127 private:
128 Widget* widget_ = nullptr;
129 ThreeFingerSwipeView* view_ = nullptr;
130
131 DISALLOW_COPY_AND_ASSIGN(ViewMacTest);
132 };
133
134 // Three-finger swipes send immediate events and they cannot be tracked.
135 TEST_F(ViewMacTest, HandlesThreeFingerSwipeGestures) {
136 // Note that positive delta is left and up for NSEvent, which is the inverse
137 // of ui::GestureEventDetails.
138 EXPECT_EQ(gfx::Point(1, 0), *SwipeGestureVector(-1, 0));
139 EXPECT_EQ(gfx::Point(-1, 0), *SwipeGestureVector(1, 0));
140 EXPECT_EQ(gfx::Point(0, 1), *SwipeGestureVector(0, -1));
141 EXPECT_EQ(gfx::Point(0, -1), *SwipeGestureVector(0, 1));
142 }
143
144 } // namespace views
OLDNEW
« chrome/browser/ui/views/frame/browser_view.cc ('K') | « ui/views/cocoa/bridged_content_view.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698