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
193
194
195
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
let mockCA = makeMockContentAnalysis();
add_setup(async function test_setup() {
mockCA = mockContentAnalysisService(mockCA);
await SpecialPowers.pushPrefEnv({
set: [
["dom.events.asyncClipboard.readText", true],
// This pref turns off the "Paste" popup
["dom.events.testing.asyncClipboard", true],
],
});
});
const PAGE_URL =
"https://example.com/browser/toolkit/components/contentanalysis/tests/browser/clipboard_read_async.html";
const CLIPBOARD_TEXT_STRING = "Some plain text";
const CLIPBOARD_HTML_STRING = "<b>Some HTML</b>";
async function testClipboardReadAsync(allowPaste) {
mockCA.setupForTest(allowPaste);
setClipboardData();
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL);
let browser = tab.linkedBrowser;
{
let result = await setDataAndStartTest(browser, allowPaste, "read");
is(result, true, "Got unexpected result from page for read()");
is(
mockCA.calls.length,
2,
"Correct number of calls to Content Analysis for read()"
);
// On Windows, widget adds extra data into HTML clipboard.
let expectedHtml = navigator.platform.includes("Win")
? `<html><body>\n<!--StartFragment-->${CLIPBOARD_HTML_STRING}<!--EndFragment-->\n</body>\n</html>`
: CLIPBOARD_HTML_STRING;
assertContentAnalysisRequest(mockCA.calls[0], expectedHtml);
assertContentAnalysisRequest(mockCA.calls[1], CLIPBOARD_TEXT_STRING);
mockCA.clearCalls();
}
{
let result = await setDataAndStartTest(browser, allowPaste, "readText");
is(result, true, "Got unexpected result from page for readText()");
is(
mockCA.calls.length,
1,
"Correct number of calls to Content Analysis for read()"
);
assertContentAnalysisRequest(mockCA.calls[0], CLIPBOARD_TEXT_STRING);
mockCA.clearCalls();
}
BrowserTestUtils.removeTab(tab);
}
async function testClipboardReadAsyncWithErrorHelper() {
mockCA.setupForTestWithError(Cr.NS_ERROR_NOT_AVAILABLE);
setClipboardData();
// This test throws a number of exceptions, so tell the framework this is OK.
// If an exception is thrown we won't get the right response from setDataAndStartTest()
// so this should be safe to do.
ignoreAllUncaughtExceptions();
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE_URL);
let browser = tab.linkedBrowser;
{
let result = await setDataAndStartTest(browser, false, "read", true);
is(result, true, "Got unexpected result from page for read()");
is(
mockCA.calls.length,
2,
"Correct number of calls to Content Analysis for read()"
);
// On Windows, widget adds extra data into HTML clipboard.
let expectedHtml = navigator.platform.includes("Win")
? `<html><body>\n<!--StartFragment-->${CLIPBOARD_HTML_STRING}<!--EndFragment-->\n</body>\n</html>`
: CLIPBOARD_HTML_STRING;
assertContentAnalysisRequest(mockCA.calls[0], expectedHtml);
assertContentAnalysisRequest(mockCA.calls[1], CLIPBOARD_TEXT_STRING);
mockCA.clearCalls();
}
{
let result = await setDataAndStartTest(browser, false, "readText", true);
is(result, true, "Got unexpected result from page for readText()");
is(
mockCA.calls.length,
1,
"Correct number of calls to Content Analysis for read()"
);
assertContentAnalysisRequest(mockCA.calls[0], CLIPBOARD_TEXT_STRING);
mockCA.clearCalls();
}
BrowserTestUtils.removeTab(tab);
}
function setDataAndStartTest(
browser,
allowPaste,
testType,
shouldError = false
) {
return SpecialPowers.spawn(
browser,
[allowPaste, testType, shouldError],
(allowPaste, testType, shouldError) => {
return new Promise(resolve => {
content.document.addEventListener(
"testresult",
event => {
resolve(event.detail.result);
},
{ once: true }
);
content.document.getElementById("pasteAllowed").checked = allowPaste;
content.document.getElementById("contentAnalysisReturnsError").checked =
shouldError;
content.document.dispatchEvent(
new content.CustomEvent("teststart", {
detail: Cu.cloneInto({ testType }, content),
})
);
});
}
);
}
function assertContentAnalysisRequest(request, expectedText) {
is(request.url.spec, PAGE_URL, "request has correct URL");
is(
request.analysisType,
Ci.nsIContentAnalysisRequest.eBulkDataEntry,
"request has correct analysisType"
);
is(
request.operationTypeForDisplay,
Ci.nsIContentAnalysisRequest.eClipboard,
"request has correct operationTypeForDisplay"
);
is(request.filePath, "", "request filePath should match");
is(request.textContent, expectedText, "request textContent should match");
is(request.printDataHandle, 0, "request printDataHandle should not be 0");
is(request.printDataSize, 0, "request printDataSize should not be 0");
ok(!!request.requestToken.length, "request requestToken should not be empty");
}
function setClipboardData() {
const trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(
Ci.nsITransferable
);
trans.init(null);
{
trans.addDataFlavor("text/plain");
const str = Cc["@mozilla.org/supports-string;1"].createInstance(
Ci.nsISupportsString
);
str.data = CLIPBOARD_TEXT_STRING;
trans.setTransferData("text/plain", str);
}
{
trans.addDataFlavor("text/html");
const str = Cc["@mozilla.org/supports-string;1"].createInstance(
Ci.nsISupportsString
);
str.data = CLIPBOARD_HTML_STRING;
trans.setTransferData("text/html", str);
}
// Write to clipboard.
Services.clipboard.setData(trans, null, Ci.nsIClipboard.kGlobalClipboard);
}
add_task(async function testClipboardReadAsyncWithContentAnalysisAllow() {
await testClipboardReadAsync(true);
});
add_task(async function testClipboardReadAsyncWithContentAnalysisBlock() {
await testClipboardReadAsync(false);
});
add_task(async function testClipboardReadAsyncWithError() {
await testClipboardReadAsyncWithErrorHelper();
});
|