summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_custom_element_lifecycle.html
blob: a02d0b297b52eff10c557471ac139952ae98444b (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=783129
-->
<head>
  <title>Test for custom elements lifecycle callback</title>
  <script 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 defining element type that is already in the document.
// create element in document -> define -> remove from document
function testRegisterUnresolved() {
  var helloElem = document.getElementById("hello");

  var connectedCallbackCalled = false;
  var disconnectedCallbackCalled = false;

  class Hello extends HTMLElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called once in this test.");
      is(this, helloElem, "The 'this' value should be the custom element.");
      connectedCallbackCalled = true;
    }

    disconnectedCallback() {
      is(connectedCallbackCalled, true, "Connected callback should be called before detached");
      is(disconnectedCallbackCalled, false, "Disconnected callback should only be called once in this test.");
      disconnectedCallbackCalled = true;
      is(this, helloElem, "The 'this' value should be the custom element.");
      runNextTest();
    }

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

  customElements.define("x-hello", Hello);

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

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

  var connectedCallbackCalled = false;
  var disconnectedCallbackCalled = false;

  class XButton extends HTMLButtonElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called once in this test.");
      is(this, buttonElem, "The 'this' value should be the custom element.");
      connectedCallbackCalled = true;
    }

    disconnectedCallback() {
      is(connectedCallbackCalled, true, "Connected callback should be called before detached");
      is(disconnectedCallbackCalled, false, "Disconnected callback should only be called once in this test.");
      disconnectedCallbackCalled = true;
      is(this, buttonElem, "The 'this' value should be the custom element.");
      runNextTest();
    }

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

  customElements.define("x-button", XButton, { extends: "button" });

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

function testInnerHTML() {
  var connectedCallbackCalled = false;

  class XInnerHTML extends HTMLElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called once in this test.");
      connectedCallbackCalled = true;
    }
  };

  customElements.define("x-inner-html", XInnerHTML);
  var div = document.createElement(div);
  document.documentElement.appendChild(div);
  div.innerHTML = '<x-inner-html></x-inner-html>';
  is(connectedCallbackCalled, true, "Connected callback should be called after setting innerHTML.");
  runNextTest();
}

function testInnerHTMLExtended() {
  var connectedCallbackCalled = false;

  class XInnerHTMLExtend extends HTMLButtonElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called once in this test.");
      connectedCallbackCalled = true;
    }
  };

  customElements.define("x-inner-html-extended", XInnerHTMLExtend, { extends: "button" });
  var div = document.createElement(div);
  document.documentElement.appendChild(div);
  div.innerHTML = '<button is="x-inner-html-extended"></button>';
  is(connectedCallbackCalled, true, "Connected callback should be called after setting innerHTML.");
  runNextTest();
}

function testInnerHTMLUpgrade() {
  var connectedCallbackCalled = false;

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

  class XInnerHTMLUpgrade extends HTMLElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called once in this test.");
      connectedCallbackCalled = true;
    }
  };

  customElements.define("x-inner-html-upgrade", XInnerHTMLUpgrade);
  is(connectedCallbackCalled, true, "Connected callback should be called after registering.");
  runNextTest();
}

function testInnerHTMLExtendedUpgrade() {
  var connectedCallbackCalled = false;

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

  class XInnerHTMLExtnedUpgrade extends HTMLButtonElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called once in this test.");
      connectedCallbackCalled = true;
    }
  };

  customElements.define("x-inner-html-extended-upgrade", XInnerHTMLExtnedUpgrade, { extends: "button" });
  is(connectedCallbackCalled, true, "Connected callback should be called after registering.");
  runNextTest();
}

