Fix API test after r241728
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / WebKitCocoa / _WKInputDelegate.mm
1 /*
2  * Copyright (C) 2017 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 #include "config.h"
27
28 #import "PlatformUtilities.h"
29 #import "Test.h"
30 #import "TestWKWebView.h"
31 #import <WebKit/WKWebViewPrivate.h>
32 #import <WebKit/_WKFocusedElementInfo.h>
33 #import <WebKit/_WKFormInputSession.h>
34 #import <WebKit/_WKInputDelegate.h>
35 #import <wtf/BlockPtr.h>
36 #import <wtf/RetainPtr.h>
37
38 #if WK_API_ENABLED
39
40 static bool done;
41 static bool willSubmitFormValuesCalled;
42
43 @interface InputDelegate : NSObject <_WKInputDelegate, WKURLSchemeHandler>
44 @property (nonatomic, copy) BOOL(^shouldStartInputSessionHandler)(id <_WKFocusedElementInfo>);
45 @end
46
47 @implementation InputDelegate {
48     BlockPtr<BOOL(id <_WKFocusedElementInfo>)> _shouldStartInputSessionHandler;
49 }
50
51 - (void)webView:(WKWebView *)webView startURLSchemeTask:(id <WKURLSchemeTask>)task
52 {
53     EXPECT_TRUE(willSubmitFormValuesCalled);
54     EXPECT_STREQ(task.request.URL.absoluteString.UTF8String, "test:///formtarget");
55     EXPECT_NOT_NULL(task.request.HTTPBody);
56     EXPECT_EQ(task.request.HTTPBody.length, 62u);
57     EXPECT_STREQ(static_cast<const char*>(task.request.HTTPBody.bytes), "testname1=testvalue1&testname2=testvalue2&testname3=testvalue3");
58     done = true;
59 }
60
61 - (void)webView:(WKWebView *)webView stopURLSchemeTask:(id <WKURLSchemeTask>)task
62 {
63 }
64
65 - (void)_webView:(WKWebView *)webView willSubmitFormValues:(NSDictionary *)values userObject:(NSObject <NSSecureCoding> *)userObject submissionHandler:(void (^)(void))submissionHandler
66 {
67     EXPECT_EQ(values.count, 2u);
68     EXPECT_STREQ([[values objectForKey:@"testname1"] UTF8String], "testvalue1");
69     EXPECT_STREQ([[values objectForKey:@"testname2"] UTF8String], "testvalue2");
70     willSubmitFormValuesCalled = true;
71     submissionHandler();
72 }
73
74 - (BOOL)_webView:(WKWebView *)webView focusShouldStartInputSession:(id <_WKFocusedElementInfo>)info
75 {
76     if (_shouldStartInputSessionHandler)
77         return _shouldStartInputSessionHandler(info);
78     return [info isUserInitiated];
79 }
80
81 - (BOOL(^)(id <_WKFocusedElementInfo>))shouldStartInputSessionHandler
82 {
83     return _shouldStartInputSessionHandler.get();
84 }
85
86 - (void)setShouldStartInputSessionHandler:(BOOL(^)(id <_WKFocusedElementInfo>))handler
87 {
88     _shouldStartInputSessionHandler = makeBlockPtr(handler);
89 }
90
91 @end
92
93 TEST(WebKit, FormSubmission)
94 {
95     auto delegate = adoptNS([[InputDelegate alloc] init]);
96     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
97     [configuration setURLSchemeHandler:delegate.get() forURLScheme:@"test"];
98     auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
99     [webView _setInputDelegate:delegate.get()];
100     [webView loadHTMLString:@"<body onload='document.getElementById(\"formID\").submit()'><form id='formID' method='post' action='test:///formtarget'>"
101         "<input type='text' name='testname1' value='testvalue1'/>"
102         "<input type='password' name='testname2' value='testvalue2'/>"
103         "<input type='hidden' name='testname3' value='testvalue3'/>"
104     "</form></body>" baseURL:nil];
105     TestWebKitAPI::Util::run(&done);
106 }
107
108 #if PLATFORM(IOS_FAMILY)
109
110 TEST(WebKit, FocusedElementInfo)
111 {
112     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
113     auto delegate = adoptNS([[InputDelegate alloc] init]);
114     [webView _setInputDelegate:delegate.get()];
115
116     __block RetainPtr<id <_WKFocusedElementInfo>> currentElement;
117     [delegate setShouldStartInputSessionHandler:^BOOL(id<_WKFocusedElementInfo> element) {
118         currentElement = element;
119         return NO;
120     }];
121
122     [webView synchronouslyLoadHTMLString:@"<label for='foo'>bar</label><input id='foo'>"];
123     [webView stringByEvaluatingJavaScript:@"foo.focus()"];
124     [webView waitForNextPresentationUpdate];
125     EXPECT_WK_STREQ("", [currentElement placeholder]);
126     EXPECT_WK_STREQ("bar", [currentElement label]);
127
128     [webView synchronouslyLoadHTMLString:@"<input placeholder='bar'>"];
129     [webView stringByEvaluatingJavaScript:@"document.querySelector('input').focus()"];
130     [webView waitForNextPresentationUpdate];
131     EXPECT_WK_STREQ("bar", [currentElement placeholder]);
132     EXPECT_WK_STREQ("", [currentElement label]);
133
134     [webView synchronouslyLoadHTMLString:@"<label for='baz'>garply</label><select id='baz'></select>"];
135     [webView stringByEvaluatingJavaScript:@"baz.focus()"];
136     [webView waitForNextPresentationUpdate];
137     EXPECT_WK_STREQ("", [currentElement placeholder]);
138     EXPECT_WK_STREQ("garply", [currentElement label]);
139
140     [webView synchronouslyLoadHTMLString:@"<label for='foo' style='display: none'>bar</label><label for='foo'></label><input id='foo'><label for='foo'>garply</label>"];
141     [webView stringByEvaluatingJavaScript:@"foo.focus()"];
142     [webView waitForNextPresentationUpdate];
143     EXPECT_WK_STREQ("", [currentElement placeholder]);
144     EXPECT_WK_STREQ("garply", [currentElement label]);
145 }
146
147 #endif // PLATFORM(IOS_FAMILY)
148
149 #endif // WK_API_ENABLED
150
151