Flutter macOS Embedder
FlutterViewTest.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 
6 
7 #import <Metal/Metal.h>
8 
9 #import "flutter/testing/testing.h"
10 
11 constexpr int64_t kImplicitViewId = 0ll;
12 
14 
15 @end
16 
17 @implementation TestFlutterViewDelegate
18 
19 - (void)viewDidReshape:(nonnull NSView*)view {
20 }
21 
22 - (BOOL)viewShouldAcceptFirstResponder:(NSView*)view {
23  return YES;
24 }
25 
26 @end
27 
28 TEST(FlutterView, ShouldInheritContentsScaleReturnsYes) {
29  id<MTLDevice> device = MTLCreateSystemDefaultDevice();
30  id<MTLCommandQueue> queue = [device newCommandQueue];
31  TestFlutterViewDelegate* delegate = [[TestFlutterViewDelegate alloc] init];
32  FlutterView* view = [[FlutterView alloc] initWithMTLDevice:device
33  commandQueue:queue
34  delegate:delegate
35  viewIdentifier:kImplicitViewId];
36  EXPECT_EQ([view layer:view.layer shouldInheritContentsScale:3.0 fromWindow:view.window], YES);
37 }
38 
40 
41 @property(readwrite, nonatomic) NSView* (^onHitTest)(NSPoint point);
42 
43 @end
44 
45 @implementation TestFlutterView
46 
47 @synthesize onHitTest;
48 
49 - (NSView*)hitTest:(NSPoint)point {
50  return self.onHitTest(point);
51 }
52 
53 - (void)reshaped {
54  // Disable resize synchronization for testing.
55 }
56 
57 @end
58 
59 @interface TestCursor : NSCursor
60 @property(readwrite, nonatomic) BOOL setCalled;
61 @end
62 
63 @implementation TestCursor
64 
65 - (void)set {
66  self.setCalled = YES;
67 }
68 
69 @end
70 
71 TEST(FlutterView, CursorUpdateDoesHitTest) {
72  id<MTLDevice> device = MTLCreateSystemDefaultDevice();
73  id<MTLCommandQueue> queue = [device newCommandQueue];
74  TestFlutterViewDelegate* delegate = [[TestFlutterViewDelegate alloc] init];
75  TestFlutterView* view = [[TestFlutterView alloc] initWithMTLDevice:device
76  commandQueue:queue
77  delegate:delegate
78  viewIdentifier:kImplicitViewId];
79  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
80  styleMask:NSBorderlessWindowMask
81  backing:NSBackingStoreBuffered
82  defer:NO];
83 
84  TestCursor* cursor = [[TestCursor alloc] init];
85 
86  window.contentView = view;
87  __weak NSView* weakView = view;
88  __block BOOL hitTestCalled = NO;
89  __block NSPoint hitTestCoordinate = NSZeroPoint;
90  view.onHitTest = ^NSView*(NSPoint point) {
91  hitTestCalled = YES;
92  hitTestCoordinate = point;
93  return weakView;
94  };
95  NSEvent* mouseEvent = [NSEvent mouseEventWithType:NSEventTypeMouseMoved
96  location:NSMakePoint(100, 100)
97  modifierFlags:0
98  timestamp:0
99  windowNumber:0
100  context:nil
101  eventNumber:0
102  clickCount:0
103  pressure:0];
104  [view didUpdateMouseCursor:cursor];
105  [view cursorUpdate:mouseEvent];
106 
107  EXPECT_TRUE(hitTestCalled);
108  // The hit test coordinate should be in the window coordinate system.
109  EXPECT_TRUE(CGPointEqualToPoint(hitTestCoordinate, CGPointMake(100, 100)));
110  EXPECT_TRUE(cursor.setCalled);
111 }
112 
113 TEST(FlutterView, CursorUpdateDoesNotOverridePlatformView) {
114  id<MTLDevice> device = MTLCreateSystemDefaultDevice();
115  id<MTLCommandQueue> queue = [device newCommandQueue];
116  TestFlutterViewDelegate* delegate = [[TestFlutterViewDelegate alloc] init];
117  TestFlutterView* view = [[TestFlutterView alloc] initWithMTLDevice:device
118  commandQueue:queue
119  delegate:delegate
120  viewIdentifier:kImplicitViewId];
121  NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 800, 600)
122  styleMask:NSBorderlessWindowMask
123  backing:NSBackingStoreBuffered
124  defer:NO];
125 
126  TestCursor* cursor = [[TestCursor alloc] init];
127 
128  NSView* platformView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
129 
130  window.contentView = view;
131  __block BOOL hitTestCalled = NO;
132  __block NSPoint hitTestCoordinate = NSZeroPoint;
133  view.onHitTest = ^NSView*(NSPoint point) {
134  hitTestCalled = YES;
135  hitTestCoordinate = point;
136  return platformView;
137  };
138  NSEvent* mouseEvent = [NSEvent mouseEventWithType:NSEventTypeMouseMoved
139  location:NSMakePoint(100, 100)
140  modifierFlags:0
141  timestamp:0
142  windowNumber:0
143  context:nil
144  eventNumber:0
145  clickCount:0
146  pressure:0];
147  [view didUpdateMouseCursor:cursor];
148  [view cursorUpdate:mouseEvent];
149 
150  EXPECT_TRUE(hitTestCalled);
151  // The hit test coordinate should be in the window coordinate system.
152  EXPECT_TRUE(CGPointEqualToPoint(hitTestCoordinate, CGPointMake(100, 100)));
153  EXPECT_FALSE(cursor.setCalled);
154 }
TEST(FlutterView, ShouldInheritContentsScaleReturnsYes)
constexpr int64_t kImplicitViewId
void didUpdateMouseCursor:(nonnull NSCursor *cursor)
NSView *(^ onHitTest)(NSPoint point)