OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 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 |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 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/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 let {createSandbox} = require("common"); |
| 21 let sandboxedRequire = createSandbox(); |
| 22 |
| 23 let {FilterNotifier} = sandboxedRequire("filterNotifier"); |
| 24 |
| 25 let triggeredListeners = []; |
| 26 let listeners = [ |
| 27 (action, item) => triggeredListeners.push(["listener1", action, item]), |
| 28 (action, item) => triggeredListeners.push(["listener2", action, item]), |
| 29 (action, item) => triggeredListeners.push(["listener3", action, item]) |
| 30 ]; |
| 31 |
| 32 function compareListeners(test, testDescription, list) |
| 33 { |
| 34 let result1 = triggeredListeners = []; |
| 35 FilterNotifier.triggerListeners("foo", {bar: true}); |
| 36 |
| 37 let result2 = triggeredListeners = []; |
| 38 for (let observer of list) |
| 39 observer("foo", {bar: true}); |
| 40 |
| 41 test.deepEqual(result1, result2, testDescription); |
| 42 } |
| 43 |
| 44 exports.testAddingRemovingListeners = function(test) |
| 45 { |
| 46 let [listener1, listener2, listener3] = listeners; |
| 47 |
| 48 compareListeners(test, "No listeners", []); |
| 49 |
| 50 FilterNotifier.addListener(listener1); |
| 51 compareListeners(test, "addListener(listener1)", [listener1]); |
| 52 |
| 53 FilterNotifier.addListener(listener1); |
| 54 compareListeners(test, "addListener(listener1) again", [listener1]); |
| 55 |
| 56 FilterNotifier.addListener(listener2); |
| 57 compareListeners(test, "addListener(listener2)", [listener1, listener2]); |
| 58 |
| 59 FilterNotifier.removeListener(listener1); |
| 60 compareListeners(test, "removeListener(listener1)", [listener2]); |
| 61 |
| 62 FilterNotifier.removeListener(listener1); |
| 63 compareListeners(test, "removeListener(listener1) again", [listener2]); |
| 64 |
| 65 FilterNotifier.addListener(listener3); |
| 66 compareListeners(test, "addListener(listener3)", [listener2, listener3]); |
| 67 |
| 68 FilterNotifier.addListener(listener1); |
| 69 compareListeners(test, "addListener(listener1)", [listener2, listener3, listen
er1]); |
| 70 |
| 71 FilterNotifier.removeListener(listener3); |
| 72 compareListeners(test, "removeListener(listener3)", [listener2, listener1]); |
| 73 |
| 74 FilterNotifier.removeListener(listener1); |
| 75 compareListeners(test, "removeListener(listener1)", [listener2]); |
| 76 |
| 77 FilterNotifier.removeListener(listener2); |
| 78 compareListeners(test, "removeListener(listener2)", []); |
| 79 |
| 80 test.done(); |
| 81 }; |
| 82 |
| 83 exports.testRemovingListenersWhileBeingCalled = function(test) |
| 84 { |
| 85 let listener1 = function() |
| 86 { |
| 87 listeners[0].apply(this, arguments); |
| 88 FilterNotifier.removeListener(listener1); |
| 89 }; |
| 90 let listener2 = listeners[1]; |
| 91 FilterNotifier.addListener(listener1); |
| 92 FilterNotifier.addListener(listener2); |
| 93 |
| 94 compareListeners(test, "Initial call", [listener1, listener2]); |
| 95 compareListeners(test, "Subsequent calls", [listener2]); |
| 96 |
| 97 test.done(); |
| 98 }; |
OLD | NEW |