-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding tests for FlutterPlatformViewController
- Loading branch information
1 parent
ac08def
commit 8fbb288
Showing
5 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController_Internal.h" | ||
|
||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" | ||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h" | ||
|
||
#include "flutter/testing/testing.h" | ||
|
||
namespace flutter::testing { | ||
|
||
TEST(FlutterPlatformViewController, TestCreatePlatformViewNoMatchingViewType) { | ||
// Use id so we can access handleMethodCall method. | ||
id platformViewController = [[FlutterPlatformViewController alloc] init]; | ||
|
||
FlutterMethodCall* methodCall = | ||
[FlutterMethodCall methodCallWithMethodName:@"create" | ||
arguments:@{ | ||
@"id" : @2, | ||
@"viewType" : @"FlutterPlatformViewMock" | ||
}]; | ||
|
||
__block bool errored = false; | ||
FlutterResult result = ^(id result) { | ||
if ([result isKindOfClass:[FlutterError class]]) { | ||
errored = true; | ||
} | ||
}; | ||
|
||
[platformViewController handleMethodCall:methodCall result:result]; | ||
|
||
// We expect the call to error since no factories are registered. | ||
EXPECT_TRUE(errored); | ||
} | ||
|
||
TEST(FlutterPlatformViewController, TestRegisterPlatformViewFactoryAndCreate) { | ||
// Use id so we can access handleMethodCall method. | ||
id platformViewController = [[FlutterPlatformViewController alloc] init]; | ||
|
||
FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc]; | ||
|
||
[platformViewController registerViewFactory:factory withId:@"MockPlatformView"]; | ||
|
||
FlutterMethodCall* methodCall = | ||
[FlutterMethodCall methodCallWithMethodName:@"create" | ||
arguments:@{ | ||
@"id" : @2, | ||
@"viewType" : @"MockPlatformView" | ||
}]; | ||
|
||
__block bool success = false; | ||
FlutterResult result = ^(id result) { | ||
// If a platform view is successfully created, the result is nil. | ||
if (result == nil) { | ||
success = true; | ||
} | ||
}; | ||
[platformViewController handleMethodCall:methodCall result:result]; | ||
|
||
EXPECT_TRUE(success); | ||
} | ||
|
||
TEST(FlutterPlatformViewController, TestCreateAndDispose) { | ||
// Use id so we can access handleMethodCall method. | ||
id platformViewController = [[FlutterPlatformViewController alloc] init]; | ||
|
||
FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc]; | ||
|
||
[platformViewController registerViewFactory:factory withId:@"MockPlatformView"]; | ||
|
||
FlutterMethodCall* methodCallOnCreate = | ||
[FlutterMethodCall methodCallWithMethodName:@"create" | ||
arguments:@{ | ||
@"id" : @2, | ||
@"viewType" : @"MockPlatformView" | ||
}]; | ||
|
||
__block bool created = false; | ||
FlutterResult resultOnCreate = ^(id result) { | ||
// If a platform view is successfully created, the result is nil. | ||
if (result == nil) { | ||
created = true; | ||
} | ||
}; | ||
|
||
[platformViewController handleMethodCall:methodCallOnCreate result:resultOnCreate]; | ||
|
||
FlutterMethodCall* methodCallOnDispose = | ||
[FlutterMethodCall methodCallWithMethodName:@"dispose" | ||
arguments:[NSNumber numberWithLongLong:2]]; | ||
|
||
__block bool disposed = false; | ||
FlutterResult resultOnDispose = ^(id result) { | ||
// If a platform view is successfully created, the result is nil. | ||
if (result == nil) { | ||
disposed = true; | ||
} | ||
}; | ||
|
||
[platformViewController handleMethodCall:methodCallOnDispose result:resultOnDispose]; | ||
|
||
EXPECT_TRUE(created); | ||
EXPECT_TRUE(disposed); | ||
} | ||
|
||
TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) { | ||
// Use id so we can access handleMethodCall method. | ||
id platformViewController = [[FlutterPlatformViewController alloc] init]; | ||
|
||
FlutterMethodCall* methodCall = | ||
[FlutterMethodCall methodCallWithMethodName:@"dispose" | ||
arguments:[NSNumber numberWithLongLong:20]]; | ||
|
||
__block bool errored = false; | ||
FlutterResult result = ^(id result) { | ||
if ([result isKindOfClass:[FlutterError class]]) { | ||
errored = true; | ||
} | ||
}; | ||
|
||
[platformViewController handleMethodCall:methodCall result:result]; | ||
|
||
EXPECT_TRUE(errored); | ||
} | ||
|
||
} // flutter::testing |
18 changes: 18 additions & 0 deletions
18
shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <Foundation/NSObject.h> | ||
|
||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h" | ||
|
||
@interface FlutterPlatformViewMock : NSObject <FlutterPlatformView> | ||
@property(nonatomic, strong) NSView* view; | ||
@end | ||
|
||
@interface MockPlatformView : NSView | ||
@end | ||
|
||
@interface FlutterPlatformViewMockFactory : NSObject <FlutterPlatformViewFactory> | ||
@end |
42 changes: 42 additions & 0 deletions
42
shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Cocoa/Cocoa.h> | ||
#import <Foundation/Foundation.h> | ||
|
||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h" | ||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h" | ||
|
||
@implementation MockPlatformView | ||
|
||
- (instancetype)initWithFrame:(CGRect)frame { | ||
self = [super initWithFrame:frame]; | ||
return self; | ||
} | ||
|
||
@end | ||
|
||
@implementation FlutterPlatformViewMock | ||
|
||
- (instancetype)initWithFrame:(CGRect)frame arguments:(id _Nullable)args { | ||
if (self = [super init]) { | ||
_view = [[MockPlatformView alloc] initWithFrame:frame]; | ||
} | ||
return self; | ||
} | ||
|
||
@end | ||
|
||
@implementation FlutterPlatformViewMockFactory | ||
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame | ||
viewIdentifier:(int64_t)viewId | ||
arguments:(id _Nullable)args { | ||
return [[FlutterPlatformViewMock alloc] initWithFrame:frame arguments:args]; | ||
} | ||
|
||
- (NSObject<FlutterMessageCodec>*)createArgsCodec { | ||
return [FlutterStandardMessageCodec sharedInstance]; | ||
} | ||
|
||
@end |