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 751744 */ 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 object. michael@0: let gScratchpad; michael@0: michael@0: // Reference to the temporary nsIFiles. michael@0: let gFile; michael@0: michael@0: // Temporary file name. michael@0: let gFileName = "testFileForBug751744.tmp" michael@0: michael@0: michael@0: // Content for the temporary file. michael@0: let gFileContent = "/* this file is already saved */\n" + michael@0: "function foo() { alert('bar') }"; michael@0: let gLength = gFileContent.length; michael@0: michael@0: // Reference to the menu entry. michael@0: let menu; michael@0: michael@0: function startTest() michael@0: { michael@0: gScratchpad = gScratchpadWindow.Scratchpad; michael@0: menu = gScratchpadWindow.document.getElementById("sp-cmd-revert"); michael@0: createAndLoadTemporaryFile(); michael@0: } michael@0: michael@0: function testAfterSaved() { michael@0: // Check if the revert menu is disabled as the file is at saved state. michael@0: ok(menu.hasAttribute("disabled"), "The revert menu entry is disabled."); michael@0: michael@0: // chancging the text in the file michael@0: gScratchpad.setText(gScratchpad.getText() + "\nfoo();"); michael@0: // Checking the text got changed michael@0: is(gScratchpad.getText(), gFileContent + "\nfoo();", michael@0: "The text changed the first time."); michael@0: michael@0: // Revert menu now should be enabled. michael@0: ok(!menu.hasAttribute("disabled"), michael@0: "The revert menu entry is enabled after changing text first time"); michael@0: michael@0: // reverting back to last saved state. michael@0: gScratchpad.revertFile(testAfterRevert); michael@0: } michael@0: michael@0: function testAfterRevert() { michael@0: // Check if the file's text got reverted michael@0: is(gScratchpad.getText(), gFileContent, michael@0: "The text reverted back to original text."); michael@0: // The revert menu should be disabled again. michael@0: ok(menu.hasAttribute("disabled"), michael@0: "The revert menu entry is disabled after reverting."); michael@0: michael@0: // chancging the text in the file again michael@0: gScratchpad.setText(gScratchpad.getText() + "\nalert(foo.toSource());"); michael@0: // Saving the file. michael@0: gScratchpad.saveFile(testAfterSecondSave); michael@0: } michael@0: michael@0: function testAfterSecondSave() { michael@0: // revert menu entry should be disabled. michael@0: ok(menu.hasAttribute("disabled"), michael@0: "The revert menu entry is disabled after saving."); michael@0: michael@0: // changing the text. michael@0: gScratchpad.setText(gScratchpad.getText() + "\nfoo();"); michael@0: michael@0: // revert menu entry should get enabled yet again. michael@0: ok(!menu.hasAttribute("disabled"), michael@0: "The revert menu entry is enabled after changing text third time"); michael@0: michael@0: // reverting back to last saved state. michael@0: gScratchpad.revertFile(testAfterSecondRevert); michael@0: } michael@0: michael@0: function testAfterSecondRevert() { michael@0: // Check if the file's text got reverted michael@0: is(gScratchpad.getText(), gFileContent + "\nalert(foo.toSource());", michael@0: "The text reverted back to the changed saved text."); michael@0: // The revert menu should be disabled again. michael@0: ok(menu.hasAttribute("disabled"), michael@0: "Revert menu entry is disabled after reverting to changed saved state."); michael@0: gFile.remove(false); michael@0: gFile = gScratchpad = menu = null; michael@0: finish(); michael@0: } michael@0: michael@0: function createAndLoadTemporaryFile() michael@0: { michael@0: // Create a temporary file. michael@0: gFile = FileUtils.getFile("TmpD", [gFileName]); michael@0: gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); michael@0: michael@0: // Write the temporary file. michael@0: let fout = Cc["@mozilla.org/network/file-output-stream;1"]. michael@0: createInstance(Ci.nsIFileOutputStream); michael@0: fout.init(gFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, michael@0: 0o644, fout.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 fileContentStream = converter.convertToInputStream(gFileContent); michael@0: michael@0: NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved); michael@0: } michael@0: michael@0: function tempFileSaved(aStatus) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file was saved successfully"); michael@0: michael@0: // Import the file into Scratchpad. michael@0: gScratchpad.setFilename(gFile.path); michael@0: gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, michael@0: testAfterSaved); michael@0: } 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 onLoad() { michael@0: gBrowser.selectedBrowser.removeEventListener("load", onLoad, true); michael@0: openScratchpad(startTest); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,
test reverting to last saved state of" + michael@0: " a file
"; michael@0: }