|
| 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_Internal.h" |
| 6 | + |
| 7 | +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" |
| 8 | +#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.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 | + FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc]; |
| 43 | + |
| 44 | + [platformViewController registerViewFactory:factory withId:@"MockPlatformView"]; |
| 45 | + |
| 46 | + FlutterMethodCall* methodCall = |
| 47 | + [FlutterMethodCall methodCallWithMethodName:@"create" |
| 48 | + arguments:@{ |
| 49 | + @"id" : @2, |
| 50 | + @"viewType" : @"MockPlatformView" |
| 51 | + }]; |
| 52 | + |
| 53 | + __block bool success = false; |
| 54 | + FlutterResult result = ^(id result) { |
| 55 | + // If a platform view is successfully created, the result is nil. |
| 56 | + if (result == nil) { |
| 57 | + success = true; |
| 58 | + } |
| 59 | + }; |
| 60 | + [platformViewController handleMethodCall:methodCall result:result]; |
| 61 | + |
| 62 | + EXPECT_TRUE(success); |
| 63 | +} |
| 64 | + |
| 65 | +TEST(FlutterPlatformViewController, TestCreateAndDispose) { |
| 66 | + // Use id so we can access handleMethodCall method. |
| 67 | + id platformViewController = [[FlutterPlatformViewController alloc] init]; |
| 68 | + |
| 69 | + FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc]; |
| 70 | + |
| 71 | + [platformViewController registerViewFactory:factory withId:@"MockPlatformView"]; |
| 72 | + |
| 73 | + FlutterMethodCall* methodCallOnCreate = |
| 74 | + [FlutterMethodCall methodCallWithMethodName:@"create" |
| 75 | + arguments:@{ |
| 76 | + @"id" : @2, |
| 77 | + @"viewType" : @"MockPlatformView" |
| 78 | + }]; |
| 79 | + |
| 80 | + __block bool created = false; |
| 81 | + FlutterResult resultOnCreate = ^(id result) { |
| 82 | + // If a platform view is successfully created, the result is nil. |
| 83 | + if (result == nil) { |
| 84 | + created = true; |
| 85 | + } |
| 86 | + }; |
| 87 | + |
| 88 | + [platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate]; |
| 89 | + |
| 90 | + FlutterMethodCall* methodCallOnDispose = |
| 91 | + [FlutterMethodCall methodCallWithMethodName:@"dispose" |
| 92 | + arguments:[NSNumber numberWithLongLong:2]]; |
| 93 | + |
| 94 | + __block bool disposed = false; |
| 95 | + FlutterResult resultOnDispose = ^(id result) { |
| 96 | + // If a platform view is successfully created, the result is nil. |
| 97 | + if (result == nil) { |
| 98 | + disposed = true; |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + [platformViewController handleMethodCall:methodCallOnDispose result:resultOnDispose]; |
| 103 | + |
| 104 | + EXPECT_TRUE(created); |
| 105 | + EXPECT_TRUE(disposed); |
| 106 | +} |
| 107 | + |
| 108 | +TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) { |
| 109 | + // Use id so we can access handleMethodCall method. |
| 110 | + id platformViewController = [[FlutterPlatformViewController alloc] init]; |
| 111 | + |
| 112 | + FlutterMethodCall* methodCall = |
| 113 | + [FlutterMethodCall methodCallWithMethodName:@"dispose" |
| 114 | + arguments:[NSNumber numberWithLongLong:20]]; |
| 115 | + |
| 116 | + __block bool errored = false; |
| 117 | + FlutterResult result = ^(id result) { |
| 118 | + if ([result isKindOfClass:[FlutterError class]]) { |
| 119 | + errored = true; |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + [platformViewController handleMethodCall:methodCall result:result]; |
| 124 | + |
| 125 | + EXPECT_TRUE(errored); |
| 126 | +} |
| 127 | + |
| 128 | +} // flutter::testing |
0 commit comments