// Test callback when creating element after defining an element type.
// define -> create element -> insert into document -> remove from document
function testRegisterResolved() {
  var connectedCallbackCalled = false;
  var disconnectedCallbackCalled = false;

  class Resolved extends HTMLElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called on in this test.");
      is(this, createdElement, "The 'this' value should be the custom element.");
      connectedCallbackCalled = true;
    }

    disconnectedCallback() {
      is(connectedCallbackCalled, true, "Connected callback should be called before detached");
      is(disconnectedCallbackCalled, false, "Disconnected callback should only be called once in this test.");
      is(this, createdElement, "The 'this' value should be the custom element.");
      disconnectedCallbackCalled = true;
      runNextTest();
    }

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

  customElements.define("x-resolved", Resolved);

  var createdElement = document.createElement("x-resolved");
  is(createdElement.__proto__, Resolved.prototype, "Prototype of custom element should be the defined 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 callbackCalled = false;

  class TestCallback extends HTMLElement
  {
    attributeChangedCallback(aName, aOldValue, aNewValue) {
      is(callbackCalled, false, "Callback should only be called once in this test.");
      callbackCalled = true;
      runNextTest();
    }

    static get observedAttributes() {
      return ["data-foo"];
    }
  }

  customElements.define("x-test-callback", TestCallback);

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

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

function testAttributeChanged() {
  var createdElement;
  // 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.
  ];

  class AttrChange extends HTMLElement
  {
    attributeChangedCallback(name, oldValue, newValue) {
      is(this, createdElement, "The 'this' value should be the custom element.");
      ok(expectedCallbackArguments.length, "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();
      }
    }

    static get observedAttributes() {
      return ["changeme"];
    }
  }

  customElements.define("x-attrchange", AttrChange);

  createdElement = document.createElement("x-attrchange");
  createdElement.setAttribute("changeme", "newvalue");
  createdElement.setAttribute("changeme", "nextvalue");
  createdElement.setAttribute("changeme", "");
  createdElement.removeAttribute("changeme");
}

function testAttributeChangedExtended() {
  var callbackCalled = false;

  class ExtnededAttributeChange extends HTMLButtonElement
  {
    attributeChangedCallback(name, oldValue, newValue) {
      is(callbackCalled, false, "Callback should only be called once in this test.");
      callbackCalled = true;
      runNextTest();
    }

    static get observedAttributes() {
      return ["foo"];
    }
  }

  customElements.define("x-extended-attribute-change", ExtnededAttributeChange,
                        { extends: "button" });

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

function testStyleAttributeChange() {
  var expectedCallbackArguments = [
    // [name, oldValue, newValue]
    ["style", null, "font-size: 10px;"],
    ["style", "font-size: 10px;", "font-size: 20px;"],
    ["style", "font-size: 20px;", "font-size: 30px;"],
  ];

  customElements.define("x-style-attribute-change", class extends HTMLElement {
    attributeChangedCallback(name, oldValue, newValue) {
      if (expectedCallbackArguments.length === 0) {
        ok(false, "Got unexpected attributeChangedCallback?");
        return;
      }

      let expectedArgument = expectedCallbackArguments.shift();
      is(name, expectedArgument[0],
         "The name argument should match the expected value.");
      is(oldValue, expectedArgument[1],
         "The old value argument should match the expected value.");
      is(newValue, expectedArgument[2],
         "The new value argument should match the expected value.");
    }

    static get observedAttributes() {
      return ["style"];
    }
  });

  var elem = document.createElement("x-style-attribute-change");
  elem.style.fontSize = "10px";
  elem.style.fontSize = "20px";
  elem.style.fontSize = "30px";

  ok(expectedCallbackArguments.length === 0,
     "The attributeChangedCallback should be fired synchronously.");
  runNextTest();
}

// 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() {
  class DestinedForFragment extends HTMLElement {
    connectedCallback() {
      ok(false, "Connected callback should not be called.");
    }

    disconnectedCallback() {
      ok(false, "Disconnected callback should not be called.");
    }
  };

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

  customElements.define("x-destined-for-fragment", DestinedForFragment);

  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 connectedCallbackCalled = false;
  var disconnectedCallbackCalled = false;

  class ElementInDiv extends HTMLElement {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should only be called on in this test.");
      connectedCallbackCalled = true;
    }

    disconnectedCallback() {
      is(connectedCallbackCalled, true, "Connected callback should be called before detached");
      is(disconnectedCallbackCalled, false, "Disconnected callback should only be called once in this test.");
      disconnectedCallbackCalled = true;
      runNextTest();
    }
  };

  var div = document.createElement("div");
  customElements.define("x-element-in-div", ElementInDiv);
  var customElement = document.createElement("x-element-in-div");
  div.appendChild(customElement);
  is(connectedCallbackCalled, false, "Appending a custom element to a node that is not in the document should not call the connected callback.");

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

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

function runNextTest() {
  if (testFunctions.length) {
    var nextTestFunction = testFunctions.shift();
    info(`Start ${nextTestFunction.name} ...`);
    nextTestFunction();
  }
}

SimpleTest.waitForExplicitFinish();

runNextTest();

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