summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/cssom-view/MediaQueryList-extends-EventTarget-interop.html
blob: 2ac4b46647b210e5e71b2120f6ce8a4f1d9190a4 (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
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
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="flags" content="dom">
<title>CSS Test: CSSOM View MediaQueryList extends EventTarget (interop)</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";

test(() => {
    const mql = window.matchMedia("all");

    let receivedEvent;
    mql.addListener(event => {
        receivedEvent = event;
    });

    const dispatchedEvent = new Event("change");
    mql.dispatchEvent(dispatchedEvent);

    assert_equals(receivedEvent, dispatchedEvent);
}, "dispatchEvent triggers listener added with addListener");

promise_test(async t => {
    const mql = await createMQL(t);

    let calls = 0;
    const listener = {
        handleEvent() {
            calls++;
        },
    };

    mql.addListener(listener);
    mql.addEventListener("change", listener);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_equals(calls, 1, "triggerMQLEvent");

    mql.dispatchEvent(new Event("change"));
    assert_equals(calls, 2, "dispatchEvent");
}, "listener added with addListener and addEventListener is called once");

promise_test(async t => {
    const mql = await createMQL(t);

    let calls = 0;
    const listener = () => {
        calls++;
    };

    mql.addListener(listener);
    mql.addEventListener("change", listener, true);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_equals(calls, 2, "triggerMQLEvent");

    mql.dispatchEvent(new Event("change"));
    assert_equals(calls, 4, "dispatchEvent");
}, "listener added with addListener and addEventListener (capture) is called twice");

promise_test(async t => {
    const mql = await createMQL(t);

    let calls = 0;
    const listener = {
        handleEvent() {
            calls++;
        },
    };

    mql.addListener(listener);
    mql.removeEventListener("change", listener);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_equals(calls, 0, "triggerMQLEvent");

    mql.dispatchEvent(new Event("change"));
    assert_equals(calls, 0, "dispatchEvent");
}, "removeEventListener removes listener added with addListener");

promise_test(async t => {
    const mql = await createMQL(t);

    let calls = 0;
    const listener = () => {
        calls++;
    };

    mql.addListener(listener);
    mql.removeEventListener("change", listener, true);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_equals(calls, 1, "triggerMQLEvent");

    mql.dispatchEvent(new Event("change"));
    assert_equals(calls, 2, "dispatchEvent");
}, "removeEventListener (capture) doesn't remove listener added with addListener");

promise_test(async t => {
    const mql = await createMQL(t);

    let calls = 0;
    const listener = {
        handleEvent() {
            calls++;
        },
    };

    mql.addEventListener("change", listener);
    mql.removeListener(listener);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_equals(calls, 0, "triggerMQLEvent");

    mql.dispatchEvent(new Event("change"));
    assert_equals(calls, 0, "dispatchEvent");
}, "removeListener removes listener added with addEventListener");

promise_test(async t => {
    const mql = await createMQL(t);

    let calls = 0;
    const listener = () => {
        calls++;
    };

    mql.addEventListener("change", listener, true);
    mql.removeListener(listener);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_equals(calls, 1, "triggerMQLEvent");

    mql.dispatchEvent(new Event("change"));
    assert_equals(calls, 2, "dispatchEvent");
}, "removeListener doesn't remove listener added with addEventListener (capture)");

// See:
//   * https://github.com/whatwg/dom/issues/746
//   * https://bugzilla.mozilla.org/show_bug.cgi?id=1492446
//   * https://bugs.chromium.org/p/chromium/issues/detail?id=949432
promise_test(async t => {
    const mql = await createMQL(t);

    let calls = [];
    mql.addListener(() => {
        calls.push("addListener");
    });
    mql.addEventListener("change", {
        handleEvent() {
            calls.push("addEventListener");
        },
    }, true);

    triggerMQLEvent(mql);
    await waitForChangesReported();
    assert_array_equals(calls, ["addEventListener", "addListener"], "triggerMQLEvent");

    calls = [];
    mql.dispatchEvent(new Event("change"));
    assert_array_equals(calls, ["addEventListener", "addListener"], "dispatchEvent");
}, "capturing event listener fires before non-capturing listener at target");
</script>