Skip to content

Commit

Permalink
Adding tests for FlutterPlatformViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardJCai committed Dec 8, 2020
1 parent ac08def commit 8fbb288
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,9 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouse
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm
Expand Down
3 changes: 3 additions & 0 deletions shell/platform/darwin/macos/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ executable("flutter_desktop_darwin_unittests") {
sources = [
"framework/Source/FlutterEngineTest.mm",
"framework/Source/FlutterGLCompositorUnittests.mm",
"framework/Source/FlutterPlatformViewControllerTest.mm",
"framework/Source/FlutterPlatformViewMock.h",
"framework/Source/FlutterPlatformViewMock.mm",
"framework/Source/FlutterViewControllerTest.mm",
"framework/Source/FlutterViewControllerTestUtils.h",
"framework/Source/FlutterViewControllerTestUtils.mm",
Expand Down
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
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
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

0 comments on commit 8fbb288

Please sign in to comment.