browser/devtools/sourceeditor/test/browser_css_getInfo.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 "use strict";
michael@0 6
michael@0 7 const cssAutoCompleter = require("devtools/sourceeditor/css-autocompleter");
michael@0 8
michael@0 9 const source = [
michael@0 10 ".devtools-toolbar {",
michael@0 11 " -moz-appearance: none;",
michael@0 12 " padding:4px 3px;border-bottom-width: 1px;",
michael@0 13 " border-bottom-style: solid;",
michael@0 14 "}",
michael@0 15 "",
michael@0 16 "#devtools-menu.devtools-menulist,",
michael@0 17 ".devtools-toolbarbutton#devtools-menu {",
michael@0 18 " -moz-appearance: none;",
michael@0 19 " -moz-box-align: center;",
michael@0 20 " min-width: 78px;",
michael@0 21 " min-height: 22px;",
michael@0 22 " text-shadow: 0 -1px 0 hsla(210,8%,5%,.45);",
michael@0 23 " border: 1px solid hsla(210,8%,5%,.45);",
michael@0 24 " border-radius: 3px;",
michael@0 25 " background: linear-gradient(hsla(212,7%,57%,.35),",
michael@0 26 " hsla(212,7%,57%,.1)) padding-box;",
michael@0 27 " margin: 0 3px;",
michael@0 28 " color: inherit;",
michael@0 29 "}",
michael@0 30 "",
michael@0 31 ".devtools-toolbarbutton > hbox.toolbarbutton-menubutton-button {",
michael@0 32 " -moz-box-orient: horizontal;",
michael@0 33 "}",
michael@0 34 "",
michael@0 35 ".devtools-menulist:active,",
michael@0 36 "#devtools-toolbarbutton:focus {",
michael@0 37 " outline: 1px dotted hsla(210,30%,85%,0.7);",
michael@0 38 " outline-offset : -4px;",
michael@0 39 "}",
michael@0 40 "",
michael@0 41 ".devtools-toolbarbutton:not([label]) {",
michael@0 42 " min-width: 32px;",
michael@0 43 "}",
michael@0 44 "",
michael@0 45 ".devtools-toolbarbutton:not([label]) > .toolbarbutton-text, .devtools-toolbar {",
michael@0 46 " display: none;",
michael@0 47 "}",
michael@0 48 ].join("\n");
michael@0 49
michael@0 50 // Format of test cases :
michael@0 51 // [
michael@0 52 // {line, ch}, - The caret position at which the getInfo call should be made
michael@0 53 // expectedState, - The expected state at the caret
michael@0 54 // expectedSelector, - The expected selector for the state
michael@0 55 // expectedProperty, - The expected property name for states value and property
michael@0 56 // expectedValue, - If state is value, then the expected value
michael@0 57 // ]
michael@0 58 const tests = [
michael@0 59 [{line: 0, ch: 13}, "selector", ".devtools-toolbar"],
michael@0 60 [{line: 8, ch: 13}, "property", ["#devtools-menu.devtools-menulist",
michael@0 61 ".devtools-toolbarbutton#devtools-menu "], "-moz-appearance"],
michael@0 62 [{line: 28, ch: 25}, "value", [".devtools-menulist:active",
michael@0 63 "#devtools-toolbarbutton:focus "], "outline-offset", "-4px"],
michael@0 64 [{line: 4, ch: 1}, "null"],
michael@0 65 [{line: 5, ch: 0}, "null"],
michael@0 66 [{line: 31, ch: 13}, "selector", ".devtools-toolbarbutton:not([label])"],
michael@0 67 [{line: 35, ch: 23}, "selector", ".devtools-toolbarbutton:not([label]) > .toolbarbutton-text"],
michael@0 68 [{line: 35, ch: 70}, "selector", ".devtools-toolbar"],
michael@0 69 [{line: 27, ch: 14}, "value", [".devtools-menulist:active",
michael@0 70 "#devtools-toolbarbutton:focus "], "outline", "1px dotted hsla(210,30%,85%,0.7)"],
michael@0 71 [{line: 16, ch: 16}, "value", ["#devtools-menu.devtools-menulist",
michael@0 72 ".devtools-toolbarbutton#devtools-menu "], "background",
michael@0 73 "linear-gradient(hsla(212,7%,57%,.35),\n hsla(212,7%,57%,.1)) padding-box"],
michael@0 74 [{line: 16, ch: 3}, "value", ["#devtools-menu.devtools-menulist",
michael@0 75 ".devtools-toolbarbutton#devtools-menu "], "background",
michael@0 76 "linear-gradient(hsla(212,7%,57%,.35),\n hsla(212,7%,57%,.1)) padding-box"],
michael@0 77 [{line: 15, ch: 25}, "value", ["#devtools-menu.devtools-menulist",
michael@0 78 ".devtools-toolbarbutton#devtools-menu "], "background",
michael@0 79 "linear-gradient(hsla(212,7%,57%,.35),\n hsla(212,7%,57%,.1)) padding-box"],
michael@0 80 ];
michael@0 81
michael@0 82 const TEST_URI = "data:text/html;charset=UTF-8," + encodeURIComponent(
michael@0 83 ["<!DOCTYPE html>",
michael@0 84 "<html>",
michael@0 85 " <head>",
michael@0 86 " <title>CSS contextual information tests.</title>",
michael@0 87 " <style type='text/css'>",
michael@0 88 "#progress {",
michael@0 89 " width: 500px; height: 30px;",
michael@0 90 " border: 1px solid black;",
michael@0 91 " position: relative",
michael@0 92 "}",
michael@0 93 "#progress div {",
michael@0 94 " width: 0%; height: 100%;",
michael@0 95 " background: green;",
michael@0 96 " position: absolute;",
michael@0 97 " z-index: -1; top: 0",
michael@0 98 "}",
michael@0 99 "#progress.failed div {",
michael@0 100 " background: red !important;",
michael@0 101 "}",
michael@0 102 "#progress.failed:after {",
michael@0 103 " content: 'Some tests failed';",
michael@0 104 " color: white",
michael@0 105 "}",
michael@0 106 "#progress:before {",
michael@0 107 " content: 'Running test ' attr(data-progress) ' of " + tests.length + "';",
michael@0 108 " color: white;",
michael@0 109 " text-shadow: 0 0 2px darkgreen;",
michael@0 110 "}",
michael@0 111 " </style>",
michael@0 112 " </head>",
michael@0 113 " <body>",
michael@0 114 " <h2>State machine tests for CSS autocompleter.</h2><br>",
michael@0 115 " <div id='progress' data-progress='0'>",
michael@0 116 " <div></div>",
michael@0 117 " </div>",
michael@0 118 " </body>",
michael@0 119 " </html>"
michael@0 120 ].join("\n"));
michael@0 121
michael@0 122 let doc = null;
michael@0 123 function test() {
michael@0 124 waitForExplicitFinish();
michael@0 125 gBrowser.selectedTab = gBrowser.addTab();
michael@0 126 gBrowser.selectedBrowser.addEventListener("load", function onload() {
michael@0 127 gBrowser.selectedBrowser.removeEventListener("load", onload, true);
michael@0 128 doc = content.document;
michael@0 129 runTests();
michael@0 130 }, true);
michael@0 131 content.location = TEST_URI;
michael@0 132 }
michael@0 133
michael@0 134 function runTests() {
michael@0 135 let completer = new cssAutoCompleter();
michael@0 136 let matches = (arr, toCheck) => !arr.some((x, i) => x != toCheck[i]);
michael@0 137 let checkState = (expected, actual) => {
michael@0 138 if (expected[0] == "null" && actual == null) {
michael@0 139 return true;
michael@0 140 } else if (expected[0] == actual.state && expected[0] == "selector" &&
michael@0 141 expected[1] == actual.selector) {
michael@0 142 return true;
michael@0 143 } else if (expected[0] == actual.state && expected[0] == "property" &&
michael@0 144 matches(expected[1], actual.selectors) &&
michael@0 145 expected[2] == actual.propertyName) {
michael@0 146 return true;
michael@0 147 } else if (expected[0] == actual.state && expected[0] == "value" &&
michael@0 148 matches(expected[1], actual.selectors) &&
michael@0 149 expected[2] == actual.propertyName &&
michael@0 150 expected[3] == actual.value) {
michael@0 151 return true;
michael@0 152 }
michael@0 153 return false;
michael@0 154 };
michael@0 155
michael@0 156 let progress = doc.getElementById("progress");
michael@0 157 let progressDiv = doc.querySelector("#progress > div");
michael@0 158 let i = 0;
michael@0 159 for (let expected of tests) {
michael@0 160 let caret = expected.splice(0, 1)[0];
michael@0 161 progress.dataset.progress = ++i;
michael@0 162 progressDiv.style.width = 100*i/tests.length + "%";
michael@0 163 let actual = completer.getInfoAt(source, caret);
michael@0 164 if (checkState(expected, actual)) {
michael@0 165 ok(true, "Test " + i + " passed. ");
michael@0 166 }
michael@0 167 else {
michael@0 168 ok(false, "Test " + i + " failed. Expected state : [" + expected + "] " +
michael@0 169 "but found [" + actual.state + ", " +
michael@0 170 (actual.selector || actual.selectors) + ", " +
michael@0 171 actual.propertyName + ", " + actual.value + "].");
michael@0 172 progress.classList.add("failed");
michael@0 173 }
michael@0 174 }
michael@0 175 gBrowser.removeCurrentTab();
michael@0 176 finish();
michael@0 177 }

mercurial