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 
6 
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 // Verifies that a create call whose viewType is not a string returns an error rather than raising.
39 // An omitted argument arrives as nil and an explicit Dart null as NSNull, and the factory lookup
40 // the argument feeds raises on a nil key.
41 TEST(FlutterPlatformViewController, TestCreatePlatformViewInvalidViewType) {
42  // Use id so we can access handleMethodCall method.
43  id platformViewController = [[FlutterPlatformViewController alloc] init];
44 
45  NSArray<NSDictionary*>* invalidArguments = @[
46  @{@"id" : @2}, // 'viewType' omitted.
47  @{@"id" : @2,
48  @"viewType" : [NSNull null]}, // 'viewType' explicitly null in Dart.
49  @{@"id" : @2,
50  @"viewType" : @7}, // 'viewType' not a string.
51  ];
52 
53  for (NSDictionary* arguments in invalidArguments) {
54  FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"create"
55  arguments:arguments];
56  __block bool errored = false;
57  FlutterResult result = ^(id result) {
58  if ([result isKindOfClass:[FlutterError class]]) {
59  errored = true;
60  }
61  };
62 
63  [platformViewController handleMethodCall:methodCall result:result];
64 
65  EXPECT_TRUE(errored) << "arguments: " << arguments.description.UTF8String;
66  }
67 }
68 
69 TEST(FlutterPlatformViewController, TestRegisterPlatformViewFactoryAndCreate) {
70  // Use id so we can access handleMethodCall method.
71  id platformViewController = [[FlutterPlatformViewController alloc] init];
72 
74 
75  [platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
76 
77  NSDictionary* creationArgs = @{
78  @"album" : @"スコットとリバース",
79  @"releaseYear" : @2013,
80  @"artists" : @[ @"Scott Murphy", @"Rivers Cuomo" ],
81  @"playlist" : @[ @"おかしいやつ", @"ほどけていたんだ" ],
82  };
83  NSObject<FlutterMessageCodec>* codec = [factory createArgsCodec];
84  FlutterStandardTypedData* creationArgsData =
85  [FlutterStandardTypedData typedDataWithBytes:[codec encode:creationArgs]];
86 
87  FlutterMethodCall* methodCall =
88  [FlutterMethodCall methodCallWithMethodName:@"create"
89  arguments:@{
90  @"id" : @2,
91  @"viewType" : @"MockPlatformView",
92  @"params" : creationArgsData,
93  }];
94 
95  __block bool success = false;
96  FlutterResult result = ^(id result) {
97  // If a platform view is successfully created, the result is nil.
98  if (result == nil) {
99  success = true;
100  }
101  };
102  [platformViewController handleMethodCall:methodCall result:result];
103  EXPECT_TRUE(success);
104 
105  // Verify PlatformView parameters are decoded correctly.
107  (TestFlutterPlatformView*)[platformViewController platformViewWithID:2];
108  ASSERT_TRUE(view != nil);
109  ASSERT_TRUE(view.args != nil);
110 
111  // Verify string type.
112  NSString* album = [view.args objectForKey:@"album"];
113  EXPECT_TRUE([album isEqualToString:@"スコットとリバース"]);
114 
115  // Verify int type.
116  NSNumber* releaseYear = [view.args objectForKey:@"releaseYear"];
117  EXPECT_EQ(releaseYear.intValue, 2013);
118 
119  // Verify list/array types.
120  NSArray* artists = [view.args objectForKey:@"artists"];
121  ASSERT_TRUE(artists != nil);
122  ASSERT_EQ(artists.count, 2ul);
123  EXPECT_TRUE([artists[0] isEqualToString:@"Scott Murphy"]);
124  EXPECT_TRUE([artists[1] isEqualToString:@"Rivers Cuomo"]);
125 
126  NSArray* playlist = [view.args objectForKey:@"playlist"];
127  ASSERT_EQ(playlist.count, 2ul);
128  EXPECT_TRUE([playlist[0] isEqualToString:@"おかしいやつ"]);
129  EXPECT_TRUE([playlist[1] isEqualToString:@"ほどけていたんだ"]);
130 }
131 
132 TEST(FlutterPlatformViewController, TestCreateAndDispose) {
133  // Use id so we can access handleMethodCall method.
134  id platformViewController = [[FlutterPlatformViewController alloc] init];
135 
137 
138  [platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
139 
140  FlutterMethodCall* methodCallOnCreate =
141  [FlutterMethodCall methodCallWithMethodName:@"create"
142  arguments:@{
143  @"id" : @2,
144  @"viewType" : @"MockPlatformView"
145  }];
146 
147  __block bool created = false;
148  FlutterResult resultOnCreate = ^(id result) {
149  // If a platform view is successfully created, the result is nil.
150  if (result == nil) {
151  created = true;
152  }
153  };
154 
155  [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
156 
157  FlutterMethodCall* methodCallOnDispose =
158  [FlutterMethodCall methodCallWithMethodName:@"dispose"
159  arguments:[NSNumber numberWithLongLong:2]];
160 
161  __block bool disposed = false;
162  FlutterResult resultOnDispose = ^(id result) {
163  // If a platform view is successfully created, the result is nil.
164  if (result == nil) {
165  disposed = true;
166  }
167  };
168 
169  [platformViewController handleMethodCall:methodCallOnDispose result:resultOnDispose];
170 
171  EXPECT_TRUE(created);
172  EXPECT_TRUE(disposed);
173 }
174 
176  // Use id so we can access handleMethodCall method.
177  id platformViewController = [[FlutterPlatformViewController alloc] init];
179 
180  [platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
181 
182  __block bool created = false;
183  FlutterResult resultOnCreate = ^(id result) {
184  // If a platform view is successfully created, the result is nil.
185  if (result == nil) {
186  created = true;
187  } else {
188  created = false;
189  }
190  };
191 
192  // Create 2 views.
193  FlutterMethodCall* methodCallOnCreate0 =
194  [FlutterMethodCall methodCallWithMethodName:@"create"
195  arguments:@{
196  @"id" : @0,
197  @"viewType" : @"MockPlatformView"
198  }];
199 
200  [platformViewController handleMethodCall:methodCallOnCreate0 result:resultOnCreate];
201  EXPECT_TRUE(created);
202 
203  FlutterMethodCall* methodCallOnCreate1 =
204  [FlutterMethodCall methodCallWithMethodName:@"create"
205  arguments:@{
206  @"id" : @1,
207  @"viewType" : @"MockPlatformView"
208  }];
209  [platformViewController handleMethodCall:methodCallOnCreate1 result:resultOnCreate];
210  EXPECT_TRUE(created);
211 
212  TestFlutterPlatformView* view = nil;
213 
214  // Before the reset, the views exist.
215  view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
216  EXPECT_TRUE(view != nil);
217  view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
218  EXPECT_TRUE(view != nil);
219 
220  // After a reset, the views should no longer exist.
221  [platformViewController reset];
222 
223  view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
224  EXPECT_TRUE(view == nil);
225  view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
226  EXPECT_TRUE(view == nil);
227 }
228 
229 TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) {
230  // Use id so we can access handleMethodCall method.
231  id platformViewController = [[FlutterPlatformViewController alloc] init];
232 
233  FlutterMethodCall* methodCall =
234  [FlutterMethodCall methodCallWithMethodName:@"dispose"
235  arguments:[NSNumber numberWithLongLong:20]];
236 
237  __block bool errored = false;
238  FlutterResult result = ^(id result) {
239  if ([result isKindOfClass:[FlutterError class]]) {
240  errored = true;
241  }
242  };
243 
244  [platformViewController handleMethodCall:methodCall result:result];
245 
246  EXPECT_TRUE(errored);
247 }
248 
249 TEST(FlutterPlatformViewController, TestAcceptGesture) {
250  FlutterPlatformViewController* platformViewController =
251  [[FlutterPlatformViewController alloc] init];
252  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
253  withId:@"MockPlatformView"];
254 
255  // Create the PlatformView.
256  const NSNumber* viewId = [NSNumber numberWithLongLong:2];
257  FlutterMethodCall* methodCallOnCreate = [FlutterMethodCall
258  methodCallWithMethodName:@"create"
259  arguments:@{@"id" : viewId, @"viewType" : @"MockPlatformView"}];
260  __block bool created = false;
261  FlutterResult resultOnCreate = ^(id result) {
262  // If a platform view is successfully created, the result is nil.
263  if (result == nil) {
264  created = true;
265  }
266  };
267  [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
268 
269  // Call acceptGesture.
270  FlutterMethodCall* methodCallAcceptGesture =
271  [FlutterMethodCall methodCallWithMethodName:@"acceptGesture" arguments:@{@"id" : viewId}];
272  __block bool acceptGestureCalled = false;
273  FlutterResult resultAcceptGesture = ^(id result) {
274  // If a acceptGesture is successful, the result is nil.
275  if (result == nil) {
276  acceptGestureCalled = true;
277  }
278  };
279  [platformViewController handleMethodCall:methodCallAcceptGesture result:resultAcceptGesture];
280 
281  EXPECT_TRUE(created);
282  EXPECT_TRUE(acceptGestureCalled);
283 }
284 
285 TEST(FlutterPlatformViewController, TestAcceptGestureOnMissingViewId) {
286  FlutterPlatformViewController* platformViewController =
287  [[FlutterPlatformViewController alloc] init];
288  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
289  withId:@"MockPlatformView"];
290 
291  // Call rejectGesture.
292  FlutterMethodCall* methodCallAcceptGesture =
293  [FlutterMethodCall methodCallWithMethodName:@"acceptGesture" arguments:@{
294  @"id" : @20
295  }];
296  __block bool errored = false;
297  FlutterResult result = ^(id result) {
298  if ([result isKindOfClass:[FlutterError class]]) {
299  errored = true;
300  }
301  };
302  [platformViewController handleMethodCall:methodCallAcceptGesture result:result];
303 
304  EXPECT_TRUE(errored);
305 }
306 
307 TEST(FlutterPlatformViewController, TestRejectGesture) {
308  FlutterPlatformViewController* platformViewController =
309  [[FlutterPlatformViewController alloc] init];
310  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
311  withId:@"MockPlatformView"];
312 
313  // Create the PlatformView.
314  const NSNumber* viewId = [NSNumber numberWithLongLong:2];
315  FlutterMethodCall* methodCallOnCreate = [FlutterMethodCall
316  methodCallWithMethodName:@"create"
317  arguments:@{@"id" : viewId, @"viewType" : @"MockPlatformView"}];
318  __block bool created = false;
319  FlutterResult resultOnCreate = ^(id result) {
320  // If a platform view is successfully created, the result is nil.
321  if (result == nil) {
322  created = true;
323  }
324  };
325  [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
326 
327  // Call rejectGesture.
328  FlutterMethodCall* methodCallRejectGesture =
329  [FlutterMethodCall methodCallWithMethodName:@"rejectGesture" arguments:@{@"id" : viewId}];
330  __block bool rejectGestureCalled = false;
331  FlutterResult resultRejectGesture = ^(id result) {
332  // If a rejectGesture is successful, the result is nil.
333  if (result == nil) {
334  rejectGestureCalled = true;
335  }
336  };
337  [platformViewController handleMethodCall:methodCallRejectGesture result:resultRejectGesture];
338 
339  EXPECT_TRUE(created);
340  EXPECT_TRUE(rejectGestureCalled);
341 }
342 
343 TEST(FlutterPlatformViewController, TestRejectGestureOnMissingViewId) {
344  FlutterPlatformViewController* platformViewController =
345  [[FlutterPlatformViewController alloc] init];
346  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
347  withId:@"MockPlatformView"];
348 
349  // Call rejectGesture.
350  FlutterMethodCall* methodCallRejectGesture =
351  [FlutterMethodCall methodCallWithMethodName:@"rejectGesture" arguments:@{
352  @"id" : @20
353  }];
354  __block bool errored = false;
355  FlutterResult result = ^(id result) {
356  if ([result isKindOfClass:[FlutterError class]]) {
357  errored = true;
358  }
359  };
360  [platformViewController handleMethodCall:methodCallRejectGesture result:result];
361 
362  EXPECT_TRUE(errored);
363 }
364 
365 } // namespace flutter::testing
void(^ FlutterResult)(id _Nullable result)
id args
Arguments passed via the params value in the create method call.
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)