forked from web-platform-tests/wpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-navigator-clipboard-basics.https.html
192 lines (162 loc) · 7.24 KB
/
async-navigator-clipboard-basics.https.html
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
186
187
188
189
190
191
192
<!doctype html>
<meta charset="utf-8">
<title>Async Clipboard input type validation tests</title>
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<body>Body needed for test_driver.click()</body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/user-activation.js"></script>
<script>
// Permissions are required in order to invoke navigator.clipboard functions in
// an automated test.
async function getPermissions() {
await tryGrantReadPermission();
await tryGrantWritePermission();
await waitForUserActivation();
}
test(() => {
assert_not_equals(navigator.clipboard, undefined);
assert_true(navigator.clipboard instanceof Clipboard);
assert_equals(navigator.clipboard, navigator.clipboard);
}, 'navigator.clipboard exists');
promise_test(async t => {
await getPermissions();
const text_plain = "This text was copied using `Clipboard.prototype.write`.";
const html_text = "<p style='color: red; font-style: oblique;'>Test</p>";
await navigator.clipboard.write([
new ClipboardItem({
"text/plain": text_plain,
"text/html" : html_text
}),
]);
}, 'navigator.clipboard.write(DOMString) succeeds');
promise_test(async () => {
await getPermissions();
const promise_text_string = Promise.resolve('hello');
const promise_html_string = Promise.resolve("<p style='color: red; font-style: oblique;'>hello</p>");
const item = new ClipboardItem({
'text/plain': promise_text_string,
'text/html': promise_html_string
});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<DOMString>) succeeds');
promise_test(async t => {
await getPermissions();
const text_plain = 'hello';
const html_text = "<p style='color: red; font-style: oblique;'>hello</p>";
const image = await fetch("/clipboard-apis/resources/greenbox.png");
const item = new ClipboardItem({
'text/plain': text_plain,
'text/html': new Blob([html_text], {type: 'text/html'}),
'image/png': image.blob(), // Promise<Blob>
'web text/csv': 'hello,world'
});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(web_custom_format) succeeds');
promise_test(async () => {
await getPermissions();
const html_text = "<p style='color: red; font-style: oblique;'>Test</p>";
const item = new ClipboardItem({
'text/plain': 'hello',
'text/html': new Blob([html_text], {type: 'text/html'})
});
const text = await item.getType('text/plain');
const blob = await item.getType('text/html');
assert_true(text instanceof Blob, "item.getType('text/plain') didn't return a Blob");
assert_true(blob instanceof Blob, "item.getType('text/html') didn't return a Blob");
}, 'validate GetType(type) on a contructed ClipboardItem returns Blob');
promise_test(async () => {
await getPermissions();
const blob = new Blob(['hello'], {type: 'text/plain'});
const item = new ClipboardItem({'text/plain': blob});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write([text/plain ClipboardItem]) succeeds');
promise_test(async t => {
await getPermissions();
const blob1 = new Blob(['hello'], {type: 'text/plain'});
const blob2 = new Blob(['world'], {type: 'text/plain'});
const item1 = new ClipboardItem({'text/plain': blob1});
const item2 = new ClipboardItem({'text/plain': blob2});
await promise_rejects_dom(t, "NotAllowedError",
navigator.clipboard.write([item1, item2]));
}, 'navigator.clipboard.write([>1 ClipboardItems]) fails (not implemented)');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError, navigator.clipboard.write());
}, 'navigator.clipboard.write() fails (expect [ClipboardItem])');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError, navigator.clipboard.write(null));
}, 'navigator.clipboard.write(null) fails (expect [ClipboardItem])');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError,
navigator.clipboard.write('Bad string'));
}, 'navigator.clipboard.write(DOMString) fails (expect [ClipboardItem])');
promise_test(async t => {
await getPermissions();
const blob = new Blob(['hello'], {type: 'text/plain'});
await promise_rejects_js(t, TypeError, navigator.clipboard.write(blob));
}, 'navigator.clipboard.write(Blob) fails (expect [ClipboardItem])');
promise_test(async () => {
await getPermissions();
await navigator.clipboard.writeText('New clipboard text');
}, 'navigator.clipboard.writeText(DOMString) succeeds');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError,
navigator.clipboard.writeText());
}, 'navigator.clipboard.writeText() fails (expect DOMString)');
promise_test(async () => {
await getPermissions();
const item = new ClipboardItem({'text/plain': 'test'});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write({string : DOMString}) succeeds');
promise_test(async () => {
await getPermissions();
const fetched = await fetch('/clipboard-apis/resources/greenbox.png');
const image = await fetched.blob();
const item = new ClipboardItem({'image/png': image});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write({string : image/png Blob}) succeeds');
promise_test(async() => {
await getPermissions();
const fetched = await fetch('/clipboard-apis/resources/greenbox.png');
const image = await fetched.blob();
const item = new ClipboardItem({
'text/plain': new Blob(['first'], {type: 'text/plain'}),
'image/png': image});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write([text + png] succeeds');
promise_test(async t => {
await getPermissions();
const item = new ClipboardItem({'image/png': 'not an image'});
await promise_rejects_js(t, TypeError, navigator.clipboard.write([item]));
}, 'navigator.clipboard.write(image/png DOMString) fails');
promise_test(async () => {
await getPermissions();
const result = await navigator.clipboard.read();
assert_true(result instanceof Object);
assert_true(result[0] instanceof ClipboardItem);
}, 'navigator.clipboard.read() succeeds');
promise_test(async () => {
await getPermissions();
const result = await navigator.clipboard.readText();
assert_equals(typeof result, 'string');
}, 'navigator.clipboard.readText() succeeds');
promise_test(async () => {
await getPermissions();
const promise_blob = Promise.resolve(new Blob(['hello'], {type: 'text/plain'}));
const item = new ClipboardItem({'text/plain': promise_blob});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<Blob>) succeeds');
promise_test(async () => {
await getPermissions();
const promise_text_blob = Promise.resolve(new Blob(['hello'], {type: 'text/plain'}));
const promise_html_blob = Promise.resolve(new Blob(["<p style='color: red; font-style: oblique;'>Test</p>"], {type: 'text/html'}));
const item = new ClipboardItem({'text/plain': promise_text_blob, 'text/html': promise_html_blob});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<Blob>s) succeeds');
</script>