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