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: michael@0: let tempScope = {}; michael@0: Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); michael@0: let NetUtil = tempScope.NetUtil; 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 gFile; michael@0: michael@0: // The temporary file content. michael@0: let gFileContent = "hello.world('bug636725');"; 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(runTests); michael@0: }, true); michael@0: michael@0: content.location = "data:text/html,
test file open and save in Scratchpad"; michael@0: } michael@0: michael@0: function runTests() michael@0: { michael@0: gScratchpad = gScratchpadWindow.Scratchpad; michael@0: michael@0: createTempFile("fileForBug636725.tmp", gFileContent, function(aStatus, aFile) { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "The temporary file was saved successfully"); michael@0: michael@0: gFile = aFile; michael@0: gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true, michael@0: fileImported); michael@0: }); michael@0: } michael@0: michael@0: function fileImported(aStatus, aFileContent) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file was imported successfully with Scratchpad"); michael@0: michael@0: is(aFileContent, gFileContent, michael@0: "received data is correct"); michael@0: michael@0: is(gScratchpad.getText(), gFileContent, michael@0: "the editor content is correct"); michael@0: michael@0: is(gScratchpad.dirty, false, michael@0: "the editor marks imported file as saved"); michael@0: michael@0: // Save the file after changes. michael@0: gFileContent += "// omg, saved!"; michael@0: gScratchpad.editor.setText(gFileContent); michael@0: michael@0: gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), true, true, michael@0: fileExported); michael@0: } michael@0: michael@0: function fileExported(aStatus) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file was exported successfully with Scratchpad"); michael@0: michael@0: let oldContent = gFileContent; michael@0: michael@0: // Attempt another file save, with confirmation which returns false. michael@0: gFileContent += "// omg, saved twice!"; michael@0: gScratchpad.editor.setText(gFileContent); michael@0: michael@0: let oldConfirm = gScratchpadWindow.confirm; michael@0: let askedConfirmation = false; michael@0: gScratchpadWindow.confirm = function() { michael@0: askedConfirmation = true; michael@0: return false; michael@0: }; michael@0: michael@0: gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), false, true, michael@0: fileExported2); michael@0: michael@0: gScratchpadWindow.confirm = oldConfirm; michael@0: michael@0: ok(askedConfirmation, "exportToFile() asked for overwrite confirmation"); michael@0: michael@0: gFileContent = oldContent; michael@0: michael@0: let channel = NetUtil.newChannel(gFile); michael@0: channel.contentType = "application/javascript"; michael@0: michael@0: // Read back the temporary file. michael@0: NetUtil.asyncFetch(channel, fileRead); michael@0: } michael@0: michael@0: function fileExported2() michael@0: { michael@0: ok(false, "exportToFile() did not cancel file overwrite"); michael@0: } michael@0: michael@0: function fileRead(aInputStream, aStatus) michael@0: { michael@0: ok(Components.isSuccessCode(aStatus), michael@0: "the temporary file was read back successfully"); michael@0: michael@0: let updatedContent = michael@0: NetUtil.readInputStreamToString(aInputStream, aInputStream.available());; michael@0: michael@0: is(updatedContent, gFileContent, "file properly updated"); michael@0: michael@0: // Done! michael@0: gFile.remove(false); michael@0: gFile = null; michael@0: gScratchpad = null; michael@0: finish(); michael@0: }