|
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 */ |
|
5 |
|
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; |
|
11 |
|
12 // Reference to the Scratchpad chrome window object. |
|
13 let gScratchpadWindow; |
|
14 |
|
15 // Reference to the Scratchpad object. |
|
16 let gScratchpad; |
|
17 |
|
18 // Reference to the temporary nsIFile we will work with. |
|
19 let gFileA; |
|
20 let gFileB; |
|
21 |
|
22 // The temporary file content. |
|
23 let gFileAContent = "// File A ** Hello World!"; |
|
24 let gFileBContent = "// File B ** Goodbye All"; |
|
25 |
|
26 // Help track if one or both files are saved |
|
27 let gFirstFileSaved = false; |
|
28 |
|
29 function test() |
|
30 { |
|
31 waitForExplicitFinish(); |
|
32 |
|
33 gBrowser.selectedTab = gBrowser.addTab(); |
|
34 gBrowser.selectedBrowser.addEventListener("load", function browserLoad() { |
|
35 gBrowser.selectedBrowser.removeEventListener("load", browserLoad, true); |
|
36 openScratchpad(runTests); |
|
37 }, true); |
|
38 |
|
39 content.location = "data:text/html,<p>test that undo get's reset after file load in Scratchpad"; |
|
40 } |
|
41 |
|
42 function runTests() |
|
43 { |
|
44 gScratchpad = gScratchpadWindow.Scratchpad; |
|
45 |
|
46 // Create a temporary file. |
|
47 gFileA = FileUtils.getFile("TmpD", ["fileAForBug684546.tmp"]); |
|
48 gFileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); |
|
49 |
|
50 gFileB = FileUtils.getFile("TmpD", ["fileBForBug684546.tmp"]); |
|
51 gFileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666); |
|
52 |
|
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); |
|
58 |
|
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); |
|
63 |
|
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); |
|
69 |
|
70 NetUtil.asyncCopy(fileContentStreamA, foutA, tempFileSaved); |
|
71 NetUtil.asyncCopy(fileContentStreamB, foutB, tempFileSaved); |
|
72 } |
|
73 |
|
74 function tempFileSaved(aStatus) |
|
75 { |
|
76 let success = Components.isSuccessCode(aStatus); |
|
77 |
|
78 ok(success, "a temporary file was saved successfully"); |
|
79 |
|
80 if (!success) |
|
81 { |
|
82 finish(); |
|
83 return; |
|
84 } |
|
85 |
|
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 } |
|
95 |
|
96 function fileAImported(aStatus, aFileContent) |
|
97 { |
|
98 ok(Components.isSuccessCode(aStatus), |
|
99 "the temporary file A was imported successfully with Scratchpad"); |
|
100 |
|
101 is(aFileContent, gFileAContent, "received data is correct"); |
|
102 |
|
103 is(gScratchpad.getText(), gFileAContent, "the editor content is correct"); |
|
104 |
|
105 gScratchpad.editor.replaceText("new text", |
|
106 gScratchpad.editor.getPosition(gScratchpad.getText().length)); |
|
107 |
|
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"); |
|
113 |
|
114 // Import the file B into Scratchpad. |
|
115 gScratchpad.importFromFile(gFileB.QueryInterface(Ci.nsILocalFile), true, |
|
116 fileBImported); |
|
117 } |
|
118 |
|
119 function fileBImported(aStatus, aFileContent) |
|
120 { |
|
121 ok(Components.isSuccessCode(aStatus), |
|
122 "the temporary file B was imported successfully with Scratchpad"); |
|
123 |
|
124 is(aFileContent, gFileBContent, "received data is correct"); |
|
125 |
|
126 is(gScratchpad.getText(), gFileBContent, "the editor content is correct"); |
|
127 |
|
128 ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load"); |
|
129 |
|
130 gScratchpad.undo(); |
|
131 is(gScratchpad.getText(), gFileBContent, |
|
132 "the editor content is still correct after undo"); |
|
133 |
|
134 gScratchpad.editor.replaceText("new text", |
|
135 gScratchpad.editor.getPosition(gScratchpad.getText().length)); |
|
136 is(gScratchpad.getText(), gFileBContent + "new text", "text updated correctly"); |
|
137 |
|
138 gScratchpad.undo(); |
|
139 is(gScratchpad.getText(), gFileBContent, "undo works"); |
|
140 ok(!gScratchpad.editor.canUndo(), "editor cannot undo after load (again)"); |
|
141 |
|
142 gScratchpad.redo(); |
|
143 is(gScratchpad.getText(), gFileBContent + "new text", "redo works"); |
|
144 |
|
145 // Done! |
|
146 finish(); |
|
147 } |
|
148 |
|
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 }); |