| LEFT | RIGHT |
| 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 assert = require("assert"); |
| 21 | 21 |
| 22 let { | 22 let { |
| 23 createSandbox, setupTimerAndXMLHttp, setupRandomResult, Cr | 23 createSandbox, setupTimerAndFetch, setupRandomResult |
| 24 } = require("./_common"); | 24 } = require("./_common"); |
| 25 | 25 |
| 26 let Prefs = null; | 26 let Prefs = null; |
| 27 let Utils = null; | 27 let Utils = null; |
| 28 let Notification = null; | 28 let Notification = null; |
| 29 |
| 30 // Only starting NodeJS 10 that URL is in the global space. |
| 31 const {URL} = require("url"); |
| 29 | 32 |
| 30 describe("Notifications", () => | 33 describe("Notifications", () => |
| 31 { | 34 { |
| 32 let runner = {}; | 35 let runner = {}; |
| 33 | 36 |
| 34 beforeEach(() => | 37 beforeEach(() => |
| 35 { | 38 { |
| 36 runner = {}; | 39 runner = {}; |
| 37 // Inject our Array and JSON to make sure that instanceof checks on arrays | 40 // Inject our Array and JSON to make sure that instanceof checks on arrays |
| 38 // within the sandbox succeed even with data passed in from outside. | 41 // within the sandbox succeed even with data passed in from outside. |
| 39 let globals = Object.assign({Array, JSON}, | 42 let globals = Object.assign({Array, JSON}, |
| 40 setupTimerAndXMLHttp.call(runner), setupRandomResult.call(runner)); | 43 setupTimerAndFetch.call(runner), setupRandomResult.call(runner)); |
| 41 | 44 |
| 42 let sandboxedRequire = createSandbox({globals}); | 45 let sandboxedRequire = createSandbox({globals}); |
| 43 ( | 46 ( |
| 44 {Prefs} = sandboxedRequire("./stub-modules/prefs"), | 47 {Prefs} = sandboxedRequire("./stub-modules/prefs"), |
| 45 {Utils} = sandboxedRequire("./stub-modules/utils"), | 48 {Utils} = sandboxedRequire("./stub-modules/utils"), |
| 46 {Notification} = sandboxedRequire("../lib/notification") | 49 {Notification} = sandboxedRequire("../lib/notification") |
| 47 ); | 50 ); |
| 48 }); | 51 }); |
| 49 | 52 |
| 50 function showNotifications(url) | 53 function showNotifications(location) |
| 51 { | 54 { |
| 52 let shownNotifications = []; | 55 let shownNotifications = []; |
| 53 function showListener(notification) | 56 function showListener(notification) |
| 54 { | 57 { |
| 55 shownNotifications.push(notification); | 58 shownNotifications.push(notification); |
| 56 Notification.markAsShown(notification.id); | 59 Notification.markAsShown(notification.id); |
| 57 } | 60 } |
| 58 Notification.addShowListener(showListener); | 61 Notification.addShowListener(showListener); |
| 59 Notification.showNext(url); | 62 Notification.showNext(location && new URL(location)); |
| 60 Notification.removeShowListener(showListener); | 63 Notification.removeShowListener(showListener); |
| 61 return shownNotifications; | 64 return shownNotifications; |
| 62 } | 65 } |
| 63 | 66 |
| 64 function* pairs(array) | 67 function* pairs(array) |
| 65 { | 68 { |
| 66 for (let element1 of array) | 69 for (let element1 of array) |
| 67 { | 70 { |
| 68 for (let element2 of array) | 71 for (let element2 of array) |
| 69 { | 72 { |
| 70 if (element1 != element2) | 73 if (element1 != element2) |
| 71 yield [element1, element2]; | 74 yield [element1, element2]; |
| 72 } | 75 } |
| 73 } | 76 } |
| 74 } | 77 } |
| 75 | 78 |
| 76 function registerHandler(notifications, checkCallback) | 79 function registerHandler(notifications, checkCallback) |
| 77 { | 80 { |
| 78 runner.registerHandler("/notification.json", metadata => | 81 runner.registerHandler("/notification.json", metadata => |
| 79 { | 82 { |
| 80 if (checkCallback) | 83 if (checkCallback) |
| 81 checkCallback(metadata); | 84 checkCallback(metadata); |
| 82 | 85 |
| 83 let notification = { | 86 let notification = { |
| 84 version: 55, | 87 version: 55, |
| 85 notifications | 88 notifications |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 return [Cr.NS_OK, 200, JSON.stringify(notification)]; | 91 return [200, JSON.stringify(notification)]; |
| 89 }); | 92 }); |
| 90 } | 93 } |
| 91 | 94 |
| 92 it("No Data", () => | 95 it("No Data", () => |
| 93 { | 96 { |
| 94 assert.deepEqual(showNotifications(), [], "No notifications should be return
ed if there is no data"); | 97 assert.deepEqual(showNotifications(), [], "No notifications should be return
ed if there is no data"); |
| 95 }); | 98 }); |
| 96 | 99 |
| 97 it("Single Notificaion", () => | 100 it("Single Notificaion", () => |
| 98 { | 101 { |
| (...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 | 436 |
| 434 registerHandler.call(this, [relentless]); | 437 registerHandler.call(this, [relentless]); |
| 435 return runner.runScheduledTasks(1).then(() => | 438 return runner.runScheduledTasks(1).then(() => |
| 436 { | 439 { |
| 437 assert.deepEqual(showNotifications(), [relentless], "Relentless notificati
ons are shown initially"); | 440 assert.deepEqual(showNotifications(), [relentless], "Relentless notificati
ons are shown initially"); |
| 438 }).then(() => | 441 }).then(() => |
| 439 { | 442 { |
| 440 assert.deepEqual(showNotifications(), [], "Relentless notifications are no
t shown before the interval"); | 443 assert.deepEqual(showNotifications(), [], "Relentless notifications are no
t shown before the interval"); |
| 441 }).then(() => | 444 }).then(() => |
| 442 { | 445 { |
| 443 // Date always returns a fixed time (see setupTimerAndXMLHttp) so we | 446 // Date always returns a fixed time (see setupTimerAndFetch) so we |
| 444 // manipulate the shown data manually. | 447 // manipulate the shown data manually. |
| 445 Prefs.notificationdata.shown[relentless.id] -= relentless.interval; | 448 Prefs.notificationdata.shown[relentless.id] -= relentless.interval; |
| 446 assert.deepEqual(showNotifications(), [relentless], "Relentless notificati
ons are shown after the interval"); | 449 assert.deepEqual(showNotifications(), [relentless], "Relentless notificati
ons are shown after the interval"); |
| 447 }); | 450 }); |
| 448 }); | 451 }); |
| 449 | 452 |
| 450 it("Relentless notification", () => | 453 it("Relentless notification", () => |
| 451 { | 454 { |
| 452 let relentless = { | 455 let relentless = { |
| 453 id: 3, | 456 id: 3, |
| 454 type: "relentless", | 457 type: "relentless", |
| 455 interval: 100, | 458 interval: 100, |
| 456 urlFilters: ["foo.com$document", "bar.foo$document"] | 459 urlFilters: ["foo.com$document", "bar.foo$document"] |
| 457 }; | 460 }; |
| 458 | 461 |
| 459 registerHandler.call(this, [relentless]); | 462 registerHandler.call(this, [relentless]); |
| 460 return runner.runScheduledTasks(1).then(() => | 463 return runner.runScheduledTasks(1).then(() => |
| 461 { | 464 { |
| 462 assert.deepEqual(showNotifications(), [], "Relentless notification is not
shown without URL"); | 465 assert.deepEqual(showNotifications(), [], "Relentless notification is not
shown without URL"); |
| 463 assert.deepEqual(showNotifications("http://bar.com"), [], "Relentless noti
fication is not shown for a non-matching URL"); | 466 assert.deepEqual(showNotifications("http://bar.com"), [], "Relentless noti
fication is not shown for a non-matching URL"); |
| 464 assert.deepEqual(showNotifications("http://foo.com"), [relentless], "Relen
tless notification is shown for a matching URL"); | 467 assert.deepEqual(showNotifications("http://foo.com"), [relentless], "Relen
tless notification is shown for a matching URL"); |
| 465 }).then(() => | 468 }).then(() => |
| 466 { | 469 { |
| 467 assert.deepEqual(showNotifications("http://foo.com"), [], "Relentless noti
fications are not shown before the interval"); | 470 assert.deepEqual(showNotifications("http://foo.com"), [], "Relentless noti
fications are not shown before the interval"); |
| 468 }).then(() => | 471 }).then(() => |
| 469 { | 472 { |
| 470 // Date always returns a fixed time (see setupTimerAndXMLHttp) so we | 473 // Date always returns a fixed time (see setupTimerAndFetch) so we |
| 471 // manipulate the shown data manually. | 474 // manipulate the shown data manually. |
| 472 Prefs.notificationdata.shown[relentless.id] -= relentless.interval; | 475 Prefs.notificationdata.shown[relentless.id] -= relentless.interval; |
| 473 assert.deepEqual(showNotifications(), [], "Relentless notifications are no
t shown after the interval without URL"); | 476 assert.deepEqual(showNotifications(), [], "Relentless notifications are no
t shown after the interval without URL"); |
| 474 assert.deepEqual(showNotifications("http://bar.com"), [], "Relentless noti
fications are not shown after the interval for a non-matching URL"); | 477 assert.deepEqual(showNotifications("http://bar.com"), [], "Relentless noti
fications are not shown after the interval for a non-matching URL"); |
| 475 assert.deepEqual(showNotifications("http://bar.foo.com"), [relentless], "R
elentless notifications are shown after the interval for a matching URL"); | 478 assert.deepEqual(showNotifications("http://bar.foo.com"), [relentless], "R
elentless notifications are shown after the interval for a matching URL"); |
| 476 }); | 479 }); |
| 477 }); | 480 }); |
| 478 | 481 |
| 479 it("Global opt-out", () => | 482 it("Global opt-out", () => |
| 480 { | 483 { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 }); | 570 }); |
| 568 | 571 |
| 569 it("Missing translation", () => | 572 it("Missing translation", () => |
| 570 { | 573 { |
| 571 let notification = {message: {"en-US": "en-US"}}; | 574 let notification = {message: {"en-US": "en-US"}}; |
| 572 Utils.appLocale = "fr"; | 575 Utils.appLocale = "fr"; |
| 573 let texts = Notification.getLocalizedTexts(notification); | 576 let texts = Notification.getLocalizedTexts(notification); |
| 574 assert.equal(texts.message, "en-US"); | 577 assert.equal(texts.message, "en-US"); |
| 575 }); | 578 }); |
| 576 }); | 579 }); |
| LEFT | RIGHT |