summaryrefslogtreecommitdiffstats
path: root/devtools/client/sourceeditor/test/browser_css_autocompletion.js
blob: 57a5ae345bc38f7c1bc2df61a5286f5ba3e68ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const CSSCompleter = require("devtools/client/sourceeditor/css-autocompleter");
const {InspectorFront} = require("devtools/shared/fronts/inspector");

const CSS_URI = "http://mochi.test:8888/browser/devtools/client/sourceeditor" +
                "/test/css_statemachine_testcases.css";
const TESTS_URI = "http://mochi.test:8888/browser/devtools/client" +
                  "/sourceeditor/test/css_autocompletion_tests.json";

const source = read(CSS_URI);
const tests = eval(read(TESTS_URI));

const TEST_URI = "data:text/html;charset=UTF-8," + encodeURIComponent(
  ["<!DOCTYPE html>",
   "<html>",
   " <head>",
   "  <title>CSS State machine tests.</title>",
   "  <style type='text/css'>",
   "#progress {",
   "  width: 500px; height: 30px;",
   "  border: 1px solid black;",
   "  position: relative",
   "}",
   "#progress div {",
   "  width: 0%; height: 100%;",
   "  background: green;",
   "  position: absolute;",
   "  z-index: -1; top: 0",
   "}",
   "#progress.failed div {",
   "  background: red !important;",
   "}",
   "#progress.failed:after {",
   "  content: 'Some tests failed';",
   "  color: white",
   "}",
   "#progress:before {",
   "  content: 'Running test ' attr(data-progress) ' of " + tests.length + "';",
   "  color: white;",
   "  text-shadow: 0 0 2px darkgreen;",
   "}",
   "  </style>",
   " </head>",
   " <body>",
   "  <h2>State machine tests for CSS autocompleter.</h2><br>",
   "  <div id='progress' data-progress='0'>",
   "   <div></div>",
   "  </div>",
   "  <div id='devtools-menu' class='devtools-toolbarbutton'></div>",
   "  <div id='devtools-toolbarbutton' class='devtools-menulist'></div>",
   "  <div id='devtools-anotherone'></div>",
   "  <div id='devtools-yetagain'></div>",
   "  <div id='devtools-itjustgoeson'></div>",
   "  <div id='devtools-okstopitnow'></div>",
   "  <div class='hidden-labels-box devtools-toolbarbutton devtools-menulist'></div>",
   "  <div class='devtools-menulist'></div>",
   "  <div class='devtools-menulist'></div>",
   "  <tabs class='devtools-toolbarbutton'><tab></tab><tab></tab><tab></tab></tabs><tabs></tabs>",
   "  <button class='category-name visible'></button>",
   "  <div class='devtools-toolbarbutton' label='true'>",
   "   <hbox class='toolbarbutton-menubutton-button'></hbox></div>",
   " </body>",
   " </html>"
  ].join("\n"));

let doc = null;
let index = 0;
let completer = null;
let progress;
let progressDiv;
let inspector;

function test() {
  waitForExplicitFinish();
  addTab(TEST_URI).then(function () {
    doc = content.document;
    runTests();
  });
}

function runTests() {
  progress = doc.getElementById("progress");
  progressDiv = doc.querySelector("#progress > div");
  let target = TargetFactory.forTab(gBrowser.selectedTab);
  target.makeRemote().then(() => {
    inspector = InspectorFront(target.client, target.form);
    inspector.getWalker().then(walker => {
      completer = new CSSCompleter({walker: walker,
                                    cssProperties: getClientCssProperties()});
      checkStateAndMoveOn();
    });
  });
}

function checkStateAndMoveOn() {
  if (index == tests.length) {
    finishUp();
    return;
  }

  let [lineCh, expectedSuggestions] = tests[index];
  let [line, ch] = lineCh;

  progress.dataset.progress = ++index;
  progressDiv.style.width = 100 * index / tests.length + "%";

  completer.complete(limit(source, lineCh), {line, ch})
           .then(actualSuggestions => checkState(expectedSuggestions, actualSuggestions))
           .then(checkStateAndMoveOn);
}

function checkState(expected, actual) {
  if (expected.length != actual.length) {
    ok(false, "Number of suggestions did not match up for state " + index +
              ". Expected: " + expected.length + ", Actual: " + actual.length);
    progress.classList.add("failed");
    return;
  }

  for (let i = 0; i < actual.length; i++) {
    if (expected[i] != actual[i].label) {
      ok(false, "Suggestion " + i + " of state " + index + " did not match up" +
                 ". Expected: " + expected[i] + ". Actual: " + actual[i].label);
      return;
    }
  }
  ok(true, "Test " + index + " passed. ");
}

function finishUp() {
  completer.walker.release().then(() => {
    inspector.destroy();
    inspector = null;
    completer = null;
    gBrowser.removeCurrentTab();
    finish();
  });
  progress = null;
  progressDiv = null;
}