summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/chrome/test_styles-applied.html
blob: 0910e9d7bc30e2bb11f70bd7bc7a9c46355f9104 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug </title>

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  <script type="application/javascript" src="inspector-helpers.js"></script>
  <script type="application/javascript">
"use strict";

window.onload = function() {
  SimpleTest.waitForExplicitFinish();
  runNextTest();
};

let gWalker = null;
let gStyles = null;

addTest(async function setup() {
  const url = document.getElementById("inspectorContent").href;
  const { commands, target } = await attachURL(url);

  // We need an active resource command before initializing the inspector front.
  const resourceCommand = commands.resourceCommand;
  // We listen to any random resource, we only need to trigger the resource command
  // onTargetAvailable callback so the `resourceCommand` attribute is set on the target front.
  await resourceCommand.watchResources([resourceCommand.TYPES.DOCUMENT_EVENT], { onAvailable: () => {} });

  const inspector = await target.getFront("inspector");
  gWalker = inspector.walker;
  gStyles = await inspector.getPageStyle();

  runNextTest();
});

addTest(async function inheritedUserStyles() {
  const node = await gWalker.querySelector(gWalker.rootNode, "#test-node")
  const applied = await gStyles.getApplied(node, { inherited: true, filter: "user" });

  ok(!applied[0].inherited, "Entry 0 should be uninherited");
  is(applied[0].rule.type, 100, "Entry 0 should be an element style");
  ok(!!applied[0].rule.href, "Element styles should have a URL");
  is(applied[0].rule.cssText, "", "Entry 0 should be an empty style");

  is(applied[1].inherited.id, "uninheritable-rule-inheritable-style",
      "Entry 1 should be inherited from the parent");
  is(applied[1].rule.type, 100, "Entry 1 should be an element style");
  is(applied[1].rule.cssText, "color: red;",
      "Entry 1 should have the expected cssText");

  is(applied[2].inherited.id, "inheritable-rule-inheritable-style",
      "Entry 2 should be inherited from the parent's parent");
  is(applied[2].rule.type, 100, "Entry 2 should be an element style");
  is(applied[2].rule.cssText, "color: blue;",
      "Entry 2 should have the expected cssText");

  is(applied[3].inherited.id, "inheritable-rule-inheritable-style",
      "Entry 3 should be inherited from the parent's parent");
  is(applied[3].rule.type, 1, "Entry 3 should be a rule style");
  is(applied[3].rule.cssText, "font-size: 15px;",
      "Entry 3 should have the expected cssText");
  ok(!applied[3].matchedDesugaredSelectors,
      "Shouldn't get matchedDesugaredSelectors with this request.");

  is(applied[4].inherited.id, "inheritable-rule-uninheritable-style",
      "Entry 4 should be inherited from the parent's parent");
  is(applied[4].rule.type, 1, "Entry 4 should be an rule style");
  is(applied[4].rule.cssText, "font-size: 15px;",
      "Entry 4 should have the expected cssText");
  ok(!applied[4].matchedDesugaredSelectors, "Shouldn't get matchedDesugaredSelectors with this request.");

  is(applied.length, 5, "Should have 5 rules.");

  runNextTest();
});

addTest(async function inheritedSystemStyles() {
  const node = await gWalker.querySelector(gWalker.rootNode, "#test-node");
  const applied = await gStyles.getApplied(node, { inherited: true, filter: "ua" });
  // If our system stylesheets are prone to churn, this might be a fragile
  // test.  If you're here because of that I apologize, file a bug
  // and we can find a different way to test.

  ok(!applied[1].inherited, "Entry 1 should not be inherited");
  ok(applied[1].rule.parentStyleSheet.system, "Entry 1 should be a system style");
  is(applied[1].rule.type, 1, "Entry 1 should be a rule style");
  is(applied.length, 9, "Should have the expected number of rules.");

  runNextTest();
});

addTest(async function noInheritedStyles() {
  const node = await gWalker.querySelector(gWalker.rootNode, "#test-node")
  const applied = await gStyles.getApplied(node, { inherited: false, filter: "user" });
  ok(!applied[0].inherited, "Entry 0 should be uninherited");
  is(applied[0].rule.type, 100, "Entry 0 should be an element style");
  is(applied[0].rule.cssText, "", "Entry 0 should be an empty style");
  is(applied.length, 1, "Should have 1 rule.");

  runNextTest();
});

addTest(async function matchedSelectors() {
  const node = await gWalker.querySelector(gWalker.rootNode, "#test-node");
  const applied = await gStyles.getApplied(node, {
    inherited: true, filter: "user", matchedSelectors: true,
  });
  is(applied[3].matchedDesugaredSelectors[0], ".inheritable-rule",
      "Entry 3 should have a matched selector");
  is(applied[4].matchedDesugaredSelectors[0], ".inheritable-rule",
      "Entry 4 should have a matched selector");

  runNextTest();
});

addTest(async function testMediaQuery() {
  const node = await gWalker.querySelector(gWalker.rootNode, "#mediaqueried")
  const applied = await gStyles.getApplied(node, {
    inherited: false,
    filter: "user",
    matchedSelectors: true,
  });

  const ruleWithMedia = applied[1].rule;
  is(ruleWithMedia.type, 1, "Entry 1 is a rule style");
  is(ruleWithMedia.ancestorData[0].type, "media", "Entry 1's rule ancestor data holds the media rule data...");
  is(ruleWithMedia.ancestorData[0].value, "screen", "...with the expected value");

  runNextTest();
});

addTest(function cleanup() {
  gStyles = null;
  gWalker = null;
  runNextTest();
});

  </script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
<a id="inspectorContent" target="_blank" href="inspector-styles-data.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>