2 <?xml-stylesheet type=
"text/css" href=
"chrome://global/skin"?>
3 <?xml-stylesheet type=
"text/css" href=
"chrome://mochikit/content/tests/SimpleTest/test.css"?>
4 <window title=
"nsTransferable with large string"
5 xmlns=
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
7 <title>nsTransferable with large string
</title>
8 <script src=
"chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
10 <script type=
"application/javascript">
12 // This value is chosen such that the size of the memory for the string exceeds
13 // the kLargeDatasetSize threshold in nsTransferable.h (one million).
14 // Each character of a JS string is internally represented by two bytes,
15 // so the following string of length
500 001 uses
1 000 002 bytes.
16 const BIG_STRING =
"x" +
"BIGGY".repeat(
100000);
18 // Some value with a length that is exactly kLargeDatasetSize (
1 000 000).
19 const SMALL_STRING =
"small".repeat(
100000);
21 const nsTransferable = Components.Constructor(
"@mozilla.org/widget/transferable;1",
"nsITransferable");
22 const nsSupportsString = Components.Constructor(
"@mozilla.org/supports-string;1",
"nsISupportsString");
24 function assignTextToTransferable(transferable, string) {
25 var Suppstr = nsSupportsString();
26 Suppstr.data = string;
27 transferable.setTransferData(
"text/unicode", Suppstr);
30 function checkTransferableText(transferable, expectedString, description) {
32 transferable.getTransferData(
"text/unicode", data);
33 var actualValue = data.value.QueryInterface(Ci.nsISupportsString).data;
34 // Use ok + shortenString instead of is(...) to avoid dumping millions of characters in the output.
35 ok(actualValue === expectedString, description +
": text should match. " +
36 "Expected " + shortenString(expectedString) +
", got " + shortenString(actualValue));
38 function shortenString(str) {
39 return str && str.length
> 30 ? str.slice(
0,
10) +
"..." + str.slice(-
10) : String(str);
43 function isFDCountingSupported() {
44 // On on-Windows we can count the number of file handles for the current process,
45 // while on Windows we need to count the number of files in ${TempD}\mozilla-temp-files\,
46 // which can be unreliable, especially because nsAnonymousTemporaryFile has documented
47 // that the deletion might not be immediate.
49 // To avoid intermittents, we only check the file descriptor counts on non-Windows.
50 // test_bug1123480.xhtml will do some basic testing for Windows.
51 const {AppConstants} = ChromeUtils.import(
"resource://gre/modules/AppConstants.jsm");
52 return AppConstants.platform !== 'win';
55 function getClipboardCacheFDCount() {
56 var dir = Cc[
"@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
57 dir.initWithPath(
"/dev/fd");
59 for (var de = dir.directoryEntries; de.hasMoreElements(); ) {
60 var fdFile = de.nextFile;
63 fileSize = fdFile.fileSize;
65 // This can happen on macOS.
68 if (fileSize === BIG_STRING.length *
2 ||
69 // We are not expecting files of this small size,
70 // but include them in the count anyway
71 // in case the files are unexpectedly created.
72 fileSize === SMALL_STRING.length *
2) {
73 // Assume that the file was created by us if the size matches.
81 const {PrivateBrowsingUtils} = ChromeUtils.import(
"resource://gre/modules/PrivateBrowsingUtils.jsm");
82 const {Services} = ChromeUtils.import(
"resource://gre/modules/Services.jsm");
84 var win = window.browsingContext.topChromeWindow.open(
"about:blank",
"_blank",
"chrome, width=500, height=200");
85 ok(win,
"should open window");
86 is(PrivateBrowsingUtils.isContentWindowPrivate(win), false,
"used correct window context");
88 // ### Part
1 - Writing to the clipboard.
90 var Loadctx = PrivateBrowsingUtils.privacyContextFromWindow(win);
91 var Transfer = nsTransferable();
92 Transfer.init(Loadctx);
93 Transfer.addDataFlavor(
"text/unicode");
94 var initialFdCount = isFDCountingSupported() ? getClipboardCacheFDCount() : -
1;
96 assignTextToTransferable(Transfer, BIG_STRING);
97 checkTransferableText(Transfer, BIG_STRING,
"transferable after assigning BIG_STRING");
98 if (isFDCountingSupported()) {
99 is(getClipboardCacheFDCount(), initialFdCount +
1,
"should use a file for BIG_STRING");
102 // Share the transferable with the system.
103 Services.clipboard.setData(Transfer, null, Services.clipboard.kGlobalClipboard);
105 // Sanity check: Copying to the clipboard should not have altered the transferable.
106 checkTransferableText(Transfer, BIG_STRING,
"transferable after copying to clipboard");
107 if (isFDCountingSupported()) {
108 // We are only counting file descriptors for the current process,
109 // so even if the test were to be multi-process and the parent process creates another
110 // nsTransferable, then the count should still be the same.
111 is(getClipboardCacheFDCount(), initialFdCount +
1,
"should still be using files for previously stored BIG_STRING");
113 // Re-establish baseline for the second part of the test below.
114 initialFdCount = getClipboardCacheFDCount();
117 // ### Part
2 - Reading from the clipboard.
119 var Transfer2 = nsTransferable();
120 Transfer2.init(Loadctx);
121 Transfer2.addDataFlavor(
"text/unicode");
123 // Iniitalize with a small string, so we can see that mData -
> mCacheFD works.
124 assignTextToTransferable(Transfer2, SMALL_STRING);
125 checkTransferableText(Transfer2, SMALL_STRING,
"transferable after assigning SMALL_STRING");
126 if (isFDCountingSupported()) {
127 is(getClipboardCacheFDCount(), initialFdCount,
"should not use file to store SMALL_STRING.");
130 // Check whether the clipboard data can be read, and simulatenously trigger mData -
> mCacheFD.
131 Services.clipboard.getData(Transfer2, Services.clipboard.kGlobalClipboard);
132 checkTransferableText(Transfer2, BIG_STRING,
"transferable after retrieving from clipboard");
133 if (isFDCountingSupported()) {
134 is(getClipboardCacheFDCount(), initialFdCount +
1,
"should use a file for BIG_STRING (read from clipboard).");
137 // Store a small string, to exercise the code path from mCacheFD -
> mData.
138 assignTextToTransferable(Transfer2, SMALL_STRING);
139 checkTransferableText(Transfer2, SMALL_STRING,
"transferable after assigning SMALL_STRING");
140 if (isFDCountingSupported()) {
141 is(getClipboardCacheFDCount(), initialFdCount,
"should release the file after clearing the transferable.");
147 <!-- test results are displayed in the html:body -->
148 <body xmlns=
"http://www.w3.org/1999/xhtml">
149 This test checks whether a big string can be copied to the clipboard, and then retrieved in the same form.
150 On non-Windows, the test also checks whether the data of the transferable is really stored in a file.