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

Add Search Web to selection controls on iOS #43324

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

constexpr char kTextPlainFormat[] = "text/plain";
const UInt32 kKeyPressClickSoundId = 1306;
const NSString* searchURLPrefix = @"x-web-search://?";

} // namespace

Expand Down Expand Up @@ -115,6 +116,9 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result([self clipboardHasStrings]);
} else if ([method isEqualToString:@"LiveText.isLiveTextInputAvailable"]) {
result(@([self isLiveTextInputAvailable]));
} else if ([method isEqualToString:@"SearchWeb.invoke"]) {
[self searchWeb:args];
result(nil);
} else if ([method isEqualToString:@"LookUp.invoke"]) {
[self showLookUpViewController:args];
result(nil);
Expand All @@ -123,6 +127,17 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}
}

- (void)searchWeb:(NSString*)searchTerm {
NSString* escapedText = [searchTerm
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet
URLHostAllowedCharacterSet]];
NSString* searchURL = [NSString stringWithFormat:@"%@%@", searchURLPrefix, escapedText];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:searchURL]
options:@{}
completionHandler:nil];
}

- (void)playSystemSound:(NSString*)soundType {
if ([soundType isEqualToString:@"SystemSoundType.click"]) {
// All feedback types are specific to Android and are treated as equal on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ @interface FlutterPlatformPluginTest : XCTestCase

@interface FlutterPlatformPlugin ()
- (BOOL)isLiveTextInputAvailable;
- (void)searchWeb:(NSString*)searchTerm;
- (void)showLookUpViewController:(NSString*)term;
@end

Expand All @@ -27,6 +28,30 @@ - (void)presentViewController:(UIViewController*)viewControllerToPresent
@end

@implementation FlutterPlatformPluginTest
- (void)testSearchWebInvoked {
FlutterEngine* engine = [[[FlutterEngine alloc] initWithName:@"test" project:nil] autorelease];
std::unique_ptr<fml::WeakPtrFactory<FlutterEngine>> _weakFactory =
std::make_unique<fml::WeakPtrFactory<FlutterEngine>>(engine);
[engine runWithEntrypoint:nil];

XCTestExpectation* invokeExpectation =
[self expectationWithDescription:@"Web search launched with search term"];

FlutterPlatformPlugin* plugin =
[[[FlutterPlatformPlugin alloc] initWithEngine:_weakFactory->GetWeakPtr()] autorelease];
FlutterPlatformPlugin* mockPlugin = OCMPartialMock(plugin);

FlutterMethodCall* methodCall = [FlutterMethodCall methodCallWithMethodName:@"SearchWeb.invoke"
arguments:@"Test"];
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Maybe you could also try testing that a string with some characters that need to be escaped get escaped? If you think it's useful.


FlutterResult result = ^(id result) {
OCMVerify([mockPlugin searchWeb:@"Test"]);
[invokeExpectation fulfill];
};

[mockPlugin handleMethodCall:methodCall result:result];
[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)testLookUpCallInitiated {
FlutterEngine* engine = [[[FlutterEngine alloc] initWithName:@"test" project:nil] autorelease];
Expand Down