|
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 const TESTCASE_URI_HTML = TEST_BASE + "simple.html"; |
|
6 const TESTCASE_URI_CSS = TEST_BASE + "simple.css"; |
|
7 |
|
8 const Cc = Components.classes; |
|
9 const Ci = Components.interfaces; |
|
10 |
|
11 let tempScope = {}; |
|
12 Components.utils.import("resource://gre/modules/FileUtils.jsm", tempScope); |
|
13 Components.utils.import("resource://gre/modules/NetUtil.jsm", tempScope); |
|
14 let FileUtils = tempScope.FileUtils; |
|
15 let NetUtil = tempScope.NetUtil; |
|
16 |
|
17 function test() |
|
18 { |
|
19 waitForExplicitFinish(); |
|
20 |
|
21 copy(TESTCASE_URI_HTML, "simple.html", function(htmlFile) { |
|
22 copy(TESTCASE_URI_CSS, "simple.css", function(cssFile) { |
|
23 addTabAndOpenStyleEditors(1, function(panel) { |
|
24 let UI = panel.UI; |
|
25 let editor = UI.editors[0]; |
|
26 editor.getSourceEditor().then(runTests.bind(this, editor)); |
|
27 }); |
|
28 |
|
29 let uri = Services.io.newFileURI(htmlFile); |
|
30 let filePath = uri.resolve(""); |
|
31 content.location = filePath; |
|
32 }); |
|
33 }); |
|
34 } |
|
35 |
|
36 function runTests(editor) |
|
37 { |
|
38 editor.sourceEditor.once("dirty-change", () => { |
|
39 is(editor.sourceEditor.isClean(), false, "Editor is dirty."); |
|
40 ok(editor.summary.classList.contains("unsaved"), |
|
41 "Star icon is present in the corresponding summary."); |
|
42 }); |
|
43 let beginCursor = {line: 0, ch: 0}; |
|
44 editor.sourceEditor.replaceText("DIRTY TEXT", beginCursor, beginCursor); |
|
45 |
|
46 editor.sourceEditor.once("dirty-change", () => { |
|
47 is(editor.sourceEditor.isClean(), true, "Editor is clean."); |
|
48 ok(!editor.summary.classList.contains("unsaved"), |
|
49 "Star icon is not present in the corresponding summary."); |
|
50 finish(); |
|
51 }); |
|
52 editor.saveToFile(null, function (file) { |
|
53 ok(file, "file should get saved directly when using a file:// URI"); |
|
54 }); |
|
55 } |
|
56 |
|
57 function copy(aSrcChromeURL, aDestFileName, aCallback) |
|
58 { |
|
59 let destFile = FileUtils.getFile("ProfD", [aDestFileName]); |
|
60 write(read(aSrcChromeURL), destFile, aCallback); |
|
61 } |
|
62 |
|
63 function read(aSrcChromeURL) |
|
64 { |
|
65 let scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"] |
|
66 .getService(Ci.nsIScriptableInputStream); |
|
67 |
|
68 let channel = Services.io.newChannel(aSrcChromeURL, null, null); |
|
69 let input = channel.open(); |
|
70 scriptableStream.init(input); |
|
71 |
|
72 let data = ""; |
|
73 while (input.available()) { |
|
74 data = data.concat(scriptableStream.read(input.available())); |
|
75 } |
|
76 scriptableStream.close(); |
|
77 input.close(); |
|
78 |
|
79 return data; |
|
80 } |
|
81 |
|
82 function write(aData, aFile, aCallback) |
|
83 { |
|
84 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] |
|
85 .createInstance(Ci.nsIScriptableUnicodeConverter); |
|
86 |
|
87 converter.charset = "UTF-8"; |
|
88 |
|
89 let istream = converter.convertToInputStream(aData); |
|
90 let ostream = FileUtils.openSafeFileOutputStream(aFile); |
|
91 |
|
92 NetUtil.asyncCopy(istream, ostream, function(status) { |
|
93 if (!Components.isSuccessCode(status)) { |
|
94 info("Coudln't write to " + aFile.path); |
|
95 return; |
|
96 } |
|
97 |
|
98 aCallback(aFile); |
|
99 }); |
|
100 } |