5e650381d33c8395cf7877546cdc8491e0974aa9
[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 <WebKit/WKPreferencesPrivate.h>
35 #import <WebKit/WKWebViewPrivate.h>
36 #import <wtf/SoftLinking.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 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
90
91 TEST(UIPasteboardTests, DoNotPastePlainTextAsURL)
92 {
93     auto webView = setUpWebViewForPasteboardTests();
94
95     NSString *testString = @"[helloworld]";
96     [UIPasteboard generalPasteboard].string = testString;
97
98     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
99     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
100     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
101
102     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
103     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
104     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
105     EXPECT_FALSE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
106 }
107
108 TEST(UIPasteboardTests, PastePlainTextAsURL)
109 {
110     auto webView = setUpWebViewForPasteboardTests();
111
112     NSString *testString = @"https://www.apple.com/iphone";
113     [UIPasteboard generalPasteboard].string = testString;
114
115     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
116     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
117     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
118
119     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
120     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
121     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
122     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
123 }
124
125 TEST(UIPasteboardTests, PasteURLWithPlainTextAsURL)
126 {
127     auto webView = setUpWebViewForPasteboardTests();
128
129     NSString *testString = @"thisisdefinitelyaurl";
130     [UIPasteboard generalPasteboard].URL = [NSURL URLWithString:testString];
131
132     [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
133     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
134     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
135
136     [webView stringByEvaluatingJavaScript:@"selectRichText()"];
137     [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
138     EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
139     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
140 }
141
142 #endif // __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
143
144 } // namespace TestWebKitAPI
145
146 #endif