summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/test/browser_inspector_highlighter-options.js
blob: 558b67059acc91e89ed2aff88726682fc8fb99fd (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// Check that the box-model highlighter supports configuration options

const TEST_URL = `
  <body style="padding:2em;">
    <div style="width:100px;height:100px;padding:2em;
                border:.5em solid black;margin:1em;">test</div>
  </body>
`;

// Test data format:
// - desc: a string that will be output to the console.
// - options: json object to be passed as options to the highlighter.
// - checkHighlighter: a generator (async) function that should check the
//   highlighter is correct.
const TEST_DATA = [
  {
    desc: "Guides and infobar should be shown by default",
    options: {},
    async checkHighlighter(highlighterTestFront) {
      let hidden = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-infobar-container",
        "hidden"
      );
      ok(!hidden, "Node infobar is visible");

      hidden = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-elements",
        "hidden"
      );
      ok(!hidden, "SVG container is visible");

      for (const side of ["top", "right", "bottom", "left"]) {
        hidden = await highlighterTestFront.getHighlighterNodeAttribute(
          "box-model-guide-" + side,
          "hidden"
        );
        ok(!hidden, side + " guide is visible");
      }
    },
  },
  {
    desc: "All regions should be shown by default",
    options: {},
    async checkHighlighter(highlighterTestFront) {
      for (const region of ["margin", "border", "padding", "content"]) {
        const { d } = await highlighterTestFront.getHighlighterRegionPath(
          region
        );
        ok(d, "Region " + region + " has set coordinates");
      }
    },
  },
  {
    desc: "Guides can be hidden",
    options: { hideGuides: true },
    async checkHighlighter(highlighterTestFront) {
      for (const side of ["top", "right", "bottom", "left"]) {
        const hidden = await highlighterTestFront.getHighlighterNodeAttribute(
          "box-model-guide-" + side,
          "hidden"
        );
        is(hidden, "true", side + " guide has been hidden");
      }
    },
  },
  {
    desc: "Infobar can be hidden",
    options: { hideInfoBar: true },
    async checkHighlighter(highlighterTestFront) {
      const hidden = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-infobar-container",
        "hidden"
      );
      is(hidden, "true", "infobar has been hidden");
    },
  },
  {
    desc: "One region only can be shown (1)",
    options: { showOnly: "content" },
    async checkHighlighter(highlighterTestFront) {
      let { d } = await highlighterTestFront.getHighlighterRegionPath("margin");
      ok(!d, "margin region is hidden");

      ({ d } = await highlighterTestFront.getHighlighterRegionPath("border"));
      ok(!d, "border region is hidden");

      ({ d } = await highlighterTestFront.getHighlighterRegionPath("padding"));
      ok(!d, "padding region is hidden");

      ({ d } = await highlighterTestFront.getHighlighterRegionPath("content"));
      ok(d, "content region is shown");
    },
  },
  {
    desc: "One region only can be shown (2)",
    options: { showOnly: "margin" },
    async checkHighlighter(highlighterTestFront) {
      let { d } = await highlighterTestFront.getHighlighterRegionPath("margin");
      ok(d, "margin region is shown");

      ({ d } = await highlighterTestFront.getHighlighterRegionPath("border"));
      ok(!d, "border region is hidden");

      ({ d } = await highlighterTestFront.getHighlighterRegionPath("padding"));
      ok(!d, "padding region is hidden");

      ({ d } = await highlighterTestFront.getHighlighterRegionPath("content"));
      ok(!d, "content region is hidden");
    },
  },
  {
    desc: "Guides can be drawn around a given region (1)",
    options: { region: "padding" },
    async checkHighlighter(highlighterTestFront) {
      const topY1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-top",
        "y1"
      );
      const rightX1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-right",
        "x1"
      );
      const bottomY1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-bottom",
        "y1"
      );
      const leftX1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-left",
        "x1"
      );

      let { points } = await highlighterTestFront.getHighlighterRegionPath(
        "padding"
      );
      points = points[0];

      is(topY1, points[0][1], "Top guide's y1 is correct");
      is(
        parseInt(rightX1, 10),
        points[1][0] - 1,
        "Right guide's x1 is correct"
      );
      is(
        parseInt(bottomY1, 10),
        points[2][1] - 1,
        "Bottom guide's y1 is correct"
      );
      is(leftX1, points[3][0], "Left guide's x1 is correct");
    },
  },
  {
    desc: "Guides can be drawn around a given region (2)",
    options: { region: "margin" },
    async checkHighlighter(highlighterTestFront) {
      const topY1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-top",
        "y1"
      );
      const rightX1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-right",
        "x1"
      );
      const bottomY1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-bottom",
        "y1"
      );
      const leftX1 = await highlighterTestFront.getHighlighterNodeAttribute(
        "box-model-guide-left",
        "x1"
      );

      let { points } = await highlighterTestFront.getHighlighterRegionPath(
        "margin"
      );
      points = points[0];

      is(topY1, points[0][1], "Top guide's y1 is correct");
      is(
        parseInt(rightX1, 10),
        points[1][0] - 1,
        "Right guide's x1 is correct"
      );
      is(
        parseInt(bottomY1, 10),
        points[2][1] - 1,
        "Bottom guide's y1 is correct"
      );
      is(leftX1, points[3][0], "Left guide's x1 is correct");
    },
  },
  {
    desc: "When showOnly is used, other regions can be faded",
    options: { showOnly: "margin", onlyRegionArea: true },
    async checkHighlighter(highlighterTestFront) {
      for (const region of ["margin", "border", "padding", "content"]) {
        const { d } = await highlighterTestFront.getHighlighterRegionPath(
          region
        );
        ok(d, "Region " + region + " is shown (it has a d attribute)");

        const faded = await highlighterTestFront.getHighlighterNodeAttribute(
          "box-model-" + region,
          "faded"
        );
        if (region === "margin") {
          ok(!faded, "The margin region is not faded");
        } else {
          is(faded, "true", "Region " + region + " is faded");
        }
      }
    },
  },
  {
    desc: "When showOnly is used, other regions can be faded (2)",
    options: { showOnly: "padding", onlyRegionArea: true },
    async checkHighlighter(highlighterTestFront) {
      for (const region of ["margin", "border", "padding", "content"]) {
        const { d } = await highlighterTestFront.getHighlighterRegionPath(
          region
        );
        ok(d, "Region " + region + " is shown (it has a d attribute)");

        const faded = await highlighterTestFront.getHighlighterNodeAttribute(
          "box-model-" + region,
          "faded"
        );
        if (region === "padding") {
          ok(!faded, "The padding region is not faded");
        } else {
          is(faded, "true", "Region " + region + " is faded");
        }
      }
    },
  },
];

add_task(async function () {
  const { inspector, highlighterTestFront } = await openInspectorForURL(
    "data:text/html;charset=utf-8," + encodeURI(TEST_URL)
  );

  const divFront = await getNodeFront("div", inspector);

  for (const { desc, options, checkHighlighter } of TEST_DATA) {
    info("Running test: " + desc);

    info("Show the box-model highlighter with options " + options);
    await inspector.highlighters.showHighlighterTypeForNode(
      inspector.highlighters.TYPES.BOXMODEL,
      divFront,
      options
    );

    await checkHighlighter(highlighterTestFront);

    info("Hide the box-model highlighter");
    await inspector.highlighters.hideHighlighterType(
      inspector.highlighters.TYPES.BOXMODEL
    );
  }
});