summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_document_register_lifecycle.html
blob: 9db9afbf48b1f386bdd623ca1e1613a24e0aa128 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=783129
-->
<head>
  <title>Test for document.registerElement lifecycle callback</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=783129">Bug 783129</a>
<div id="container">
  <x-hello id="hello"></x-hello>
  <button id="extbutton" is="x-button"></button>
</div>
<script>

var container = document.getElementById("container");

// Tests callbacks after registering element type that is already in the document.
// create element in document -> register -> remove from document
function testRegisterUnresolved() {
  var helloElem = document.getElementById("hello");

  var createdCallbackCalled = false;
  var attachedCallbackCalled = false;
  var detachedCallbackCalled = false;

  var p = Object.create(HTMLElement.prototype);
  p.createdCallback = function() {
    is(helloElem.__proto__, p, "Prototype should be adjusted just prior to invoking the created callback.");
    is(createdCallbackCalled, false, "Created callback should only be called once in this tests.");
    is(this, helloElem, "The 'this' value should be the custom element.");
    createdCallbackCalled = true;
  };

  p.attachedCallback = function() {
    is(createdCallbackCalled, true, "Created callback should be called before attached");
    is(attachedCallbackCalled, false, "attached callback should only be called once in this test.");
    is(this, helloElem, "The 'this' value should be the custom element.");
    attachedCallbackCalled = true;
  };

  p.detachedCallback = function() {
    is(attachedCallbackCalled, true, "attached callback should be called before detached");
    is(detachedCallbackCalled, false, "detached callback should only be called once in this test.");
    detachedCallbackCalled = true;
    is(this, helloElem, "The 'this' value should be the custom element.");
    runNextTest();
  };

  p.attributeChangedCallback = function(name, oldValue, newValue) {
    ok(false, "attributeChanged callback should never be called in this test.");
  };

  document.registerElement("x-hello", { prototype: p });
  is(createdCallbackCalled, true, "created callback should be called when control returns to script from user agent code");

  // Remove element from document to trigger detached callback.
  container.removeChild(helloElem);
}

// Tests callbacks after registering an extended element type that is already in the document.
// create element in document -> register -> remove from document
function testRegisterUnresolvedExtended() {
  var buttonElem = document.getElementById("extbutton");

  var createdCallbackCalled = false;
  var attachedCallbackCalled = false;
  var detachedCallbackCalled = false;

  var p = Object.create(HTMLButtonElement.prototype);
  p.createdCallback = function() {
    is(buttonElem.__proto__, p, "Prototype should be adjusted just prior to invoking the created callback.");
    is(createdCallbackCalled, false, "Created callback should only be called once in this tests.");
    is(this, buttonElem, "The 'this' value should be the custom element.");
    createdCallbackCalled = true;
  };

  p.attachedCallback = function() {
    is(createdCallbackCalled, true, "Created callback should be called before attached");
    is(attachedCallbackCalled, false, "attached callback should only be called once in this test.");
    is(this, buttonElem, "The 'this' value should be the custom element.");
    attachedCallbackCalled = true;
  };

  p.detachedCallback = function() {
    is(attachedCallbackCalled, true, "attached callback should be called before detached");
    is(detachedCallbackCalled, false, "detached callback should only be called once in this test.");
    detachedCallbackCalled = true;
    is(this, buttonElem, "The 'this' value should be the custom element.");
    runNextTest();
  };

  p.attributeChangedCallback = function(name, oldValue, newValue) {
    ok(false, "attributeChanged callback should never be called in this test.");
  };

  document.registerElement("x-button", { prototype: p, extends: "button" });
  is(createdCallbackCalled, true, "created callback should be called when control returns to script from user agent code");

  // Remove element from document to trigger detached callback.
  container.removeChild(buttonElem);
}

function testInnerHTML() {
  var createdCallbackCalled = false;

  var p = Object.create(HTMLElement.prototype);
  p.createdCallback = function() {
    is(createdCallbackCalled, false, "created callback should only be called once in this test.");
    createdCallbackCalled = true;
  };

  document.registerElement("x-inner-html", { prototype: p });
  var div = document.createElement(div);
  div.innerHTML = '<x-inner-html></x-inner-html>';
  is(createdCallbackCalled, true, "created callback should be called after setting innerHTML.");
  runNextTest();
}

function testInnerHTMLExtended() {
  var createdCallbackCalled = false;

  var p = Object.create(HTMLButtonElement.prototype);
  p.createdCallback = function() {
    is(createdCallbackCalled, false, "created callback should only be called once in this test.");
    createdCallbackCalled = true;
  };

  document.registerElement("x-inner-html-extended", { prototype: p, extends: "button" });
  var div = document.createElement(div);
  div.innerHTML = '<button is="x-inner-html-extended"></button>';
  is(createdCallbackCalled, true, "created callback should be called after setting innerHTML.");
  runNextTest();
}

