summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/test_custom_element_stack.html
blob: 1b20103eb7863874ee68df1dbeddaa84a24756f2 (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
<!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">
</div>
<script>

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

function testChangeAttributeInEnteredViewCallback() {
  var attributeChangedCallbackCalled = false;
  var connectedCallbackCalled = false;

  class Two extends HTMLElement
  {
    connectedCallback() {
      is(connectedCallbackCalled, false, "Connected callback should be called only once in this test.");
      connectedCallbackCalled = true;
      is(attributeChangedCallbackCalled, false, "Attribute changed callback should not be called before changing attribute.");
      this.setAttribute("foo", "bar");
      is(attributeChangedCallbackCalled, true, "Transition from user-agent implementation to script should result in attribute changed callback being called.");
      runNextTest();
    }

    attributeChangedCallback() {
      is(connectedCallbackCalled, true, "Connected callback should have been called prior to attribute changed callback.");
      is(attributeChangedCallbackCalled, false, "Attribute changed callback should only be called once in this tests.");
      attributeChangedCallbackCalled = true;
    }

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

  customElements.define("x-two", Two);
  var elem = document.createElement("x-two");

  var container = document.getElementById("container");
  container.appendChild(elem);
}

function testLeaveViewInEnteredViewCallback() {
  var connectedCallbackCalled = false;
  var disconnectedCallbackCalled = false;
  var container = document.getElementById("container");

  class Three extends HTMLElement {
    connectedCallback() {
      is(this.parentNode, container, "Parent node should the container in which the node was appended.");
      is(connectedCallbackCalled, false, "Connected callback should be called only once in this test.");
      connectedCallbackCalled = true;
      is(disconnectedCallbackCalled, false, "Disconnected callback should not be called prior to removing element from document.");
      container.removeChild(this);
      is(disconnectedCallbackCalled, true, "Transition from user-agent implementation to script should run left view callback.");
      runNextTest();
    }

    disconnectedCallback() {
      is(disconnectedCallbackCalled, false, "The disconnected callback should only be called once in this test.");
      is(connectedCallbackCalled, true, "The connected callback should be called prior to disconnected callback.");
      disconnectedCallbackCalled = true;
    }
  };

  customElements.define("x-three", Three);
  var elem = document.createElement("x-three");

  container.appendChild(elem);
}

function testStackedAttributeChangedCallback() {
  var attributeChangedCallbackCount = 0;

  var attributeSequence = ["foo", "bar", "baz"];

  class Four extends HTMLElement
  {
    attributeChangedCallback(attrName, oldValue, newValue) {
      if (newValue == "baz") {
        return;
      }

      var nextAttribute = attributeSequence.shift();
      ok(true, nextAttribute);
      // Setting this attribute will call this function again, when
      // control returns to the script, the last attribute in the sequence should
      // be set on the element.
      this.setAttribute("foo", nextAttribute);
      is(this.getAttribute("foo"), "baz", "The last value in the sequence should be the value of the attribute.");

      attributeChangedCallbackCount++;
      if (attributeChangedCallbackCount == 3) {
        runNextTest();
      }
    }

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

  customElements.define("x-four", Four);
  var elem = document.createElement("x-four");
  elem.setAttribute("foo", "changeme");
}

var testFunctions = [
  testChangeAttributeInEnteredViewCallback,
  testLeaveViewInEnteredViewCallback,
  testStackedAttributeChangedCallback,
  SimpleTest.finish
];

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

SimpleTest.waitForExplicitFinish();

runNextTest();

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