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