| 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, 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 } // namespace flutter::testing
FlutterPlatformViewController
Definition: FlutterPlatformViewController.h:14
FlutterPlatformViewController.h
FlutterError
Definition: FlutterCodecs.h:246
FlutterChannels.h
TestFlutterPlatformViewFactory
Definition: TestFlutterPlatformView.h:16
flutter::testing
Definition: AccessibilityBridgeMacTest.mm:11
TestFlutterPlatformView
Definition: TestFlutterPlatformView.h:9
TestFlutterPlatformView::args
id args
Arguments passed via the params value in the create method call.
Definition: TestFlutterPlatformView.h:12
TestFlutterPlatformView.h
FlutterMethodCall
Definition: FlutterCodecs.h:220
flutter::testing::TEST
TEST(AccessibilityBridgeMacTest, SendsAccessibilityCreateNotificationToWindowOfFlutterView)
Definition: AccessibilityBridgeMacTest.mm:61
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:196
FlutterStandardTypedData
Definition: FlutterCodecs.h:298