summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_filePosting.xhtml
blob: 3ffa219516bcc9f0690dc5979c1604b3e1c08ee1 (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
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=664783
-->
<window title="Mozilla Bug 664783"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript" src="dom_worker_helper.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=664783"
     target="_blank">Mozilla Bug 664783</a>

  <div id="content" style="display: none">
    <input id="fileList" type="file"></input>
  </div>

  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[

  /** Test for Bug 664783 **/

  var fileNum = 0;

  /**
   * Create a file which contains the given data.
   */
  function createFileWithData(fileData) {
    var testFile = Cc["@mozilla.org/file/directory_service;1"]
                       .getService(Ci.nsIProperties)
                       .get("ProfD", Ci.nsIFile);
    testFile.append("workerFilePosting" + fileNum++);

    var outStream = Cc["@mozilla.org/network/file-output-stream;1"]
                        .createInstance(Ci.nsIFileOutputStream);
    outStream.init(testFile, 0x02 | 0x08 | 0x20, // write, create, truncate
                   0o666, 0);
    outStream.write(fileData, fileData.length);
    outStream.close();

    var fileList = document.getElementById('fileList');
    fileList.value = testFile.path;

    return fileList.files[0];
  }

  /**
   * Create a worker which posts the same file given. Used to test cloning of files.
   * Checks the size, type, name and path of the file posted from the worker to ensure it
   * is the same as the original.
   */
  function postFile(file) {
    var worker = new Worker("file_worker.js");

    worker.onerror = function(event) {
      ok(false, "Worker had an error: " + event.message);
      finish();
    };

    worker.onmessage = function(event) {
      is(event.data.size, file.size, "size of file posted from worker does not match file posted to worker.");
      is(event.data.type, file.type, "type of file posted from worker does not match file posted to worker.");
      is(event.data.name, file.name, "name of file posted from worker does not match file posted to worker.");
      finish();
    };

    worker.postMessage(file);
    waitForWorkerFinish();
  }

  // Empty file.
  postFile(createFileWithData(""));

  // Typical use case.
  postFile(createFileWithData("Hello"));

  ]]>
  </script>
</window>