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