browser/devtools/styleinspector/test/browser_styleinspector_output-parser.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 ft=javascript 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 // Test expected outputs of the output-parser's parseCssProperty function.
michael@0 8
michael@0 9 // This is more of a unit test than a mochitest-browser test, but can't be
michael@0 10 // tested with an xpcshell test as the output-parser requires the DOM to work.
michael@0 11
michael@0 12 let {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
michael@0 13 let {OutputParser} = devtools.require("devtools/output-parser");
michael@0 14
michael@0 15 const COLOR_CLASS = "color-class";
michael@0 16 const URL_CLASS = "url-class";
michael@0 17
michael@0 18 function test() {
michael@0 19 function countAll(fragment) {
michael@0 20 return fragment.querySelectorAll("*").length;
michael@0 21 }
michael@0 22 function countColors(fragment) {
michael@0 23 return fragment.querySelectorAll("." + COLOR_CLASS).length;
michael@0 24 }
michael@0 25 function countUrls(fragment) {
michael@0 26 return fragment.querySelectorAll("." + URL_CLASS).length;
michael@0 27 }
michael@0 28 function getColor(fragment, index) {
michael@0 29 return fragment.querySelectorAll("." + COLOR_CLASS)[index||0].textContent;
michael@0 30 }
michael@0 31 function getUrl(fragment, index) {
michael@0 32 return fragment.querySelectorAll("." + URL_CLASS)[index||0].textContent;
michael@0 33 }
michael@0 34
michael@0 35 let testData = [
michael@0 36 {
michael@0 37 name: "width",
michael@0 38 value: "100%",
michael@0 39 test: fragment => {
michael@0 40 is(countAll(fragment), 0);
michael@0 41 is(fragment.textContent, "100%");
michael@0 42 }
michael@0 43 },
michael@0 44 {
michael@0 45 name: "width",
michael@0 46 value: "blue",
michael@0 47 test: fragment => {
michael@0 48 is(countAll(fragment), 0);
michael@0 49 }
michael@0 50 },
michael@0 51 {
michael@0 52 name: "content",
michael@0 53 value: "'red url(test.png) repeat top left'",
michael@0 54 test: fragment => {
michael@0 55 is(countAll(fragment), 0);
michael@0 56 }
michael@0 57 },
michael@0 58 {
michael@0 59 name: "content",
michael@0 60 value: "\"blue\"",
michael@0 61 test: fragment => {
michael@0 62 is(countAll(fragment), 0);
michael@0 63 }
michael@0 64 },
michael@0 65 {
michael@0 66 name: "margin-left",
michael@0 67 value: "url(something.jpg)",
michael@0 68 test: fragment => {
michael@0 69 is(countAll(fragment), 0);
michael@0 70 }
michael@0 71 },
michael@0 72 {
michael@0 73 name: "background-color",
michael@0 74 value: "transparent",
michael@0 75 test: fragment => {
michael@0 76 is(countAll(fragment), 1);
michael@0 77 is(countColors(fragment), 1);
michael@0 78 is(fragment.textContent, "transparent");
michael@0 79 }
michael@0 80 },
michael@0 81 {
michael@0 82 name: "color",
michael@0 83 value: "red",
michael@0 84 test: fragment => {
michael@0 85 is(countColors(fragment), 1);
michael@0 86 is(fragment.textContent, "red");
michael@0 87 }
michael@0 88 },
michael@0 89 {
michael@0 90 name: "color",
michael@0 91 value: "#F06",
michael@0 92 test: fragment => {
michael@0 93 is(countColors(fragment), 1);
michael@0 94 is(fragment.textContent, "#F06");
michael@0 95 }
michael@0 96 },
michael@0 97 {
michael@0 98 name: "border-top-left-color",
michael@0 99 value: "rgba(14, 255, 20, .5)",
michael@0 100 test: fragment => {
michael@0 101 is(countAll(fragment), 1);
michael@0 102 is(countColors(fragment), 1);
michael@0 103 is(fragment.textContent, "rgba(14, 255, 20, .5)");
michael@0 104 }
michael@0 105 },
michael@0 106 {
michael@0 107 name: "border",
michael@0 108 value: "80em dotted pink",
michael@0 109 test: fragment => {
michael@0 110 is(countAll(fragment), 1);
michael@0 111 is(countColors(fragment), 1);
michael@0 112 is(getColor(fragment), "pink");
michael@0 113 }
michael@0 114 },
michael@0 115 {
michael@0 116 name: "color",
michael@0 117 value: "red !important",
michael@0 118 test: fragment => {
michael@0 119 is(countColors(fragment), 1);
michael@0 120 is(fragment.textContent, "red !important");
michael@0 121 }
michael@0 122 },
michael@0 123 {
michael@0 124 name: "background",
michael@0 125 value: "red url(test.png) repeat top left",
michael@0 126 test: fragment => {
michael@0 127 is(countColors(fragment), 1);
michael@0 128 is(countUrls(fragment), 1);
michael@0 129 is(getColor(fragment), "red");
michael@0 130 is(getUrl(fragment), "test.png");
michael@0 131 is(countAll(fragment), 2);
michael@0 132 }
michael@0 133 },
michael@0 134 {
michael@0 135 name: "background",
michael@0 136 value: "blue url(test.png) repeat top left !important",
michael@0 137 test: fragment => {
michael@0 138 is(countColors(fragment), 1);
michael@0 139 is(countUrls(fragment), 1);
michael@0 140 is(getColor(fragment), "blue");
michael@0 141 is(getUrl(fragment), "test.png");
michael@0 142 is(countAll(fragment), 2);
michael@0 143 }
michael@0 144 },
michael@0 145 {
michael@0 146 name: "list-style-image",
michael@0 147 value: "url(\"images/arrow.gif\")",
michael@0 148 test: fragment => {
michael@0 149 is(countAll(fragment), 1);
michael@0 150 is(getUrl(fragment), "images/arrow.gif");
michael@0 151 }
michael@0 152 },
michael@0 153 {
michael@0 154 name: "list-style-image",
michael@0 155 value: "url(\"images/arrow.gif\")!important",
michael@0 156 test: fragment => {
michael@0 157 is(countAll(fragment), 1);
michael@0 158 is(getUrl(fragment), "images/arrow.gif");
michael@0 159 is(fragment.textContent, "url('images/arrow.gif')!important");
michael@0 160 }
michael@0 161 },
michael@0 162 {
michael@0 163 name: "-moz-binding",
michael@0 164 value: "url(http://somesite.com/path/to/binding.xml#someid)",
michael@0 165 test: fragment => {
michael@0 166 is(countAll(fragment), 1);
michael@0 167 is(countUrls(fragment), 1);
michael@0 168 is(getUrl(fragment), "http://somesite.com/path/to/binding.xml#someid");
michael@0 169 }
michael@0 170 },
michael@0 171 {
michael@0 172 name: "background",
michael@0 173 value: "linear-gradient(to right, rgba(183,222,237,1) 0%, rgba(33,180,226,1) 30%, rgba(31,170,217,.5) 44%, #F06 75%, red 100%)",
michael@0 174 test: fragment => {
michael@0 175 is(countAll(fragment), 5);
michael@0 176 let allSwatches = fragment.querySelectorAll("." + COLOR_CLASS);
michael@0 177 is(allSwatches.length, 5);
michael@0 178 is(allSwatches[0].textContent, "rgba(183,222,237,1)");
michael@0 179 is(allSwatches[1].textContent, "rgba(33,180,226,1)");
michael@0 180 is(allSwatches[2].textContent, "rgba(31,170,217,.5)");
michael@0 181 is(allSwatches[3].textContent, "#F06");
michael@0 182 is(allSwatches[4].textContent, "red");
michael@0 183 }
michael@0 184 },
michael@0 185 {
michael@0 186 name: "background",
michael@0 187 value: "-moz-radial-gradient(center 45deg, circle closest-side, orange 0%, red 100%)",
michael@0 188 test: fragment => {
michael@0 189 is(countAll(fragment), 2);
michael@0 190 let allSwatches = fragment.querySelectorAll("." + COLOR_CLASS);
michael@0 191 is(allSwatches.length, 2);
michael@0 192 is(allSwatches[0].textContent, "orange");
michael@0 193 is(allSwatches[1].textContent, "red");
michael@0 194 }
michael@0 195 },
michael@0 196 {
michael@0 197 name: "background",
michael@0 198 value: "white url(http://test.com/wow_such_image.png) no-repeat top left",
michael@0 199 test: fragment => {
michael@0 200 is(countAll(fragment), 2);
michael@0 201 is(countUrls(fragment), 1);
michael@0 202 is(countColors(fragment), 1);
michael@0 203 }
michael@0 204 },
michael@0 205 {
michael@0 206 name: "background",
michael@0 207 value: "url(\"http://test.com/wow_such_(oh-noes)image.png?testid=1&color=red#w00t\")",
michael@0 208 test: fragment => {
michael@0 209 is(countAll(fragment), 1);
michael@0 210 is(getUrl(fragment), "http://test.com/wow_such_(oh-noes)image.png?testid=1&color=red#w00t");
michael@0 211 }
michael@0 212 },
michael@0 213 {
michael@0 214 name: "background-image",
michael@0 215 value: "url(this-is-an-incredible-image.jpeg)",
michael@0 216 test: fragment => {
michael@0 217 is(countAll(fragment), 1);
michael@0 218 is(getUrl(fragment), "this-is-an-incredible-image.jpeg");
michael@0 219 }
michael@0 220 },
michael@0 221 {
michael@0 222 name: "background",
michael@0 223 value: "red url( \"http://wow.com/cool/../../../you're(doingit)wrong\" ) repeat center",
michael@0 224 test: fragment => {
michael@0 225 is(countAll(fragment), 2);
michael@0 226 is(countColors(fragment), 1);
michael@0 227 is(getUrl(fragment), "http://wow.com/cool/../../../you're(doingit)wrong");
michael@0 228 }
michael@0 229 },
michael@0 230 {
michael@0 231 name: "background-image",
michael@0 232 value: "url(../../../look/at/this/folder/structure/../../red.blue.green.svg )",
michael@0 233 test: fragment => {
michael@0 234 is(countAll(fragment), 1);
michael@0 235 is(getUrl(fragment), "../../../look/at/this/folder/structure/../../red.blue.green.svg");
michael@0 236 }
michael@0 237 }
michael@0 238 ];
michael@0 239
michael@0 240 let parser = new OutputParser();
michael@0 241 for (let i = 0; i < testData.length; i ++) {
michael@0 242 let data = testData[i];
michael@0 243 info("Output-parser test data " + i + ". {" + data.name + " : " + data.value + ";}");
michael@0 244 data.test(parser.parseCssProperty(data.name, data.value, {
michael@0 245 colorClass: COLOR_CLASS,
michael@0 246 urlClass: URL_CLASS,
michael@0 247 defaultColorType: false
michael@0 248 }));
michael@0 249 }
michael@0 250
michael@0 251 finish();
michael@0 252 }

mercurial