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