Skip to content

Commit

Permalink
TestWebKitAPI.KeyboardInputTests.EditableWebViewRequiresKeyboardWhenF…
Browse files Browse the repository at this point in the history
…irstResponder fails on iPhone Simulator

https://bugs.webkit.org/show_bug.cgi?id=273737
rdar://127547640

Reviewed by Abrar Rahman Protyasha and Megan Gardner.

Prior to BrowserEngineKit adoption, WebKit would use UIKit SPI (`-[UIPressesEvent _hidEvent]`) to
keep track of when a hardware keyboard key is pressed; when observed, we'd then set a boolean flag,
`_seenHardwareKeyDownInNonEditableElement`, which allows us to then return YES from
`-_requiresKeyboardWhenFirstResponder`. This is crucial for key event compatibility, in the case
where the web page listens for key events outside of the context of editing, and the user is using a
hardware keyboard on iOS (and can therefore dispatch keyboard events without the help of a software
keyboard).

With BrowserEngineKit adoption, we no longer rely on the aforementioned SPI; UIKit instead
automatically returns `YES`, as long as `-hardwareKeyboardAttached` is set. However, because this is
set by default when running tests in the iOS simulator, `-_requiresKeyboardWhenFirstResponder` now
returns `YES` right after loading the test markup.

Account for this change in the test by swizzling out `-hardwareKeyboardAttached` to simulate the
keyboard being detached, when running this test.

* Tools/TestRunnerShared/spi/UIKitSPIForTesting.h:
* Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm:
(TestWebKitAPI::overrideHardwareKeyboardAttached):
(TestWebKitAPI::TEST(KeyboardInputTests, EditableWebViewRequiresKeyboardWhenFirstResponder)):

Canonical link: https://commits.webkit.org/278427@main
  • Loading branch information
whsieh committed May 6, 2024
1 parent 5d88444 commit a03f439
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions Tools/TestRunnerShared/spi/UIKitSPIForTesting.h
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ typedef enum {
- (void)setInlineCompletionAsMarkedText:(NSAttributedString *)inlineCompletion selectedRange:(NSRange)selectedRange inputString:(NSString *)inputString searchString:(NSString *)searchString;
@property (nonatomic, readonly) BOOL hasInlineCompletionAsMarkedText;
@property (nonatomic, readonly) UIKeyboardInputMode *currentInputModeInPreference;
@property (nonatomic, readonly) BOOL hardwareKeyboardAttached;
@end

#if PLATFORM(IOS) || PLATFORM(VISION)
Expand Down
11 changes: 11 additions & 0 deletions Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -925,8 +925,19 @@ static BOOL shouldSimulateKeyboardInputOnTextInsertionOverride(id, SEL)
[webView waitForNextPresentationUpdate];
}

static BOOL overrideHardwareKeyboardAttached(id, SEL)
{
return NO;
}

TEST(KeyboardInputTests, EditableWebViewRequiresKeyboardWhenFirstResponder)
{
InstanceMethodSwizzler swizzler {
UIKeyboardImpl.class,
@selector(hardwareKeyboardAttached),
reinterpret_cast<IMP>(overrideHardwareKeyboardAttached)
};

auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
auto delegate = adoptNS([TestInputDelegate new]);
[webView _setInputDelegate:delegate.get()];
Expand Down

0 comments on commit a03f439

Please sign in to comment.