browser_scratchpad_files.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. // Reference to the Scratchpad object.
  4. var gScratchpad;
  5. // Reference to the temporary nsIFile we will work with.
  6. var gFile;
  7. // The temporary file content.
  8. var gFileContent = "hello.world('bug636725');";
  9. function test()
  10. {
  11. waitForExplicitFinish();
  12. gBrowser.selectedTab = gBrowser.addTab();
  13. gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
  14. gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
  15. openScratchpad(runTests);
  16. }, true);
  17. content.location = "data:text/html,<p>test file open and save in Scratchpad";
  18. }
  19. function runTests()
  20. {
  21. gScratchpad = gScratchpadWindow.Scratchpad;
  22. createTempFile("fileForBug636725.tmp", gFileContent, function (aStatus, aFile) {
  23. ok(Components.isSuccessCode(aStatus),
  24. "The temporary file was saved successfully");
  25. gFile = aFile;
  26. gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
  27. fileImported);
  28. });
  29. }
  30. function fileImported(aStatus, aFileContent)
  31. {
  32. ok(Components.isSuccessCode(aStatus),
  33. "the temporary file was imported successfully with Scratchpad");
  34. is(aFileContent, gFileContent,
  35. "received data is correct");
  36. is(gScratchpad.getText(), gFileContent,
  37. "the editor content is correct");
  38. is(gScratchpad.dirty, false,
  39. "the editor marks imported file as saved");
  40. // Save the file after changes.
  41. gFileContent += "// omg, saved!";
  42. gScratchpad.editor.setText(gFileContent);
  43. gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), true, true,
  44. fileExported);
  45. }
  46. function fileExported(aStatus)
  47. {
  48. ok(Components.isSuccessCode(aStatus),
  49. "the temporary file was exported successfully with Scratchpad");
  50. let oldContent = gFileContent;
  51. // Attempt another file save, with confirmation which returns false.
  52. gFileContent += "// omg, saved twice!";
  53. gScratchpad.editor.setText(gFileContent);
  54. let oldConfirm = gScratchpadWindow.confirm;
  55. let askedConfirmation = false;
  56. gScratchpadWindow.confirm = function () {
  57. askedConfirmation = true;
  58. return false;
  59. };
  60. gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), false, true,
  61. fileExported2);
  62. gScratchpadWindow.confirm = oldConfirm;
  63. ok(askedConfirmation, "exportToFile() asked for overwrite confirmation");
  64. gFileContent = oldContent;
  65. let channel = NetUtil.newChannel({
  66. uri: NetUtil.newURI(gFile),
  67. loadUsingSystemPrincipal: true});
  68. channel.contentType = "application/javascript";
  69. // Read back the temporary file.
  70. NetUtil.asyncFetch(channel, fileRead);
  71. }
  72. function fileExported2()
  73. {
  74. ok(false, "exportToFile() did not cancel file overwrite");
  75. }
  76. function fileRead(aInputStream, aStatus)
  77. {
  78. ok(Components.isSuccessCode(aStatus),
  79. "the temporary file was read back successfully");
  80. let updatedContent =
  81. NetUtil.readInputStreamToString(aInputStream, aInputStream.available());
  82. is(updatedContent, gFileContent, "file properly updated");
  83. // Done!
  84. gFile.remove(false);
  85. gFile = null;
  86. gScratchpad = null;
  87. finish();
  88. }