OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
| 20 const assert = require("assert"); |
20 const {createSandbox} = require("./_common"); | 21 const {createSandbox} = require("./_common"); |
21 | 22 |
22 let filterNotifier = null; | 23 let filterNotifier = null; |
23 | 24 |
24 exports.setUp = function(callback) | 25 describe("Filter Notifier", () => |
25 { | 26 { |
26 let sandboxedRequire = createSandbox(); | 27 beforeEach(() => |
27 ( | 28 { |
28 {filterNotifier} = sandboxedRequire("../lib/filterNotifier") | 29 let sandboxedRequire = createSandbox(); |
29 ); | 30 ( |
| 31 {filterNotifier} = sandboxedRequire("../lib/filterNotifier") |
| 32 ); |
| 33 }); |
30 | 34 |
31 callback(); | 35 let triggeredListeners = []; |
32 }; | 36 let listeners = [ |
| 37 (...args) => triggeredListeners.push(["listener1", ...args]), |
| 38 (...args) => triggeredListeners.push(["listener2", ...args]), |
| 39 (...args) => triggeredListeners.push(["listener3", ...args]) |
| 40 ]; |
33 | 41 |
34 let triggeredListeners = []; | 42 function addListener(listener) |
35 let listeners = [ | 43 { |
36 (...args) => triggeredListeners.push(["listener1", ...args]), | 44 filterNotifier.on("foo", listener); |
37 (...args) => triggeredListeners.push(["listener2", ...args]), | 45 } |
38 (...args) => triggeredListeners.push(["listener3", ...args]) | |
39 ]; | |
40 | 46 |
41 function addListener(listener) | 47 function removeListener(listener) |
42 { | 48 { |
43 filterNotifier.on("foo", listener); | 49 filterNotifier.off("foo", listener); |
44 } | 50 } |
45 | 51 |
46 function removeListener(listener) | 52 function compareListeners(testDescription, list) |
47 { | 53 { |
48 filterNotifier.off("foo", listener); | 54 assert.equal(filterNotifier.hasListeners(), list.length > 0, testDescription
); |
49 } | 55 assert.equal(filterNotifier.hasListeners("foo"), list.length > 0, |
| 56 testDescription); |
50 | 57 |
51 function compareListeners(test, testDescription, list) | 58 assert.equal(filterNotifier.hasListeners("bar"), false, testDescription); |
52 { | |
53 test.equal(filterNotifier.hasListeners(), list.length > 0, testDescription); | |
54 test.equal(filterNotifier.hasListeners("foo"), list.length > 0, | |
55 testDescription); | |
56 | 59 |
57 test.equal(filterNotifier.hasListeners("bar"), false, testDescription); | 60 let result1 = triggeredListeners = []; |
| 61 filterNotifier.emit("foo", {bar: true}); |
58 | 62 |
59 let result1 = triggeredListeners = []; | 63 let result2 = triggeredListeners = []; |
60 filterNotifier.emit("foo", {bar: true}); | 64 for (let observer of list) |
| 65 observer({bar: true}); |
61 | 66 |
62 let result2 = triggeredListeners = []; | 67 assert.deepEqual(result1, result2, testDescription); |
63 for (let observer of list) | 68 } |
64 observer({bar: true}); | |
65 | 69 |
66 test.deepEqual(result1, result2, testDescription); | 70 it("Add/Remove Listeners", () => |
67 } | 71 { |
| 72 let [listener1, listener2, listener3] = listeners; |
68 | 73 |
69 exports.testAddingRemovingListeners = function(test) | 74 compareListeners("No listeners", []); |
70 { | |
71 let [listener1, listener2, listener3] = listeners; | |
72 | 75 |
73 compareListeners(test, "No listeners", []); | 76 addListener(listener1); |
| 77 compareListeners("addListener(listener1)", [listener1]); |
74 | 78 |
75 addListener(listener1); | 79 addListener(listener1); |
76 compareListeners(test, "addListener(listener1)", [listener1]); | 80 compareListeners("addListener(listener1) again", [listener1, listener1]); |
77 | 81 |
78 addListener(listener1); | 82 addListener(listener2); |
79 compareListeners(test, "addListener(listener1) again", [listener1, listener1])
; | 83 compareListeners("addListener(listener2)", [listener1, listener1, listener2]
); |
80 | 84 |
81 addListener(listener2); | 85 removeListener(listener1); |
82 compareListeners(test, "addListener(listener2)", [listener1, listener1, listen
er2]); | 86 compareListeners("removeListener(listener1)", [listener1, listener2]); |
83 | 87 |
84 removeListener(listener1); | 88 removeListener(listener1); |
85 compareListeners(test, "removeListener(listener1)", [listener1, listener2]); | 89 compareListeners("removeListener(listener1) again", [listener2]); |
86 | 90 |
87 removeListener(listener1); | 91 addListener(listener3); |
88 compareListeners(test, "removeListener(listener1) again", [listener2]); | 92 compareListeners("addListener(listener3)", [listener2, listener3]); |
89 | 93 |
90 addListener(listener3); | 94 addListener(listener1); |
91 compareListeners(test, "addListener(listener3)", [listener2, listener3]); | 95 compareListeners("addListener(listener1)", [listener2, listener3, listener1]
); |
92 | 96 |
93 addListener(listener1); | 97 removeListener(listener3); |
94 compareListeners(test, "addListener(listener1)", [listener2, listener3, listen
er1]); | 98 compareListeners("removeListener(listener3)", [listener2, listener1]); |
95 | 99 |
96 removeListener(listener3); | 100 removeListener(listener1); |
97 compareListeners(test, "removeListener(listener3)", [listener2, listener1]); | 101 compareListeners("removeListener(listener1)", [listener2]); |
98 | 102 |
99 removeListener(listener1); | 103 removeListener(listener2); |
100 compareListeners(test, "removeListener(listener1)", [listener2]); | 104 compareListeners("removeListener(listener2)", []); |
| 105 }); |
101 | 106 |
102 removeListener(listener2); | 107 it("Removing listeners while being called", () => |
103 compareListeners(test, "removeListener(listener2)", []); | 108 { |
| 109 let listener1 = function(...args) |
| 110 { |
| 111 listeners[0](...args); |
| 112 removeListener(listener1); |
| 113 }; |
| 114 let listener2 = listeners[1]; |
| 115 addListener(listener1); |
| 116 addListener(listener2); |
104 | 117 |
105 test.done(); | 118 compareListeners("Initial call", [listener1, listener2]); |
106 }; | 119 compareListeners("Subsequent calls", [listener2]); |
107 | 120 }); |
108 exports.testRemovingListenersWhileBeingCalled = function(test) | 121 }); |
109 { | |
110 let listener1 = function(...args) | |
111 { | |
112 listeners[0](...args); | |
113 removeListener(listener1); | |
114 }; | |
115 let listener2 = listeners[1]; | |
116 addListener(listener1); | |
117 addListener(listener2); | |
118 | |
119 compareListeners(test, "Initial call", [listener1, listener2]); | |
120 compareListeners(test, "Subsequent calls", [listener2]); | |
121 | |
122 test.done(); | |
123 }; | |
OLD | NEW |