[macOS] API test DragAndDropTests.DropColor is failing on macOS 10.12
[WebKit-https.git] / Tools / TestWebKitAPI / Tests / mac / DragAndDropTestsMac.mm
1 /*
2  * Copyright (C) 2018 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 #import "config.h"
27
28 #import "DragAndDropSimulator.h"
29 #import "PlatformUtilities.h"
30
31 #if WK_API_ENABLED && ENABLE(DRAG_SUPPORT) && PLATFORM(MAC)
32
33 static void waitForConditionWithLogging(BOOL(^condition)(), NSTimeInterval loggingTimeout, NSString *message, ...)
34 {
35     NSDate *startTime = [NSDate date];
36     BOOL exceededLoggingTimeout = NO;
37     while ([[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]]) {
38         if (condition())
39             break;
40
41         if (exceededLoggingTimeout || [[NSDate date] timeIntervalSinceDate:startTime] < loggingTimeout)
42             continue;
43
44         va_list args;
45         va_start(args, message);
46         NSLogv(message, args);
47         va_end(args);
48         exceededLoggingTimeout = YES;
49     }
50 }
51
52 TEST(DragAndDropTests, NumberOfValidItemsForDrop)
53 {
54     NSPasteboard *pasteboard = [NSPasteboard pasteboardWithUniqueName];
55     [pasteboard declareTypes:@[NSFilenamesPboardType] owner:nil];
56     [pasteboard setPropertyList:@[@"file-name"] forType:NSFilenamesPboardType];
57
58     auto simulator = adoptNS([[DragAndDropSimulator alloc] initWithWebViewFrame:NSMakeRect(0, 0, 400, 400)]);
59     TestWKWebView *webView = [simulator webView];
60     [simulator setExternalDragPasteboard:pasteboard];
61     [webView synchronouslyLoadTestPageNamed:@"full-page-dropzone"];
62
63     NSInteger numberOfValidItemsForDrop = 0;
64     [simulator setWillEndDraggingHandler:[&numberOfValidItemsForDrop, simulator] {
65         numberOfValidItemsForDrop = [simulator draggingInfo].numberOfValidItemsForDrop;
66     }];
67
68     [simulator runFrom:NSMakePoint(0, 0) to:NSMakePoint(200, 200)];
69
70     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"observedDragEnter"].boolValue);
71     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"observedDragOver"].boolValue);
72     EXPECT_TRUE([webView stringByEvaluatingJavaScript:@"observedDrop"].boolValue);
73     EXPECT_EQ(1U, numberOfValidItemsForDrop);
74 }
75
76 #if ENABLE(INPUT_TYPE_COLOR)
77 TEST(DragAndDropTests, DropColor)
78 {
79     NSPasteboard *pasteboard = [NSPasteboard pasteboardWithUniqueName];
80     [pasteboard declareTypes:@[NSColorPboardType] owner:nil];
81     [[NSColor colorWithRed:1 green:0 blue:0 alpha:1] writeToPasteboard:pasteboard];
82
83     auto simulator = adoptNS([[DragAndDropSimulator alloc] initWithWebViewFrame:NSMakeRect(0, 0, 400, 400)]);
84     TestWKWebView *webView = [simulator webView];
85     [simulator setExternalDragPasteboard:pasteboard];
86
87     [webView synchronouslyLoadTestPageNamed:@"color-drop"];
88     [simulator runFrom:NSMakePoint(0, 0) to:NSMakePoint(50, 50)];
89     EXPECT_WK_STREQ(@"#ff0000", [webView stringByEvaluatingJavaScript:@"document.querySelector(\"input\").value"]);
90 }
91 #endif // ENABLE(INPUT_TYPE_COLOR)
92
93 TEST(DragAndDropTests, DragImageElementIntoFileUpload)
94 {
95     auto simulator = adoptNS([[DragAndDropSimulator alloc] initWithWebViewFrame:NSMakeRect(0, 0, 400, 400)]);
96     TestWKWebView *webView = [simulator webView];
97     [webView synchronouslyLoadTestPageNamed:@"image-and-file-upload"];
98     [simulator runFrom:NSMakePoint(100, 100) to:NSMakePoint(100, 300)];
99
100     waitForConditionWithLogging([&] () -> BOOL {
101         return [webView stringByEvaluatingJavaScript:@"imageload.textContent"].boolValue;
102     }, 2, @"Expected image to finish loading.");
103     EXPECT_EQ(1, [webView stringByEvaluatingJavaScript:@"filecount.textContent"].integerValue);
104 }
105
106 TEST(DragAndDropTests, DragPromisedImageFileIntoFileUpload)
107 {
108     auto simulator = adoptNS([[DragAndDropSimulator alloc] initWithWebViewFrame:NSMakeRect(0, 0, 400, 400)]);
109     TestWKWebView *webView = [simulator webView];
110     [webView synchronouslyLoadTestPageNamed:@"image-and-file-upload"];
111
112     NSURL *imageURL = [NSBundle.mainBundle URLForResource:@"apple" withExtension:@"gif" subdirectory:@"TestWebKitAPI.resources"];
113     [simulator writePromisedFiles:@[ imageURL ]];
114     [simulator runFrom:NSMakePoint(100, 100) to:NSMakePoint(100, 300)];
115
116     waitForConditionWithLogging([&] () -> BOOL {
117         return [webView stringByEvaluatingJavaScript:@"imageload.textContent"].boolValue;
118     }, 2, @"Expected image to finish loading.");
119     EXPECT_EQ(1, [webView stringByEvaluatingJavaScript:@"filecount.textContent"].integerValue);
120 }
121
122 TEST(DragAndDropTests, DragImageFileIntoFileUpload)
123 {
124     auto simulator = adoptNS([[DragAndDropSimulator alloc] initWithWebViewFrame:NSMakeRect(0, 0, 400, 400)]);
125     TestWKWebView *webView = [simulator webView];
126     [webView synchronouslyLoadTestPageNamed:@"image-and-file-upload"];
127
128     NSURL *imageURL = [NSBundle.mainBundle URLForResource:@"apple" withExtension:@"gif" subdirectory:@"TestWebKitAPI.resources"];
129     [simulator writeFiles:@[ imageURL ]];
130     [simulator runFrom:NSMakePoint(100, 100) to:NSMakePoint(100, 300)];
131
132     waitForConditionWithLogging([&] () -> BOOL {
133         return [webView stringByEvaluatingJavaScript:@"imageload.textContent"].boolValue;
134     }, 2, @"Expected image to finish loading.");
135     EXPECT_EQ(1, [webView stringByEvaluatingJavaScript:@"filecount.textContent"].integerValue);
136 }
137
138 #endif // WK_API_ENABLED && ENABLE(DRAG_SUPPORT) && PLATFORM(MAC)