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 |
(...skipping 15 matching lines...) Expand all Loading... |
26 let sandboxedRequire = createSandbox(); | 26 let sandboxedRequire = createSandbox(); |
27 ( | 27 ( |
28 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier") | 28 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier") |
29 ); | 29 ); |
30 | 30 |
31 callback(); | 31 callback(); |
32 }; | 32 }; |
33 | 33 |
34 let triggeredListeners = []; | 34 let triggeredListeners = []; |
35 let listeners = [ | 35 let listeners = [ |
36 (action, item) => triggeredListeners.push(["listener1", action, item]), | 36 (...args) => triggeredListeners.push(["listener1", ...args]), |
37 (action, item) => triggeredListeners.push(["listener2", action, item]), | 37 (...args) => triggeredListeners.push(["listener2", ...args]), |
38 (action, item) => triggeredListeners.push(["listener3", action, item]) | 38 (...args) => triggeredListeners.push(["listener3", ...args]) |
39 ]; | 39 ]; |
40 | 40 |
| 41 function addListener(listener) |
| 42 { |
| 43 FilterNotifier.on("foo", listener); |
| 44 } |
| 45 |
| 46 function removeListener(listener) |
| 47 { |
| 48 FilterNotifier.off("foo", listener); |
| 49 } |
| 50 |
41 function compareListeners(test, testDescription, list) | 51 function compareListeners(test, testDescription, list) |
42 { | 52 { |
43 let result1 = triggeredListeners = []; | 53 let result1 = triggeredListeners = []; |
44 FilterNotifier.triggerListeners("foo", {bar: true}); | 54 FilterNotifier.emit("foo", {bar: true}); |
45 | 55 |
46 let result2 = triggeredListeners = []; | 56 let result2 = triggeredListeners = []; |
47 for (let observer of list) | 57 for (let observer of list) |
48 observer("foo", {bar: true}); | 58 observer({bar: true}); |
49 | 59 |
50 test.deepEqual(result1, result2, testDescription); | 60 test.deepEqual(result1, result2, testDescription); |
51 } | 61 } |
52 | 62 |
53 exports.testAddingRemovingListeners = function(test) | 63 exports.testAddingRemovingListeners = function(test) |
54 { | 64 { |
55 let [listener1, listener2, listener3] = listeners; | 65 let [listener1, listener2, listener3] = listeners; |
56 | 66 |
57 compareListeners(test, "No listeners", []); | 67 compareListeners(test, "No listeners", []); |
58 | 68 |
59 FilterNotifier.addListener(listener1); | 69 addListener(listener1); |
60 compareListeners(test, "addListener(listener1)", [listener1]); | 70 compareListeners(test, "addListener(listener1)", [listener1]); |
61 | 71 |
62 FilterNotifier.addListener(listener1); | 72 addListener(listener1); |
63 compareListeners(test, "addListener(listener1) again", [listener1]); | 73 compareListeners(test, "addListener(listener1) again", [listener1, listener1])
; |
64 | 74 |
65 FilterNotifier.addListener(listener2); | 75 addListener(listener2); |
66 compareListeners(test, "addListener(listener2)", [listener1, listener2]); | 76 compareListeners(test, "addListener(listener2)", [listener1, listener1, listen
er2]); |
67 | 77 |
68 FilterNotifier.removeListener(listener1); | 78 removeListener(listener1); |
| 79 compareListeners(test, "removeListener(listener1)", [listener1, listener2]); |
| 80 |
| 81 removeListener(listener1); |
| 82 compareListeners(test, "removeListener(listener1) again", [listener2]); |
| 83 |
| 84 addListener(listener3); |
| 85 compareListeners(test, "addListener(listener3)", [listener2, listener3]); |
| 86 |
| 87 addListener(listener1); |
| 88 compareListeners(test, "addListener(listener1)", [listener2, listener3, listen
er1]); |
| 89 |
| 90 removeListener(listener3); |
| 91 compareListeners(test, "removeListener(listener3)", [listener2, listener1]); |
| 92 |
| 93 removeListener(listener1); |
69 compareListeners(test, "removeListener(listener1)", [listener2]); | 94 compareListeners(test, "removeListener(listener1)", [listener2]); |
70 | 95 |
71 FilterNotifier.removeListener(listener1); | 96 removeListener(listener2); |
72 compareListeners(test, "removeListener(listener1) again", [listener2]); | |
73 | |
74 FilterNotifier.addListener(listener3); | |
75 compareListeners(test, "addListener(listener3)", [listener2, listener3]); | |
76 | |
77 FilterNotifier.addListener(listener1); | |
78 compareListeners(test, "addListener(listener1)", [listener2, listener3, listen
er1]); | |
79 | |
80 FilterNotifier.removeListener(listener3); | |
81 compareListeners(test, "removeListener(listener3)", [listener2, listener1]); | |
82 | |
83 FilterNotifier.removeListener(listener1); | |
84 compareListeners(test, "removeListener(listener1)", [listener2]); | |
85 | |
86 FilterNotifier.removeListener(listener2); | |
87 compareListeners(test, "removeListener(listener2)", []); | 97 compareListeners(test, "removeListener(listener2)", []); |
88 | 98 |
89 test.done(); | 99 test.done(); |
90 }; | 100 }; |
91 | 101 |
92 exports.testRemovingListenersWhileBeingCalled = function(test) | 102 exports.testRemovingListenersWhileBeingCalled = function(test) |
93 { | 103 { |
94 let listener1 = function(...args) | 104 let listener1 = function(...args) |
95 { | 105 { |
96 listeners[0](...args); | 106 listeners[0](...args); |
97 FilterNotifier.removeListener(listener1); | 107 removeListener(listener1); |
98 }; | 108 }; |
99 let listener2 = listeners[1]; | 109 let listener2 = listeners[1]; |
100 FilterNotifier.addListener(listener1); | 110 addListener(listener1); |
101 FilterNotifier.addListener(listener2); | 111 addListener(listener2); |
102 | 112 |
103 compareListeners(test, "Initial call", [listener1, listener2]); | 113 compareListeners(test, "Initial call", [listener1, listener2]); |
104 compareListeners(test, "Subsequent calls", [listener2]); | 114 compareListeners(test, "Subsequent calls", [listener2]); |
105 | 115 |
106 test.done(); | 116 test.done(); |
107 }; | 117 }; |
OLD | NEW |