summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_container-queries.js
blob: 1a1857be05e0a8efd5c7a3a6df6bf713636ba7a0 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that the rule-view content is correct when the page defines container queries.
const TEST_URI = `
  <!DOCTYPE html>
  <style type="text/css">
    body {
      container: mycontainer / size;
    }

    section {
      container: mycontainer / inline-size;
    }

    @container (width > 0px) {
      h1, [test-hint="nocontainername"]{
        outline-color: chartreuse;
      }
    }

    @container unknowncontainer (min-width: 2vw) {
      h1, [test-hint="unknowncontainer"] {
        border-color: salmon;
      }
    }

    @container mycontainer (1px < width < 10000px) {
      h1, [test-hint="container"] {
        color: tomato;
      }

      section, [test-hint="container-duplicate-name--body"] {
        color: gold;
      }

      div, [test-hint="container-duplicate-name--section"] {
        color: salmon;
      }
    }
  </style>
  <body id=myBody class="a-container test">
    <h1>Hello @container!</h1>
    <section>
      <div>
        <h2>You rock</h2>
      </div>
    </section>
  </body>
`;

add_task(async function () {
  await pushPref("layout.css.container-queries.enabled", true);

  await addTab(
    "https://example.com/document-builder.sjs?html=" +
      encodeURIComponent(TEST_URI)
  );
  const { inspector, view } = await openRuleView();

  await selectNode("h1", inspector);
  assertContainerQueryData(view, [
    { selector: "element", ancestorRulesData: null },
    {
      selector: `h1, [test-hint="container"]`,
      ancestorRulesData: ["@container mycontainer (1px < width < 10000px) {"],
    },
    {
      selector: `h1, [test-hint="nocontainername"]`,
      ancestorRulesData: ["@container (width > 0px) {"],
    },
  ]);

  info("Check that the query container tooltip works as expected");
  // Retrieve query containers sizes
  const { bodyInlineSize, bodyBlockSize, sectionInlineSize } =
    await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
      const body = content.document.body;
      const section = content.document.querySelector("section");
      return {
        bodyInlineSize: content.getComputedStyle(body).inlineSize,
        bodyBlockSize: content.getComputedStyle(body).blockSize,
        sectionInlineSize: content.getComputedStyle(section).inlineSize,
      };
    });

  await assertQueryContainerTooltip({
    inspector,
    view,
    ruleIndex: 1,
    expectedHeaderText: "<body#myBody.a-container.test>",
    expectedBodyText: [
      "container-type: size",
      `inline-size: ${bodyInlineSize}`,
      `block-size: ${bodyBlockSize}`,
    ],
  });

  info("Check that the 'jump to container' button works as expected");
  await assertJumpToContainerButton(inspector, view, 1, "body");

  info("Check that inherited rules display container query data as expected");
  await selectNode("h2", inspector);

  assertContainerQueryData(view, [
    { selector: "element", ancestorRulesData: null },
    {
      selector: `div, [test-hint="container-duplicate-name--section"]`,
      ancestorRulesData: ["@container mycontainer (1px < width < 10000px) {"],
    },
    {
      selector: `section, [test-hint="container-duplicate-name--body"]`,
      ancestorRulesData: ["@container mycontainer (1px < width < 10000px) {"],
    },
  ]);

  info(
    "Check that the query container tooltip works as expected for inherited rules as well"
  );
  await assertQueryContainerTooltip({
    inspector,
    view,
    ruleIndex: 1,
    expectedHeaderText: "<section>",
    expectedBodyText: [
      "container-type: inline-size",
      `inline-size: ${sectionInlineSize}`,
    ],
  });
  await assertQueryContainerTooltip({
    inspector,
    view,
    ruleIndex: 2,
    expectedHeaderText: "<body#myBody.a-container.test>",
    expectedBodyText: [
      "container-type: size",
      `inline-size: ${bodyInlineSize}`,
      `block-size: ${bodyBlockSize}`,
    ],
  });

  info(
    "Check that the 'jump to container' button works as expected for inherited rules"
  );
  await assertJumpToContainerButton(inspector, view, 1, "section");

  await selectNode("h2", inspector);
  await assertJumpToContainerButton(inspector, view, 2, "body");
});

