browser/devtools/styleeditor/test/browser_styleeditor_sourcemap_watching.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* vim: set ts=2 et sw=2 tw=80: */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 4
michael@0 5 Components.utils.import("resource://gre/modules/Task.jsm");
michael@0 6 let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
michael@0 7 let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
michael@0 8
michael@0 9 const TESTCASE_URI_HTML = TEST_BASE + "sourcemaps.html";
michael@0 10 const TESTCASE_URI_CSS = TEST_BASE + "sourcemap-css/sourcemaps.css";
michael@0 11 const TESTCASE_URI_CSS2 = TEST_BASE + "sourcemap-css/contained.css";
michael@0 12 const TESTCASE_URI_REG_CSS = TEST_BASE + "simple.css";
michael@0 13 const TESTCASE_URI_SCSS = TEST_BASE + "sourcemap-sass/sourcemaps.scss";
michael@0 14 const TESTCASE_URI_MAP = TEST_BASE + "sourcemap-css/sourcemaps.css.map";
michael@0 15 const TESTCASE_SCSS_NAME = "sourcemaps.scss";
michael@0 16
michael@0 17 const SOURCE_MAP_PREF = "devtools.styleeditor.source-maps-enabled";
michael@0 18 const TRANSITIONS_PREF = "devtools.styleeditor.transitions";
michael@0 19
michael@0 20 const CSS_TEXT = "* { color: blue }";
michael@0 21
michael@0 22 const Cc = Components.classes;
michael@0 23 const Ci = Components.interfaces;
michael@0 24
michael@0 25 let tempScope = {};
michael@0 26 Components.utils.import("resource://gre/modules/FileUtils.jsm", tempScope);
michael@0 27 Components.utils.import("resource://gre/modules/NetUtil.jsm", tempScope);
michael@0 28 let FileUtils = tempScope.FileUtils;
michael@0 29 let NetUtil = tempScope.NetUtil;
michael@0 30
michael@0 31 function test()
michael@0 32 {
michael@0 33 waitForExplicitFinish();
michael@0 34
michael@0 35 Services.prefs.setBoolPref(SOURCE_MAP_PREF, true);
michael@0 36 Services.prefs.setBoolPref(TRANSITIONS_PREF, false);
michael@0 37
michael@0 38 Task.spawn(function() {
michael@0 39 // copy all our files over so we don't screw them up for other tests
michael@0 40 let HTMLFile = yield copy(TESTCASE_URI_HTML, ["sourcemaps.html"]);
michael@0 41 let CSSFile = yield copy(TESTCASE_URI_CSS, ["sourcemap-css", "sourcemaps.css"]);
michael@0 42 let CSSFile2 = yield copy(TESTCASE_URI_CSS2, ["sourcemap-css", "contained.css"]);
michael@0 43 yield copy(TESTCASE_URI_SCSS, ["sourcemap-sass", "sourcemaps.scss"]);
michael@0 44 yield copy(TESTCASE_URI_MAP, ["sourcemap-css", "sourcemaps.css.map"]);
michael@0 45 yield copy(TESTCASE_URI_REG_CSS, ["simple.css"]);
michael@0 46
michael@0 47 let uri = Services.io.newFileURI(HTMLFile);
michael@0 48 let testcaseURI = uri.resolve("");
michael@0 49
michael@0 50 let editor = yield openEditor(testcaseURI);
michael@0 51
michael@0 52 let element = content.document.querySelector("div");
michael@0 53 let style = content.getComputedStyle(element, null);
michael@0 54
michael@0 55 is(style.color, "rgb(255, 0, 102)", "div is red before saving file");
michael@0 56
michael@0 57 editor.styleSheet.relatedStyleSheet.once("style-applied", function() {
michael@0 58 is(style.color, "rgb(0, 0, 255)", "div is blue after saving file");
michael@0 59 finishUp();
michael@0 60 });
michael@0 61
michael@0 62 yield pauseForTimeChange();
michael@0 63
michael@0 64 // Edit and save Sass in the editor. This will start off a file-watching
michael@0 65 // process waiting for the CSS file to change.
michael@0 66 yield editSCSS(editor);
michael@0 67
michael@0 68 // We can't run Sass or another compiler, so we fake it by just
michael@0 69 // directly changing the CSS file.
michael@0 70 yield editCSSFile(CSSFile);
michael@0 71
michael@0 72 info("wrote to CSS file");
michael@0 73 })
michael@0 74 }
michael@0 75
michael@0 76 function openEditor(testcaseURI) {
michael@0 77 let deferred = promise.defer();
michael@0 78
michael@0 79 addTabAndOpenStyleEditors(5, panel => {
michael@0 80 let UI = panel.UI;
michael@0 81
michael@0 82 // wait for 5 editors - 1 for first style sheet, 2 for the
michael@0 83 // generated style sheets, and 2 for original source after it
michael@0 84 // loads and replaces the generated style sheets.
michael@0 85 let editor = UI.editors[1];
michael@0 86 if (getStylesheetNameFor(editor) != TESTCASE_SCSS_NAME) {
michael@0 87 editor = UI.editors[2];
michael@0 88 }
michael@0 89 is(getStylesheetNameFor(editor), TESTCASE_SCSS_NAME, "found scss editor");
michael@0 90
michael@0 91 let link = getLinkFor(editor);
michael@0 92 link.click();
michael@0 93
michael@0 94 editor.getSourceEditor().then(deferred.resolve);
michael@0 95 });
michael@0 96 content.location = testcaseURI;
michael@0 97
michael@0 98 return deferred.promise;
michael@0 99 }
michael@0 100
michael@0 101 function editSCSS(editor) {
michael@0 102 let deferred = promise.defer();
michael@0 103
michael@0 104 let pos = {line: 0, ch: 0};
michael@0 105 editor.sourceEditor.replaceText(CSS_TEXT, pos, pos);
michael@0 106
michael@0 107 editor.saveToFile(null, function (file) {
michael@0 108 ok(file, "Scss file should be saved");
michael@0 109 deferred.resolve();
michael@0 110 });
michael@0 111
michael@0 112 return deferred.promise;
michael@0 113 }
michael@0 114
michael@0 115 function editCSSFile(CSSFile) {
michael@0 116 return write(CSS_TEXT, CSSFile);
michael@0 117 }
michael@0 118
michael@0 119 function pauseForTimeChange() {
michael@0 120 let deferred = promise.defer();
michael@0 121
michael@0 122 // We have to wait for the system time to turn over > 1000 ms so that
michael@0 123 // our file's last change time will show a change. This reflects what
michael@0 124 // would happen in real life with a user manually saving the file.
michael@0 125 setTimeout(deferred.resolve, 2000);
michael@0 126
michael@0 127 return deferred.promise;
michael@0 128 }
michael@0 129
michael@0 130 function finishUp() {
michael@0 131 Services.prefs.clearUserPref(SOURCE_MAP_PREF);
michael@0 132 Services.prefs.clearUserPref(TRANSITIONS_PREF);
michael@0 133 finish();
michael@0 134 }
michael@0 135
michael@0 136 /* Helpers */
michael@0 137
michael@0 138 function getLinkFor(editor) {
michael@0 139 return editor.summary.querySelector(".stylesheet-name");
michael@0 140 }
michael@0 141
michael@0 142 function getStylesheetNameFor(editor) {
michael@0 143 return editor.summary.querySelector(".stylesheet-name > label")
michael@0 144 .getAttribute("value")
michael@0 145 }
michael@0 146
michael@0 147 function copy(aSrcChromeURL, aDestFilePath)
michael@0 148 {
michael@0 149 let destFile = FileUtils.getFile("ProfD", aDestFilePath);
michael@0 150 return write(read(aSrcChromeURL), destFile);
michael@0 151 }
michael@0 152
michael@0 153 function read(aSrcChromeURL)
michael@0 154 {
michael@0 155 let scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"]
michael@0 156 .getService(Ci.nsIScriptableInputStream);
michael@0 157
michael@0 158 let channel = Services.io.newChannel(aSrcChromeURL, null, null);
michael@0 159 let input = channel.open();
michael@0 160 scriptableStream.init(input);
michael@0 161
michael@0 162 let data = "";
michael@0 163 while (input.available()) {
michael@0 164 data = data.concat(scriptableStream.read(input.available()));
michael@0 165 }
michael@0 166 scriptableStream.close();
michael@0 167 input.close();
michael@0 168
michael@0 169 return data;
michael@0 170 }
michael@0 171
michael@0 172 function write(aData, aFile)
michael@0 173 {
michael@0 174 let deferred = promise.defer();
michael@0 175
michael@0 176 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
michael@0 177 .createInstance(Ci.nsIScriptableUnicodeConverter);
michael@0 178
michael@0 179 converter.charset = "UTF-8";
michael@0 180
michael@0 181 let istream = converter.convertToInputStream(aData);
michael@0 182 let ostream = FileUtils.openSafeFileOutputStream(aFile);
michael@0 183
michael@0 184 NetUtil.asyncCopy(istream, ostream, function(status) {
michael@0 185 if (!Components.isSuccessCode(status)) {
michael@0 186 info("Coudln't write to " + aFile.path);
michael@0 187 return;
michael@0 188 }
michael@0 189 deferred.resolve(aFile);
michael@0 190 });
michael@0 191
michael@0 192 return deferred.promise;
michael@0 193 }

mercurial