function testInnerHTMLUpgrade() {
  var createdCallbackCalled = false;

  var div = document.createElement(div);
  div.innerHTML = '<x-inner-html-upgrade></x-inner-html-upgrade>';

  var p = Object.create(HTMLElement.prototype);
  p.createdCallback = function() {
    is(createdCallbackCalled, false, "created callback should only be called once in this test.");
    createdCallbackCalled = true;
  };

  document.registerElement("x-inner-html-upgrade", { prototype: p });
  is(createdCallbackCalled, true, "created callback should be called after registering.");
  runNextTest();
}

function testInnerHTMLExtendedUpgrade() {
  var createdCallbackCalled = false;

  var div = document.createElement(div);
  div.innerHTML = '<button is="x-inner-html-extended-upgrade"></button>';

  var p = Object.create(HTMLButtonElement.prototype);
  p.createdCallback = function() {
    is(createdCallbackCalled, false, "created callback should only be called once in this test.");
    createdCallbackCalled = true;
  };

  document.registerElement("x-inner-html-extended-upgrade", { prototype: p, extends: "button" });
  is(createdCallbackCalled, true, "created callback should be called after registering.");
  runNextTest();
}

// Test callback when creating element after registering an element type.
// register -> create element -> insert into document -> remove from document
function testRegisterResolved() {
  var createdCallbackCalled = false;
  var attachedCallbackCalled = false;
  var detachedCallbackCalled = false;

  var createdCallbackThis;

  var p = Object.create(HTMLElement.prototype);
  p.createdCallback = function() {
    is(createdCallbackCalled, false, "Created callback should only be called once in this test.");
    createdCallbackThis = this;
    createdCallbackCalled = true;
  };

  p.attachedCallback = function() {
    is(createdCallbackCalled, true, "created callback should be called before attached callback.");
    is(attachedCallbackCalled, false, "attached callback should only be called on in this test.");
    is(this, createdElement, "The 'this' value should be the custom element.");
    attachedCallbackCalled = true;
  };

  p.detachedCallback = function() {
    is(attachedCallbackCalled, true, "attached callback should be called before detached");
    is(detachedCallbackCalled, false, "detached callback should only be called once in this test.");
    is(this, createdElement, "The 'this' value should be the custom element.");
    detachedCallbackCalled = true;
    runNextTest();
  };

  p.attributeChangedCallback = function() {
    ok(false, "attributeChanged callback should never be called in this test.");
  };

  document.registerElement("x-resolved", { prototype: p });
  is(createdCallbackCalled, false, "Created callback should not be called when custom element instance has not been created.");

  var createdElement = document.createElement("x-resolved");
  is(createdCallbackThis, createdElement, "The 'this' value in the created callback should be the custom element.");
  is(createdElement.__proto__, p, "Prototype of custom element should be the registered prototype.");

  // Insert element into document to trigger attached callback.
  container.appendChild(createdElement);

  // Remove element from document to trigger detached callback.
  container.removeChild(createdElement);
}

// Callbacks should always be the same ones when registered.
function testChangingCallback() {
  var p = Object.create(HTMLElement.prototype);
  var callbackCalled = false;
  p.attributeChangedCallback = function(name, oldValue, newValue) {
    is(callbackCalled, false, "Callback should only be called once in this test.");
    callbackCalled = true;
    runNextTest();
  };

  document.registerElement("x-test-callback", { prototype: p });

  p.attributeChangedCallback = function(name, oldValue, newValue) {
    ok(false, "Only callbacks at registration should be called.");
  };

  var elem = document.createElement("x-test-callback");
  elem.setAttribute("foo", "bar");
}

