ProcessingInstruction::clearExistingCachedSheet doesn't really exist.
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / ios / UIPasteboardTests.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 #if PLATFORM(IOS) && WK_API_ENABLED
29
30 #import "PlatformUtilities.h"
31 #import "TestWKWebView.h"
32 #import <MobileCoreServices/MobileCoreServices.h>
33 #import <UIKit/UIPasteboard.h>
34 #import <WebCore/SoftLinking.h>
35 #import <WebKit/WKPreferencesPrivate.h>
36 #import <WebKit/WKWebViewPrivate.h>
37
38 SOFT_LINK_FRAMEWORK(UIKit)
39 SOFT_LINK(UIKit, UIApplicationInitialize, void, (void), ())
40
41 namespace TestWebKitAPI {
42
43 NSData *dataForPasteboardType(CFStringRef type)
44 {
45     return [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString *)type inItemSet:[NSIndexSet indexSetWithIndex:0]].firstObject;
46 }
47
48 RetainPtr<TestWKWebView> setUpWebViewForPasteboardTests()
49 {
50     // UIPasteboard's type coercion codepaths only take effect when the UIApplication has been initialized.
51     UIApplicationInitialize();
52
53     [UIPasteboard generalPasteboard].items = @[];
54     EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF8PlainText).length);
55     EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF16PlainText).length);
56
57     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
58     WKPreferences *preferences = [webView configuration].preferences;
59     preferences._javaScriptCanAccessClipboard = YES;
60     preferences._domPasteAllowed = YES;
61     [webView synchronouslyLoadTestPageNamed:@"rich-and-plain-text"];
62     return webView;
63 }
64
65 TEST(UIPasteboardTests, CopyPlainTextWritesConcreteTypes)
66 {
67     auto webView = setUpWebViewForPasteboardTests();
68     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
69     [webView stringByEvaluatingJavaScript:@"document.execCommand('copy')"];
70
71     auto utf8Result = adoptNS([[NSString alloc] initWithData:dataForPasteboardType(kUTTypeUTF8PlainText) encoding:NSUTF8StringEncoding]);
72     auto utf16Result = adoptNS([[NSString alloc] initWithData:dataForPasteboardType(kUTTypeUTF16PlainText) encoding:NSUTF16StringEncoding]);
73     EXPECT_WK_STREQ("Hello world", [utf8Result UTF8String]);
74     EXPECT_WK_STREQ("Hello world", [utf16Result UTF8String]);
75 }
76
77 TEST(UIPasteboardTests, CopyRichTextWritesConcreteTypes)
78 {
79     auto webView = setUpWebViewForPasteboardTests();
80     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
81     [webView stringByEvaluatingJavaScript:@"document.execCommand('copy')"];
82
83     auto utf8Result = adoptNS([[NSString alloc] initWithData:dataForPasteboardType(kUTTypeUTF8PlainText) encoding:NSUTF8StringEncoding]);
84     auto utf16Result = adoptNS([[NSString alloc] initWithData:dataForPasteboardType(kUTTypeUTF16PlainText) encoding:NSUTF16StringEncoding]);
85     EXPECT_WK_STREQ("Hello world", [utf8Result UTF8String]);
86     EXPECT_WK_STREQ("Hello world", [utf16Result UTF8String]);
87 }
88
89 TEST(UIPasteboardTests, DoNotPastePlainTextAsURL)
90 {
91     auto webView = setUpWebViewForPasteboardTests();
92
93     NSString *testString = @"[helloworld]";
94     [UIPasteboard generalPasteboard].string = testString;
95
96     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
97     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
98     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
99
100     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
101     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
102     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
103     EXPECT_FALSE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
104 }
105
106 TEST(UIPasteboardTests, PastePlainTextAsURL)
107 {
108     auto webView = setUpWebViewForPasteboardTests();
109
110     NSString *testString = @"https://www.apple.com/iphone";
111     [UIPasteboard generalPasteboard].string = testString;
112
113     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
114     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
115     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
116
117     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
118     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
119     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
120     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
121 }
122
123 TEST(UIPasteboardTests, PasteURLWithPlainTextAsURL)
124 {
125     auto webView = setUpWebViewForPasteboardTests();
126
127     NSString *testString = @"thisisdefinitelyaurl";
128     [UIPasteboard generalPasteboard].URL = [NSURL URLWithString:testString];
129
130     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
131     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
132     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
133
134     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
135     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
136     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
137     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
138 }
139
140 } // namespace TestWebKitAPI
141
142 #endif