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
|
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="flags" content="dom">
<title>CSS Test: CSSOM View MediaQueryList extends EventTarget</title>
<link rel="help" href="https://www.w3.org/TR/cssom-view-1/#the-mediaquerylist-interface">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/matchMedia.js"></script>
<div id="log"></div>
<script>
"use strict";
promise_test(async t => {
const mql = await createMQL(t);
let _event;
mql.onchange = event => {
_event = event;
};
triggerMQLEvent(mql);
await waitForChangesReported();
assert_equals(_event.media, mql.media);
assert_equals(_event.matches, mql.matches);
}, "onchange adds listener");
promise_test(async t => {
const mql = await createMQL(t);
let calls = 0;
mql.onchange = () => {
calls++;
};
triggerMQLEvent(mql);
await waitForChangesReported();
assert_equals(calls, 1);
mql.onchange = null;
triggerMQLEvent(mql);
await waitForChangesReported();
assert_equals(calls, 1);
}, "onchange removes listener");
promise_test(async t => {
const mql = await createMQL(t);
let calls = 0;
mql.addEventListener("change", {
handleEvent() {
calls++;
},
});
triggerMQLEvent(mql);
await waitForChangesReported();
assert_equals(calls, 1);
}, 'listeners for "change" type are called');
promise_test(async t => {
const mql = await createMQL(t);
mql.addEventListener("matches", t.unreached_func("should not be called"));
triggerMQLEvent(mql);
await waitForChangesReported();
}, 'listeners with different type are not called');
promise_test(async t => {
const mql = await createMQL(t);
let calls = 0;
mql.addEventListener("change", {
handleEvent() {
calls++;
},
}, {once: true});
triggerMQLEvent(mql);
await waitForChangesReported();
assert_equals(calls, 1);
triggerMQLEvent(mql);
await waitForChangesReported();
assert_equals(calls, 1);
}, 'addEventListener "once" option is respected');
promise_test(async t => {
const mql = await createMQL(t);
const listener = t.unreached_func("should not be called");
mql.addEventListener("change", listener);
mql.removeEventListener("change", listener);
triggerMQLEvent(mql);
await waitForChangesReported();
}, "removeEventListener removes listener");
test(() => {
const mql = window.matchMedia("all");
let receivedEvent;
mql.addEventListener("custom", event => {
receivedEvent = event;
event.preventDefault();
}, true);
const dispatchedEvent = new CustomEvent("custom", {
cancelable: true,
detail: {},
});
const defaultAction = mql.dispatchEvent(dispatchedEvent);
assert_equals(receivedEvent, dispatchedEvent);
assert_false(defaultAction);
}, "dispatchEvent works as expected");
</script>
|