summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_pseudo_lock_options.js
blob: 438b96807f87b8760421b527bf2502b37d4f6664 (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that the rule view pseudo lock options work properly.

const {
  PSEUDO_CLASSES,
} = require("resource://devtools/shared/css/constants.js");
const TEST_URI = `
  <style type='text/css'>
    div {
      color: red;
    }
    div:hover {
      color: blue;
    }
    div:active {
      color: yellow;
    }
    div:focus {
      color: green;
    }
    div:focus-within {
      color: papayawhip;
    }
    div:visited {
      color: orange;
    }
    div:focus-visible {
      color: wheat;
    }
    div:target {
      color: crimson;
    }
  </style>
  <div>test div</div>
`;

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

  info("Check that the toggle button exists");
  const button = inspector.panelDoc.getElementById("pseudo-class-panel-toggle");
  ok(button, "The pseudo-class panel toggle button exists");
  is(
    view.pseudoClassToggle,
    button,
    "The rule-view refers to the right element"
  );
  is(
    inspector.panelDoc.getElementById(button.getAttribute("aria-controls")),
    view.pseudoClassPanel,
    "The pseudo-class panel toggle button has valid aria-controls attribute"
  );

  await assertPseudoPanelClosed(view);

  info("Toggle the pseudo class panel open");
  view.pseudoClassToggle.click();
  await assertPseudoPanelOpened(view);

  info("Toggle each pseudo lock and check that the pseudo lock is added");
  for (const pseudo of PSEUDO_CLASSES) {
    await togglePseudoClass(inspector, view, pseudo);
    await assertPseudoAdded(inspector, view, pseudo, 3, 1);
    await togglePseudoClass(inspector, view, pseudo);
    await assertPseudoRemoved(inspector, view, 2);
  }

  info("Toggle all pseudo locks and check that the pseudo lock is added");
  await togglePseudoClass(inspector, view, ":hover");
  await togglePseudoClass(inspector, view, ":active");
  await togglePseudoClass(inspector, view, ":focus");
  await togglePseudoClass(inspector, view, ":target");
  await assertPseudoAdded(inspector, view, ":target", 6, 1);
  await assertPseudoAdded(inspector, view, ":focus", 6, 2);
  await assertPseudoAdded(inspector, view, ":active", 6, 3);
  await assertPseudoAdded(inspector, view, ":hover", 6, 4);
  await togglePseudoClass(inspector, view, ":hover");
  await togglePseudoClass(inspector, view, ":active");
  await togglePseudoClass(inspector, view, ":focus");
  await togglePseudoClass(inspector, view, ":target");
  await assertPseudoRemoved(inspector, view, 2);

  info("Select a null element");
  await view.selectElement(null);

  info("Check that all pseudo locks are unchecked and disabled");
  for (const pseudo of PSEUDO_CLASSES) {
    const checkbox = getPseudoClassCheckbox(view, pseudo);
    ok(
      !checkbox.checked && checkbox.disabled,
      `${pseudo} checkbox is unchecked and disabled`
    );
  }

  info("Toggle the pseudo class panel close");
  view.pseudoClassToggle.click();
  await assertPseudoPanelClosed(view);
});

async function togglePseudoClass(inspector, view, pseudoClass) {
  info(`Toggle the pseudo-class ${pseudoClass}, wait for it to be applied`);
  const onRefresh = inspector.once("rule-view-refreshed");
  const checkbox = getPseudoClassCheckbox(view, pseudoClass);
  if (checkbox) {
    checkbox.click();
  }
  await onRefresh;
}

function assertPseudoAdded(inspector, view, pseudoClass, numRules, childIndex) {
  info("Check that the rule view contains the pseudo-class rule");
  is(
    view.element.children.length,
    numRules,
    "Should have " + numRules + " rules."
  );
  is(
    getRuleViewRuleEditor(view, childIndex).rule.selectorText,
    "div" + pseudoClass,
    "rule view is showing " + pseudoClass + " rule"
  );
}

function assertPseudoRemoved(inspector, view, numRules) {
  info("Check that the rule view no longer contains the pseudo-class rule");
  is(
    view.element.children.length,
    numRules,
    "Should have " + numRules + " rules."
  );
  is(
    getRuleViewRuleEditor(view, 1).rule.selectorText,
    "div",
    "Second rule is div"
  );
}

function assertPseudoPanelOpened(view) {
  info("Check the opened state of the pseudo class panel");
  ok(!view.pseudoClassPanel.hidden, "Pseudo Class Panel Opened");
  is(
    view.pseudoClassToggle.getAttribute("aria-pressed"),
    "true",
    "The toggle button is pressed"
  );

  for (const pseudo of PSEUDO_CLASSES) {
    const checkbox = getPseudoClassCheckbox(view, pseudo);
    ok(!checkbox.disabled, `${pseudo} checkbox is not disabled`);
    is(
      checkbox.getAttribute("tabindex"),
      "0",
      `${pseudo} checkbox has a tabindex of 0`
    );
  }
}

function assertPseudoPanelClosed(view) {
  info("Check the closed state of the pseudo clas panel");
  ok(view.pseudoClassPanel.hidden, "Pseudo Class Panel Hidden");
  is(
    view.pseudoClassToggle.getAttribute("aria-pressed"),
    "false",
    "The toggle button is not pressed"
  );

  for (const pseudo of PSEUDO_CLASSES) {
    const checkbox = getPseudoClassCheckbox(view, pseudo);
    is(
      checkbox.getAttribute("tabindex"),
      "-1",
      `${pseudo} checkbox has a tabindex of -1`
    );
  }
}