Title: [237820] trunk
Revision
237820
Author
[email protected]
Date
2018-11-05 11:18:05 -0800 (Mon, 05 Nov 2018)

Log Message

[iOS] Allow WKWebView clients to customize bar button item groups in the accessory view when editing
https://bugs.webkit.org/show_bug.cgi?id=191240
<rdar://problem/44872806>

Reviewed by Tim Horton.

Source/WebKit:

Allow WKWebView clients to override WKContentView's default input assistant item by plumbing the call to
`-inputAssistantItem` through `-[WKWebView inputAssistantItem]`, such that if a client asks WKWebView for its
input assistant item and changes its leading or trailing bar button groups, the changes will be reflected by
WKContentView; additionally, this allows clients to achieve the same effect by overriding `-inputAssistantItem`
on WKWebView.

Test:   KeyboardInputTests.ModifyInputAssistantItemBarButtonGroups
        KeyboardInputTests.OverrideInputAssistantItemBarButtonGroups

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView inputAssistantItem]):
* UIProcess/ios/WKContentViewInteraction.h:
* UIProcess/ios/WKContentViewInteraction.mm:
(-[WKContentView inputAssistantItem]):

Route the call to `[super inputAssistantItem]` through `-[WKWebView inputAssistantItem]`, so that the input
assistant item may be overridden via WKWebView.

(-[WKContentView inputAssistantItemForWebView]):

Tools:

Add new API tests to verify that setting leading and trailing bar button groups on WKWebView sets the first
responder's (i.e. WKContentView's) leading and trailing bar button groups, and that this can also be achieved by
overriding `-[WKWebView inputAssistantItem]`.

* TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm:
(-[InputAssistantItemTestingWebView fakeLeadingBarButtonItemAction]):
(-[InputAssistantItemTestingWebView fakeTrailingBarButtonItemAction]):
(+[InputAssistantItemTestingWebView barButtonIcon]):
(+[InputAssistantItemTestingWebView leadingItemsForWebView:]):
(+[InputAssistantItemTestingWebView trailingItemsForWebView:]):
(-[InputAssistantItemTestingWebView inputAssistantItem]):
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (237819 => 237820)


--- trunk/Source/WebKit/ChangeLog	2018-11-05 19:15:14 UTC (rev 237819)
+++ trunk/Source/WebKit/ChangeLog	2018-11-05 19:18:05 UTC (rev 237820)
@@ -1,3 +1,31 @@
+2018-11-05  Wenson Hsieh  <[email protected]>
+
+        [iOS] Allow WKWebView clients to customize bar button item groups in the accessory view when editing
+        https://bugs.webkit.org/show_bug.cgi?id=191240
+        <rdar://problem/44872806>
+
+        Reviewed by Tim Horton.
+
+        Allow WKWebView clients to override WKContentView's default input assistant item by plumbing the call to
+        `-inputAssistantItem` through `-[WKWebView inputAssistantItem]`, such that if a client asks WKWebView for its
+        input assistant item and changes its leading or trailing bar button groups, the changes will be reflected by
+        WKContentView; additionally, this allows clients to achieve the same effect by overriding `-inputAssistantItem`
+        on WKWebView.
+
+        Test:   KeyboardInputTests.ModifyInputAssistantItemBarButtonGroups
+                KeyboardInputTests.OverrideInputAssistantItemBarButtonGroups
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView inputAssistantItem]):
+        * UIProcess/ios/WKContentViewInteraction.h:
+        * UIProcess/ios/WKContentViewInteraction.mm:
+        (-[WKContentView inputAssistantItem]):
+
+        Route the call to `[super inputAssistantItem]` through `-[WKWebView inputAssistantItem]`, so that the input
+        assistant item may be overridden via WKWebView.
+
+        (-[WKContentView inputAssistantItemForWebView]):
+
 2018-11-05  Ryan Haddad  <[email protected]>
 
         Unreviewed, rolling out r237785.

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm (237819 => 237820)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2018-11-05 19:15:14 UTC (rev 237819)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm	2018-11-05 19:18:05 UTC (rev 237820)
@@ -4531,6 +4531,11 @@
 {
 }
 
+- (UITextInputAssistantItem *)inputAssistantItem
+{
+    return [_contentView inputAssistantItemForWebView];
+}
+
 #endif
 
 - (NSData *)_sessionStateData

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h (237819 => 237820)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h	2018-11-05 19:15:14 UTC (rev 237819)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.h	2018-11-05 19:18:05 UTC (rev 237820)
@@ -317,6 +317,7 @@
 @property (nonatomic, readonly) const WebKit::AssistedNodeInformation& assistedNodeInformation;
 @property (nonatomic, readonly) UIWebFormAccessory *formAccessoryView;
 @property (nonatomic) BOOL suppressAssistantSelectionView;
+@property (nonatomic, readonly) UITextInputAssistantItem *inputAssistantItemForWebView;
 
 #if ENABLE(DATALIST_ELEMENT)
 @property (nonatomic, strong) UIView <WKFormControl> *dataListTextSuggestionsInputView;

