6f478675ade3f271514bb38ed1ea2ea3c1083e0e
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / ios / PositionInformationTests.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 ENABLE(DATA_INTERACTION)
29
30 #import "PlatformUtilities.h"
31 #import "TestWKWebView.h"
32 #import <WebKit/WKWebViewPrivate.h>
33 #import <WebKit/_WKDraggableElementInfo.h>
34
35 @implementation _WKDraggableElementInfo (PositionInformationTests)
36
37 - (void)expectToBeLink:(BOOL)isLink image:(BOOL)isImage atPoint:(CGPoint)point
38 {
39     EXPECT_EQ(isLink, self.isLink);
40     EXPECT_EQ(isImage, self.isImage);
41     EXPECT_EQ(point.x, self.point.x);
42     EXPECT_EQ(point.y, self.point.y);
43 }
44
45 @end
46
47 namespace TestWebKitAPI {
48
49 TEST(PositionInformationTests, FindDraggableLinkAtPosition)
50 {
51     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
52     [webView synchronouslyLoadTestPageNamed:@"link-and-input"];
53
54     _WKDraggableElementInfo *information = [webView draggableElementAtPosition:CGPointMake(100, 50)];
55     [information expectToBeLink:YES image:NO atPoint:CGPointMake(100, 50)];
56 }
57
58 TEST(PositionInformationTests, RequestDraggableLinkAtPosition)
59 {
60     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
61     [webView synchronouslyLoadTestPageNamed:@"link-and-input"];
62
63     __block bool isDone = false;
64     [webView requestDraggableElementAtPosition:CGPointMake(100, 50) completionBlock:^(_WKDraggableElementInfo *information) {
65         [information expectToBeLink:YES image:NO atPoint:CGPointMake(100, 50)];
66         isDone = true;
67     }];
68
69     TestWebKitAPI::Util::run(&isDone);
70 }
71
72 TEST(PositionInformationTests, FindDraggableLinkAtDifferentPositionWithinRequestBlock)
73 {
74     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
75     [webView synchronouslyLoadTestPageNamed:@"link-and-input"];
76
77     __block bool isDone = false;
78     [webView requestDraggableElementAtPosition:CGPointMake(100, 50) completionBlock:^(_WKDraggableElementInfo *information) {
79         _WKDraggableElementInfo *synchronousInformation = [webView draggableElementAtPosition:CGPointMake(100, 300)];
80         [synchronousInformation expectToBeLink:NO image:NO atPoint:CGPointMake(100, 300)];
81         [information expectToBeLink:YES image:NO atPoint:CGPointMake(100, 50)];
82         isDone = true;
83     }];
84
85     TestWebKitAPI::Util::run(&isDone);
86 }
87
88 TEST(PositionInformationTests, FindDraggableLinkAtSamePositionWithinRequestBlock)
89 {
90     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
91     [webView synchronouslyLoadTestPageNamed:@"link-and-input"];
92
93     __block bool isDone = false;
94     [webView requestDraggableElementAtPosition:CGPointMake(100, 50) completionBlock:^(_WKDraggableElementInfo *information) {
95         _WKDraggableElementInfo *synchronousInformation = [webView draggableElementAtPosition:CGPointMake(100, 50)];
96         [synchronousInformation expectToBeLink:YES image:NO atPoint:CGPointMake(100, 50)];
97         [information expectToBeLink:YES image:NO atPoint:CGPointMake(100, 50)];
98         isDone = true;
99     }];
100
101     TestWebKitAPI::Util::run(&isDone);
102 }
103
104 TEST(PositionInformationTests, RequestDraggableLinkAtSamePositionWithinRequestBlock)
105 {
106     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
107     [webView synchronouslyLoadTestPageNamed:@"image-and-contenteditable"];
108
109     __block bool isDoneWithInner = false;
110     __block bool isDoneWithOuter = false;
111
112     [webView requestDraggableElementAtPosition:CGPointMake(100, 50) completionBlock:^(_WKDraggableElementInfo *outerInformation) {
113         [webView requestDraggableElementAtPosition:CGPointMake(100, 50) completionBlock:^(_WKDraggableElementInfo *innerInformation) {
114             [innerInformation expectToBeLink:NO image:YES atPoint:CGPointMake(100, 50)];
115             isDoneWithInner = true;
116         }];
117         [outerInformation expectToBeLink:NO image:YES atPoint:CGPointMake(100, 50)];
118         isDoneWithOuter = true;
119     }];
120
121     TestWebKitAPI::Util::run(&isDoneWithOuter);
122     TestWebKitAPI::Util::run(&isDoneWithInner);
123 }
124
125 TEST(PositionInformationTests, RequestDraggableLinkAtDifferentPositionWithinRequestBlock)
126 {
127     auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)]);
128     [webView synchronouslyLoadTestPageNamed:@"image-and-contenteditable"];
129
130     __block bool isDoneWithInner = false;
131     __block bool isDoneWithOuter = false;
132
133     [webView requestDraggableElementAtPosition:CGPointMake(100, 50) completionBlock:^(_WKDraggableElementInfo *outerInformation) {
134         [webView requestDraggableElementAtPosition:CGPointMake(100, 300) completionBlock:^(_WKDraggableElementInfo *innerInformation) {
135             [innerInformation expectToBeLink:NO image:NO atPoint:CGPointMake(100, 300)];
136             isDoneWithInner = true;
137         }];
138         [outerInformation expectToBeLink:NO image:YES atPoint:CGPointMake(100, 50)];
139         isDoneWithOuter = true;
140     }];
141
142     TestWebKitAPI::Util::run(&isDoneWithOuter);
143     TestWebKitAPI::Util::run(&isDoneWithInner);
144 }
145
146 } // namespace TestWebKitAPI
147
148 #endif