Unreviewed, guard iOS 11-dependent UIPasteboardTests to be iOS 11+ only
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / ios / UIPasteboardTests.mm
index c3adebb3bfc39b529080b41e54e8f5772df504e1..3ebde6b230e43d6049e5f3b6c6aceaaefea85db1 100644 (file)
 #import "TestWKWebView.h"
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <UIKit/UIPasteboard.h>
 #import "TestWKWebView.h"
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <UIKit/UIPasteboard.h>
+#import <WebCore/SoftLinking.h>
 #import <WebKit/WKPreferencesPrivate.h>
 #import <WebKit/WKWebViewPrivate.h>
 
 #import <WebKit/WKPreferencesPrivate.h>
 #import <WebKit/WKWebViewPrivate.h>
 
+SOFT_LINK_FRAMEWORK(UIKit)
+SOFT_LINK(UIKit, UIApplicationInitialize, void, (void), ())
+
 namespace TestWebKitAPI {
 
 NSData *dataForPasteboardType(CFStringRef type)
 namespace TestWebKitAPI {
 
 NSData *dataForPasteboardType(CFStringRef type)
@@ -43,12 +47,17 @@ NSData *dataForPasteboardType(CFStringRef type)
 
 RetainPtr<TestWKWebView> setUpWebViewForPasteboardTests()
 {
 
 RetainPtr<TestWKWebView> setUpWebViewForPasteboardTests()
 {
+    // UIPasteboard's type coercion codepaths only take effect when the UIApplication has been initialized.
+    UIApplicationInitialize();
+
     [UIPasteboard generalPasteboard].items = @[];
     EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF8PlainText).length);
     EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF16PlainText).length);
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
     [UIPasteboard generalPasteboard].items = @[];
     EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF8PlainText).length);
     EXPECT_TRUE(!dataForPasteboardType(kUTTypeUTF16PlainText).length);
 
     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
-    [webView configuration].preferences._javaScriptCanAccessClipboard = YES;
+    WKPreferences *preferences = [webView configuration].preferences;
+    preferences._javaScriptCanAccessClipboard = YES;
+    preferences._domPasteAllowed = YES;
     [webView synchronouslyLoadTestPageNamed:@"rich-and-plain-text"];
     return webView;
 }
     [webView synchronouslyLoadTestPageNamed:@"rich-and-plain-text"];
     return webView;
 }
@@ -77,6 +86,61 @@ TEST(UIPasteboardTests, CopyRichTextWritesConcreteTypes)
     EXPECT_WK_STREQ("Hello world", [utf16Result UTF8String]);
 }
 
     EXPECT_WK_STREQ("Hello world", [utf16Result UTF8String]);
 }
 
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
+
+TEST(UIPasteboardTests, DoNotPastePlainTextAsURL)
+{
+    auto webView = setUpWebViewForPasteboardTests();
+
+    NSString *testString = @"[helloworld]";
+    [UIPasteboard generalPasteboard].string = testString;
+
+    [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
+    [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
+    EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
+    EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
+    EXPECT_FALSE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
+}
+
+TEST(UIPasteboardTests, PastePlainTextAsURL)
+{
+    auto webView = setUpWebViewForPasteboardTests();
+
+    NSString *testString = @"https://www.apple.com/iphone";
+    [UIPasteboard generalPasteboard].string = testString;
+
+    [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
+    [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
+    EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
+    EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
+    EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
+}
+
+TEST(UIPasteboardTests, PasteURLWithPlainTextAsURL)
+{
+    auto webView = setUpWebViewForPasteboardTests();
+
+    NSString *testString = @"thisisdefinitelyaurl";
+    [UIPasteboard generalPasteboard].URL = [NSURL URLWithString:testString];
+
+    [webView stringByEvaluatingJavaScript:@"selectPlainText()"];
+    [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
+    EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"plain.value"]);
+
+    [webView stringByEvaluatingJavaScript:@"selectRichText()"];
+    [webView stringByEvaluatingJavaScript:@"document.execCommand('paste')"];
+    EXPECT_WK_STREQ(testString, [webView stringByEvaluatingJavaScript:@"rich.textContent"]);
+    EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"!!rich.querySelector('a')"].boolValue);
+}
+
+#endif // __IPHONE_OS_VERSION_MIN_REQUIRED >= 110000
+
 } // namespace TestWebKitAPI
 
 #endif
 } // namespace TestWebKitAPI
 
 #endif