Unreviewed test gardening for iOS API tests.
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / ios / FocusPreservationTests.mm
1 /*
2  * Copyright (C) 2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "Test.h"
28
29 #if PLATFORM(IOS_FAMILY)
30
31 #import "PlatformUtilities.h"
32 #import "TestInputDelegate.h"
33 #import "TestWKWebView.h"
34 #import "UIKitSPI.h"
35 #import <WebKit/WKWebViewPrivate.h>
36 #import <WebKit/WKWebViewPrivateForTesting.h>
37 #import <WebKit/_WKInputDelegate.h>
38 #import <wtf/BlockPtr.h>
39
40 static std::pair<RetainPtr<TestWKWebView>, RetainPtr<TestInputDelegate>> webViewForTestingFocusPreservation(void(^focusHandler)(id <_WKFocusedElementInfo>))
41 {
42     auto inputDelegate = adoptNS([[TestInputDelegate alloc] init]);
43     [inputDelegate setFocusStartsInputSessionPolicyHandler:[&, focusHandler = makeBlockPtr(focusHandler)] (WKWebView *, id<_WKFocusedElementInfo> info) {
44         focusHandler(info);
45         return _WKFocusStartsInputSessionPolicyAllow;
46     }];
47
48     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
49     [webView _setInputDelegate:inputDelegate.get()];
50     [webView synchronouslyLoadHTMLString:@"<input></input><select><option selected>foo</option><option>bar</option></select>"];
51     return { webView, inputDelegate };
52 }
53
54 namespace TestWebKitAPI {
55
56 TEST(FocusPreservationTests, PreserveAndRestoreFocus)
57 {
58     bool inputFocused = false;
59     auto webViewAndDelegate = webViewForTestingFocusPreservation([&inputFocused] (id <_WKFocusedElementInfo> info) {
60         inputFocused = true;
61     });
62
63     TestWKWebView *webView = webViewAndDelegate.first.get();
64     [webView evaluateJavaScript:@"document.querySelector('input').focus()" completionHandler:nil];
65     Util::run(&inputFocused);
66
67     NSUUID *focusToken = NSUUID.UUID;
68     [webView.textInputContentView _preserveFocusWithToken:focusToken destructively:YES];
69
70     EXPECT_TRUE([webView resignFirstResponder]);
71     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"document.activeElement == document.querySelector('input')"].boolValue);
72
73     [webView.textInputContentView _restoreFocusWithToken:focusToken];
74     EXPECT_TRUE([webView becomeFirstResponder]);
75 }
76
77 // FIXME: Re-enable this test once rdar://60644908 is resolved
78 TEST(FocusPreservationTests, DISABLED_ChangingFocusedNodeResetsFocusPreservationState)
79 {
80     bool inputFocused = false;
81     bool selectFocused = false;
82     auto webViewAndDelegate = webViewForTestingFocusPreservation([&inputFocused, &selectFocused] (id <_WKFocusedElementInfo> info) {
83         if (info.type == WKInputTypeSelect)
84             selectFocused = true;
85         else
86             inputFocused = true;
87     });
88
89     TestWKWebView *webView = webViewAndDelegate.first.get();
90     [webView evaluateJavaScript:@"document.querySelector('input').focus()" completionHandler:nil];
91     Util::run(&inputFocused);
92
93     NSUUID *focusToken = NSUUID.UUID;
94     [webView.textInputContentView _preserveFocusWithToken:focusToken destructively:YES];
95
96     [webView evaluateJavaScript:@"document.querySelector('select').focus()" completionHandler:nil];
97     Util::run(&selectFocused);
98
99     BOOL isPhone = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone;
100     if (isPhone)
101         EXPECT_NOT_NULL(webView.textInputContentView.inputView);
102
103     [webView selectFormAccessoryPickerRow:1];
104     [webView dismissFormAccessoryView];
105     EXPECT_EQ(1, [webView stringByEvaluatingJavaScript:@"document.querySelector('select').selectedIndex"].intValue);
106
107     if (isPhone)
108         EXPECT_FALSE([webView stringByEvaluatingJavaScript:@"document.activeElement == document.querySelector('select')"].boolValue);
109
110     [webView.textInputContentView _restoreFocusWithToken:focusToken];
111 }
112
113 } // namespace TestWebKitAPI
114
115 #endif // PLATFORM(IOS_FAMILY)