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/ */
5 let tempScope = {};
6 Cu.import("resource://gre/modules/NetUtil.jsm", tempScope);
7 let NetUtil = tempScope.NetUtil;
9 // Reference to the Scratchpad object.
10 let gScratchpad;
12 // Reference to the temporary nsIFile we will work with.
13 let gFile;
15 // The temporary file content.
16 let gFileContent = "hello.world('bug636725');";
18 function test()
19 {
20 waitForExplicitFinish();
22 gBrowser.selectedTab = gBrowser.addTab();
23 gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
24 gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
25 openScratchpad(runTests);
26 }, true);
28 content.location = "data:text/html,<p>test file open and save in Scratchpad";
29 }
31 function runTests()
32 {
33 gScratchpad = gScratchpadWindow.Scratchpad;
35 createTempFile("fileForBug636725.tmp", gFileContent, function(aStatus, aFile) {
36 ok(Components.isSuccessCode(aStatus),
37 "The temporary file was saved successfully");
39 gFile = aFile;
40 gScratchpad.importFromFile(gFile.QueryInterface(Ci.nsILocalFile), true,
41 fileImported);
42 });
43 }
45 function fileImported(aStatus, aFileContent)
46 {
47 ok(Components.isSuccessCode(aStatus),
48 "the temporary file was imported successfully with Scratchpad");
50 is(aFileContent, gFileContent,
51 "received data is correct");
53 is(gScratchpad.getText(), gFileContent,
54 "the editor content is correct");
56 is(gScratchpad.dirty, false,
57 "the editor marks imported file as saved");
59 // Save the file after changes.
60 gFileContent += "// omg, saved!";
61 gScratchpad.editor.setText(gFileContent);
63 gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), true, true,
64 fileExported);
65 }
67 function fileExported(aStatus)
68 {
69 ok(Components.isSuccessCode(aStatus),
70 "the temporary file was exported successfully with Scratchpad");
72 let oldContent = gFileContent;
74 // Attempt another file save, with confirmation which returns false.
75 gFileContent += "// omg, saved twice!";
76 gScratchpad.editor.setText(gFileContent);
78 let oldConfirm = gScratchpadWindow.confirm;
79 let askedConfirmation = false;
80 gScratchpadWindow.confirm = function() {
81 askedConfirmation = true;
82 return false;
83 };
85 gScratchpad.exportToFile(gFile.QueryInterface(Ci.nsILocalFile), false, true,
86 fileExported2);
88 gScratchpadWindow.confirm = oldConfirm;
90 ok(askedConfirmation, "exportToFile() asked for overwrite confirmation");
92 gFileContent = oldContent;
94 let channel = NetUtil.newChannel(gFile);
95 channel.contentType = "application/javascript";
97 // Read back the temporary file.
98 NetUtil.asyncFetch(channel, fileRead);
99 }
101 function fileExported2()
102 {
103 ok(false, "exportToFile() did not cancel file overwrite");
104 }
106 function fileRead(aInputStream, aStatus)
107 {
108 ok(Components.isSuccessCode(aStatus),
109 "the temporary file was read back successfully");
111 let updatedContent =
112 NetUtil.readInputStreamToString(aInputStream, aInputStream.available());;
114 is(updatedContent, gFileContent, "file properly updated");
116 // Done!
117 gFile.remove(false);
118 gFile = null;
119 gScratchpad = null;
120 finish();
121 }