Modified: trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm (237819 => 237820)


--- trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2018-11-05 19:15:14 UTC (rev 237819)
+++ trunk/Source/WebKit/UIProcess/ios/WKContentViewInteraction.mm	2018-11-05 19:18:05 UTC (rev 237820)
@@ -2139,6 +2139,16 @@
     }
 }
 
+- (UITextInputAssistantItem *)inputAssistantItem
+{
+    return [_webView inputAssistantItem];
+}
+
+- (UITextInputAssistantItem *)inputAssistantItemForWebView
+{
+    return [super inputAssistantItem];
+}
+
 - (void)_ensureFormAccessoryView
 {
     if (_formAccessoryView)

Modified: trunk/Tools/ChangeLog (237819 => 237820)


--- trunk/Tools/ChangeLog	2018-11-05 19:15:14 UTC (rev 237819)
+++ trunk/Tools/ChangeLog	2018-11-05 19:18:05 UTC (rev 237820)
@@ -1,5 +1,26 @@
 2018-11-05  Wenson Hsieh  <[email protected]>
 
+        [iOS] Allow WKWebView clients to customize bar button item groups in the accessory view when editing
+        https://bugs.webkit.org/show_bug.cgi?id=191240
+        <rdar://problem/44872806>
+
+        Reviewed by Tim Horton.
+
+        Add new API tests to verify that setting leading and trailing bar button groups on WKWebView sets the first
+        responder's (i.e. WKContentView's) leading and trailing bar button groups, and that this can also be achieved by
+        overriding `-[WKWebView inputAssistantItem]`.
+
+        * TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm:
+        (-[InputAssistantItemTestingWebView fakeLeadingBarButtonItemAction]):
+        (-[InputAssistantItemTestingWebView fakeTrailingBarButtonItemAction]):
+        (+[InputAssistantItemTestingWebView barButtonIcon]):
+        (+[InputAssistantItemTestingWebView leadingItemsForWebView:]):
+        (+[InputAssistantItemTestingWebView trailingItemsForWebView:]):
+        (-[InputAssistantItemTestingWebView inputAssistantItem]):
+        (TestWebKitAPI::TEST):
+
+2018-11-05  Wenson Hsieh  <[email protected]>
+
         [iOS] Changing view scale sometimes does not zoom the page to the new initial scale when the page is zoomed in when ignoring meta viewport
         https://bugs.webkit.org/show_bug.cgi?id=191226
         <rdar://problem/45781765>

Modified: trunk/Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm (237819 => 237820)


--- trunk/Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm	2018-11-05 19:15:14 UTC (rev 237819)
+++ trunk/Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm	2018-11-05 19:18:05 UTC (rev 237820)
@@ -35,6 +35,61 @@
 #import <WebKitLegacy/WebEvent.h>
 #import <cmath>
 
+@interface InputAssistantItemTestingWebView : TestWKWebView
++ (UIBarButtonItemGroup *)leadingItemsForWebView:(WKWebView *)webView;
++ (UIBarButtonItemGroup *)trailingItemsForWebView:(WKWebView *)webView;
+@end
+
+@implementation InputAssistantItemTestingWebView {
+    RetainPtr<UIBarButtonItemGroup> _leadingItems;
+    RetainPtr<UIBarButtonItemGroup> _trailingItems;
+}
+
+- (void)fakeLeadingBarButtonItemAction
+{
+}
+
+- (void)fakeTrailingBarButtonItemAction
+{
+}
+
++ (UIImage *)barButtonIcon
+{
+    return [UIImage imageNamed:@"TestWebKitAPI.resources/icon.png"];
+}
+
++ (UIBarButtonItemGroup *)leadingItemsForWebView:(WKWebView *)webView
+{
+    static dispatch_once_t onceToken;
+    static UIBarButtonItemGroup *sharedItems;
+    dispatch_once(&onceToken, ^{
+        auto leadingItem = adoptNS([[UIBarButtonItem alloc] initWithImage:self.barButtonIcon style:UIBarButtonItemStylePlain target:webView action:@selector(fakeLeadingBarButtonItemAction)]);
+        sharedItems = [[UIBarButtonItemGroup alloc] initWithBarButtonItems:@[ leadingItem.get() ] representativeItem:nil];
+    });
+    return sharedItems;
+}
+
++ (UIBarButtonItemGroup *)trailingItemsForWebView:(WKWebView *)webView
+{
+    static dispatch_once_t onceToken;
+    static UIBarButtonItemGroup *sharedItems;
+    dispatch_once(&onceToken, ^{
+        auto trailingItem = adoptNS([[UIBarButtonItem alloc] initWithImage:self.barButtonIcon style:UIBarButtonItemStylePlain target:webView action:@selector(fakeTrailingBarButtonItemAction)]);
+        sharedItems = [[UIBarButtonItemGroup alloc] initWithBarButtonItems:@[ trailingItem.get() ] representativeItem:nil];
+    });
+    return sharedItems;
+}
+
+- (UITextInputAssistantItem *)inputAssistantItem
+{
+    auto assistantItem = adoptNS([[UITextInputAssistantItem alloc] init]);
+    [assistantItem setLeadingBarButtonGroups:@[[InputAssistantItemTestingWebView leadingItemsForWebView:self]]];
+    [assistantItem setTrailingBarButtonGroups:@[[InputAssistantItemTestingWebView trailingItemsForWebView:self]]];
+    return assistantItem.autorelease();
+}
+
+@end
+
 @implementation TestWKWebView (KeyboardInputTests)
 
 static CGRect rounded(CGRect rect)
