|
| 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/FlutterViewController_Internal.h" |
| 6 | + |
| 7 | +#include "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h" |
| 8 | +#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h" |
| 9 | +#include "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h" |
| 10 | +#include "flutter/testing/testing.h" |
| 11 | +#import "third_party/ocmock/Source/OCMock/OCMock.h" |
| 12 | + |
| 13 | +namespace flutter::testing { |
| 14 | + |
| 15 | +// Returns a mock FlutterViewController that is able to work in environments |
| 16 | +// without a real pasteboard. |
| 17 | +id mockViewController(NSString* pasteboardString) { |
| 18 | + NSString* fixtures = @(testing::GetFixturesPath()); |
| 19 | + FlutterDartProject* project = [[FlutterDartProject alloc] |
| 20 | + initWithAssetsPath:fixtures |
| 21 | + ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; |
| 22 | + FlutterViewController* viewController = [[FlutterViewController alloc] initWithProject:project]; |
| 23 | + |
| 24 | + // Mock pasteboard so that this test will work in environments without a |
| 25 | + // real pasteboard. |
| 26 | + id pasteboardMock = OCMClassMock([NSPasteboard class]); |
| 27 | + OCMExpect([pasteboardMock stringForType:[OCMArg any]]).andDo(^(NSInvocation* invocation) { |
| 28 | + NSString* returnValue = pasteboardString.length > 0 ? pasteboardString : nil; |
| 29 | + [invocation setReturnValue:&returnValue]; |
| 30 | + }); |
| 31 | + id viewControllerMock = OCMPartialMock(viewController); |
| 32 | + OCMStub([viewControllerMock pasteboard]).andReturn(pasteboardMock); |
| 33 | + return viewControllerMock; |
| 34 | +} |
| 35 | + |
| 36 | +TEST(FlutterViewControllerTest, HasStringsWhenPasteboardEmpty) { |
| 37 | + // Mock FlutterViewController so that it behaves like the pasteboard is empty. |
| 38 | + id viewControllerMock = mockViewController(nil); |
| 39 | + |
| 40 | + // Call hasStrings and expect it to be false. |
| 41 | + __block bool calledAfterClear = false; |
| 42 | + __block bool valueAfterClear; |
| 43 | + FlutterResult resultAfterClear = ^(id result) { |
| 44 | + calledAfterClear = true; |
| 45 | + NSNumber* valueNumber = [result valueForKey:@"value"]; |
| 46 | + valueAfterClear = [valueNumber boolValue]; |
| 47 | + }; |
| 48 | + FlutterMethodCall* methodCallAfterClear = |
| 49 | + [FlutterMethodCall methodCallWithMethodName:@"Clipboard.hasStrings" arguments:nil]; |
| 50 | + [viewControllerMock handleMethodCall:methodCallAfterClear result:resultAfterClear]; |
| 51 | + ASSERT_TRUE(calledAfterClear); |
| 52 | + ASSERT_FALSE(valueAfterClear); |
| 53 | +} |
| 54 | + |
| 55 | +TEST(FlutterViewControllerTest, HasStringsWhenPasteboardFull) { |
| 56 | + // Mock FlutterViewController so that it behaves like the pasteboard has a |
| 57 | + // valid string. |
| 58 | + id viewControllerMock = mockViewController(@"some string"); |
| 59 | + |
| 60 | + // Call hasStrings and expect it to be true. |
| 61 | + __block bool called = false; |
| 62 | + __block bool value; |
| 63 | + FlutterResult result = ^(id result) { |
| 64 | + called = true; |
| 65 | + NSNumber* valueNumber = [result valueForKey:@"value"]; |
| 66 | + value = [valueNumber boolValue]; |
| 67 | + }; |
| 68 | + FlutterMethodCall* methodCall = |
| 69 | + [FlutterMethodCall methodCallWithMethodName:@"Clipboard.hasStrings" arguments:nil]; |
| 70 | + [viewControllerMock handleMethodCall:methodCall result:result]; |
| 71 | + ASSERT_TRUE(called); |
| 72 | + ASSERT_TRUE(value); |
| 73 | +} |
| 74 | + |
| 75 | +} // flutter::testing |
0 commit comments