browser/devtools/scratchpad/test/browser_scratchpad_reset_undo.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial