summaryrefslogtreecommitdiffstats
path: root/devtools/client/styleeditor/test/browser_styleeditor_filesave.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /devtools/client/styleeditor/test/browser_styleeditor_filesave.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/styleeditor/test/browser_styleeditor_filesave.js')
-rw-r--r--devtools/client/styleeditor/test/browser_styleeditor_filesave.js93
1 files changed, 93 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 0000000000..d59137af25
--- /dev/null
+++ b/devtools/client/styleeditor/test/browser_styleeditor_filesave.js
@@ -0,0 +1,93 @@
+/* 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";
+
+add_task(async function () {
+ const htmlFile = await copy(TESTCASE_URI_HTML, "simple.html");
+ await copy(TESTCASE_URI_CSS, "simple.css");
+ const uri = Services.io.newFileURI(htmlFile);
+ const filePath = uri.resolve("");
+
+ const { ui } = await openStyleEditorForURL(filePath);
+
+ const editor = ui.editors[0];
+ await editor.getSourceEditor();
+
+ info("Editing the style sheet.");
+ let dirty = editor.sourceEditor.once("dirty-change");
+ const beginCursor = { line: 0, ch: 0 };
+ editor.sourceEditor.replaceText("DIRTY TEXT", beginCursor, beginCursor);
+
+ await 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");
+ });
+
+ await 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) {
+ return new Promise(resolve => {
+ const destFile = new FileUtils.File(
+ PathUtils.join(PathUtils.profileDir, destFileName)
+ );
+ write(read(srcChromeURL), destFile, resolve);
+ });
+}
+
+function read(srcChromeURL) {
+ const scriptableStream = Cc[
+ "@mozilla.org/scriptableinputstream;1"
+ ].getService(Ci.nsIScriptableInputStream);
+
+ const channel = NetUtil.newChannel({
+ uri: srcChromeURL,
+ loadUsingSystemPrincipal: true,
+ });
+ const input = channel.open();
+ 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) {
+ const istream = getInputStream(data);
+ const ostream = FileUtils.openSafeFileOutputStream(file);
+
+ NetUtil.asyncCopy(istream, ostream, function (status) {
+ if (!Components.isSuccessCode(status)) {
+ info("Couldn't write to " + file.path);
+ return;
+ }
+
+ callback(file);
+ });
+}