/* * Copyright (C) 2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include "config.h" #if ENABLE(DATA_INTERACTION) #import "DataInteractionSimulator.h" #import "PlatformUtilities.h" #import "TestWKWebView.h" #import #import @implementation TestWKWebView (DataInteractionTests) - (BOOL)editorContainsImageElement { return [self stringByEvaluatingJavaScript:@"!!editor.querySelector('img')"].boolValue; } - (NSString *)editorValue { return [self stringByEvaluatingJavaScript:@"editor.value"]; } @end namespace TestWebKitAPI { TEST(DataInteractionTests, ImageToContentEditable) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"image-and-contenteditable"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_TRUE([webView editorContainsImageElement]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, ImageToTextarea) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"image-and-textarea"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; NSURL *imageURL = [NSURL fileURLWithPath:[webView editorValue]]; EXPECT_WK_STREQ("icon.png", imageURL.lastPathComponent); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, ContentEditableToContentEditable) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"autofocus-contenteditable"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_EQ([webView stringByEvaluatingJavaScript:@"source.textContent"].length, 0UL); EXPECT_WK_STREQ("Hello world", [webView stringByEvaluatingJavaScript:@"editor.textContent"].UTF8String); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, ContentEditableToTextarea) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"contenteditable-and-textarea"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_EQ([webView stringByEvaluatingJavaScript:@"source.textContent"].length, 0UL); EXPECT_WK_STREQ("Hello world", [webView editorValue].UTF8String); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, LinkToInput) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"link-and-input"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_WK_STREQ("https://www.daringfireball.net/", [webView editorValue].UTF8String); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, BackgroundImageLinkToInput) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"background-image-link-and-input"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_WK_STREQ("https://www.daringfireball.net/", [webView editorValue].UTF8String); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, CanPreventStart) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"prevent-start"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_FALSE([webView editorContainsImageElement]); EXPECT_FALSE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_FALSE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_FALSE([observedEventNames containsObject:DataInteractionOverEventName]); } TEST(DataInteractionTests, CanPreventOperation) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"prevent-operation"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_FALSE([webView editorContainsImageElement]); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); } TEST(DataInteractionTests, EnterAndLeaveEvents) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"link-and-input"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 450)]; EXPECT_WK_STREQ("", [webView editorValue].UTF8String); EXPECT_TRUE([dataInteractionSimulator didTryToBeginDataInteraction]); NSArray *observedEventNames = [dataInteractionSimulator observedEventNames]; EXPECT_TRUE([observedEventNames containsObject:DataInteractionEnterEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionOverEventName]); EXPECT_TRUE([observedEventNames containsObject:DataInteractionLeaveEventName]); EXPECT_FALSE([observedEventNames containsObject:DataInteractionPerformOperationEventName]); } TEST(DataInteractionTests, HandlesDataInteractionFailureGracefully) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"link-and-input"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); [dataInteractionSimulator setForceRequestToFail:YES]; [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_WK_STREQ("", [webView editorValue].UTF8String); // Before r212266, starting a subsequent gesture would have caused a debug assertion in the web process. [dataInteractionSimulator setForceRequestToFail:NO]; [dataInteractionSimulator runFrom:CGPointMake(100, 50) to:CGPointMake(100, 300)]; EXPECT_WK_STREQ("https://www.daringfireball.net/", [webView editorValue].UTF8String); } TEST(DataInteractionTests, ExternalSourceUTF8PlainTextOnly) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"autofocus-contenteditable"]; NSString *textPayload = @"Ceci n'est pas une string"; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); RetainPtr simulatedItemProvider = adoptNS([[UIItemProvider alloc] init]); [simulatedItemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypeUTF8PlainText options:nil loadHandler:^NSProgress *(UIItemProviderDataLoadCompletionBlock completionBlock) { completionBlock([textPayload dataUsingEncoding:NSUTF8StringEncoding], nil); return [NSProgress discreteProgressWithTotalUnitCount:100]; }]; [dataInteractionSimulator setExternalItemProvider:simulatedItemProvider.get()]; [dataInteractionSimulator runFrom:CGPointMake(300, 400) to:CGPointMake(100, 300)]; EXPECT_WK_STREQ(textPayload.UTF8String, [webView stringByEvaluatingJavaScript:@"editor.textContent"].UTF8String); } TEST(DataInteractionTests, ExternalSourceJPEGOnly) { RetainPtr webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]); [webView synchronouslyLoadTestPageNamed:@"autofocus-contenteditable"]; UIImage *testImage = [UIImage imageNamed:@"TestWebKitAPI.resources/icon.png"]; RetainPtr dataInteractionSimulator = adoptNS([[DataInteractionSimulator alloc] initWithWebView:webView.get()]); RetainPtr simulatedItemProvider = adoptNS([[UIItemProvider alloc] init]); [simulatedItemProvider registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypeJPEG options:nil loadHandler:^NSProgress *(UIItemProviderDataLoadCompletionBlock completionBlock) { completionBlock(UIImageJPEGRepresentation(testImage, 0.5), nil); return [NSProgress discreteProgressWithTotalUnitCount:100]; }]; [dataInteractionSimulator setExternalItemProvider:simulatedItemProvider.get()]; [dataInteractionSimulator runFrom:CGPointMake(300, 400) to:CGPointMake(100, 300)]; EXPECT_TRUE([webView editorContainsImageElement]); } } // namespace TestWebKitAPI #endif // ENABLE(DATA_INTERACTION)