Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 87de964

Browse files
committed
Adding tests for FlutterPlatformViewController
1 parent d954488 commit 87de964

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,9 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouse
10741074
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
10751075
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h
10761076
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm
1077+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm
1078+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h
1079+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.mm
10771080
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h
10781081
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
10791082
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm

shell/platform/darwin/macos/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ executable("flutter_desktop_darwin_unittests") {
130130
sources = [
131131
"framework/Source/FlutterEngineTest.mm",
132132
"framework/Source/FlutterGLCompositorUnittests.mm",
133+
"framework/Source/FlutterPlatformViewControllerTest.mm",
134+
"framework/Source/FlutterPlatformViewMock.h",
135+
"framework/Source/FlutterPlatformViewMock.mm",
133136
"framework/Source/FlutterViewControllerTest.mm",
134137
"framework/Source/FlutterViewControllerTestUtils.h",
135138
"framework/Source/FlutterViewControllerTestUtils.mm",
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 <Foundation/Foundation.h>
6+
#import <Foundation/NSObject.h>
7+
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h"
9+
10+
@interface FlutterPlatformViewMock : NSObject <FlutterPlatformView>
11+
@property(nonatomic, strong) NSView* view;
12+
@end
13+
14+
@interface MockPlatformView : NSView
15+
@end
16+
17+
@interface FlutterPlatformViewMockFactory : NSObject <FlutterPlatformViewFactory>
18+
@end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 <Cocoa/Cocoa.h>
6+
#import <Foundation/Foundation.h>
7+
8+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h"
9+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h"
10+
11+
@implementation MockPlatformView
12+
13+
- (instancetype)initWithFrame:(CGRect)frame {
14+
self = [super initWithFrame:frame];
15+
return self;
16+
}
17+
18+
@end
19+
20+
@implementation FlutterPlatformViewMock
21+
22+
- (instancetype)initWithFrame:(CGRect)frame arguments:(id _Nullable)args {
23+
if (self = [super init]) {
24+
_view = [[MockPlatformView alloc] initWithFrame:frame];
25+
}
26+
return self;
27+
}
28+
29+
@end
30+
31+
@implementation FlutterPlatformViewMockFactory
32+
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
33+
viewIdentifier:(int64_t)viewId
34+
arguments:(id _Nullable)args {
35+
return [[FlutterPlatformViewMock alloc] initWithFrame:frame arguments:args];
36+
}
37+
38+
- (NSObject<FlutterMessageCodec>*)createArgsCodec {
39+
return [FlutterStandardMessageCodec sharedInstance];
40+
}
41+
42+
@end

0 commit comments

Comments
 (0)