@@ -112,6 +167,77 @@
 
 namespace TestWebKitAPI {
 
+TEST(KeyboardInputTests, ModifyInputAssistantItemBarButtonGroups)
+{
+    auto inputDelegate = adoptNS([TestInputDelegate new]);
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
+    [webView _setInputDelegate:inputDelegate.get()];
+    [webView synchronouslyLoadHTMLString:@"<body contenteditable>"];
+    UITextInputAssistantItem *item = [webView inputAssistantItem];
+    UIBarButtonItemGroup *leadingItems = [InputAssistantItemTestingWebView leadingItemsForWebView:webView.get()];
+    UIBarButtonItemGroup *trailingItems = [InputAssistantItemTestingWebView trailingItemsForWebView:webView.get()];
+    item.leadingBarButtonGroups = @[ leadingItems ];
+    item.trailingBarButtonGroups = @[ trailingItems ];
+
+    bool doneWaitingForInputSession = false;
+    [inputDelegate setFocusStartsInputSessionPolicyHandler:[&] (WKWebView *, id <_WKFocusedElementInfo>) -> _WKFocusStartsInputSessionPolicy {
+        doneWaitingForInputSession = true;
+        return _WKFocusStartsInputSessionPolicyAllow;
+    }];
+    [webView evaluateJavaScript:@"document.body.focus()" completionHandler:nil];
+    TestWebKitAPI::Util::run(&doneWaitingForInputSession);
+
+    EXPECT_EQ([webView firstResponder], [webView textInputContentView]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.leadingBarButtonGroups containsObject:leadingItems]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.trailingBarButtonGroups containsObject:trailingItems]);
+
+    // Now blur and refocus the editable area, and check that the same leading and trailing button items are present.
+    [webView resignFirstResponder];
+    [webView waitForNextPresentationUpdate];
+
+    doneWaitingForInputSession = false;
+    [webView evaluateJavaScript:@"document.body.focus()" completionHandler:nil];
+    TestWebKitAPI::Util::run(&doneWaitingForInputSession);
+
+    EXPECT_EQ([webView firstResponder], [webView textInputContentView]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.leadingBarButtonGroups containsObject:leadingItems]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.trailingBarButtonGroups containsObject:trailingItems]);
+}
+
+TEST(KeyboardInputTests, OverrideInputAssistantItemBarButtonGroups)
+{
+    auto inputDelegate = adoptNS([TestInputDelegate new]);
+    auto webView = adoptNS([[InputAssistantItemTestingWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
+    [webView _setInputDelegate:inputDelegate.get()];
+    [webView synchronouslyLoadHTMLString:@"<body contenteditable>"];
+
+    bool doneWaitingForInputSession = false;
+    [inputDelegate setFocusStartsInputSessionPolicyHandler:[&] (WKWebView *, id <_WKFocusedElementInfo>) -> _WKFocusStartsInputSessionPolicy {
+        doneWaitingForInputSession = true;
+        return _WKFocusStartsInputSessionPolicyAllow;
+    }];
+    [webView evaluateJavaScript:@"document.body.focus()" completionHandler:nil];
+    TestWebKitAPI::Util::run(&doneWaitingForInputSession);
+
+    UIBarButtonItemGroup *leadingItems = [InputAssistantItemTestingWebView leadingItemsForWebView:webView.get()];
+    UIBarButtonItemGroup *trailingItems = [InputAssistantItemTestingWebView trailingItemsForWebView:webView.get()];
+    EXPECT_EQ([webView firstResponder], [webView textInputContentView]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.leadingBarButtonGroups containsObject:leadingItems]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.trailingBarButtonGroups containsObject:trailingItems]);
+
+    // Now blur and refocus the editable area, and check that the same leading and trailing button items are present.
+    [webView resignFirstResponder];
+    [webView waitForNextPresentationUpdate];
+
+    doneWaitingForInputSession = false;
+    [webView evaluateJavaScript:@"document.body.focus()" completionHandler:nil];
+    TestWebKitAPI::Util::run(&doneWaitingForInputSession);
+
+    EXPECT_EQ([webView firstResponder], [webView textInputContentView]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.leadingBarButtonGroups containsObject:leadingItems]);
+    EXPECT_TRUE([[webView firstResponder].inputAssistantItem.trailingBarButtonGroups containsObject:trailingItems]);
+}
+
 TEST(KeyboardInputTests, CustomInputViewAndInputAccessoryView)
 {
     auto inputView = adoptNS([[UIView alloc] init]);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to