1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_reset_undo.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +/* vim: set ts=2 et sw=2 tw=80: */ 1.5 +/* Any copyright is dedicated to the Public Domain. 1.6 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.7 +/* Bug 684546 */ 1.8 + 1.9 +let tempScope = {}; 1.10 +Cu.import("resource://gre/modules/NetUtil.jsm", tempScope); 1.11 +Cu.import("resource://gre/modules/FileUtils.jsm", tempScope); 1.12 +let NetUtil = tempScope.NetUtil; 1.13 +let FileUtils = tempScope.FileUtils; 1.14 + 1.15 +// Reference to the Scratchpad chrome window object. 1.16 +let gScratchpadWindow; 1.17 + 1.18 +// Reference to the Scratchpad object. 1.19 +let gScratchpad; 1.20 + 1.21 +// Reference to the temporary nsIFile we will work with. 1.22 +let gFileA; 1.23 +let gFileB; 1.24 + 1.25 +// The temporary file content. 1.26 +let gFileAContent = "// File A ** Hello World!"; 1.27 +let gFileBContent = "// File B ** Goodbye All"; 1.28 + 1.29 +// Help track if one or both files are saved 1.30 +let gFirstFileSaved = false; 1.31 + 1.32 +function test() 1.33 +{ 1.34 + waitForExplicitFinish(); 1.35 + 1.36 + gBrowser.selectedTab = gBrowser.addTab(); 1.37 + gBrowser.selectedBrowser.addEventListener("load", function browserLoad() { 1.38 + gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true); 1.39 + openScratchpad(runTests); 1.40 + }, true); 1.41 + 1.42 + content.location = "data:text/html,<p>test that undo get's reset after file load in Scratchpad"; 1.43 +} 1.44 + 1.45 +function runTests() 1.46 +{ 1.47 + gScratchpad = gScratchpadWindow.Scratchpad; 1.48 + 1.49 + // Create a temporary file. 1.50 + gFileA = FileUtils.getFile("TmpD", ["fileAForBug684546.tmp"]); 1.51 + gFileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); 1.52 + 1.53 + gFileB = FileUtils.getFile("TmpD", ["fileBForBug684546.tmp"]); 1.54 + gFileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); 1.55 + 1.56 + // Write the temporary file. 1.57 + let foutA = Cc["@mozilla.org/network/file-output-stream;1"]. 1.58 + createInstance(Ci.nsIFileOutputStream); 1.59 + foutA.init(gFileA.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, 1.60 + 0o644, foutA.DEFER_OPEN); 1.61 + 1.62 + let foutB = Cc["@mozilla.org/network/file-output-stream;1"]. 1.63 + createInstance(Ci.nsIFileOutputStream); 1.64 + foutB.init(gFileB.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20, 1.65 + 0o644, foutB.DEFER_OPEN); 1.66 + 1.67 + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. 1.68 + createInstance(Ci.nsIScriptableUnicodeConverter); 1.69 + converter.charset = "UTF-8"; 1.70 + let fileContentStreamA = converter.convertToInputStream(gFileAContent); 1.71 + let fileContentStreamB = converter.convertToInputStream(gFileBContent); 1.72 + 1.73 + NetUtil.asyncCopy(fileContentStreamA, foutA, tempFileSaved); 1.74 + NetUtil.asyncCopy(fileContentStreamB, foutB, tempFileSaved); 1.75 +} 1.76 + 1.77 +function tempFileSaved(aStatus) 1.78 +{ 1.79 + let success = Components.isSuccessCode(aStatus); 1.80 + 1.81 + ok(success, "a temporary file was saved successfully"); 1.82 + 1.83 + if (!success) 1.84 + { 1.85 + finish(); 1.86 + return; 1.87 + } 1.88 + 1.89 + if (gFirstFileSaved && success) 1.90 + { 1.91 + ok((gFirstFileSaved && success), "Both files loaded"); 1.92 + // Import the file A into Scratchpad. 1.93 + gScratchpad.importFromFile(gFileA.QueryInterface(Ci.nsILocalFile), true, 1.94 + fileAImported); 1.95 + } 1.96 + gFirstFileSaved = success; 1.97 +} 1.98 + 1.99 +function fileAImported(aStatus, aFileContent) 1.100 +{ 1.101 + ok(Components.isSuccessCode(aStatus), 1.102 + "the temporary file A was imported successfully with Scratchpad"); 1.103 + 1.104 + is(aFileContent, gFileAContent, "received data is correct"); 1.105 + 1.106 + is(gScratchpad.getText(), gFileAContent, "the editor content is correct"); 1.107 + 1.108 + gScratchpad.editor.replaceText("new text", 1.109 + gScratchpad.editor.getPosition(gScratchpad.getText().length)); 1.110 + 1.111 + is(gScratchpad.getText(), gFileAContent + "new text", "text updated correctly"); 1.112 + gScratchpad.undo(); 1.113 + is(gScratchpad.getText(), gFileAContent, "undo works"); 1.114 + gScratchpad.redo(); 1.115 + is(gScratchpad.getText(), gFileAContent + "new text", "redo works"); 1.116 + 1.117 + // Import the file B into Scratchpad. 1.118 + gScratchpad.importFromFile(gFileB.QueryInterface(Ci.nsILocalFile), true, 1.119 + fileBImported); 1.120 +} 1.121 + 1.122 +function fileBImported(aStatus, aFileContent) 1.123 +{ 1.124 + ok(Components.isSuccessCode(aStatus), 1.125 + "the temporary file B was imported successfully with Scratchpad"); 1.126 + 1.127 + is(aFileContent, gFileBContent, "received data is correct"); 1.128 + 1.129 + is(gScratchpad.getText(), gFileBContent, "the editor content is correct"); 1.130 + 1.131 + ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load"); 1.132 + 1.133 + gScratchpad.undo(); 1.134 + is(gScratchpad.getText(), gFileBContent, 1.135 + "the editor content is still correct after undo"); 1.136 + 1.137 + gScratchpad.editor.replaceText("new text", 1.138 + gScratchpad.editor.getPosition(gScratchpad.getText().length)); 1.139 + is(gScratchpad.getText(), gFileBContent + "new text", "text updated correctly"); 1.140 + 1.141 + gScratchpad.undo(); 1.142 + is(gScratchpad.getText(), gFileBContent, "undo works"); 1.143 + ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load (again)"); 1.144 + 1.145 + gScratchpad.redo(); 1.146 + is(gScratchpad.getText(), gFileBContent + "new text", "redo works"); 1.147 + 1.148 + // Done! 1.149 + finish(); 1.150 +} 1.151 + 1.152 +registerCleanupFunction(function() { 1.153 + if (gFileA && gFileA.exists()) 1.154 + { 1.155 + gFileA.remove(false); 1.156 + gFileA = null; 1.157 + } 1.158 + if (gFileB && gFileB.exists()) 1.159 + { 1.160 + gFileB.remove(false); 1.161 + gFileB = null; 1.162 + } 1.163 + gScratchpad = null; 1.164 +});