summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_custom_element_in_shadow.html
blob: 4371d296ce75473687b158049b3e80da13d2a73f (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1087460
-->
<head>
  <title>Test for custom element callbacks in shadow DOM.</title>
  <script type="text/javascript" src="head.js"></script>
  <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=1087460">Bug 1087460</a>

<script>

SimpleTest.waitForExplicitFinish();

var content = '<div id="container"></div>';
createIframe(content)
  .then((aDocument) => {

    // Test callback for custom element when used after registration.

    var iframeWin = aDocument.defaultView;
    var connectedCallbackCount = 0;
    var disconnectedCallbackCount = 0;
    var attributeChangedCallbackCount = 0;

    class Foo extends iframeWin.HTMLElement
    {
      connectedCallback() {
        connectedCallbackCount++;
      }

      disconnectedCallback() {
        disconnectedCallbackCount++;
      }

      attributeChangedCallback(aName, aOldValue, aNewValue) {
        attributeChangedCallbackCount++;
      }

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

    iframeWin.customElements.define("x-foo", Foo);

    var container = aDocument.getElementById("container");
    var shadow = container.attachShadow({mode: "open"});
    var customElem = aDocument.createElement("x-foo");

    is(attributeChangedCallbackCount, 0, "attributeChangedCallback should not be called after just creating an element.");
    customElem.setAttribute("data-foo", "bar");
    is(attributeChangedCallbackCount, 1, "attributeChangedCallback should be called after setting an attribute.");

    is(connectedCallbackCount, 0, "connectedCallback should not be called on an element that is not in a document/composed document.");
    shadow.appendChild(customElem);
    is(connectedCallbackCount, 1, "connectedCallback should be called after attaching custom element to the composed document.");

    is(disconnectedCallbackCount, 0, "disconnectedCallback should not be called without detaching custom element.");
    shadow.removeChild(customElem);
    is(disconnectedCallbackCount, 1, "disconnectedCallback should be called after detaching custom element from the composed document.");

    // Test callback for custom element already in the composed doc when created.

    connectedCallbackCount = 0;
    disconnectedCallbackCount = 0;
    attributeChangedCallbackCount = 0;

    shadow.innerHTML = "<x-foo></x-foo>";
    is(connectedCallbackCount, 1, "connectedCallback should be called after creating an element in the composed document.");

    shadow.innerHTML = "";
    is(disconnectedCallbackCount, 1, "disconnectedCallback should be called after detaching custom element from the composed document.");

    // Test callback for custom element in shadow DOM when host attached/detached to/from document.

    connectedCallbackCount = 0;
    disconnectedCallbackCount = 0;
    attributeChangedCallbackCount = 0;

    var host = aDocument.createElement("div");
    shadow = host.attachShadow({mode: "open"});
    customElem = aDocument.createElement("x-foo");

    is(connectedCallbackCount, 0, "connectedCallback should not be called on newly created element.");
    shadow.appendChild(customElem);
    is(connectedCallbackCount, 0, "connectedCallback should not be called on attaching to a tree that is not in the composed document.");

    is(disconnectedCallbackCount, 0, "disconnectedCallback should not be called.");
    shadow.removeChild(customElem);
    is(disconnectedCallbackCount, 0, "disconnectedCallback should not be called when detaching from a tree that is not in the composed document.");

    shadow.appendChild(customElem);
    is(connectedCallbackCount, 0, "connectedCallback should still not be called after reattaching to a shadow tree that is not in the composed document.");

    container.appendChild(host);
    is(connectedCallbackCount, 1, "connectedCallback should be called after host is inserted into document.");

    container.removeChild(host);
    is(disconnectedCallbackCount, 1, "disconnectedCallback should be called after host is removed from document.");

    // Test callback for custom element for upgraded element.

    connectedCallbackCount = 0;
    disconnectedCallbackCount = 0;
    attributeChangedCallbackCount = 0;

    shadow = container.shadowRoot;
    shadow.innerHTML = "<x-bar></x-bar>";

    class Bar extends iframeWin.HTMLElement {
      connectedCallback() {
        connectedCallbackCount++;
      }
    };

    iframeWin.customElements.define("x-bar", Bar);
    is(connectedCallbackCount, 1, "connectedCallback should be called after upgrading element in composed document.");

    SimpleTest.finish();
  });
</script>

</body>
</html>