diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /devtools/client/styleeditor/test/browser_styleeditor_filesave.js | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/client/styleeditor/test/browser_styleeditor_filesave.js')
-rw-r--r-- | devtools/client/styleeditor/test/browser_styleeditor_filesave.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/devtools/client/styleeditor/test/browser_styleeditor_filesave.js b/devtools/client/styleeditor/test/browser_styleeditor_filesave.js new file mode 100644 index 000000000..b04b74c80 --- /dev/null +++ b/devtools/client/styleeditor/test/browser_styleeditor_filesave.js @@ -0,0 +1,99 @@ +/* vim: set ts=2 et sw=2 tw=80: */ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ +"use strict"; + +// Test that 'Save' function works. + +const TESTCASE_URI_HTML = TEST_BASE_HTTP + "simple.html"; +const TESTCASE_URI_CSS = TEST_BASE_HTTP + "simple.css"; + +var tempScope = {}; +Components.utils.import("resource://gre/modules/FileUtils.jsm", tempScope); +Components.utils.import("resource://gre/modules/NetUtil.jsm", tempScope); +var FileUtils = tempScope.FileUtils; +var NetUtil = tempScope.NetUtil; + +add_task(function* () { + let htmlFile = yield copy(TESTCASE_URI_HTML, "simple.html"); + yield copy(TESTCASE_URI_CSS, "simple.css"); + let uri = Services.io.newFileURI(htmlFile); + let filePath = uri.resolve(""); + + let { ui } = yield openStyleEditorForURL(filePath); + + let editor = ui.editors[0]; + yield editor.getSourceEditor(); + + info("Editing the style sheet."); + let dirty = editor.sourceEditor.once("dirty-change"); + let beginCursor = {line: 0, ch: 0}; + editor.sourceEditor.replaceText("DIRTY TEXT", beginCursor, beginCursor); + + yield dirty; + + is(editor.sourceEditor.isClean(), false, "Editor is dirty."); + ok(editor.summary.classList.contains("unsaved"), + "Star icon is present in the corresponding summary."); + + info("Saving the changes."); + dirty = editor.sourceEditor.once("dirty-change"); + + editor.saveToFile(null, function (file) { + ok(file, "file should get saved directly when using a file:// URI"); + }); + + yield dirty; + + is(editor.sourceEditor.isClean(), true, "Editor is clean."); + ok(!editor.summary.classList.contains("unsaved"), + "Star icon is not present in the corresponding summary."); +}); + +function copy(srcChromeURL, destFileName) { + let deferred = defer(); + let destFile = FileUtils.getFile("ProfD", [destFileName]); + write(read(srcChromeURL), destFile, deferred.resolve); + + return deferred.promise; +} + +function read(srcChromeURL) { + let scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"] + .getService(Ci.nsIScriptableInputStream); + + let channel = NetUtil.newChannel({ + uri: srcChromeURL, + loadUsingSystemPrincipal: true + }); + let input = channel.open2(); + scriptableStream.init(input); + + let data = ""; + while (input.available()) { + data = data.concat(scriptableStream.read(input.available())); + } + scriptableStream.close(); + input.close(); + + return data; +} + +function write(data, file, callback) { + let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] + .createInstance(Ci.nsIScriptableUnicodeConverter); + + converter.charset = "UTF-8"; + + let istream = converter.convertToInputStream(data); + let ostream = FileUtils.openSafeFileOutputStream(file); + + NetUtil.asyncCopy(istream, ostream, function (status) { + if (!Components.isSuccessCode(status)) { + info("Couldn't write to " + file.path); + return; + } + + callback(file); + }); +} |