function testAttributeChanged() {
  var createdCallbackCalled = false;

  var createdElement;
  var createdCallbackThis;

  var p = Object.create(HTMLElement.prototype);
  p.createdCallback = function() {
    is(createdCallbackCalled, false, "Created callback should only be called once in this test.");
    createdCallbackThis = this;
    createdCallbackCalled = true;
  };

  // Sequence of callback arguments that we expect from attribute changed callback
  // after changing attributes values in a specific order.
  var expectedCallbackArguments = [
    // [oldValue, newValue]
    [null, "newvalue"], // Setting the attribute value to "newvalue"
    ["newvalue", "nextvalue"], // Changing the attribute value from "newvalue" to "nextvalue"
    ["nextvalue", ""], // Changing the attribute value from "nextvalue" to empty string
    ["", null], // Removing the attribute.
  ];

  p.attributeChangedCallback = function(name, oldValue, newValue) {
    is(createdCallbackCalled, true, "created callback should be called before attribute changed.");
    is(this, createdElement, "The 'this' value should be the custom element.");
    ok(expectedCallbackArguments.length > 0, "Attribute changed callback should not be called more than expected.");

    is(name, "changeme", "name arugment in attribute changed callback should be the name of the changed attribute.");

    var expectedArgs = expectedCallbackArguments.shift();
    is(oldValue, expectedArgs[0], "The old value argument should match the expected value.");
    is(newValue, expectedArgs[1], "The new value argument should match the expected value.");

    if (expectedCallbackArguments.length === 0) {
      // Done with the attribute changed callback test.
      runNextTest();
    }
  };

  document.registerElement("x-attrchange", { prototype: p });

  var createdElement = document.createElement("x-attrchange");
  is(createdCallbackThis, createdElement, "The 'this' value in the created callback should be the custom element.");
  createdElement.setAttribute("changeme", "newvalue");
  createdElement.setAttribute("changeme", "nextvalue");
  createdElement.setAttribute("changeme", "");
  createdElement.removeAttribute("changeme");
}

function testAttributeChangedExtended() {
  var p = Object.create(HTMLButtonElement.prototype);
  var callbackCalled = false;
  p.attributeChangedCallback = function(name, oldValue, newValue) {
    is(callbackCalled, false, "Callback should only be called once in this test.");
    callbackCalled = true;
    runNextTest();
  };

  document.registerElement("x-extended-attribute-change", { prototype: p, extends: "button" });

  var elem = document.createElement("button", {is: "x-extended-attribute-change"});
  elem.setAttribute("foo", "bar");
}

// Creates a custom element that is an upgrade candidate (no registration)
// and mutate the element in ways that would call callbacks for registered
// elements.
function testUpgradeCandidate() {
  var createdElement = document.createElement("x-upgrade-candidate");
  container.appendChild(createdElement);
  createdElement.setAttribute("foo", "bar");
  container.removeChild(createdElement);
  ok(true, "Nothing bad should happen when trying to mutating upgrade candidates.");
  runNextTest();
}

function testNotInDocEnterLeave() {
  var p = Object.create(HTMLElement.prototype);

  p.attached = function() {
    ok(false, "attached should not be called when not entering the document.");
  };

  p.detached = function() {
    ok(false, "leaveView should not be called when not leaving the document.");
  };

  var createdElement = document.createElement("x-destined-for-fragment");

  document.registerElement("x-destined-for-fragment", { prototype: p });

  var fragment = new DocumentFragment();
  fragment.appendChild(createdElement);
  fragment.removeChild(createdElement);

  var divNotInDoc = document.createElement("div");
  divNotInDoc.appendChild(createdElement);
  divNotInDoc.removeChild(createdElement);

  runNextTest();
}

function testEnterLeaveView() {
  var attachedCallbackCalled = false;
  var detachedCallbackCalled = false;

  var p = Object.create(HTMLElement.prototype);
  p.attachedCallback = function() {
    is(attachedCallbackCalled, false, "attached callback should only be called on in this test.");
    attachedCallbackCalled = true;
  };

  p.detachedCallback = function() {
    is(attachedCallbackCalled, true, "attached callback should be called before detached");
    is(detachedCallbackCalled, false, "detached callback should only be called once in this test.");
    detachedCallbackCalled = true;
    runNextTest();
  };

  var div = document.createElement("div");
  document.registerElement("x-element-in-div", { prototype: p });
  var customElement = document.createElement("x-element-in-div");
  div.appendChild(customElement);
  is(attachedCallbackCalled, false, "Appending a custom element to a node that is not in the document should not call the attached callback.");

  container.appendChild(div);
  container.removeChild(div);
}

var testFunctions = [
  testRegisterUnresolved,
  testRegisterUnresolvedExtended,
  testInnerHTML,
  testInnerHTMLExtended,
  testInnerHTMLUpgrade,
  testInnerHTMLExtendedUpgrade,
  testRegisterResolved,
  testAttributeChanged,
  testAttributeChangedExtended,
  testUpgradeCandidate,
  testChangingCallback,
  testNotInDocEnterLeave,
  testEnterLeaveView,
  SimpleTest.finish
];

function runNextTest() {
  if (testFunctions.length > 0) {
    var nextTestFunction = testFunctions.shift();
    nextTestFunction();
  }
}

SimpleTest.waitForExplicitFinish();

runNextTest();

</script>
</body>
</html>