| Flutter macOS Embedder |

FlutterPlatformViewControllerTest.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
5 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
6
7 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
8 #import "flutter/shell/platform/darwin/macos/framework/Source/TestFlutterPlatformView.h"
9
10 #include "flutter/testing/testing.h"
11
12 namespace flutter::testing {
13
14 TEST(FlutterPlatformViewController, TestCreatePlatformViewNoMatchingViewType) {
15 // Use id so we can access handleMethodCall method.
16 id platformViewController = [[FlutterPlatformViewController alloc] init];
17
18 FlutterMethodCall* methodCall =
19 [FlutterMethodCall methodCallWithMethodName:@"create"
20 arguments:@{
21 @"id" : @2,
22 @"viewType" : @"FlutterPlatformViewMock"
23 }];
24
25 __block bool errored = false;
26 FlutterResult result = ^(id result) {
27 if ([result isKindOfClass:[FlutterError class]]) {
28 errored = true;
29 }
30 };
31
32 [platformViewController handleMethodCall:methodCall result:result];
33
34 // We expect the call to error since no factories are registered.
35 EXPECT_TRUE(errored);
36 }
37
38 TEST(FlutterPlatformViewController, TestRegisterPlatformViewFactoryAndCreate) {
39 // Use id so we can access handleMethodCall method.
40 id platformViewController = [[FlutterPlatformViewController alloc] init];
41
42 TestFlutterPlatformViewFactory* factory = [TestFlutterPlatformViewFactory alloc];
43
44 [platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
45
46 NSDictionary* creationArgs = @{
47 @"album" : @"スコットとリバース",
48 @"releaseYear" : @2013,
49 @"artists" : @[ @"Scott Murphy", @"Rivers Cuomo" ],
50 @"playlist" : @[ @"おかしいやつ", @"ほどけていたんだ" ],
51 };
52 NSObject<FlutterMessageCodec>* codec = [factory createArgsCodec];
53 FlutterStandardTypedData* creationArgsData =
54 [FlutterStandardTypedData typedDataWithBytes:[codec encode:creationArgs]];
55
56 FlutterMethodCall* methodCall =
57 [FlutterMethodCall methodCallWithMethodName:@"create"
58 arguments:@{
59 @"id" : @2,
60 @"viewType" : @"MockPlatformView",
61 @"params" : creationArgsData,
62 }];
63
64 __block bool success = false;
65 FlutterResult result = ^(id result) {
66 // If a platform view is successfully created, the result is nil.
67 if (result == nil) {
68 success = true;
69 }
70 };
71 [platformViewController handleMethodCall:methodCall result:result];
72 EXPECT_TRUE(success);
73
74 // Verify PlatformView parameters are decoded correctly.
75 TestFlutterPlatformView* view =
76 (TestFlutterPlatformView*)[platformViewController platformViewWithID:2];
77 ASSERT_TRUE(view != nil);
78 ASSERT_TRUE(view.args != nil);
79
80 // Verify string type.
81 NSString* album = [view.args objectForKey:@"album"];
82 EXPECT_TRUE([album isEqualToString:@"スコットとリバース"]);
83
84 // Verify int type.
85 NSNumber* releaseYear = [view.args objectForKey:@"releaseYear"];
86 EXPECT_EQ(releaseYear.intValue, 2013);
87
88 // Verify list/array types.
89 NSArray* artists = [view.args objectForKey:@"artists"];
90 ASSERT_TRUE(artists != nil);
91 ASSERT_EQ(artists.count, 2ul);
92 EXPECT_TRUE([artists[0] isEqualToString:@"Scott Murphy"]);
93 EXPECT_TRUE([artists[1] isEqualToString:@"Rivers Cuomo"]);
94
95 NSArray* playlist = [view.args objectForKey:@"playlist"];
96 ASSERT_EQ(playlist.count, 2ul);
97 EXPECT_TRUE([playlist[0] isEqualToString:@"おかしいやつ"]);
98 EXPECT_TRUE([playlist[1] isEqualToString:@"ほどけていたんだ"]);
99 }
100
101 TEST(FlutterPlatformViewController, TestCreateAndDispose) {
102 // Use id so we can access handleMethodCall method.
103 id platformViewController = [[FlutterPlatformViewController alloc] init];
104
105 TestFlutterPlatformViewFactory* factory = [TestFlutterPlatformViewFactory alloc];
106
107 [platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
108
109 FlutterMethodCall* methodCallOnCreate =
110 [FlutterMethodCall methodCallWithMethodName:@"create"
111 arguments:@{
112 @"id" : @2,
113 @"viewType" : @"MockPlatformView"
114 }];
115
116 __block bool created = false;
117 FlutterResult resultOnCreate = ^(id result) {
118 // If a platform view is successfully created, the result is nil.
119 if (result == nil) {
120 created = true;
121 }
122 };
123
124 [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
125
126 FlutterMethodCall* methodCallOnDispose =
127 [FlutterMethodCall methodCallWithMethodName:@"dispose"
128 arguments:[NSNumber numberWithLongLong:2]];
129
130 __block bool disposed = false;
131 FlutterResult resultOnDispose = ^(id result) {
132 // If a platform view is successfully created, the result is nil.
133 if (result == nil) {
134 disposed = true;
135 }
136 };
137
138 [platformViewController handleMethodCall:methodCallOnDispose result:resultOnDispose];
139
140 EXPECT_TRUE(created);
141 EXPECT_TRUE(disposed);
142 }
143
144 TEST(FlutterPlatformViewController, TestReset) {
145 // Use id so we can access handleMethodCall method.
146 id platformViewController = [[FlutterPlatformViewController alloc] init];
147 TestFlutterPlatformViewFactory* factory = [TestFlutterPlatformViewFactory alloc];
148
149 [platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
150
151 __block bool created = false;
152 FlutterResult resultOnCreate = ^(id result) {
153 // If a platform view is successfully created, the result is nil.
154 if (result == nil) {
155 created = true;
156 } else {
157 created = false;
158 }
159 };
160
161 // Create 2 views.
162 FlutterMethodCall* methodCallOnCreate0 =
163 [FlutterMethodCall methodCallWithMethodName:@"create"
164 arguments:@{
165 @"id" : @0,
166 @"viewType" : @"MockPlatformView"
167 }];
168
169 [platformViewController handleMethodCall:methodCallOnCreate0 result:resultOnCreate];
170 EXPECT_TRUE(created);
171
172 FlutterMethodCall* methodCallOnCreate1 =
173 [FlutterMethodCall methodCallWithMethodName:@"create"
174 arguments:@{
175 @"id" : @1,
176 @"viewType" : @"MockPlatformView"
177 }];
178 [platformViewController handleMethodCall:methodCallOnCreate1 result:resultOnCreate];
179 EXPECT_TRUE(created);
180
181 TestFlutterPlatformView* view = nil;
182
183 // Before the reset, the views exist.
184 view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
185 EXPECT_TRUE(view != nil);
186 view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
187 EXPECT_TRUE(view != nil);
188
189 // After a reset, the views should no longer exist.
190 [platformViewController reset];
191
192 view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
193 EXPECT_TRUE(view == nil);
194 view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
195 EXPECT_TRUE(view == nil);
196 }
197
198 TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) {
199 // Use id so we can access handleMethodCall method.
200 id platformViewController = [[FlutterPlatformViewController alloc] init];
201
202 FlutterMethodCall* methodCall =
203 [FlutterMethodCall methodCallWithMethodName:@"dispose"
204 arguments:[NSNumber numberWithLongLong:20]];
205
206 __block bool errored = false;
207 FlutterResult result = ^(id result) {
208 if ([result isKindOfClass:[FlutterError class]]) {
209 errored = true;
210 }
211 };
212
213 [platformViewController handleMethodCall:methodCall result:result];
214
215 EXPECT_TRUE(errored);
216 }
217
218 TEST(FlutterPlatformViewController, TestAcceptGesture) {
219 FlutterPlatformViewController* platformViewController =
220 [[FlutterPlatformViewController alloc] init];
221 [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
222 withId:@"MockPlatformView"];
223
224 // Create the PlatformView.
225 const NSNumber* viewId = [NSNumber numberWithLongLong:2];
226 FlutterMethodCall* methodCallOnCreate = [FlutterMethodCall
227 methodCallWithMethodName:@"create"
228 arguments:@{@"id" : viewId, @"viewType" : @"MockPlatformView"}];
229 __block bool created = false;
230 FlutterResult resultOnCreate = ^(id result) {
231 // If a platform view is successfully created, the result is nil.
232 if (result == nil) {
233 created = true;
234 }
235 };
236 [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
237
238 // Call acceptGesture.
239 FlutterMethodCall* methodCallAcceptGesture =
240 [FlutterMethodCall methodCallWithMethodName:@"acceptGesture" arguments:@{@"id" : viewId}];
241 __block bool acceptGestureCalled = false;
242 FlutterResult resultAcceptGesture = ^(id result) {
243 // If a acceptGesture is successful, the result is nil.
244 if (result == nil) {
245 acceptGestureCalled = true;
246 }
247 };
248 [platformViewController handleMethodCall:methodCallAcceptGesture result:resultAcceptGesture];
249
250 EXPECT_TRUE(created);
251 EXPECT_TRUE(acceptGestureCalled);
252 }
253
254 TEST(FlutterPlatformViewController, TestAcceptGestureOnMissingViewId) {
255 FlutterPlatformViewController* platformViewController =
256 [[FlutterPlatformViewController alloc] init];
257 [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
258 withId:@"MockPlatformView"];
259
260 // Call rejectGesture.
261 FlutterMethodCall* methodCallAcceptGesture =
262 [FlutterMethodCall methodCallWithMethodName:@"acceptGesture" arguments:@{
263 @"id" : @20
264 }];
265 __block bool errored = false;
266 FlutterResult result = ^(id result) {
267 if ([result isKindOfClass:[FlutterError class]]) {
268 errored = true;
269 }
270 };
271 [platformViewController handleMethodCall:methodCallAcceptGesture result:result];
272
273 EXPECT_TRUE(errored);
274 }
275
276 TEST(FlutterPlatformViewController, TestRejectGesture) {
277 FlutterPlatformViewController* platformViewController =
278 [[FlutterPlatformViewController alloc] init];
279 [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
280 withId:@"MockPlatformView"];
281
282 // Create the PlatformView.
283 const NSNumber* viewId = [NSNumber numberWithLongLong:2];
284 FlutterMethodCall* methodCallOnCreate = [FlutterMethodCall
285 methodCallWithMethodName:@"create"
286 arguments:@{@"id" : viewId, @"viewType" : @"MockPlatformView"}];
287 __block bool created = false;
288 FlutterResult resultOnCreate = ^(id result) {
289 // If a platform view is successfully created, the result is nil.
290 if (result == nil) {
291 created = true;
292 }
293 };
294 [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
295
296 // Call rejectGesture.
297 FlutterMethodCall* methodCallRejectGesture =
298 [FlutterMethodCall methodCallWithMethodName:@"rejectGesture" arguments:@{@"id" : viewId}];
299 __block bool rejectGestureCalled = false;
300 FlutterResult resultRejectGesture = ^(id result) {
301 // If a rejectGesture is successful, the result is nil.
302 if (result == nil) {
303 rejectGestureCalled = true;
304 }
305 };
306 [platformViewController handleMethodCall:methodCallRejectGesture result:resultRejectGesture];
307
308 EXPECT_TRUE(created);
309 EXPECT_TRUE(rejectGestureCalled);
310 }
311
312 TEST(FlutterPlatformViewController, TestRejectGestureOnMissingViewId) {
313 FlutterPlatformViewController* platformViewController =
314 [[FlutterPlatformViewController alloc] init];
315 [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
316 withId:@"MockPlatformView"];
317
318 // Call rejectGesture.
319 FlutterMethodCall* methodCallRejectGesture =
320 [FlutterMethodCall methodCallWithMethodName:@"rejectGesture" arguments:@{
321 @"id" : @20
322 }];
323 __block bool errored = false;
324 FlutterResult result = ^(id result) {
325 if ([result isKindOfClass:[FlutterError class]]) {
326 errored = true;
327 }
328 };
329 [platformViewController handleMethodCall:methodCallRejectGesture result:result];
330
331 EXPECT_TRUE(errored);
332 }
333
334 } // namespace flutter::testing
FlutterChannels.h
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:194
FlutterPlatformViewController.h
TestFlutterPlatformView.h
FlutterError
Definition: FlutterCodecs.h:247
FlutterMethodCall
Definition: FlutterCodecs.h:221
FlutterPlatformViewController
Definition: FlutterPlatformViewController.h:18
FlutterStandardTypedData
Definition: FlutterCodecs.h:301
TestFlutterPlatformViewFactory
Definition: TestFlutterPlatformView.h:19
TestFlutterPlatformView
Definition: TestFlutterPlatformView.h:13
TestFlutterPlatformView::args
id args
Arguments passed via the params value in the create method call.
Definition: TestFlutterPlatformView.h:15
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:13
flutter::testing::TEST
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)
Definition: FlutterAppDelegateTest.mm:32