summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/webcomponents/upgrade_tests.js
blob: 85cff984f0fa844df9fb33e7e3bc1e79f543e547 (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
function test_upgrade(f, msg) {
  // Run upgrading test on an element created by HTML parser.
  test_with_new_window(function (testWindow, testMsg) {
    let elementParser = testWindow.document.querySelector("unresolved-element");
    f(testWindow, elementParser, testMsg);
  }, msg + " (HTML parser)");

  // Run upgrading test on an element created by document.createElement.
  test_with_new_window(function (testWindow, testMsg) {
    let element = testWindow.document.createElement("unresolved-element");
    testWindow.document.documentElement.appendChild(element);
    f(testWindow, element, testMsg);
  }, msg + " (document.createElement)");
}

// Test cases

test_upgrade(function (testWindow, testElement, msg) {
  class MyCustomElement extends testWindow.HTMLElement {}
  testWindow.customElements.define("unresolved-element", MyCustomElement);
  SimpleTest.is(
    Object.getPrototypeOf(Cu.waiveXrays(testElement)),
    MyCustomElement.prototype,
    msg
  );
}, "Custom element must be upgraded if there is a matching definition");

test_upgrade(function (testWindow, testElement, msg) {
  testElement.remove();
  class MyCustomElement extends testWindow.HTMLElement {}
  testWindow.customElements.define("unresolved-element", MyCustomElement);
  SimpleTest.is(
    Object.getPrototypeOf(testElement),
    testWindow.HTMLElement.prototype,
    msg
  );
}, "Custom element must not be upgraded if it has been removed from tree");

test_upgrade(function (testWindow, testElement, msg) {
  let exceptionToThrow = { name: "exception thrown by a custom constructor" };
  class ThrowCustomElement extends testWindow.HTMLElement {
    constructor() {
      super();
      if (exceptionToThrow) {
        throw exceptionToThrow;
      }
    }
  }

  let uncaughtError;
  window.onerror = function (message, url, lineNumber, columnNumber, error) {
    uncaughtError = error;
    return true;
  };
  testWindow.customElements.define("unresolved-element", ThrowCustomElement);

  SimpleTest.is(uncaughtError.name, exceptionToThrow.name, msg);
}, "Upgrading must report an exception thrown by a custom element constructor");

test_upgrade(function (testWindow, testElement, msg) {
  class InstantiatesItselfAfterSuper extends testWindow.HTMLElement {
    constructor(doNotCreateItself) {
      super();
      if (!doNotCreateItself) {
        new InstantiatesItselfAfterSuper(true);
      }
    }
  }

  let uncaughtError;
  window.onerror = function (message, url, lineNumber, columnNumber, error) {
    uncaughtError = error;
    return true;
  };
  testWindow.customElements.define(
    "unresolved-element",
    InstantiatesItselfAfterSuper
  );

  SimpleTest.is(uncaughtError.name, "TypeError", msg);
}, "Upgrading must report an TypeError when the top of the " +
  "construction stack is marked AlreadyConstructed");

test_upgrade(function (testWindow, testElement, msg) {
  class InstantiatesItselfBeforeSuper extends testWindow.HTMLElement {
    constructor(doNotCreateItself) {
      if (!doNotCreateItself) {
        new InstantiatesItselfBeforeSuper(true);
      }
      super();
    }
  }

  let uncaughtError;
  window.onerror = function (message, url, lineNumber, columnNumber, error) {
    uncaughtError = error;
    return true;
  };
  testWindow.customElements.define(
    "unresolved-element",
    InstantiatesItselfBeforeSuper
  );

  SimpleTest.is(uncaughtError.name, "TypeError", msg);
}, "Upgrading must report an TypeError when the top of the " +
  "construction stack is marked AlreadyConstructed due to a custom element " +
  "constructor constructing itself before super() call");

test_upgrade(function (testWindow, testElement, msg) {
  class MyOtherElement extends testWindow.HTMLElement {
    constructor() {
      super();
      if (this == testElement) {
        return testWindow.document.createElement("other-element");
      }
    }
  }

  let uncaughtError;
  window.onerror = function (message, url, lineNumber, columnNumber, error) {
    uncaughtError = error;
    return true;
  };
  testWindow.customElements.define("unresolved-element", MyOtherElement);

  SimpleTest.is(uncaughtError.name, "TypeError", msg);
}, "Upgrading must report an TypeError when the returned element is " +
  "not SameValue as the upgraded element");