|
6 | 6 |
|
7 | 7 | #import <OCMock/OCMock.h>
|
8 | 8 |
|
| 9 | +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" |
9 | 10 | #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
|
10 | 11 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
|
11 | 12 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
|
| 13 | +#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h" |
| 14 | +#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h" |
12 | 15 | #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"
|
| 16 | + |
13 | 17 | #include "flutter/testing/testing.h"
|
14 | 18 |
|
15 | 19 | namespace flutter::testing {
|
|
53 | 57 | EXPECT_TRUE(value);
|
54 | 58 | }
|
55 | 59 |
|
| 60 | +TEST(FlutterViewController, TestCreatePlatformViewNoMatchingViewType) { |
| 61 | + NSString* fixtures = @(testing::GetFixturesPath()); |
| 62 | + FlutterDartProject* project = [[FlutterDartProject alloc] |
| 63 | + initWithAssetsPath:fixtures |
| 64 | + ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; |
| 65 | + |
| 66 | + // Use id so we can access handleMethodCall method. |
| 67 | + id viewController = [[FlutterViewController alloc] initWithProject:project]; |
| 68 | + |
| 69 | + FlutterMethodCall* methodCall = |
| 70 | + [FlutterMethodCall methodCallWithMethodName:@"create" |
| 71 | + arguments:@{ |
| 72 | + @"id" : @2, |
| 73 | + @"viewType" : @"FlutterPlatformViewMock" |
| 74 | + }]; |
| 75 | + |
| 76 | + __block bool errored = false; |
| 77 | + FlutterResult result = ^(id result) { |
| 78 | + if ([result isKindOfClass:[FlutterError class]]) { |
| 79 | + errored = true; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + [viewController handleMethodCall:methodCall result:result]; |
| 84 | + |
| 85 | + // We expect the call to error since no factories are registered. |
| 86 | + EXPECT_TRUE(errored); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(FlutterViewController, TestRegisterPlatformViewFactoryAndCreate) { |
| 90 | + NSString* fixtures = @(testing::GetFixturesPath()); |
| 91 | + FlutterDartProject* project = [[FlutterDartProject alloc] |
| 92 | + initWithAssetsPath:fixtures |
| 93 | + ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; |
| 94 | + |
| 95 | + // Use id so we can access handleMethodCall method. |
| 96 | + id viewController = [[FlutterViewController alloc] initWithProject:project]; |
| 97 | + |
| 98 | + FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc]; |
| 99 | + |
| 100 | + [viewController registerViewFactory:factory withId:@"MockPlatformView"]; |
| 101 | + |
| 102 | + FlutterMethodCall* methodCall = |
| 103 | + [FlutterMethodCall methodCallWithMethodName:@"create" |
| 104 | + arguments:@{ |
| 105 | + @"id" : @2, |
| 106 | + @"viewType" : @"MockPlatformView" |
| 107 | + }]; |
| 108 | + |
| 109 | + __block bool success = false; |
| 110 | + FlutterResult result = ^(id result) { |
| 111 | + // If a platform view is successfully created, the result is nil. |
| 112 | + if (result == nil) { |
| 113 | + success = true; |
| 114 | + } |
| 115 | + }; |
| 116 | + [viewController handleMethodCall:methodCall result:result]; |
| 117 | + |
| 118 | + EXPECT_TRUE(success); |
| 119 | +} |
| 120 | + |
| 121 | +TEST(FlutterViewController, TestCreateAndDispose) { |
| 122 | + NSString* fixtures = @(testing::GetFixturesPath()); |
| 123 | + FlutterDartProject* project = [[FlutterDartProject alloc] |
| 124 | + initWithAssetsPath:fixtures |
| 125 | + ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; |
| 126 | + |
| 127 | + // Use id so we can access handleMethodCall method. |
| 128 | + id viewController = [[FlutterViewController alloc] initWithProject:project]; |
| 129 | + |
| 130 | + FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc]; |
| 131 | + |
| 132 | + [viewController registerViewFactory:factory withId:@"MockPlatformView"]; |
| 133 | + |
| 134 | + FlutterMethodCall* methodCallOnCreate = |
| 135 | + [FlutterMethodCall methodCallWithMethodName:@"create" |
| 136 | + arguments:@{ |
| 137 | + @"id" : @2, |
| 138 | + @"viewType" : @"MockPlatformView" |
| 139 | + }]; |
| 140 | + |
| 141 | + __block bool created = false; |
| 142 | + FlutterResult resultOnCreate = ^(id result) { |
| 143 | + // If a platform view is successfully created, the result is nil. |
| 144 | + if (result == nil) { |
| 145 | + created = true; |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + [viewController handleMethodCall:methodCallOnCreate result:resultOnCreate]; |
| 150 | + |
| 151 | + FlutterMethodCall* methodCallOnDispose = |
| 152 | + [FlutterMethodCall methodCallWithMethodName:@"dispose" |
| 153 | + arguments:[NSNumber numberWithLongLong:2]]; |
| 154 | + |
| 155 | + __block bool disposed = false; |
| 156 | + FlutterResult resultOnDispose = ^(id result) { |
| 157 | + // If a platform view is successfully created, the result is nil. |
| 158 | + if (result == nil) { |
| 159 | + disposed = true; |
| 160 | + } |
| 161 | + }; |
| 162 | + |
| 163 | + [viewController handleMethodCall:methodCallOnDispose result:resultOnDispose]; |
| 164 | + |
| 165 | + EXPECT_TRUE(created); |
| 166 | + EXPECT_TRUE(disposed); |
| 167 | +} |
| 168 | + |
| 169 | +TEST(FlutterViewController, TestDisposeOnMissingViewId) { |
| 170 | + NSString* fixtures = @(testing::GetFixturesPath()); |
| 171 | + FlutterDartProject* project = [[FlutterDartProject alloc] |
| 172 | + initWithAssetsPath:fixtures |
| 173 | + ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; |
| 174 | + |
| 175 | + // Use id so we can access handleMethodCall method. |
| 176 | + id viewController = [[FlutterViewController alloc] initWithProject:project]; |
| 177 | + |
| 178 | + FlutterMethodCall* methodCall = |
| 179 | + [FlutterMethodCall methodCallWithMethodName:@"dispose" |
| 180 | + arguments:[NSNumber numberWithLongLong:20]]; |
| 181 | + |
| 182 | + __block bool errored = false; |
| 183 | + FlutterResult result = ^(id result) { |
| 184 | + if ([result isKindOfClass:[FlutterError class]]) { |
| 185 | + errored = true; |
| 186 | + } |
| 187 | + }; |
| 188 | + |
| 189 | + [viewController handleMethodCall:methodCall result:result]; |
| 190 | + |
| 191 | + EXPECT_TRUE(errored); |
| 192 | +} |
| 193 | + |
56 | 194 | } // flutter::testing
|
0 commit comments