diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 8e53d1f41cb29..3ea829761233c 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -925,6 +925,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlay FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h diff --git a/shell/platform/darwin/ios/BUILD.gn b/shell/platform/darwin/ios/BUILD.gn index 737e72731232f..32c3fbf234a36 100644 --- a/shell/platform/darwin/ios/BUILD.gn +++ b/shell/platform/darwin/ios/BUILD.gn @@ -171,6 +171,7 @@ source_set("ios_test_flutter_mrc") { ] sources = [ "framework/Source/FlutterEnginePlatformViewTest.mm", + "framework/Source/FlutterPlatformPluginTest.mm", "framework/Source/FlutterPlatformViewsTest.mm", "framework/Source/accessibility_bridge_test.mm", ] diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm index 5238be44eaef1..87db2313dfb8e 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm @@ -88,6 +88,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { } else if ([method isEqualToString:@"Clipboard.setData"]) { [self setClipboardData:args]; result(nil); + } else if ([method isEqualToString:@"Clipboard.hasStrings"]) { + result([self clipboardHasStrings]); } else { result(FlutterMethodNotImplemented); } @@ -248,4 +250,16 @@ - (void)setClipboardData:(NSDictionary*)data { } } +- (NSDictionary*)clipboardHasStrings { + bool hasStrings = false; + UIPasteboard* pasteboard = [UIPasteboard generalPasteboard]; + if (@available(iOS 10, *)) { + hasStrings = pasteboard.hasStrings; + } else { + NSString* stringInPasteboard = pasteboard.string; + hasStrings = stringInPasteboard != nil; + } + return @{@"value" : @(hasStrings)}; +} + @end diff --git a/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm new file mode 100644 index 0000000000000..01f3ca4e611d4 --- /dev/null +++ b/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm @@ -0,0 +1,51 @@ +// 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 + +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h" +#import "flutter/shell/platform/darwin/ios/platform_view_ios.h" +#import "third_party/ocmock/Source/OCMock/OCMock.h" + +@interface FlutterPlatformPluginTest : XCTestCase +@end + +@implementation FlutterPlatformPluginTest + +- (void)testHasStrings { + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil]; + std::unique_ptr> _weakFactory = + std::make_unique>(engine); + FlutterPlatformPlugin* plugin = + [[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()]; + + // Set some string to the pasteboard. + __block bool calledSet = false; + FlutterResult resultSet = ^(id result) { + calledSet = true; + }; + FlutterMethodCall* methodCallSet = + [FlutterMethodCall methodCallWithMethodName:@"Clipboard.setClipboardData" + arguments:@{@"text" : @"some string"}]; + [plugin handleMethodCall:methodCallSet result:resultSet]; + XCTAssertEqual(calledSet, true); + + // Call hasStrings and expect it to be true. + __block bool called = false; + __block bool value; + FlutterResult result = ^(id result) { + called = true; + value = result[@"value"]; + }; + FlutterMethodCall* methodCall = + [FlutterMethodCall methodCallWithMethodName:@"Clipboard.hasStrings" arguments:nil]; + [plugin handleMethodCall:methodCall result:result]; + + XCTAssertEqual(called, true); + XCTAssertEqual(value, true); +} + +@end