toolkit/devtools/server/tests/mochitest/test_inspector-pseudoclass-lock.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/devtools/server/tests/mochitest/test_inspector-pseudoclass-lock.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,177 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=
     1.8 +-->
     1.9 +<head>
    1.10 +  <meta charset="utf-8">
    1.11 +  <title>Test for Bug </title>
    1.12 +
    1.13 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.14 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
    1.15 +  <script type="application/javascript;version=1.8" src="inspector-helpers.js"></script>
    1.16 +  <script type="application/javascript;version=1.8">
    1.17 +Components.utils.import("resource://gre/modules/devtools/Loader.jsm");
    1.18 +const {Promise: promise} = Components.utils.import("resource://gre/modules/Promise.jsm", {});
    1.19 +
    1.20 +const inspector = devtools.require("devtools/server/actors/inspector");
    1.21 +const DOMUtils = Components.classes["@mozilla.org/inspector/dom-utils;1"].
    1.22 +                   getService(Components.interfaces.inIDOMUtils);
    1.23 +
    1.24 +const KNOWN_PSEUDOCLASSES = [':hover', ':active', ':focus']
    1.25 +
    1.26 +window.onload = function() {
    1.27 +  SimpleTest.waitForExplicitFinish();
    1.28 +  runNextTest();
    1.29 +}
    1.30 +
    1.31 +var gInspectee = null;
    1.32 +var gWalker = null;
    1.33 +var gClient = null;
    1.34 +var gCleanupConnection = null;
    1.35 +
    1.36 +function setup(callback) {
    1.37 +  let url = document.getElementById("inspectorContent").href;
    1.38 +  gCleanupConnection = attachURL(url, function(err, client, tab, doc) {
    1.39 +    gInspectee = doc;
    1.40 +    let {InspectorFront} = devtools.require("devtools/server/actors/inspector");
    1.41 +    let inspector = InspectorFront(client, tab);
    1.42 +    promiseDone(inspector.getWalker().then(walker => {
    1.43 +      gClient = client;
    1.44 +      gWalker = walker;
    1.45 +    }).then(callback));
    1.46 +  });
    1.47 +}
    1.48 +
    1.49 +function teardown() {
    1.50 +  gWalker = null;
    1.51 +  gClient = null;
    1.52 +  gInspectee = null;
    1.53 +  if (gCleanupConnection) {
    1.54 +    gCleanupConnection();
    1.55 +    gCleanupConnection = null;
    1.56 +  }
    1.57 +}
    1.58 +
    1.59 +function checkChange(change, expectation) {
    1.60 +  is(change.type, "pseudoClassLock", "Expect a pseudoclass lock change.");
    1.61 +  let target = change.target;
    1.62 +  if (expectation.id)
    1.63 +    is(target.id, expectation.id, "Expect a change on node id " + expectation.id);
    1.64 +  if (expectation.nodeName)
    1.65 +    is(target.nodeName, expectation.nodeName, "Expect a change on node name " + expectation.nodeName);
    1.66 +
    1.67 +  is(target.pseudoClassLocks.length, expectation.pseudos.length,
    1.68 +     "Expect " + expectation.pseudos.length + " pseudoclass locks.");
    1.69 +  for (let pseudo of expectation.pseudos) {
    1.70 +    ok(target.hasPseudoClassLock(pseudo), "Expect lock: " + pseudo);
    1.71 +    ok(DOMUtils.hasPseudoClassLock(target.rawNode(), pseudo), "Expect lock in dom: " + pseudo);
    1.72 +  }
    1.73 +
    1.74 +  for (let pseudo of KNOWN_PSEUDOCLASSES) {
    1.75 +    if (!expectation.pseudos.some(expected => pseudo === expected)) {
    1.76 +      ok(!target.hasPseudoClassLock(pseudo), "Don't expect lock: " + pseudo);
    1.77 +      ok(!DOMUtils.hasPseudoClassLock(target.rawNode(), pseudo), "Don't expect lock in dom: " + pseudo);
    1.78 +
    1.79 +    }
    1.80 +  }
    1.81 +}
    1.82 +
    1.83 +function checkMutations(mutations, expectations) {
    1.84 +  is(mutations.length, expectations.length, "Should get the right number of mutations.");
    1.85 +  for (let i = 0; i < mutations.length; i++) {
    1.86 +    checkChange(mutations[i] , expectations[i]);
    1.87 +  }
    1.88 +}
    1.89 +
    1.90 +addTest(function testPseudoClassLock() {
    1.91 +  let contentNode;
    1.92 +  let nodeFront;
    1.93 +  setup(() => {
    1.94 +    contentNode = gInspectee.querySelector("#b");
    1.95 +    return promiseDone(gWalker.querySelector(gWalker.rootNode, "#b").then(front => {
    1.96 +      nodeFront = front;
    1.97 +      // Lock the pseudoclass alone, no parents.
    1.98 +      gWalker.addPseudoClassLock(nodeFront, ':active');
    1.99 +      // Expect a single pseudoClassLock mutation.
   1.100 +      return promiseOnce(gWalker, "mutations");
   1.101 +    }).then(mutations => {
   1.102 +      is(mutations.length, 1, "Should get one mutations");
   1.103 +      is(mutations[0].target, nodeFront, "Should be the node we tried to apply to");
   1.104 +      checkChange(mutations[0], {
   1.105 +        id: "b",
   1.106 +        nodeName: "DIV",
   1.107 +        pseudos: [":active"]
   1.108 +      });
   1.109 +    }).then(() => {
   1.110 +      // Now add :hover, this time with parents.
   1.111 +      gWalker.addPseudoClassLock(nodeFront, ':hover', {parents: true});
   1.112 +      return promiseOnce(gWalker, "mutations");
   1.113 +    }).then(mutations => {
   1.114 +      let expectedMutations = [{
   1.115 +        id: 'b',
   1.116 +        nodeName: 'DIV',
   1.117 +        pseudos: [':hover', ':active'],
   1.118 +      },
   1.119 +      {
   1.120 +        id: 'longlist',
   1.121 +        nodeName: 'DIV',
   1.122 +        pseudos: [':hover']
   1.123 +      },
   1.124 +      {
   1.125 +        nodeName: 'BODY',
   1.126 +        pseudos: [':hover']
   1.127 +      },
   1.128 +      {
   1.129 +        nodeName: 'HTML',
   1.130 +        pseudos: [':hover']
   1.131 +      }];
   1.132 +      checkMutations(mutations, expectedMutations);
   1.133 +    }).then(() => {
   1.134 +      // Now remove the :hover on all parents
   1.135 +      gWalker.removePseudoClassLock(nodeFront, ':hover', {parents: true});
   1.136 +      return promiseOnce(gWalker, "mutations");
   1.137 +    }).then(mutations => {
   1.138 +      let expectedMutations = [{
   1.139 +        id: 'b',
   1.140 +        nodeName: 'DIV',
   1.141 +        // Should still have :active on the original node.
   1.142 +        pseudos: [':active']
   1.143 +      },
   1.144 +      {
   1.145 +        id: 'longlist',
   1.146 +        nodeName: 'DIV',
   1.147 +        pseudos: []
   1.148 +      },
   1.149 +      {
   1.150 +        nodeName: 'BODY',
   1.151 +        pseudos: []
   1.152 +      },
   1.153 +      {
   1.154 +        nodeName: 'HTML',
   1.155 +        pseudos: []
   1.156 +      }];
   1.157 +      checkMutations(mutations, expectedMutations);
   1.158 +    }).then(() => {
   1.159 +      // Now shut down the walker and make sure that clears up the remaining lock.
   1.160 +      return gWalker.release();
   1.161 +    }).then(() => {
   1.162 +      ok(!DOMUtils.hasPseudoClassLock(contentNode, ':active'), "Pseudoclass should have been removed during destruction.");
   1.163 +      teardown();
   1.164 +    }).then(runNextTest));
   1.165 +  });
   1.166 +});
   1.167 +
   1.168 +  </script>
   1.169 +</head>
   1.170 +<body>
   1.171 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug </a>
   1.172 +<a id="inspectorContent" target="_blank" href="inspector-traversal-data.html">Test Document</a>
   1.173 +<p id="display"></p>
   1.174 +<div id="content" style="display: none">
   1.175 +
   1.176 +</div>
   1.177 +<pre id="test">
   1.178 +</pre>
   1.179 +</body>
   1.180 +</html>

mercurial