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 TEST(FlutterPlatformViewController, TestRegisterPlatformViewFactoryAndCreate) {
39  // Use id so we can access handleMethodCall method.
40  id platformViewController = [[FlutterPlatformViewController alloc] init];
41 
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.
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 
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, TestDisposeOnMissingViewId) {
145  // Use id so we can access handleMethodCall method.
146  id platformViewController = [[FlutterPlatformViewController alloc] init];
147 
148  FlutterMethodCall* methodCall =
149  [FlutterMethodCall methodCallWithMethodName:@"dispose"
150  arguments:[NSNumber numberWithLongLong:20]];
151 
152  __block bool errored = false;
153  FlutterResult result = ^(id result) {
154  if ([result isKindOfClass:[FlutterError class]]) {
155  errored = true;
156  }
157  };
158 
159  [platformViewController handleMethodCall:methodCall result:result];
160 
161  EXPECT_TRUE(errored);
162 }
163 
164 TEST(FlutterPlatformViewController, TestAcceptGesture) {
165  FlutterPlatformViewController* platformViewController =
166  [[FlutterPlatformViewController alloc] init];
167  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
168  withId:@"MockPlatformView"];
169 
170  // Create the PlatformView.
171  const NSNumber* viewId = [NSNumber numberWithLongLong:2];
172  FlutterMethodCall* methodCallOnCreate = [FlutterMethodCall
173  methodCallWithMethodName:@"create"
174  arguments:@{@"id" : viewId, @"viewType" : @"MockPlatformView"}];
175  __block bool created = false;
176  FlutterResult resultOnCreate = ^(id result) {
177  // If a platform view is successfully created, the result is nil.
178  if (result == nil) {
179  created = true;
180  }
181  };
182  [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
183 
184  // Call acceptGesture.
185  FlutterMethodCall* methodCallAcceptGesture =
186  [FlutterMethodCall methodCallWithMethodName:@"acceptGesture" arguments:@{@"id" : viewId}];
187  __block bool acceptGestureCalled = false;
188  FlutterResult resultAcceptGesture = ^(id result) {
189  // If a acceptGesture is successful, the result is nil.
190  if (result == nil) {
191  acceptGestureCalled = true;
192  }
193  };
194  [platformViewController handleMethodCall:methodCallAcceptGesture result:resultAcceptGesture];
195 
196  EXPECT_TRUE(created);
197  EXPECT_TRUE(acceptGestureCalled);
198 }
199 
200 TEST(FlutterPlatformViewController, TestAcceptGestureOnMissingViewId) {
201  FlutterPlatformViewController* platformViewController =
202  [[FlutterPlatformViewController alloc] init];
203  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
204  withId:@"MockPlatformView"];
205 
206  // Call rejectGesture.
207  FlutterMethodCall* methodCallAcceptGesture =
208  [FlutterMethodCall methodCallWithMethodName:@"acceptGesture" arguments:@{
209  @"id" : @20
210  }];
211  __block bool errored = false;
212  FlutterResult result = ^(id result) {
213  if ([result isKindOfClass:[FlutterError class]]) {
214  errored = true;
215  }
216  };
217  [platformViewController handleMethodCall:methodCallAcceptGesture result:result];
218 
219  EXPECT_TRUE(errored);
220 }
221 
222 TEST(FlutterPlatformViewController, TestRejectGesture) {
223  FlutterPlatformViewController* platformViewController =
224  [[FlutterPlatformViewController alloc] init];
225  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
226  withId:@"MockPlatformView"];
227 
228  // Create the PlatformView.
229  const NSNumber* viewId = [NSNumber numberWithLongLong:2];
230  FlutterMethodCall* methodCallOnCreate = [FlutterMethodCall
231  methodCallWithMethodName:@"create"
232  arguments:@{@"id" : viewId, @"viewType" : @"MockPlatformView"}];
233  __block bool created = false;
234  FlutterResult resultOnCreate = ^(id result) {
235  // If a platform view is successfully created, the result is nil.
236  if (result == nil) {
237  created = true;
238  }
239  };
240  [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate];
241 
242  // Call rejectGesture.
243  FlutterMethodCall* methodCallRejectGesture =
244  [FlutterMethodCall methodCallWithMethodName:@"rejectGesture" arguments:@{@"id" : viewId}];
245  __block bool rejectGestureCalled = false;
246  FlutterResult resultRejectGesture = ^(id result) {
247  // If a rejectGesture is successful, the result is nil.
248  if (result == nil) {
249  rejectGestureCalled = true;
250  }
251  };
252  [platformViewController handleMethodCall:methodCallRejectGesture result:resultRejectGesture];
253 
254  EXPECT_TRUE(created);
255  EXPECT_TRUE(rejectGestureCalled);
256 }
257 
258 TEST(FlutterPlatformViewController, TestRejectGestureOnMissingViewId) {
259  FlutterPlatformViewController* platformViewController =
260  [[FlutterPlatformViewController alloc] init];
261  [platformViewController registerViewFactory:[TestFlutterPlatformViewFactory alloc]
262  withId:@"MockPlatformView"];
263 
264  // Call rejectGesture.
265  FlutterMethodCall* methodCallRejectGesture =
266  [FlutterMethodCall methodCallWithMethodName:@"rejectGesture" arguments:@{
267  @"id" : @20
268  }];
269  __block bool errored = false;
270  FlutterResult result = ^(id result) {
271  if ([result isKindOfClass:[FlutterError class]]) {
272  errored = true;
273  }
274  };
275  [platformViewController handleMethodCall:methodCallRejectGesture result:result];
276 
277  EXPECT_TRUE(errored);
278 }
279 
280 } // namespace flutter::testing
FlutterPlatformViewController
Definition: FlutterPlatformViewController.h:17
FlutterPlatformViewController.h
FlutterError
Definition: FlutterCodecs.h:246
FlutterChannels.h
TestFlutterPlatformViewFactory
Definition: TestFlutterPlatformView.h:19
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:13
flutter::testing::TEST
TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler)
Definition: FlutterAppDelegateTest.mm:32
TestFlutterPlatformView
Definition: TestFlutterPlatformView.h:12
TestFlutterPlatformView::args
id args
Arguments passed via the params value in the create method call.
Definition: TestFlutterPlatformView.h:15
TestFlutterPlatformView.h
FlutterMethodCall
Definition: FlutterCodecs.h:220
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:194
FlutterStandardTypedData
Definition: FlutterCodecs.h:300