summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/markup/test/browser_markup_overflow_badge.js
blob: 6770929594a381a23fef8be2e92a1c9c65ef737e (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
/* 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";

// Tests that the overflow badge is shown to overflow causing elements and is updated
// dynamically.

const TEST_URI = `
  <style type="text/css">
    .parent {
        width: 200px;
        height: 200px;
        overflow: scroll;
    }
    .fixed {
        width: 50px;
        height: 50px;
    }
    .shift {
        margin-left: 300px;
    }
  </style>
  <div id="top" class="parent">
    <div id="child1" class="fixed shift">
        <div id="child2" class="fixed"></div>
    </div>
    <div id="child3" class="shift">
        <div id="child4" class="fixed"></div>
    </div>
  </div>
`;

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

  await expandChildContainers(inspector);

  const child1 = await getContainerForSelector("#child1", inspector);
  const child2 = await getContainerForSelector("#child2", inspector);
  const child3 = await getContainerForSelector("#child3", inspector);
  const child4 = await getContainerForSelector("#child4", inspector);

  info(
    "Checking if overflow badges appear correctly right after the markup-view is loaded"
  );

  checkBadges([child1, child4], [child2, child3]);

  info("Changing the elements to check whether the badge updates correctly");

  await toggleClass(inspector);

  checkBadges([child2, child3], [child1, child4]);

  info(
    "Once again, changing the elements to check whether the badge updates correctly"
  );

  await toggleClass(inspector);

  checkBadges([child1, child4], [child2, child3]);
});

async function expandChildContainers(inspector) {
  const container = await getContainerForSelector("#top", inspector);

  await expandContainer(inspector, container);
  for (const child of container.getChildContainers()) {
    await expandContainer(inspector, child);
  }
}

async function toggleClass(inspector) {
  const onStateChanged = inspector.walker.once("overflow-change");

  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
    content.document.querySelector("#child1").classList.toggle("fixed");
    content.document.querySelector("#child3").classList.toggle("fixed");
  });

  await onStateChanged;
}

function checkBadges(elemsWithBadges, elemsWithNoBadges) {
  const hasBadge = elem =>
    elem.elt.querySelector(
      `#${elem.elt.children[0].id} > span.editor > .inspector-badge.overflow-badge`
    );

  for (const elem of elemsWithBadges) {
    ok(hasBadge(elem), `Overflow badge exists in ${elem.node.id}`);
  }

  for (const elem of elemsWithNoBadges) {
    ok(!hasBadge(elem), `Overflow badge does not exist in ${elem.node.id}`);
  }
}