browser/devtools/styleeditor/test/browser_styleeditor_filesave.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/styleeditor/test/browser_styleeditor_filesave.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* vim: set ts=2 et sw=2 tw=80: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +
     1.8 +const TESTCASE_URI_HTML = TEST_BASE + "simple.html";
     1.9 +const TESTCASE_URI_CSS = TEST_BASE + "simple.css";
    1.10 +
    1.11 +const Cc = Components.classes;
    1.12 +const Ci = Components.interfaces;
    1.13 +
    1.14 +let tempScope = {};
    1.15 +Components.utils.import("resource://gre/modules/FileUtils.jsm", tempScope);
    1.16 +Components.utils.import("resource://gre/modules/NetUtil.jsm", tempScope);
    1.17 +let FileUtils = tempScope.FileUtils;
    1.18 +let NetUtil = tempScope.NetUtil;
    1.19 +
    1.20 +function test()
    1.21 +{
    1.22 +  waitForExplicitFinish();
    1.23 +
    1.24 +  copy(TESTCASE_URI_HTML, "simple.html", function(htmlFile) {
    1.25 +    copy(TESTCASE_URI_CSS, "simple.css", function(cssFile) {
    1.26 +      addTabAndOpenStyleEditors(1, function(panel) {
    1.27 +        let UI = panel.UI;
    1.28 +        let editor = UI.editors[0];
    1.29 +        editor.getSourceEditor().then(runTests.bind(this, editor));
    1.30 +      });
    1.31 +
    1.32 +      let uri = Services.io.newFileURI(htmlFile);
    1.33 +      let filePath = uri.resolve("");
    1.34 +      content.location = filePath;
    1.35 +    });
    1.36 +  });
    1.37 +}
    1.38 +
    1.39 +function runTests(editor)
    1.40 +{
    1.41 +  editor.sourceEditor.once("dirty-change", () => {
    1.42 +    is(editor.sourceEditor.isClean(), false, "Editor is dirty.");
    1.43 +    ok(editor.summary.classList.contains("unsaved"),
    1.44 +       "Star icon is present in the corresponding summary.");
    1.45 +  });
    1.46 +  let beginCursor = {line: 0, ch: 0};
    1.47 +  editor.sourceEditor.replaceText("DIRTY TEXT", beginCursor, beginCursor);
    1.48 +
    1.49 +  editor.sourceEditor.once("dirty-change", () => {
    1.50 +    is(editor.sourceEditor.isClean(), true, "Editor is clean.");
    1.51 +    ok(!editor.summary.classList.contains("unsaved"),
    1.52 +       "Star icon is not present in the corresponding summary.");
    1.53 +    finish();
    1.54 +  });
    1.55 +  editor.saveToFile(null, function (file) {
    1.56 +    ok(file, "file should get saved directly when using a file:// URI");
    1.57 +  });
    1.58 +}
    1.59 +
    1.60 +function copy(aSrcChromeURL, aDestFileName, aCallback)
    1.61 +{
    1.62 +  let destFile = FileUtils.getFile("ProfD", [aDestFileName]);
    1.63 +  write(read(aSrcChromeURL), destFile, aCallback);
    1.64 +}
    1.65 +
    1.66 +function read(aSrcChromeURL)
    1.67 +{
    1.68 +  let scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"]
    1.69 +    .getService(Ci.nsIScriptableInputStream);
    1.70 +
    1.71 +  let channel = Services.io.newChannel(aSrcChromeURL, null, null);
    1.72 +  let input = channel.open();
    1.73 +  scriptableStream.init(input);
    1.74 +
    1.75 +  let data = "";
    1.76 +  while (input.available()) {
    1.77 +    data = data.concat(scriptableStream.read(input.available()));
    1.78 +  }
    1.79 +  scriptableStream.close();
    1.80 +  input.close();
    1.81 +
    1.82 +  return data;
    1.83 +}
    1.84 +
    1.85 +function write(aData, aFile, aCallback)
    1.86 +{
    1.87 +  let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
    1.88 +    .createInstance(Ci.nsIScriptableUnicodeConverter);
    1.89 +
    1.90 +  converter.charset = "UTF-8";
    1.91 +
    1.92 +  let istream = converter.convertToInputStream(aData);
    1.93 +  let ostream = FileUtils.openSafeFileOutputStream(aFile);
    1.94 +
    1.95 +  NetUtil.asyncCopy(istream, ostream, function(status) {
    1.96 +    if (!Components.isSuccessCode(status)) {
    1.97 +      info("Coudln't write to " + aFile.path);
    1.98 +      return;
    1.99 +    }
   1.100 +
   1.101 +    aCallback(aFile);
   1.102 +  });
   1.103 +}

mercurial