Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 11964ec

Browse files
authored
Clipboard hasStrings method on iOS (#19859)
* Implement Clipboard hasStrings method on iOS * Test call to hasStrings * Formatting fixes * Move test to its own file * Alphabetical order * Update licenses * arguments nil instead of empty dictionary * Guarantee hasStrings will be true when tested * Formatting
1 parent c6fba72 commit 11964ec

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlay
929929
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.mm
930930
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h
931931
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm
932+
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPluginTest.mm
932933
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm
933934
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm
934935
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h

shell/platform/darwin/ios/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ source_set("ios_test_flutter_mrc") {
171171
]
172172
sources = [
173173
"framework/Source/FlutterEnginePlatformViewTest.mm",
174+
"framework/Source/FlutterPlatformPluginTest.mm",
174175
"framework/Source/FlutterPlatformViewsTest.mm",
175176
"framework/Source/accessibility_bridge_test.mm",
176177
]

shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.mm

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
8888
} else if ([method isEqualToString:@"Clipboard.setData"]) {
8989
[self setClipboardData:args];
9090
result(nil);
91+
} else if ([method isEqualToString:@"Clipboard.hasStrings"]) {
92+
result([self clipboardHasStrings]);
9193
} else {
9294
result(FlutterMethodNotImplemented);
9395
}
@@ -248,4 +250,16 @@ - (void)setClipboardData:(NSDictionary*)data {
248250
}
249251
}
250252

253+
- (NSDictionary*)clipboardHasStrings {
254+
bool hasStrings = false;
255+
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
256+
if (@available(iOS 10, *)) {
257+
hasStrings = pasteboard.hasStrings;
258+
} else {
259+
NSString* stringInPasteboard = pasteboard.string;
260+
hasStrings = stringInPasteboard != nil;
261+
}
262+
return @{@"value" : @(hasStrings)};
263+
}
264+
251265
@end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 <XCTest/XCTest.h>
6+
7+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
8+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
9+
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
10+
#import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
11+
#import "third_party/ocmock/Source/OCMock/OCMock.h"
12+
13+
@interface FlutterPlatformPluginTest : XCTestCase
14+
@end
15+
16+
@implementation FlutterPlatformPluginTest
17+
18+
- (void)testHasStrings {
19+
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
20+
std::unique_ptr<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
21+
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(engine);
22+
FlutterPlatformPlugin* plugin =
23+
[[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()];
24+
25+
// Set some string to the pasteboard.
26+
__block bool calledSet = false;
27+
FlutterResult resultSet = ^(id result) {
28+
calledSet = true;
29+
};
30+
FlutterMethodCall* methodCallSet =
31+
[FlutterMethodCall methodCallWithMethodName:@"Clipboard.setClipboardData"
32+
arguments:@{@"text" : @"some string"}];
33+
[plugin handleMethodCall:methodCallSet result:resultSet];
34+
XCTAssertEqual(calledSet, true);
35+
36+
// Call hasStrings and expect it to be true.
37+
__block bool called = false;
38+
__block bool value;
39+
FlutterResult result = ^(id result) {
40+
called = true;
41+
value = result[@"value"];
42+
};
43+
FlutterMethodCall* methodCall =
44+
[FlutterMethodCall methodCallWithMethodName:@"Clipboard.hasStrings" arguments:nil];
45+
[plugin handleMethodCall:methodCall result:result];
46+
47+
XCTAssertEqual(called, true);
48+
XCTAssertEqual(value, true);
49+
}
50+
51+
@end

0 commit comments

Comments
 (0)