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