tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_styleeditor_filesave.js (2528B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 "use strict";
      4 
      5 // Test that 'Save' function works.
      6 
      7 const TESTCASE_URI_HTML = TEST_BASE_HTTP + "simple.html";
      8 const TESTCASE_URI_CSS = TEST_BASE_HTTP + "simple.css";
      9 
     10 add_task(async function () {
     11  const htmlFile = await copy(TESTCASE_URI_HTML, "simple.html");
     12  await copy(TESTCASE_URI_CSS, "simple.css");
     13  const uri = Services.io.newFileURI(htmlFile);
     14  const filePath = uri.resolve("");
     15 
     16  const { ui } = await openStyleEditorForURL(filePath);
     17 
     18  const editor = ui.editors[0];
     19  await editor.getSourceEditor();
     20 
     21  info("Editing the style sheet.");
     22  let dirty = editor.sourceEditor.once("dirty-change");
     23  const beginCursor = { line: 0, ch: 0 };
     24  editor.sourceEditor.replaceText("DIRTY TEXT", beginCursor, beginCursor);
     25 
     26  await dirty;
     27 
     28  is(editor.sourceEditor.isClean(), false, "Editor is dirty.");
     29  ok(
     30    editor.summary.classList.contains("unsaved"),
     31    "Star icon is present in the corresponding summary."
     32  );
     33 
     34  info("Saving the changes.");
     35  dirty = editor.sourceEditor.once("dirty-change");
     36 
     37  editor.saveToFile(null, function (file) {
     38    ok(file, "file should get saved directly when using a file:// URI");
     39  });
     40 
     41  await dirty;
     42 
     43  is(editor.sourceEditor.isClean(), true, "Editor is clean.");
     44  ok(
     45    !editor.summary.classList.contains("unsaved"),
     46    "Star icon is not present in the corresponding summary."
     47  );
     48 });
     49 
     50 function copy(srcChromeURL, destFileName) {
     51  return new Promise(resolve => {
     52    const destFile = new FileUtils.File(
     53      PathUtils.join(PathUtils.profileDir, destFileName)
     54    );
     55    write(read(srcChromeURL), destFile, resolve);
     56  });
     57 }
     58 
     59 function read(srcChromeURL) {
     60  const scriptableStream = Cc[
     61    "@mozilla.org/scriptableinputstream;1"
     62  ].getService(Ci.nsIScriptableInputStream);
     63 
     64  const channel = NetUtil.newChannel({
     65    uri: srcChromeURL,
     66    loadUsingSystemPrincipal: true,
     67  });
     68  const input = channel.open();
     69  scriptableStream.init(input);
     70 
     71  let data = "";
     72  while (input.available()) {
     73    data = data.concat(scriptableStream.read(input.available()));
     74  }
     75  scriptableStream.close();
     76  input.close();
     77 
     78  return data;
     79 }
     80 
     81 function write(data, file, callback) {
     82  const istream = getInputStream(data);
     83  const ostream = FileUtils.openSafeFileOutputStream(file);
     84 
     85  NetUtil.asyncCopy(istream, ostream, function (status) {
     86    if (!Components.isSuccessCode(status)) {
     87      info("Couldn't write to " + file.path);
     88      return;
     89    }
     90 
     91    callback(file);
     92  });
     93 }