summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/clipboard-apis/async-navigator-clipboard-basics.https.html
blob: 5d6f701bdb751641343feda37cea44f31ecaf338 (plain)
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
<!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 promise_rejects_dom(t, "NotAllowedError", navigator.clipboard.write([
   new ClipboardItem({
            "text/plain":  text_plain,
            "text/html" : html_text
        }),
    ]));
  }, 'navigator.clipboard.write(Promise<DOMString>) fails');

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>