summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_inspector-pseudoclass-lock.html
blob: 64bb03f80a577d0789f154b32c65a5071d6270dc (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug </title>

  <script type="application/javascript" 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;version=1.8" src="inspector-helpers.js"></script>
  <script type="application/javascript;version=1.8">
const inspector = require("devtools/shared/fronts/inspector");
const DOMUtils = Components.classes["@mozilla.org/inspector/dom-utils;1"].
                   getService(Components.interfaces.inIDOMUtils);

const KNOWN_PSEUDOCLASSES = [':hover', ':active', ':focus']

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

var gInspectee = null;
var gWalker = null;
var gClient = null;
var gCleanupConnection = null;

function setup(callback) {
  let url = document.getElementById("inspectorContent").href;
  gCleanupConnection = attachURL(url, function(err, client, tab, doc) {
    gInspectee = doc;
    let {InspectorFront} = require("devtools/shared/fronts/inspector");
    let inspector = InspectorFront(client, tab);
    promiseDone(inspector.getWalker().then(walker => {
      gClient = client;
      gWalker = walker;
    }).then(callback));
  });
}

function teardown() {
  gWalker = null;
  gClient = null;
  gInspectee = null;
  if (gCleanupConnection) {
    gCleanupConnection();
    gCleanupConnection = null;
  }
}

function checkChange(change, expectation) {
  is(change.type, "pseudoClassLock", "Expect a pseudoclass lock change.");
  let target = change.target;
  if (expectation.id)
    is(target.id, expectation.id, "Expect a change on node id " + expectation.id);
  if (expectation.nodeName)
    is(target.nodeName, expectation.nodeName, "Expect a change on node name " + expectation.nodeName);

  is(target.pseudoClassLocks.length, expectation.pseudos.length,
     "Expect " + expectation.pseudos.length + " pseudoclass locks.");
  for (let pseudo of expectation.pseudos) {
    ok(target.hasPseudoClassLock(pseudo), "Expect lock: " + pseudo);
    ok(DOMUtils.hasPseudoClassLock(target.rawNode(), pseudo), "Expect lock in dom: " + pseudo);
  }

  for (let pseudo of KNOWN_PSEUDOCLASSES) {
    if (!expectation.pseudos.some(expected => pseudo === expected)) {
      ok(!target.hasPseudoClassLock(pseudo), "Don't expect lock: " + pseudo);
      ok(!DOMUtils.hasPseudoClassLock(target.rawNode(), pseudo), "Don't expect lock in dom: " + pseudo);

    }
  }
}

function checkMutations(mutations, expectations) {
  is(mutations.length, expectations.length, "Should get the right number of mutations.");
  for (let i = 0; i < mutations.length; i++) {
    checkChange(mutations[i] , expectations[i]);
  }
}

addTest(function testPseudoClassLock() {
  let contentNode;
  let nodeFront;
  setup(() => {
    contentNode = gInspectee.querySelector("#b");
    return promiseDone(gWalker.querySelector(gWalker.rootNode, "#b").then(front => {
      nodeFront = front;
      // Lock the pseudoclass alone, no parents.
      gWalker.addPseudoClassLock(nodeFront, ':active');
      // Expect a single pseudoClassLock mutation.
      return promiseOnce(gWalker, "mutations");
    }).then(mutations => {
      is(mutations.length, 1, "Should get one mutations");
      is(mutations[0].target, nodeFront, "Should be the node we tried to apply to");
      checkChange(mutations[0], {
        id: "b",
        nodeName: "DIV",
        pseudos: [":active"]
      });
    }).then(() => {
      // Now add :hover, this time with parents.
      gWalker.addPseudoClassLock(nodeFront, ':hover', {parents: true});
      return promiseOnce(gWalker, "mutations");
    }).then(mutations => {
      let expectedMutations = [{
        id: 'b',
        nodeName: 'DIV',
        pseudos: [':hover', ':active'],
      },
      {
        id: 'longlist',
        nodeName: 'DIV',
        pseudos: [':hover']
      },
      {
        nodeName: 'BODY',
        pseudos: [':hover']
      },
      {
        nodeName: 'HTML',
        pseudos: [':hover']
      }];
      checkMutations(mutations, expectedMutations);
    }).then(() => {
      // Now remove the :hover on all parents
      gWalker.removePseudoClassLock(nodeFront, ':hover', {parents: true});
      return promiseOnce(gWalker, "mutations");
    }).then(mutations => {
      let expectedMutations = [{
        id: 'b',
        nodeName: 'DIV',
        // Should still have :active on the original node.
        pseudos: [':active']
      },
      {
        id: 'longlist',
        nodeName: 'DIV',
        pseudos: []
      },
      {
        nodeName: 'BODY',
        pseudos: []
      },
      {
        nodeName: 'HTML',
        pseudos: []
      }];
      checkMutations(mutations, expectedMutations);
    }).then(() => {
      // Now shut down the walker and make sure that clears up the remaining lock.
      return gWalker.release();
    }).then(() => {
      ok(!DOMUtils.hasPseudoClassLock(contentNode, ':active'), "Pseudoclass should have been removed during destruction.");
      teardown();
    }).then(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-traversal-data.html">Test Document</a>
<p id="display"></p>
<div id="content" style="display: none">

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