summaryrefslogtreecommitdiff
path: root/chromium/ui/views/view_unittest_mac.mm
blob: 66762709cf510c5967d005a2579986ed971cca71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/view.h"

#import <Cocoa/Cocoa.h>

#import "base/mac/scoped_nsobject.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/events/gesture_event_details.h"
#include "ui/views/test/widget_test.h"

// We can't create NSEventTypeSwipe using normal means, and rely on duck typing
// instead.
@interface FakeSwipeEvent : NSEvent
@property CGFloat deltaX;
@property CGFloat deltaY;
@property(assign) NSWindow* window;
@property NSPoint locationInWindow;
@property NSEventModifierFlags modifierFlags;
@property NSTimeInterval timestamp;
@end

@implementation FakeSwipeEvent
@synthesize deltaX;
@synthesize deltaY;
@synthesize window;
@synthesize locationInWindow;
@synthesize modifierFlags;
@synthesize timestamp;

- (NSEventType)type {
  return NSEventTypeSwipe;
}

- (NSEventSubtype)subtype {
  // themblsha: In my testing, all native three-finger NSEventTypeSwipe events
  // all had 0 as their subtype.
  return static_cast<NSEventSubtype>(0);
}
@end

namespace views {

namespace {

// Stores last received swipe gesture direction vector in
// |last_swipe_gesture()|.
class ThreeFingerSwipeView : public View {
 public:
  ThreeFingerSwipeView() = default;

  ThreeFingerSwipeView(const ThreeFingerSwipeView&) = delete;
  ThreeFingerSwipeView& operator=(const ThreeFingerSwipeView&) = delete;

  // View:
  void OnGestureEvent(ui::GestureEvent* event) override {
    EXPECT_EQ(ui::ET_GESTURE_SWIPE, event->details().type());

    int dx = 0, dy = 0;
    if (event->details().swipe_left())
      dx = -1;

    if (event->details().swipe_right()) {
      EXPECT_EQ(0, dx);
      dx = 1;
    }

    if (event->details().swipe_down())
      dy = 1;

    if (event->details().swipe_up()) {
      EXPECT_EQ(0, dy);
      dy = -1;
    }

    last_swipe_gesture_ = gfx::Point(dx, dy);
  }

  absl::optional<gfx::Point> last_swipe_gesture() const {
    return last_swipe_gesture_;
  }

 private:
  absl::optional<gfx::Point> last_swipe_gesture_;
};

}  // namespace

class ViewMacTest : public test::WidgetTest {
 public:
  ViewMacTest() = default;

  ViewMacTest(const ViewMacTest&) = delete;
  ViewMacTest& operator=(const ViewMacTest&) = delete;

  absl::optional<gfx::Point> SwipeGestureVector(int dx, int dy) {
    base::scoped_nsobject<FakeSwipeEvent> swipe_event(
        [[FakeSwipeEvent alloc] init]);
    [swipe_event setDeltaX:dx];
    [swipe_event setDeltaY:dy];
    [swipe_event setWindow:widget_->GetNativeWindow().GetNativeNSWindow()];
    [swipe_event setLocationInWindow:NSMakePoint(50, 50)];
    [swipe_event setTimestamp:[[NSProcessInfo processInfo] systemUptime]];

    // BridgedContentView should create an appropriate ui::GestureEvent and pass
    // it to the Widget.
    [[widget_->GetNativeWindow().GetNativeNSWindow() contentView]
        swipeWithEvent:swipe_event];
    return view_->last_swipe_gesture();
  }

  // testing::Test:
  void SetUp() override {
    WidgetTest::SetUp();

    widget_ = CreateTopLevelPlatformWidget();
    widget_->SetBounds(gfx::Rect(0, 0, 100, 100));
    widget_->Show();

    view_ = new ThreeFingerSwipeView;
    view_->SetSize(widget_->GetClientAreaBoundsInScreen().size());
    widget_->non_client_view()->frame_view()->AddChildView(view_);
  }

  void TearDown() override {
    widget_->CloseNow();
    WidgetTest::TearDown();
  }

 private:
  Widget* widget_ = nullptr;
  ThreeFingerSwipeView* view_ = nullptr;
};

// Three-finger swipes send immediate events and they cannot be tracked.
TEST_F(ViewMacTest, HandlesThreeFingerSwipeGestures) {
  // Note that positive delta is left and up for NSEvent, which is the inverse
  // of ui::GestureEventDetails.
  EXPECT_EQ(gfx::Point(1, 0), *SwipeGestureVector(-1, 0));
  EXPECT_EQ(gfx::Point(-1, 0), *SwipeGestureVector(1, 0));
  EXPECT_EQ(gfx::Point(0, 1), *SwipeGestureVector(0, -1));
  EXPECT_EQ(gfx::Point(0, -1), *SwipeGestureVector(0, 1));
}

}  // namespace views