michael@0: /* vim: set ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: /* Bug 684546 */ michael@0: michael@0: let tempScope = {}; michael@0: Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); michael@0: Cu.import("resource://gre/modules/FileUtils.jsm", tempScope); michael@0: let NetUtil = tempScope.NetUtil; michael@0: let FileUtils = tempScope.FileUtils; michael@0: michael@0: // Reference to the Scratchpad chrome window object. michael@0: let gScratchpadWindow; michael@0: michael@0: // Reference to the Scratchpad object. michael@0: let gScratchpad; michael@0: michael@0: // Reference to the temporary nsIFile we will work with. michael@0: let gFileA; michael@0: let gFileB; michael@0: michael@0: // The temporary file content. michael@0: let gFileAContent = "// File A ** Hello World!"; michael@0: let gFileBContent = "// File B ** Goodbye All"; michael@0: michael@0: // Help track if one or both files are saved michael@0: let gFirstFileSaved = false; michael@0: michael@0: function test() michael@0: { michael@0: waitForExplicitFinish(); michael@0: michael@0: gBrowser.selectedTab = gBrowser.addTab(); michael@0: gBrowser.selectedBrowser.addEventListener("load", function browserLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true); michael@0: openScratchpad(runTests); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,

test that undo get's reset after file load in Scratchpad"; michael@0: } michael@0: michael@0: function runTests() michael@0: { michael@0: gScratchpad = gScratchpadWindow.Scratchpad; michael@0: michael@0: // Create a temporary file. michael@0: gFileA = FileUtils.getFile("TmpD", ["fileAForBug684546.tmp"]); michael@0: gFileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); michael@0: michael@0: gFileB = FileUtils.getFile("TmpD", ["fileBForBug684546.tmp"]); michael@0: gFileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); michael@0: michael@0: // Write the temporary file. michael@0: let foutA = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: foutA.init(gFileA.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, michael@0: 0o644, foutA.DEFER_OPEN); michael@0: michael@0: let foutB = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: foutB.init(gFileB.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, michael@0: 0o644, foutB.DEFER_OPEN); michael@0: michael@0: let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. michael@0: createInstance(Ci.nsIScriptableUnicodeConverter); michael@0: converter.charset = "UTF-8"; michael@0: let fileContentStreamA = converter.convertToInputStream(gFileAContent); michael@0: let fileContentStreamB = converter.convertToInputStream(gFileBContent); michael@0: michael@0: NetUtil.asyncCopy(fileContentStreamA, foutA, tempFileSaved); michael@0: NetUtil.asyncCopy(fileContentStreamB, foutB, tempFileSaved); michael@0: } michael@0: michael@0: function tempFileSaved(aStatus) michael@0: { michael@0: let success = Components.isSuccessCode(aStatus); michael@0: michael@0: ok(success, "a temporary file was saved successfully"); michael@0: michael@0: if (!success) michael@0: { michael@0: finish(); michael@0: return; michael@0: } michael@0: michael@0: if (gFirstFileSaved && success) michael@0: { michael@0: ok((gFirstFileSaved && success), "Both files loaded"); michael@0: // Import the file A into Scratchpad. michael@0: gScratchpad.importFromFile(gFileA.QueryInterface(Ci.nsILocalFile), true, michael@0: fileAImported); michael@0: } michael@0: gFirstFileSaved = success; michael@0: } michael@0: michael@0: function fileAImported(aStatus, aFileContent) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file A was imported successfully with Scratchpad"); michael@0: michael@0: is(aFileContent, gFileAContent, "received data is correct"); michael@0: michael@0: is(gScratchpad.getText(), gFileAContent, "the editor content is correct"); michael@0: michael@0: gScratchpad.editor.replaceText("new text", michael@0: gScratchpad.editor.getPosition(gScratchpad.getText().length)); michael@0: michael@0: is(gScratchpad.getText(), gFileAContent + "new text", "text updated correctly"); michael@0: gScratchpad.undo(); michael@0: is(gScratchpad.getText(), gFileAContent, "undo works"); michael@0: gScratchpad.redo(); michael@0: is(gScratchpad.getText(), gFileAContent + "new text", "redo works"); michael@0: michael@0: // Import the file B into Scratchpad. michael@0: gScratchpad.importFromFile(gFileB.QueryInterface(Ci.nsILocalFile), true, michael@0: fileBImported); michael@0: } michael@0: michael@0: function fileBImported(aStatus, aFileContent) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file B was imported successfully with Scratchpad"); michael@0: michael@0: is(aFileContent, gFileBContent, "received data is correct"); michael@0: michael@0: is(gScratchpad.getText(), gFileBContent, "the editor content is correct"); michael@0: michael@0: ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load"); michael@0: michael@0: gScratchpad.undo(); michael@0: is(gScratchpad.getText(), gFileBContent, michael@0: "the editor content is still correct after undo"); michael@0: michael@0: gScratchpad.editor.replaceText("new text", michael@0: gScratchpad.editor.getPosition(gScratchpad.getText().length)); michael@0: is(gScratchpad.getText(), gFileBContent + "new text", "text updated correctly"); michael@0: michael@0: gScratchpad.undo(); michael@0: is(gScratchpad.getText(), gFileBContent, "undo works"); michael@0: ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load (again)"); michael@0: michael@0: gScratchpad.redo(); michael@0: is(gScratchpad.getText(), gFileBContent + "new text", "redo works"); michael@0: michael@0: // Done! michael@0: finish(); michael@0: } michael@0: michael@0: registerCleanupFunction(function() { michael@0: if (gFileA && gFileA.exists()) michael@0: { michael@0: gFileA.remove(false); michael@0: gFileA = null; michael@0: } michael@0: if (gFileB && gFileB.exists()) michael@0: { michael@0: gFileB.remove(false); michael@0: gFileB = null; michael@0: } michael@0: gScratchpad = null; michael@0: });