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 751744 */
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 object.
13 let gScratchpad;
15 // Reference to the temporary nsIFiles.
16 let gFile;
18 // Temporary file name.
19 let gFileName = "testFileForBug751744.tmp"
22 // Content for the temporary file.
23 let gFileContent = "/* this file is already saved */\n" +
24 "function foo() { alert('bar') }";
25 let gLength = gFileContent.length;
27 // Reference to the menu entry.
28 let menu;
30 function startTest()
31 {
32 gScratchpad = gScratchpadWindow.Scratchpad;
33 menu = gScratchpadWindow.document.getElementById("sp-cmd-revert");
34 createAndLoadTemporaryFile();
35 }
37 function testAfterSaved() {
38 // Check if the revert menu is disabled as the file is at saved state.
39 ok(menu.hasAttribute("disabled"), "The revert menu entry is disabled.");
41 // chancging the text in the file
42 gScratchpad.setText(gScratchpad.getText() + "\nfoo();");
43 // Checking the text got changed
44 is(gScratchpad.getText(), gFileContent + "\nfoo();",
45 "The text changed the first time.");
47 // Revert menu now should be enabled.
48 ok(!menu.hasAttribute("disabled"),
49 "The revert menu entry is enabled after changing text first time");
51 // reverting back to last saved state.
52 gScratchpad.revertFile(testAfterRevert);
53 }
55 function testAfterRevert() {
56 // Check if the file's text got reverted
57 is(gScratchpad.getText(), gFileContent,
58 "The text reverted back to original text.");
59 // The revert menu should be disabled again.
60 ok(menu.hasAttribute("disabled"),
61 "The revert menu entry is disabled after reverting.");
63 // chancging the text in the file again
64 gScratchpad.setText(gScratchpad.getText() + "\nalert(foo.toSource());");
65 // Saving the file.
66 gScratchpad.saveFile(testAfterSecondSave);
67 }
69 function testAfterSecondSave() {
70 // revert menu entry should be disabled.
71 ok(menu.hasAttribute("disabled"),
72 "The revert menu entry is disabled after saving.");
74 // changing the text.
75 gScratchpad.setText(gScratchpad.getText() + "\nfoo();");
77 // revert menu entry should get enabled yet again.
78 ok(!menu.hasAttribute("disabled"),
79 "The revert menu entry is enabled after changing text third time");
81 // reverting back to last saved state.
82 gScratchpad.revertFile(testAfterSecondRevert);
83 }
85 function testAfterSecondRevert() {
86 // Check if the file's text got reverted
87 is(gScratchpad.getText(), gFileContent + "\nalert(foo.toSource());",
88 "The text reverted back to the changed saved text.");
89 // The revert menu should be disabled again.
90 ok(menu.hasAttribute("disabled"),
91 "Revert menu entry is disabled after reverting to changed saved state.");
92 gFile.remove(false);
93 gFile = gScratchpad = menu = null;
94 finish();
95 }
97 function createAndLoadTemporaryFile()
98 {
99 // Create a temporary file.
100 gFile = FileUtils.getFile("TmpD", [gFileName]);
101 gFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
103 // Write the temporary file.
104 let fout = Cc["@mozilla.org/network/file-output-stream;1"].
105 createInstance(Ci.nsIFileOutputStream);
106 fout.init(gFile.QueryInterface(Ci.nsILocalFile), 0x02 | 0x08 | 0x20,
107 0o644, fout.DEFER_OPEN);
109 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
110 createInstance(Ci.nsIScriptableUnicodeConverter);
111 converter.charset = "UTF-8";
112 let fileContentStream = converter.convertToInputStream(gFileContent);
114 NetUtil.asyncCopy(fileContentStream, fout, tempFileSaved);
115 }
117 function tempFileSaved(aStatus)
118 {
119 ok(Components.isSuccessCode(aStatus),
120 "the temporary file was saved successfully");
122 // Import the file into Scratchpad.
123 gScratchpad.setFilename(gFile.path);
124 gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
125 testAfterSaved);
126 }
128 function test()
129 {
130 waitForExplicitFinish();
132 gBrowser.selectedTab = gBrowser.addTab();
133 gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
134 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
135 openScratchpad(startTest);
136 }, true);
138 content.location = "data:text/html,<p>test reverting to last saved state of" +
139 " a file </p>";
140 }