function assertContainerQueryData(view, expectedRules) {
  const rulesInView = Array.from(
    view.element.querySelectorAll(".ruleview-rule")
  );

  is(
    rulesInView.length,
    expectedRules.length,
    "All expected rules are displayed"
  );

  for (let i = 0; i < expectedRules.length; i++) {
    const expectedRule = expectedRules[i];
    info(`Checking rule #${i}: ${expectedRule.selector}`);

    const selector = rulesInView[i].querySelector(
      ".ruleview-selectors-container"
    ).innerText;
    is(selector, expectedRule.selector, `Expected selector for ${selector}`);

    const ancestorDataEl = getRuleViewAncestorRulesDataElementByIndex(view, i);

    if (expectedRule.ancestorRulesData == null) {
      is(
        ancestorDataEl,
        null,
        `No ancestor rules data displayed for ${selector}`
      );
    } else {
      is(
        ancestorDataEl?.innerText,
        expectedRule.ancestorRulesData.join("\n"),
        `Expected ancestor rules data displayed for ${selector}`
      );
      Assert.notStrictEqual(
        ancestorDataEl.querySelector(".container-query .open-inspector"),
        null,
        "An icon is displayed to select the container in the markup view"
      );
    }
  }
}

async function assertJumpToContainerButton(
  inspector,
  view,
  ruleIndex,
  expectedSelectedNodeAfterClick
) {
  const selectContainerButton = getRuleViewAncestorRulesDataElementByIndex(
    view,
    ruleIndex
  ).querySelector(".open-inspector");

  // Ensure that the button can be targetted from EventUtils.
  selectContainerButton.scrollIntoView();

  const { waitForHighlighterTypeShown, waitForHighlighterTypeHidden } =
    getHighlighterTestHelpers(inspector);

  const onNodeHighlight = waitForHighlighterTypeShown(
    inspector.highlighters.TYPES.BOXMODEL
  );
  EventUtils.synthesizeMouseAtCenter(
    selectContainerButton,
    { type: "mouseover" },
    selectContainerButton.ownerDocument.defaultView
  );
  const { nodeFront: highlightedNodeFront } = await onNodeHighlight;
  is(
    highlightedNodeFront.displayName,
    expectedSelectedNodeAfterClick,
    "The correct node was highlighted"
  );

  const onceNewNodeFront = inspector.selection.once("new-node-front");
  const onNodeUnhighlight = waitForHighlighterTypeHidden(
    inspector.highlighters.TYPES.BOXMODEL
  );

  EventUtils.synthesizeMouseAtCenter(
    selectContainerButton,
    {},
    selectContainerButton.ownerDocument.defaultView
  );

  const nodeFront = await onceNewNodeFront;
  is(
    nodeFront.displayName,
    expectedSelectedNodeAfterClick,
    "The correct node has been selected"
  );

  await onNodeUnhighlight;
  ok(true, "Highlighter was hidden when clicking on icon");

  // Move mouse so it does stay in a position where it could hover something impacting
  // the test.
  EventUtils.synthesizeMouse(
    selectContainerButton.closest("body"),
    0,
    0,
    { type: "mouseover" },
    selectContainerButton.ownerDocument.defaultView
  );
}

async function assertQueryContainerTooltip({
  inspector,
  view,
  ruleIndex,
  expectedHeaderText,
  expectedBodyText,
}) {
  const tooltipTriggerEl = getRuleViewAncestorRulesDataElementByIndex(
    view,
    ruleIndex
  ).querySelector(".container-query-declaration");

  // Ensure that the element can be targetted from EventUtils.
  tooltipTriggerEl.scrollIntoView();

  const { waitForHighlighterTypeShown, waitForHighlighterTypeHidden } =
    getHighlighterTestHelpers(inspector);

  const onNodeHighlight = waitForHighlighterTypeShown(
    inspector.highlighters.TYPES.BOXMODEL
  );

  const tooltip = view.tooltips.getTooltip("interactiveTooltip");
  const onTooltipReady = tooltip.once("shown");
  info("synthesizing mousemove: " + tooltip.isVisible());
  EventUtils.synthesizeMouseAtCenter(
    tooltipTriggerEl,
    { type: "mousemove" },
    tooltipTriggerEl.ownerDocument.defaultView
  );
  await onTooltipReady;
  info("tooltip was shown");
  await onNodeHighlight;
  info("node was highlighted");

  is(
    tooltip.panel.querySelector("header").textContent,
    expectedHeaderText,
    "Tooltip has expected header content"
  );

  const lis = Array.from(tooltip.panel.querySelectorAll("li")).map(
    li => li.textContent
  );
  Assert.deepEqual(lis, expectedBodyText, "Tooltip has expected body items");

  info("Hide the tooltip");
  const onHidden = tooltip.once("hidden");
  const onNodeUnhighlight = waitForHighlighterTypeHidden(
    inspector.highlighters.TYPES.BOXMODEL
  );
  // Move the mouse elsewhere to hide the tooltip
  EventUtils.synthesizeMouse(
    tooltipTriggerEl.ownerDocument.body,
    1,
    1,
    { type: "mousemove" },
    tooltipTriggerEl.ownerDocument.defaultView
  );
  await onHidden;
  await onNodeUnhighlight;
}