1<!DOCTYPE html> 2<meta charset="utf-8"> 3<title>Event constructors</title> 4<script src="/resources/testharness.js"></script> 5<script src="/resources/testharnessreport.js"></script> 6<div id="log"></div> 7<script> 8function assert_props(iface, event, defaults) { 9 assert_true(event instanceof self[iface]); 10 expected[iface].properties.forEach(function(p) { 11 var property = p[0], value = p[defaults ? 1 : 2]; 12 assert_true(property in event, 13 "Event " + format_value(event) + " should have a " + 14 property + " property"); 15 assert_equals(event[property], value, 16 "The value of the " + property + " property should be " + 17 format_value(value)); 18 }); 19 if ("parent" in expected[iface]) { 20 assert_props(expected[iface].parent, event, defaults); 21 } 22} 23 24// Class declarations don't go on the global by default, so put it there ourselves: 25 26self.SubclassedEvent = class SubclassedEvent extends Event { 27 constructor(name, props) { 28 super(name, props); 29 if (props && typeof(props) == "object" && "customProp" in props) { 30 this.customProp = props.customProp; 31 } else { 32 this.customProp = 5; 33 } 34 } 35 36 get fixedProp() { 37 return 17; 38 } 39} 40 41var EventModifierInit = [ 42 ["ctrlKey", false, true], 43 ["shiftKey", false, true], 44 ["altKey", false, true], 45 ["metaKey", false, true], 46]; 47var expected = { 48 "Event": { 49 "properties": [ 50 ["bubbles", false, true], 51 ["cancelable", false, true], 52 ["isTrusted", false, false], 53 ], 54 }, 55 56 "UIEvent": { 57 "parent": "Event", 58 "properties": [ 59 ["view", null, window], 60 ["detail", 0, 7], 61 ], 62 }, 63 64 "FocusEvent": { 65 "parent": "UIEvent", 66 "properties": [ 67 ["relatedTarget", null, document], 68 ], 69 }, 70 71 "MouseEvent": { 72 "parent": "UIEvent", 73 "properties": EventModifierInit.concat([ 74 ["screenX", 0, 40], 75 ["screenY", 0, 40], 76 ["clientX", 0, 40], 77 ["clientY", 0, 40], 78 ["button", 0, 40], 79 ["buttons", 0, 40], 80 ["relatedTarget", null, document], 81 ]), 82 }, 83 84 "WheelEvent": { 85 "parent": "MouseEvent", 86 "properties": [ 87 ["deltaX", 0.0, 3.1], 88 ["deltaY", 0.0, 3.1], 89 ["deltaZ", 0.0, 3.1], 90 ["deltaMode", 0, 40], 91 ], 92 }, 93 94 "KeyboardEvent": { 95 "parent": "UIEvent", 96 "properties": EventModifierInit.concat([ 97 ["key", "", "string"], 98 ["code", "", "string"], 99 ["location", 0, 7], 100 ["repeat", false, true], 101 ["isComposing", false, true], 102 ["charCode", 0, 7], 103 ["keyCode", 0, 7], 104 ["which", 0, 7], 105 ]), 106 }, 107 108 "CompositionEvent": { 109 "parent": "UIEvent", 110 "properties": [ 111 ["data", "", "string"], 112 ], 113 }, 114 115 "SubclassedEvent": { 116 "parent": "Event", 117 "properties": [ 118 ["customProp", 5, 8], 119 ["fixedProp", 17, 17], 120 ], 121 }, 122}; 123 124Object.keys(expected).forEach(function(iface) { 125 test(function() { 126 var event = new self[iface]("type"); 127 assert_props(iface, event, true); 128 }, iface + " constructor (no argument)"); 129 130 test(function() { 131 var event = new self[iface]("type", undefined); 132 assert_props(iface, event, true); 133 }, iface + " constructor (undefined argument)"); 134 135 test(function() { 136 var event = new self[iface]("type", null); 137 assert_props(iface, event, true); 138 }, iface + " constructor (null argument)"); 139 140 test(function() { 141 var event = new self[iface]("type", {}); 142 assert_props(iface, event, true); 143 }, iface + " constructor (empty argument)"); 144 145 test(function() { 146 var dictionary = {}; 147 expected[iface].properties.forEach(function(p) { 148 var property = p[0], value = p[1]; 149 dictionary[property] = value; 150 }); 151 var event = new self[iface]("type", dictionary); 152 assert_props(iface, event, true); 153 }, iface + " constructor (argument with default values)"); 154 155 test(function() { 156 function fill_in(iface, dictionary) { 157 if ("parent" in expected[iface]) { 158 fill_in(expected[iface].parent, dictionary) 159 } 160 expected[iface].properties.forEach(function(p) { 161 var property = p[0], value = p[2]; 162 dictionary[property] = value; 163 }); 164 } 165 166 var dictionary = {}; 167 fill_in(iface, dictionary); 168 169 var event = new self[iface]("type", dictionary); 170 assert_props(iface, event, false); 171 }, iface + " constructor (argument with non-default values)"); 172}); 173 174test(function () { 175 assert_throws_js(TypeError, function() { 176 new UIEvent("x", { view: 7 }) 177 }); 178}, "UIEvent constructor (view argument with wrong type)") 179</script> 180