summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_inspector-mutations-attr.html
blob: 15c14608b32c949055261bea8f61721d69b68f39 (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
<!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");

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

var gInspectee = null;
var gWalker = null;
var gClient = null;
var attrNode;
var attrFront;

addTest(function setup() {
  let url = document.getElementById("inspectorContent").href;
  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 => {
      ok(walker, "getWalker() should return an actor.");
      gClient = client;
      gWalker = walker;
    }).then(runNextTest));
  });
});

addTest(setupAttrTest);
addTest(testAddAttribute);
addTest(testChangeAttribute);
addTest(testRemoveAttribute);
addTest(testQueuedMutations);
addTest(setupFrameAttrTest);
addTest(testAddAttribute);
addTest(testChangeAttribute);
addTest(testRemoveAttribute);
addTest(testQueuedMutations);

function setupAttrTest() {
  attrNode = gInspectee.querySelector("#a")
  promiseDone(gWalker.querySelector(gWalker.rootNode, "#a").then(node => {
    attrFront = node;
  }).then(runNextTest));
}

function setupFrameAttrTest() {
  let frame = gInspectee.querySelector('#childFrame');
  attrNode = frame.contentDocument.querySelector("#a");

  promiseDone(gWalker.querySelector(gWalker.rootNode, "#childFrame").then(childFrame => {
    return gWalker.children(childFrame);
  }).then(children => {
    let nodes = children.nodes;
    ok(nodes.length, 1, "There should be only one child of the iframe");
    is(nodes[0].nodeType, Node.DOCUMENT_NODE, "iframe child should be a document node");
    return gWalker.querySelector(nodes[0], "#a");
  }).then(node => {
    attrFront = node;
  }).then(runNextTest));
}

function testAddAttribute() {
  attrNode.setAttribute("data-newattr", "newvalue");
  attrNode.setAttribute("data-newattr2", "newvalue");
  gWalker.once("mutations", () => {
    is(attrFront.attributes.length, 3, "Should have id and two new attributes.");
    is(attrFront.getAttribute("data-newattr"), "newvalue", "Node front should have the first new attribute");
    is(attrFront.getAttribute("data-newattr2"), "newvalue", "Node front should have the second new attribute.");
    runNextTest();
  });
}

function testChangeAttribute() {
  attrNode.setAttribute("data-newattr", "changedvalue1");
  attrNode.setAttribute("data-newattr", "changedvalue2");
  attrNode.setAttribute("data-newattr", "changedvalue3");
  gWalker.once("mutations", mutations => {
    is(mutations.length, 1, "Only one mutation is sent for multiple queued attribute changes");
    is(attrFront.attributes.length, 3, "Should have id and two new attributes.");
    is(attrFront.getAttribute("data-newattr"), "changedvalue3", "Node front should have the changed first value");
    is(attrFront.getAttribute("data-newattr2"), "newvalue", "Second value should remain unchanged.");
    runNextTest();
  });
}

function testRemoveAttribute() {
  attrNode.removeAttribute("data-newattr2");
  gWalker.once("mutations", () => {
    is(attrFront.attributes.length, 2, "Should have id and one remaining attribute.");
    is(attrFront.getAttribute("data-newattr"), "changedvalue3", "Node front should still have the first value");
    ok(!attrFront.hasAttribute("data-newattr2"), "Second value should be removed.");
    runNextTest();
  })
}

function testQueuedMutations() {
  // All modifications to each attribute should be queued in one mutation event.

  attrNode.removeAttribute("data-newattr");
  attrNode.setAttribute("data-newattr", "1");
  attrNode.removeAttribute("data-newattr");
  attrNode.setAttribute("data-newattr", "2");
  attrNode.removeAttribute("data-newattr");

  for (var i = 0; i <= 1000; i++) {
    attrNode.setAttribute("data-newattr2", i);
  }

  attrNode.removeAttribute("data-newattr3");
  attrNode.setAttribute("data-newattr3", "1");
  attrNode.removeAttribute("data-newattr3");
  attrNode.setAttribute("data-newattr3", "2");
  attrNode.removeAttribute("data-newattr3");
  attrNode.setAttribute("data-newattr3", "3");

  // This shouldn't be added in the attribute set, since it's a new
  // attribute that's been added and removed.
  attrNode.setAttribute("data-newattr4", "4");
  attrNode.removeAttribute("data-newattr4");

  gWalker.once("mutations", mutations => {
    is(mutations.length, 4, "Only one mutation each is sent for multiple queued attribute changes");
    is(attrFront.attributes.length, 3, "Should have id, data-newattr2, and data-newattr3.");

    is(attrFront.getAttribute("data-newattr2"), "1000", "Node front should still have the correct value");
    is(attrFront.getAttribute("data-newattr3"), "3", "Node front should still have the correct value");
    ok(!attrFront.hasAttribute("data-newattr"), "Attribute value should be removed.");
    ok(!attrFront.hasAttribute("data-newattr4"), "Attribute value should be removed.");

    runNextTest();
  })
}

addTest(function cleanup() {
  delete gInspectee;
  delete gWalker;
  delete gClient;
  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>