forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipboardTests.mm
185 lines (157 loc) · 8.58 KB
/
ClipboardTests.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (C) 2019 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.
*/
#import "config.h"
#import "PlatformUtilities.h"
#import "TestWKWebView.h"
#import "UIKitSPI.h"
#import <CoreServices/CoreServices.h>
#import <WebCore/LegacyNSPasteboardTypes.h>
#import <WebKit/WKPreferencesPrivate.h>
#import <WebKit/_WKFeature.h>
@interface TestWKWebView (ClipboardTests)
- (void)readClipboard;
@end
@implementation TestWKWebView (ClipboardTests)
- (void)readClipboard
{
__block bool done = false;
[self performAfterReceivingMessage:@"readClipboard" action:^{
done = true;
}];
[self evaluateJavaScript:@"readClipboard()" completionHandler:nil];
TestWebKitAPI::Util::run(&done);
}
- (void)writeString:(NSString *)string toClipboardWithType:(NSString *)type
{
__block bool done = false;
[self performAfterReceivingMessage:@"wroteStringToClipboard" action:^{
done = true;
}];
[self evaluateJavaScript:[NSString stringWithFormat:@"writeStringToClipboard(`%@`, `%@`)", type, string] completionHandler:nil];
TestWebKitAPI::Util::run(&done);
}
@end
static RetainPtr<TestWKWebView> createWebViewForClipboardTests()
{
#if PLATFORM(IOS_FAMILY)
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// UIPasteboard's type coercion codepaths only take effect when the UIApplication has been initialized.
UIApplicationInitialize();
});
#endif // PLATFORM(IOS_FAMILY)
auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
[[configuration preferences] _setDOMPasteAllowed:YES];
[[configuration preferences] _setJavaScriptCanAccessClipboard:YES];
for (_WKFeature *feature in [WKPreferences _features]) {
if ([feature.key isEqualToString:@"AsyncClipboardAPIEnabled"]) {
[[configuration preferences] _setEnabled:YES forFeature:feature];
break;
}
}
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 400) configuration:configuration.get()]);
[webView synchronouslyLoadTestPageNamed:@"clipboard"];
return webView;
}
static void writeMultipleObjectsToPlatformPasteboard()
{
#if PLATFORM(MAC)
auto firstItem = adoptNS([[NSPasteboardItem alloc] init]);
[firstItem setString:@"Hello" forType:NSPasteboardTypeString];
auto secondItem = adoptNS([[NSPasteboardItem alloc] init]);
[secondItem setString:@"https://apple.com/" forType:NSPasteboardTypeURL];
auto thirdItem = adoptNS([[NSPasteboardItem alloc] init]);
[thirdItem setString:@"<strong style='color: rgb(0, 255, 0);'>Hello world</strong>" forType:NSPasteboardTypeHTML];
auto fourthItem = adoptNS([[NSPasteboardItem alloc] init]);
[fourthItem setString:@"WebKit" forType:NSPasteboardTypeString];
[fourthItem setString:@"https://webkit.org/" forType:NSPasteboardTypeURL];
[fourthItem setString:@"<a href='https://webkit.org/'>Hello world</a>" forType:NSPasteboardTypeHTML];
NSPasteboard *pasteboard = NSPasteboard.generalPasteboard;
[pasteboard clearContents];
[pasteboard writeObjects:@[firstItem.get(), secondItem.get(), thirdItem.get(), fourthItem.get()]];
#elif PLATFORM(IOS_FAMILY) && !PLATFORM(WATCHOS) && !PLATFORM(APPLETV)
auto firstItem = adoptNS([[NSItemProvider alloc] initWithObject:@"Hello"]);
auto secondItem = adoptNS([[NSItemProvider alloc] initWithObject:[NSURL URLWithString:@"https://apple.com/"]]);
auto thirdItem = adoptNS([[NSItemProvider alloc] init]);
[thirdItem registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypeHTML visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[&] (void (^completionHandler)(NSData *, NSError *)) -> NSProgress * {
completionHandler([@"<strong style='color: rgb(0, 255, 0);'>Hello world</strong>" dataUsingEncoding:NSUTF8StringEncoding], nil);
return nil;
}];
auto fourthItem = adoptNS([[NSItemProvider alloc] init]);
[fourthItem registerObject:@"WebKit" visibility:NSItemProviderRepresentationVisibilityAll];
[fourthItem registerObject:[NSURL URLWithString:@"https://webkit.org/"] visibility:NSItemProviderRepresentationVisibilityAll];
[fourthItem registerDataRepresentationForTypeIdentifier:(__bridge NSString *)kUTTypeHTML visibility:NSItemProviderRepresentationVisibilityAll loadHandler:[&] (void (^completionHandler)(NSData *, NSError *)) -> NSProgress * {
completionHandler([@"<a href='https://webkit.org/'>Hello world</a>" dataUsingEncoding:NSUTF8StringEncoding], nil);
return nil;
}];
UIPasteboard.generalPasteboard.itemProviders = @[ firstItem.get(), secondItem.get(), thirdItem.get(), fourthItem.get() ];
#endif
}
static RetainPtr<NSString> readMarkupFromPasteboard()
{
#if PLATFORM(MAC)
NSData *rawData = [NSPasteboard.generalPasteboard dataForType:WebCore::legacyHTMLPasteboardType()];
#else
NSData *rawData = [UIPasteboard.generalPasteboard dataForPasteboardType:(__bridge NSString *)kUTTypeHTML];
#endif
return adoptNS([[NSString alloc] initWithData:rawData encoding:NSUTF8StringEncoding]);
}
TEST(ClipboardTests, ReadMultipleItems)
{
auto webView = createWebViewForClipboardTests();
writeMultipleObjectsToPlatformPasteboard();
[webView readClipboard];
EXPECT_WK_STREQ("", [webView stringByEvaluatingJavaScript:@"exception ? exception.message : ''"]);
EXPECT_EQ(4U, [[webView objectByEvaluatingJavaScript:@"clipboardData.length"] unsignedIntValue]);
EXPECT_WK_STREQ("Hello", [webView stringByEvaluatingJavaScript:@"clipboardData[0]['text/plain']"]);
EXPECT_WK_STREQ("https://apple.com/", [webView stringByEvaluatingJavaScript:@"clipboardData[1]['text/uri-list']"]);
EXPECT_WK_STREQ("rgb(0, 255, 0)", [webView stringByEvaluatingJavaScript:@"getComputedStyle(clipboardData[2]['text/html'].querySelector('strong')).color"]);
EXPECT_WK_STREQ("WebKit", [webView stringByEvaluatingJavaScript:@"clipboardData[3]['text/plain']"]);
EXPECT_WK_STREQ("https://webkit.org/", [webView objectByEvaluatingJavaScript:@"clipboardData[3]['text/uri-list']"]);
EXPECT_WK_STREQ("https://webkit.org/", [webView stringByEvaluatingJavaScript:@"clipboardData[3]['text/html'].querySelector('a').href"]);
}
TEST(ClipboardTests, WriteSanitizedMarkup)
{
auto webView = createWebViewForClipboardTests();
[webView writeString:@"<script>/* super secret */</script>This is a test." toClipboardWithType:@"text/html"];
auto writtenMarkup = readMarkupFromPasteboard();
EXPECT_TRUE([writtenMarkup containsString:@"This is a test."]);
EXPECT_FALSE([writtenMarkup containsString:@"super secret"]);
EXPECT_FALSE([writtenMarkup containsString:@"<script>"]);
}
#if PLATFORM(MAC)
TEST(ClipboardTests, ConvertTIFFToPNGWhenPasting)
{
auto webView = createWebViewForClipboardTests();
auto url = [[NSBundle mainBundle] URLForResource:@"sunset-in-cupertino-100px" withExtension:@"tiff" subdirectory:@"TestWebKitAPI.resources"];
auto pasteboard = NSPasteboard.generalPasteboard;
[pasteboard clearContents];
[pasteboard setData:[NSData dataWithContentsOfURL:url] forType:NSPasteboardTypeTIFF];
[webView readClipboard];
EXPECT_WK_STREQ("", [webView stringByEvaluatingJavaScript:@"exception ? exception.message : ''"]);
EXPECT_EQ(1U, [[webView objectByEvaluatingJavaScript:@"clipboardData.length"] unsignedIntValue]);
EXPECT_TRUE([[webView stringByEvaluatingJavaScript:@"clipboardData[0]['image/png'].src"] containsString:@"blob:"]);
}
#endif // PLATFORM(MAC)