browser/devtools/sourceeditor/test/browser_css_autocompletion.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/sourceeditor/test/browser_css_autocompletion.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     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 +"use strict";
     1.9 +
    1.10 +const cssAutoCompleter  = require("devtools/sourceeditor/css-autocompleter");
    1.11 +const {InspectorFront} = require("devtools/server/actors/inspector");
    1.12 +const { Cc, Ci } = require("chrome");
    1.13 +
    1.14 +const CSS_URI = "http://mochi.test:8888/browser/browser/devtools/sourceeditor" +
    1.15 +                "/test/css_statemachine_testcases.css";
    1.16 +const TESTS_URI = "http://mochi.test:8888/browser/browser/devtools/sourceeditor" +
    1.17 +                  "/test/css_autocompletion_tests.json";
    1.18 +
    1.19 +const source = read(CSS_URI);
    1.20 +const tests = eval(read(TESTS_URI));
    1.21 +
    1.22 +const TEST_URI = "data:text/html;charset=UTF-8," + encodeURIComponent(
    1.23 +  ["<!DOCTYPE html>",
    1.24 +   "<html>",
    1.25 +   " <head>",
    1.26 +   "  <title>CSS State machine tests.</title>",
    1.27 +   "  <style type='text/css'>",
    1.28 +   "#progress {",
    1.29 +   "  width: 500px; height: 30px;",
    1.30 +   "  border: 1px solid black;",
    1.31 +   "  position: relative",
    1.32 +   "}",
    1.33 +   "#progress div {",
    1.34 +   "  width: 0%; height: 100%;",
    1.35 +   "  background: green;",
    1.36 +   "  position: absolute;",
    1.37 +   "  z-index: -1; top: 0",
    1.38 +   "}",
    1.39 +   "#progress.failed div {",
    1.40 +   "  background: red !important;",
    1.41 +   "}",
    1.42 +   "#progress.failed:after {",
    1.43 +   "  content: 'Some tests failed';",
    1.44 +   "  color: white",
    1.45 +   "}",
    1.46 +   "#progress:before {",
    1.47 +   "  content: 'Running test ' attr(data-progress) ' of " + tests.length + "';",
    1.48 +   "  color: white;",
    1.49 +   "  text-shadow: 0 0 2px darkgreen;",
    1.50 +   "}",
    1.51 +   "  </style>",
    1.52 +   " </head>",
    1.53 +   " <body>",
    1.54 +   "  <h2>State machine tests for CSS autocompleter.</h2><br>",
    1.55 +   "  <div id='progress' data-progress='0'>",
    1.56 +   "   <div></div>",
    1.57 +   "  </div>",
    1.58 +   "  <div id='devtools-menu' class='devtools-toolbarbutton'></div>",
    1.59 +   "  <div id='devtools-toolbarbutton' class='devtools-menulist'></div>",
    1.60 +   "  <div id='devtools-anotherone'></div>",
    1.61 +   "  <div id='devtools-yetagain'></div>",
    1.62 +   "  <div id='devtools-itjustgoeson'></div>",
    1.63 +   "  <div id='devtools-okstopitnow'></div>",
    1.64 +   "  <div class='hidden-labels-box devtools-toolbarbutton devtools-menulist'></div>",
    1.65 +   "  <div class='devtools-menulist'></div>",
    1.66 +   "  <div class='devtools-menulist'></div>",
    1.67 +   "  <tabs class='devtools-toolbarbutton'><tab></tab><tab></tab><tab></tab></tabs><tabs></tabs>",
    1.68 +   "  <button class='category-name visible'></button>",
    1.69 +   "  <div class='devtools-toolbarbutton' label='true'>",
    1.70 +   "   <hbox class='toolbarbutton-menubutton-button'></hbox></div>",
    1.71 +   " </body>",
    1.72 +   " </html>"
    1.73 +  ].join("\n"));
    1.74 +
    1.75 +let doc = null;
    1.76 +let index = 0;
    1.77 +let completer = null;
    1.78 +let progress;
    1.79 +let progressDiv;
    1.80 +let inspector;
    1.81 +
    1.82 +function test() {
    1.83 +  waitForExplicitFinish();
    1.84 +  gBrowser.selectedTab = gBrowser.addTab();
    1.85 +  gBrowser.selectedBrowser.addEventListener("load", function onload() {
    1.86 +    gBrowser.selectedBrowser.removeEventListener("load", onload, true);
    1.87 +    doc = content.document;
    1.88 +    runTests();
    1.89 +  }, true);
    1.90 +  content.location = TEST_URI;
    1.91 +}
    1.92 +
    1.93 +function runTests() {
    1.94 +  progress = doc.getElementById("progress");
    1.95 +  progressDiv = doc.querySelector("#progress > div");
    1.96 +  let target = devtools.TargetFactory.forTab(gBrowser.selectedTab);
    1.97 +  target.makeRemote().then(() => {
    1.98 +    inspector = InspectorFront(target.client, target.form);
    1.99 +    inspector.getWalker().then(walker => {
   1.100 +      completer = new cssAutoCompleter({walker: walker});
   1.101 +      checkStateAndMoveOn();
   1.102 +    });
   1.103 +  });
   1.104 +}
   1.105 +
   1.106 +function checkStateAndMoveOn() {
   1.107 +  if (index == tests.length) {
   1.108 +    finishUp();
   1.109 +    return;
   1.110 +  }
   1.111 +
   1.112 +  let test = tests[index];
   1.113 +  progress.dataset.progress = ++index;
   1.114 +  progressDiv.style.width = 100*index/tests.length + "%";
   1.115 +  completer.complete(limit(source, test[0]),
   1.116 +                     {line: test[0][0], ch: test[0][1]}).then(suggestions => {
   1.117 +    checkState(test[1], suggestions);
   1.118 +  }).then(checkStateAndMoveOn);
   1.119 +}
   1.120 +
   1.121 +function checkState(expected, actual) {
   1.122 +  if (expected.length != actual.length) {
   1.123 +    ok(false, "Number of suggestions did not match up for state " + index +
   1.124 +              ". Expected: " + expected.length + ", Actual: " + actual.length);
   1.125 +    progress.classList.add("failed");
   1.126 +    return;
   1.127 +  }
   1.128 +
   1.129 +  for (let i = 0; i < actual.length; i++) {
   1.130 +    if (expected[i] != actual[i].label) {
   1.131 +      ok (false, "Suggestion " + i + " of state " + index + " did not match up" +
   1.132 +                 ". Expected: " + expected[i] + ". Actual: " + actual[i].label);
   1.133 +      return;
   1.134 +    }
   1.135 +  }
   1.136 +  ok(true, "Test " + index + " passed. ");
   1.137 +}
   1.138 +
   1.139 +function finishUp() {
   1.140 +  completer.walker.release().then(() => {
   1.141 +    inspector.destroy();
   1.142 +    inspector = null;
   1.143 +    completer = null;
   1.144 +  });
   1.145 +  progress = null;
   1.146 +  progressDiv = null;
   1.147 +  gBrowser.removeCurrentTab();
   1.148 +  finish();
   1.149 +}

mercurial