test_file_uri.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf8">
  5. <title>Test for file activity tracking</title>
  6. <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  7. <script type="text/javascript;version=1.8" src="common.js"></script>
  8. <!-- Any copyright is dedicated to the Public Domain.
  9. - http://creativecommons.org/publicdomain/zero/1.0/ -->
  10. </head>
  11. <body>
  12. <p>Test for file activity tracking</p>
  13. <script class="testbody" type="text/javascript;version=1.8">
  14. SimpleTest.waitForExplicitFinish();
  15. Cu.import("resource://gre/modules/NetUtil.jsm");
  16. Cu.import("resource://gre/modules/FileUtils.jsm");
  17. let gState;
  18. let gTmpFile;
  19. function doFileActivity()
  20. {
  21. info("doFileActivity");
  22. let fileContent = "<p>hello world from bug 798764";
  23. gTmpFile = FileUtils.getFile("TmpD", ["bug798764.html"]);
  24. gTmpFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
  25. let fout = FileUtils.openSafeFileOutputStream(gTmpFile,
  26. FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE);
  27. let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
  28. createInstance(Ci.nsIScriptableUnicodeConverter);
  29. converter.charset = "UTF-8";
  30. let fileContentStream = converter.convertToInputStream(fileContent);
  31. NetUtil.asyncCopy(fileContentStream, fout, addIframe);
  32. }
  33. function addIframe(aStatus)
  34. {
  35. ok(Components.isSuccessCode(aStatus),
  36. "the temporary file was saved successfully");
  37. let iframe = document.createElement("iframe");
  38. iframe.src = NetUtil.newURI(gTmpFile).spec;
  39. document.body.appendChild(iframe);
  40. }
  41. function startTest()
  42. {
  43. removeEventListener("load", startTest);
  44. attachConsole(["FileActivity"], onAttach);
  45. }
  46. function onAttach(aState, aResponse)
  47. {
  48. gState = aState;
  49. gState.dbgClient.addListener("fileActivity", onFileActivity);
  50. doFileActivity();
  51. }
  52. function onFileActivity(aType, aPacket)
  53. {
  54. is(aPacket.from, gState.actor, "fileActivity actor");
  55. gState.dbgClient.removeListener("fileActivity", onFileActivity);
  56. info("aPacket.uri: " + aPacket.uri);
  57. ok(/\bbug798764\b.*\.html$/.test(aPacket.uri), "file URI match");
  58. testEnd();
  59. }
  60. function testEnd()
  61. {
  62. if (gTmpFile) {
  63. SimpleTest.executeSoon(function() {
  64. try {
  65. gTmpFile.remove(false);
  66. }
  67. catch (ex if (ex.name == "NS_ERROR_FILE_IS_LOCKED")) {
  68. // Sometimes remove() throws because the file is not unlocked soon
  69. // enough.
  70. }
  71. gTmpFile = null;
  72. });
  73. }
  74. if (gState) {
  75. closeDebugger(gState, function() {
  76. gState = null;
  77. SimpleTest.finish();
  78. });
  79. } else {
  80. SimpleTest.finish();
  81. }
  82. }
  83. addEventListener("load", startTest);
  84. </script>
  85. </body>
  86. </html>