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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// META: script=/resources/test-only-api.js
// META: script=/serial/resources/common.js
// META: script=resources/automation.js
serial_test(async (t, fake) => {
const {port, fakePort} = await getFakeSerialPort(fake);
const targets = [navigator.serial, port];
const expectedTargets = [navigator.serial];
const actualTargets = [];
function eventHandler(evt) {
actualTargets.push(evt.currentTarget);
if (evt.currentTarget == navigator.serial) {
evt.stopPropagation();
}
}
targets.forEach((target) => {
target.addEventListener('foo', eventHandler, {capture: true});
// stopPropagation() during capturing prevents bubbling.
target.addEventListener('foo', eventHandler);
t.add_cleanup(() => {
target.removeEventListener('foo', eventHandler, {capture: true});
target.removeEventListener('foo', eventHandler);
});
});
port.dispatchEvent(new CustomEvent('foo', {bubbles: true}));
assert_array_equals(actualTargets, expectedTargets, 'actualTargets');
}, 'stopPropagation() during capturing');
serial_test(async (t, fake) => {
const {port, fakePort} = await getFakeSerialPort(fake);
const targets = [navigator.serial, port];
const expectedTargets = [navigator.serial];
const actualTargets = [];
function eventHandler(evt) {
actualTargets.push(evt.currentTarget);
if (evt.currentTarget == navigator.serial) {
evt.cancelBubble = true;
}
}
targets.forEach((target) => {
target.addEventListener('foo', eventHandler, {capture: true});
// Setting cancelBubble during capturing prevents bubbling.
target.addEventListener('foo', eventHandler);
t.add_cleanup(() => {
target.removeEventListener('foo', eventHandler, {capture: true});
target.removeEventListener('foo', eventHandler);
});
});
port.dispatchEvent(new CustomEvent('foo', {bubbles: true}));
assert_array_equals(actualTargets, expectedTargets, 'actualTargets');
}, 'Set cancelBubble during capturing');
serial_test(async (t, fake) => {
const {port, fakePort} = await getFakeSerialPort(fake);
const targets = [navigator.serial, port];
const expectedTargets = [port];
const actualTargets = [];
function eventHandler(evt) {
actualTargets.push(evt.currentTarget);
if (evt.currentTarget == port) {
evt.stopPropagation();
}
}
targets.forEach((target) => {
target.addEventListener('foo', eventHandler);
t.add_cleanup(() => {
target.removeEventListener('foo', eventHandler);
});
});
port.dispatchEvent(new CustomEvent('foo', {bubbles: true}));
assert_array_equals(actualTargets, expectedTargets, 'actualTargets');
}, 'stopPropagation() during bubbling');
serial_test(async (t, fake) => {
const {port, fakePort} = await getFakeSerialPort(fake);
const targets = [navigator.serial, port];
const expectedTargets = [port];
const actualTargets = [];
function eventHandler(evt) {
actualTargets.push(evt.currentTarget);
if (evt.currentTarget == port) {
evt.cancelBubble = true;
}
}
targets.forEach((target) => {
target.addEventListener('foo', eventHandler);
t.add_cleanup(() => {
target.removeEventListener('foo', eventHandler);
});
});
port.dispatchEvent(new CustomEvent('foo', {bubbles: true}));
assert_array_equals(actualTargets, expectedTargets, 'actualTargets');
}, 'Set cancelBubble during bubbling');
serial_test(async (t, fake) => {
const {port, fakePort} = await getFakeSerialPort(fake);
const targets = [navigator.serial, port];
const expectedTargets = [
navigator.serial,
port,
navigator.serial,
port,
];
const expectedTypes = [
'foo',
'bar',
'bar',
'foo',
];
const actualTargets = [];
const actualTypes = [];
function eventHandler(evt) {
actualTargets.push(evt.currentTarget);
actualTypes.push(evt.type);
if (evt.currentTarget == navigator.serial && evt.type == 'foo') {
port.dispatchEvent(new CustomEvent('bar', {bubbles: true}));
}
}
targets.forEach((target) => {
target.addEventListener('foo', eventHandler, {capture: true});
target.addEventListener('bar', eventHandler);
t.add_cleanup(() => {
target.removeEventListener('foo', eventHandler, {capture: true});
target.removeEventListener('bar', eventHandler);
});
});
port.dispatchEvent(new CustomEvent('foo', {bubbles: true}));
assert_array_equals(actualTargets, expectedTargets, 'actualTargets');
assert_array_equals(actualTypes, expectedTypes, 'actualTypes');
}, 'An event dispatched in an event handler is propagated before continuing');
serial_test(async (t, fake) => {
const {port, fakePort} = await getFakeSerialPort(fake);
const targets = [navigator.serial, port];
const expected = [
'capturing Serial',
'capturing SerialPort',
'bubbling SerialPort',
'bubbling Serial',
];
const actual = [];
targets.forEach((target) => {
const bubblingEventHandler = () => {
actual.push(`bubbling ${target.constructor.name}`);
};
target.addEventListener('foo', bubblingEventHandler);
const capturingEventHandler = () => {
actual.push(`capturing ${target.constructor.name}`);
};
target.addEventListener('foo', capturingEventHandler, {capture: true});
t.add_cleanup(() => {
target.removeEventListener('foo', bubblingEventHandler, {capture: true});
target.removeEventListener('foo', capturingEventHandler);
});
});
port.dispatchEvent(new CustomEvent('foo', {bubbles: true}));
assert_array_equals(actual, expected);
}, 'Capturing and bubbling events delivered to listeners in the expected order');
|