browser/devtools/shared/test/browser_outputparser.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/shared/test/browser_outputparser.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
     1.8 +let {Loader} = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {});
     1.9 +let {OutputParser} = devtools.require("devtools/output-parser");
    1.10 +
    1.11 +let parser;
    1.12 +let doc;
    1.13 +
    1.14 +function test() {
    1.15 +  waitForExplicitFinish();
    1.16 +
    1.17 +  gBrowser.selectedTab = gBrowser.addTab();
    1.18 +  gBrowser.selectedBrowser.addEventListener("load", function onload() {
    1.19 +    gBrowser.selectedBrowser.removeEventListener("load", onload, true);
    1.20 +    waitForFocus(init, content);
    1.21 +    doc = content.document;
    1.22 +  }, true);
    1.23 +
    1.24 +  content.location = "data:text/html,<h1>browser_outputParser.js</h1>" +
    1.25 +                     "<div></div>";
    1.26 +}
    1.27 +
    1.28 +function init() {
    1.29 +  parser = new OutputParser();
    1.30 +  testParseCssProperty();
    1.31 +}
    1.32 +
    1.33 +function testParseCssProperty() {
    1.34 +  let frag = parser.parseCssProperty("border", "1px solid red", {
    1.35 +    colorSwatchClass: "test-colorswatch"
    1.36 +  });
    1.37 +
    1.38 +  let target = doc.querySelector("div");
    1.39 +  ok(target, "captain, we have the div");
    1.40 +  target.appendChild(frag);
    1.41 +
    1.42 +  is(target.innerHTML,
    1.43 +     '1px solid <span style="background-color:red" class="test-colorswatch"></span><span>#F00</span>',
    1.44 +     "CSS property correctly parsed");
    1.45 +
    1.46 +  target.innerHTML = "";
    1.47 +
    1.48 +  let frag = parser.parseCssProperty("background-image", "linear-gradient(to right, #F60 10%, rgba(0,0,0,1))", {
    1.49 +    colorSwatchClass: "test-colorswatch",
    1.50 +    colorClass: "test-color"
    1.51 +  });
    1.52 +  target.appendChild(frag);
    1.53 +  is(target.innerHTML,
    1.54 +     'linear-gradient(to right, <span style="background-color:#F60" class="test-colorswatch"></span><span class="test-color">#F60</span> 10%, ' +
    1.55 +     '<span style="background-color:rgba(0,0,0,1)" class="test-colorswatch"></span><span class="test-color">#000</span>)',
    1.56 +     "Gradient CSS property correctly parsed");
    1.57 +
    1.58 +  target.innerHTML = "";
    1.59 +
    1.60 +  testParseHTMLAttribute();
    1.61 +}
    1.62 +
    1.63 +function testParseHTMLAttribute() {
    1.64 +  let attrib = "color:red; font-size: 12px; background-image: " +
    1.65 +               "url(chrome://branding/content/about-logo.png)";
    1.66 +  let frag = parser.parseHTMLAttribute(attrib, {
    1.67 +    urlClass: "theme-link",
    1.68 +    colorClass: "theme-color"
    1.69 +  });
    1.70 +
    1.71 +  let target = doc.querySelector("div");
    1.72 +  ok(target, "captain, we have the div");
    1.73 +  target.appendChild(frag);
    1.74 +
    1.75 +  let expected = 'color:<span class="theme-color">#F00</span>; font-size: 12px; ' +
    1.76 +                 'background-image: url(\'<a href="chrome://branding/content/about-logo.png" ' +
    1.77 +                 'class="theme-link" ' +
    1.78 +                 'target="_blank">chrome://branding/content/about-logo.png</a>\')';
    1.79 +
    1.80 +  is(target.innerHTML, expected, "HTML Attribute correctly parsed");
    1.81 +  target.innerHTML = "";
    1.82 +  testParseNonCssHTMLAttribute();
    1.83 +}
    1.84 +
    1.85 +function testParseNonCssHTMLAttribute() {
    1.86 +  let attrib = "someclass background someotherclass red";
    1.87 +  let frag = parser.parseHTMLAttribute(attrib);
    1.88 +
    1.89 +  let target = doc.querySelector("div");
    1.90 +  ok(target, "captain, we have the div");
    1.91 +  target.appendChild(frag);
    1.92 +
    1.93 +  let expected = 'someclass background someotherclass red';
    1.94 +
    1.95 +  is(target.innerHTML, expected, "Non-CSS HTML Attribute correctly parsed");
    1.96 +  target.innerHTML = "";
    1.97 +  finishUp();
    1.98 +}
    1.99 +
   1.100 +
   1.101 +function finishUp() {
   1.102 +  Services = Loader = OutputParser = parser = doc = null;
   1.103 +  gBrowser.removeCurrentTab();
   1.104 +  finish();
   1.105 +}

mercurial