2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if PLATFORM(IOS) && WK_API_ENABLED
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>
38 SOFT_LINK_FRAMEWORK(UIKit)
39 SOFT_LINK(UIKit, UIApplicationInitialize, void, (void), ())
41 namespace TestWebKitAPI {
43 NSData *dataForPasteboardType(CFStringRef type)
45 return [[UIPasteboard generalPasteboard] dataForPasteboardType:(NSString *)type inItemSet:[NSIndexSet indexSetWithIndex:0]].firstObject;
48 RetainPtr<TestWKWebView> setUpWebViewForPasteboardTests()
50 // UIPasteboard's type coercion codepaths only take effect when the UIApplication has been initialized.
51 UIApplicationInitialize();
53 [UIPasteboard generalPasteboard].items = @[];
54 EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF8PlainText).length);
55 EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF16PlainText).length);
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"];
65 TEST(UIPasteboardTests, CopyPlainTextWritesConcreteTypes)
67 auto webView = setUpWebViewForPasteboardTests();
68 [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
69 [webView stringByEvaluatingJavaScript:@"document.execCommand('copy')"];
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]);
77 TEST(UIPasteboardTests, CopyRichTextWritesConcreteTypes)
79 auto webView = setUpWebViewForPasteboardTests();
80 [webView stringByEvaluatingJavaScript:@"selectRichText()"];
81 [webView stringByEvaluatingJavaScript:@"document.execCommand('copy')"];
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]);
89 #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
91 TEST(UIPasteboardTests, DoNotPastePlainTextAsURL)
93 auto webView = setUpWebViewForPasteboardTests();
95 NSString *testString = @"[helloworld]";
96 [UIPasteboard generalPasteboard].string = testString;
98 [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
99 [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
100 EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
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);
108 TEST(UIPasteboardTests, PastePlainTextAsURL)
110 auto webView = setUpWebViewForPasteboardTests();
112 NSString *testString = @"https://www.apple.com/iphone";
113 [UIPasteboard generalPasteboard].string = testString;
115 [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
116 [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
117 EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
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);
125 TEST(UIPasteboardTests, PasteURLWithPlainTextAsURL)
127 auto webView = setUpWebViewForPasteboardTests();
129 NSString *testString = @"thisisdefinitelyaurl";
130 [UIPasteboard generalPasteboard].URL = [NSURL URLWithString:testString];
132 [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
133 [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
134 EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
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);
142 #endif // __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
144 } // namespace TestWebKitAPI