Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix set clipboardData to null
  • Loading branch information
Chris Yang committed Apr 4, 2022
commit 85f5de65fa0d4e43e0468fe1755ca6605389b6ec
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else if ([method isEqualToString:@"Clipboard.getData"]) {
result([self getClipboardData:args]);
} else if ([method isEqualToString:@"Clipboard.setData"]) {
NSLog(@"Clipboard.setData called");
[self setClipboardData:args];
result(nil);
} else if ([method isEqualToString:@"Clipboard.hasStrings"]) {
Expand Down Expand Up @@ -267,8 +268,9 @@ - (NSDictionary*)getClipboardData:(NSString*)format {

- (void)setClipboardData:(NSDictionary*)data {
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
if (data[@"text"]) {
pasteboard.string = data[@"text"];
id copyText = data[@"text"];
if ([copyText isKindOfClass:NSString.class]) {
pasteboard.string = copyText;
} else {
pasteboard.string = @"null";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ @interface FlutterPlatformPluginTest : XCTestCase

@implementation FlutterPlatformPluginTest

- (void)testHasStrings {
- (void)testClipboardHasCorrectStrings {
[UIPasteboard generalPasteboard].string = nil;
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
std::unique_ptr<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(engine);
Expand All @@ -29,7 +30,7 @@ - (void)testHasStrings {
calledSet = true;
};
FlutterMethodCall* methodCallSet =
[FlutterMethodCall methodCallWithMethodName:@"Clipboard.setClipboardData"
[FlutterMethodCall methodCallWithMethodName:@"Clipboard.setData"
arguments:@{@"text" : @"some string"}];
[plugin handleMethodCall:methodCallSet result:resultSet];
XCTAssertEqual(calledSet, true);
Expand All @@ -39,14 +40,56 @@ - (void)testHasStrings {
__block bool value;
FlutterResult result = ^(id result) {
called = true;
value = result[@"value"];
value = [result[@"value"] boolValue];
};
Copy link
Member

@jmagman jmagman Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The __block pattern that was here and you copied is kind of hard to read imo since the assertion about the value is after the method call, how about:

  XCTestExpectation *hasStringsExpectation = [self expectationWithDescription:@"hasStrings"];
  FlutterResult result = ^(id result) {
    XCTAssertTrue(result[@"value"]);
    [hasStringsExpectation fulfill];
  };
...
  [self waitForExpectationsWithTimeout:1 handler:nil];

At the least these should be BOOL and not bool and XCTAssertTrue(called);

FlutterMethodCall* methodCall =
[FlutterMethodCall methodCallWithMethodName:@"Clipboard.hasStrings" arguments:nil];
[plugin handleMethodCall:methodCall result:result];

XCTAssertEqual(called, true);
XCTAssertEqual(value, true);

// Call getData and expect it to be "null"
__block NSString* text;
FlutterResult getDataResult = ^(id result) {
text = result[@"text"];
};
FlutterMethodCall* methodCallGetData =
[FlutterMethodCall methodCallWithMethodName:@"Clipboard.getData" arguments:@"text/plain"];
[plugin handleMethodCall:methodCallGetData result:getDataResult];

XCTAssertEqualObjects(text, @"some string");
}

- (void)testClipboardSetDataToNullDoNotCrash {
[UIPasteboard generalPasteboard].string = nil;
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
std::unique_ptr<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(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.setData"
arguments:@{@"text" : [NSNull null]}];
[plugin handleMethodCall:methodCallSet result:resultSet];
XCTAssertEqual(calledSet, true);

// Call getData and expect it to be "null"
__block NSString* value;
FlutterResult result = ^(id result) {
value = result[@"text"];
};
FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"Clipboard.getData"
arguments:@"text/plain"];
[plugin handleMethodCall:methodCall result:result];

XCTAssertEqualObjects(value, @"null");
}

- (void)testPopSystemNavigator {
Expand Down