browser_scratchpad_reset_undo.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /* Bug 684546 */
  4. // Reference to the Scratchpad chrome window object.
  5. var gScratchpadWindow;
  6. // Reference to the Scratchpad object.
  7. var gScratchpad;
  8. // Reference to the temporary nsIFile we will work with.
  9. var gFileA;
  10. var gFileB;
  11. // The temporary file content.
  12. var gFileAContent = "// File A ** Hello World!";
  13. var gFileBContent = "// File B ** Goodbye All";
  14. // Help track if one or both files are saved
  15. var gFirstFileSaved = false;
  16. function test()
  17. {
  18. waitForExplicitFinish();
  19. gBrowser.selectedTab = gBrowser.addTab();
  20. gBrowser.selectedBrowser.addEventListener("load", function browserLoad() {
  21. gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true);
  22. openScratchpad(runTests);
  23. }, true);
  24. content.location = "data:text/html,<p>test that undo get's reset after file load in Scratchpad";
  25. }
  26. function runTests()
  27. {
  28. gScratchpad = gScratchpadWindow.Scratchpad;
  29. // Create a temporary file.
  30. gFileA = FileUtils.getFile("TmpD", ["fileAForBug684546.tmp"]);
  31. gFileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
  32. gFileB = FileUtils.getFile("TmpD", ["fileBForBug684546.tmp"]);
  33. gFileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
  34. // Write the temporary file.
  35. let foutA = Cc["@mozilla.org/network/file-output-stream;1"].
  36. createInstance(Ci.nsIFileOutputStream);
  37. foutA.init(gFileA.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
  38. 0o644, foutA.DEFER_OPEN);
  39. let foutB = Cc["@mozilla.org/network/file-output-stream;1"].
  40. createInstance(Ci.nsIFileOutputStream);
  41. foutB.init(gFileB.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
  42. 0o644, foutB.DEFER_OPEN);
  43. let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
  44. createInstance(Ci.nsIScriptableUnicodeConverter);
  45. converter.charset = "UTF-8";
  46. let fileContentStreamA = converter.convertToInputStream(gFileAContent);
  47. let fileContentStreamB = converter.convertToInputStream(gFileBContent);
  48. NetUtil.asyncCopy(fileContentStreamA, foutA, tempFileSaved);
  49. NetUtil.asyncCopy(fileContentStreamB, foutB, tempFileSaved);
  50. }
  51. function tempFileSaved(aStatus)
  52. {
  53. let success = Components.isSuccessCode(aStatus);
  54. ok(success, "a temporary file was saved successfully");
  55. if (!success)
  56. {
  57. finish();
  58. return;
  59. }
  60. if (gFirstFileSaved && success)
  61. {
  62. ok((gFirstFileSaved && success), "Both files loaded");
  63. // Import the file A into Scratchpad.
  64. gScratchpad.importFromFile(gFileA.QueryInterface(Ci.nsILocalFile), true,
  65. fileAImported);
  66. }
  67. gFirstFileSaved = success;
  68. }
  69. function fileAImported(aStatus, aFileContent)
  70. {
  71. ok(Components.isSuccessCode(aStatus),
  72. "the temporary file A was imported successfully with Scratchpad");
  73. is(aFileContent, gFileAContent, "received data is correct");
  74. is(gScratchpad.getText(), gFileAContent, "the editor content is correct");
  75. gScratchpad.editor.replaceText("new text",
  76. gScratchpad.editor.getPosition(gScratchpad.getText().length));
  77. is(gScratchpad.getText(), gFileAContent + "new text", "text updated correctly");
  78. gScratchpad.undo();
  79. is(gScratchpad.getText(), gFileAContent, "undo works");
  80. gScratchpad.redo();
  81. is(gScratchpad.getText(), gFileAContent + "new text", "redo works");
  82. // Import the file B into Scratchpad.
  83. gScratchpad.importFromFile(gFileB.QueryInterface(Ci.nsILocalFile), true,
  84. fileBImported);
  85. }
  86. function fileBImported(aStatus, aFileContent)
  87. {
  88. ok(Components.isSuccessCode(aStatus),
  89. "the temporary file B was imported successfully with Scratchpad");
  90. is(aFileContent, gFileBContent, "received data is correct");
  91. is(gScratchpad.getText(), gFileBContent, "the editor content is correct");
  92. ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load");
  93. gScratchpad.undo();
  94. is(gScratchpad.getText(), gFileBContent,
  95. "the editor content is still correct after undo");
  96. gScratchpad.editor.replaceText("new text",
  97. gScratchpad.editor.getPosition(gScratchpad.getText().length));
  98. is(gScratchpad.getText(), gFileBContent + "new text", "text updated correctly");
  99. gScratchpad.undo();
  100. is(gScratchpad.getText(), gFileBContent, "undo works");
  101. ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load (again)");
  102. gScratchpad.redo();
  103. is(gScratchpad.getText(), gFileBContent + "new text", "redo works");
  104. // Done!
  105. finish();
  106. }
  107. registerCleanupFunction(function () {
  108. if (gFileA && gFileA.exists())
  109. {
  110. gFileA.remove(false);
  111. gFileA = null;
  112. }
  113. if (gFileB && gFileB.exists())
  114. {
  115. gFileB.remove(false);
  116. gFileB = null;
  117. }
  118. gScratchpad = null;
  119. });