Skip to content

Commit

Permalink
Adding tests for ViewController platform view support.
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardJCai committed Dec 7, 2020
1 parent 330c99e commit 444f71e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 1 deletion.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSur
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.mm
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.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
2 changes: 2 additions & 0 deletions shell/platform/darwin/macos/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ executable("flutter_desktop_darwin_unittests") {
sources = [
"framework/Source/FlutterEngineTest.mm",
"framework/Source/FlutterGLCompositorUnittests.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,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
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ - (void)onCreate:(nonnull FlutterMethodCall*)call result:(nonnull FlutterResult)
- (void)onDispose:(nonnull FlutterMethodCall*)call result:(nonnull FlutterResult)result {
NSNumber* arg = [call arguments];
int64_t viewId = [arg longLongValue];
NSLog(@"onDispose ViewId: %lld", viewId);

if (_platformViews.count(viewId) == 0) {
result([FlutterError errorWithCode:@"unknown_view"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

#import <OCMock/OCMock.h>

#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewMock.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewControllerTestUtils.h"

#include "flutter/testing/testing.h"

namespace flutter::testing {
Expand Down Expand Up @@ -53,4 +57,138 @@
EXPECT_TRUE(value);
}

TEST(FlutterViewController, TestCreatePlatformViewNoMatchingViewType) {
NSString* fixtures = @(testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];

// Use id so we can access handleMethodCall method.
id viewController = [[FlutterViewController alloc] initWithProject:project];

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;
}
};

[viewController handleMethodCall:methodCall result:result];

// We expect the call to error since no factories are registered.
EXPECT_TRUE(errored);
}

TEST(FlutterViewController, TestRegisterPlatformViewFactoryAndCreate) {
NSString* fixtures = @(testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];

// Use id so we can access handleMethodCall method.
id viewController = [[FlutterViewController alloc] initWithProject:project];

FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc];

[viewController 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;
}
};
[viewController handleMethodCall:methodCall result:result];

EXPECT_TRUE(success);
}

TEST(FlutterViewController, TestCreateAndDispose) {
NSString* fixtures = @(testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];

// Use id so we can access handleMethodCall method.
id viewController = [[FlutterViewController alloc] initWithProject:project];

FlutterPlatformViewMockFactory* factory = [FlutterPlatformViewMockFactory alloc];

[viewController 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;
}
};

[viewController 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;
}
};

[viewController handleMethodCall:methodCallOnDispose result:resultOnDispose];

EXPECT_TRUE(created);
EXPECT_TRUE(disposed);
}

TEST(FlutterViewController, TestDisposeOnMissingViewId) {
NSString* fixtures = @(testing::GetFixturesPath());
FlutterDartProject* project = [[FlutterDartProject alloc]
initWithAssetsPath:fixtures
ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]];

// Use id so we can access handleMethodCall method.
id viewController = [[FlutterViewController alloc] initWithProject:project];

FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"dispose"
arguments:[NSNumber numberWithLongLong:20]];

__block bool errored = false;
FlutterResult result = ^(id result) {
if ([result isKindOfClass:[FlutterError class]]) {
errored = true;
}
};

[viewController handleMethodCall:methodCall result:result];

EXPECT_TRUE(errored);
}

} // flutter::testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>

#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViews.h"

@interface MockFlutterPlatformView : NSObject <FlutterPlatformView>
@property(nonatomic, strong) NSView* view;
@end

@interface MockPlatformView : NSTextView
@end

@interface MockFlutterPlatformFactory : NSObject <FlutterPlatformViewFactory>
@end

0 comments on commit 444f71e

Please sign in to comment.