summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/WebIDL/ecmascript-binding/es-exceptions/exceptions.html
blob: bc1d7fe6395e70aac39ccc636f839109d581eae4 (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
<!doctype html>
<title>DOMException-throwing tests</title>
<link rel=author title="Aryeh Gregor" href=ayg@aryeh.name>
<div id=log></div>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>
/**
 * This file just picks one case where browsers are supposed to throw an
 * exception, and tests the heck out of whether it meets the spec.  In the
 * future, all these checks should be in assert_throws(), but we don't want
 * every browser failing every assert_throws() check until they fix every
 * single bug in their exception-throwing.
 *
 * We don't go out of our way to test everything that's already tested by
 * interfaces.html, like whether all constants are present on the object, but
 * some things are duplicated.
 */
setup({explicit_done: true});

function testException(exception, global, desc) {
  // https://heycam.github.io/webidl/#es-exception-objects
  // (as of 2015-01-03): "The value of the internal [[Prototype]] property of a
  // DOMException object MUST be the DOMException prototype object from the
  // global environment the exception object is associated with."
  test(function() {
    assert_equals(global.Object.getPrototypeOf(exception),
                  global.DOMException.prototype);
  }, desc + "Object.getPrototypeOf(exception) === DOMException.prototype");


  // https://heycam.github.io/webidl/#es-creating-throwing-exceptions
  // (as of 2015-01-03): "Call the [[DefineOwnProperty]] internal method of /O/
  // passing “name”, Property Descriptor { [[Value]]: /N/, [[Writable]]: true,
  // [[Enumerable]]: true, [[Configurable]]: true }, and false as arguments."
  test(function() {
    assert_true(exception.hasOwnProperty("name"));
  }, desc + "exception.hasOwnProperty(\"name\")");

  test(function() {
    assert_equals(exception.name, "HierarchyRequestError");
  }, desc + "exception.name === \"HierarchyRequestError\"");

  test(function() {
    var desc = global.Object.getOwnPropertyDescriptor(exception, "name");
    assert_true(desc.writable, "must be writable");
    assert_true(desc.enumerable, "must be enumerable");
    assert_true(desc.configurable, "must be configurable");
  }, desc + "Object.getOwnPropertyDescriptor(exception, \"name\")");


  // https://heycam.github.io/webidl/#es-creating-throwing-exceptions
  // (as of 2015-01-03): "If the optional user agent-defined message /M/ was
  // specified, then this list has a single element whose value is the result
  // of converting /M/ to a String value. Otherwise, the list is empty."
  //
  // https://heycam.github.io/webidl/#es-DOMException-constructor-object
  // (as of 2015-01-03): "Call the [[DefineOwnProperty]] internal method of /O/
  // passing “message”, Property Descriptor { [[Value]]: /S/,
  // [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }, and
  // false as arguments."
  test(function() {
    if (exception.hasOwnProperty("message")) {
      var desc = global.Object.getOwnPropertyDescriptor(exception, "message");
      assert_true(desc.writable, "must be writable");
      assert_false(desc.enumerable, "must not be enumerable");
      assert_true(desc.configurable, "must be configurable");
    }
  }, desc + "Object.getOwnPropertyDescriptor(exception, \"message\")");

  test(function() {
    if (exception.hasOwnProperty("message")) {
      // Can't test anything more specific, since it's implementation-defined :(
      assert_equals(typeof exception.message, "string");
    } else {
      // Error.prototype.message
      assert_equals(exception.message, "");
    }
  }, desc + "typeof exception.message === \"string\"");


  // https://heycam.github.io/webidl/#es-exception-objects
  // (as of 2015-01-03): "The class string of a DOMException object MUST be
  // “DOMException”."
  test(function() {
    assert_equals(global.Object.prototype.toString.call(exception),
                  "[object DOMException]");
  }, desc + "Object.prototype.toString.call(exception) === \"[object DOMException]\"");


  // https://heycam.github.io/webidl/#es-creating-throwing-exceptions
  // (as of 2015-01-03): "Call the [[DefineOwnProperty]] internal method of /O/
  // passing “code”, Property Descriptor { [[Value]]: /code/,
  // [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true }, and
  // false as arguments."
  test(function() {
    assert_equals(exception.code, global.DOMException.HIERARCHY_REQUEST_ERR);
  }, desc + "exception.code === DOMException.HIERARCHY_REQUEST_ERR");

  test(function() {
    var desc = global.Object.getOwnPropertyDescriptor(exception, "name");
    assert_true(desc.writable, "must be writable");
    assert_true(desc.enumerable, "must be enumerable");
    assert_true(desc.configurable, "must be configurable");
  }, desc + "Object.getOwnPropertyDescriptor(exception, \"code\")");
}


// Test in current window
var exception = null;
try {
  // This should throw a HierarchyRequestError in every browser since the
  // Stone Age, so we're really only testing exception-throwing details.
  document.documentElement.appendChild(document);
} catch(e) {
  exception = e;
}
testException(exception, window, "");

// Test in iframe
var iframe = document.createElement("iframe");
iframe.src = "about:blank";
iframe.onload = function() {
  var exception = null;
  try {
    iframe.contentDocument.documentElement.appendChild(iframe.contentDocument);
  } catch(e) {
    exception = e;
  }
  testException(exception, iframe.contentWindow, "In iframe: ");

  document.body.removeChild(iframe);
  done();
};
document.body.appendChild(iframe);
</script>