LEFT | RIGHT |
(no file at all) | |
| 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 |
| 22 let FilterNotifier = null; |
| 23 |
| 24 exports.setUp = function(callback) |
| 25 { |
| 26 let sandboxedRequire = createSandbox(); |
| 27 ( |
| 28 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier") |
| 29 ); |
| 30 |
| 31 callback(); |
| 32 }; |
| 33 |
| 34 let triggeredListeners = []; |
| 35 let listeners = [ |
| 36 (action, item) => triggeredListeners.push(["listener1", action, item]), |
| 37 (action, item) => triggeredListeners.push(["listener2", action, item]), |
| 38 (action, item) => triggeredListeners.push(["listener3", action, item]) |
| 39 ]; |
| 40 |
| 41 function compareListeners(test, testDescription, list) |
| 42 { |
| 43 let result1 = triggeredListeners = []; |
| 44 FilterNotifier.triggerListeners("foo", {bar: true}); |
| 45 |
| 46 let result2 = triggeredListeners = []; |
| 47 for (let observer of list) |
| 48 observer("foo", {bar: true}); |
| 49 |
| 50 test.deepEqual(result1, result2, testDescription); |
| 51 } |
| 52 |
| 53 exports.testAddingRemovingListeners = function(test) |
| 54 { |
| 55 let [listener1, listener2, listener3] = listeners; |
| 56 |
| 57 compareListeners(test, "No listeners", []); |
| 58 |
| 59 FilterNotifier.addListener(listener1); |
| 60 compareListeners(test, "addListener(listener1)", [listener1]); |
| 61 |
| 62 FilterNotifier.addListener(listener1); |
| 63 compareListeners(test, "addListener(listener1) again", [listener1]); |
| 64 |
| 65 FilterNotifier.addListener(listener2); |
| 66 compareListeners(test, "addListener(listener2)", [listener1, listener2]); |
| 67 |
| 68 FilterNotifier.removeListener(listener1); |
| 69 compareListeners(test, "removeListener(listener1)", [listener2]); |
| 70 |
| 71 FilterNotifier.removeListener(listener1); |
| 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)", []); |
| 88 |
| 89 test.done(); |
| 90 }; |
| 91 |
| 92 exports.testRemovingListenersWhileBeingCalled = function(test) |
| 93 { |
| 94 let listener1 = function() |
| 95 { |
| 96 listeners[0].apply(this, arguments); |
| 97 FilterNotifier.removeListener(listener1); |
| 98 }; |
| 99 let listener2 = listeners[1]; |
| 100 FilterNotifier.addListener(listener1); |
| 101 FilterNotifier.addListener(listener2); |
| 102 |
| 103 compareListeners(test, "Initial call", [listener1, listener2]); |
| 104 compareListeners(test, "Subsequent calls", [listener2]); |
| 105 |
| 106 test.done(); |
| 107 }; |
LEFT | RIGHT |