| 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 {createSandbox, unexpectedError} = require("./_common"); | 20 const {createSandbox, unexpectedError} = require("./_common"); |
| 21 | 21 |
| 22 let Filter = null; | 22 let Filter = null; |
| 23 let FilterStorage = null; | 23 let FilterStorage = null; |
| 24 let IO = null; | 24 let IO = null; |
| 25 let Prefs = null; | 25 let Prefs = null; |
| 26 let ExternalSubscription = null; | 26 let ExternalSubscription = null; |
| 27 let SpecialSubscription = null; |
| 27 | 28 |
| 28 exports.setUp = function(callback) | 29 exports.setUp = function(callback) |
| 29 { | 30 { |
| 30 let sandboxedRequire = createSandbox(); | 31 let sandboxedRequire = createSandbox(); |
| 31 ( | 32 ( |
| 32 {Filter} = sandboxedRequire("../lib/filterClasses"), | 33 {Filter} = sandboxedRequire("../lib/filterClasses"), |
| 33 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), | 34 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), |
| 34 {IO} = sandboxedRequire("./stub-modules/io"), | 35 {IO} = sandboxedRequire("./stub-modules/io"), |
| 35 {Prefs} = sandboxedRequire("./stub-modules/prefs"), | 36 {Prefs} = sandboxedRequire("./stub-modules/prefs"), |
| 36 {ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses") | 37 {ExternalSubscription, SpecialSubscription} = sandboxedRequire("../lib/subsc
riptionClasses") |
| 37 ); | 38 ); |
| 38 | 39 |
| 39 FilterStorage.addFilter(Filter.fromText("foobar")); | 40 FilterStorage.addFilter(Filter.fromText("foobar")); |
| 40 callback(); | 41 callback(); |
| 41 }; | 42 }; |
| 42 | 43 |
| 43 let testData = new Promise((resolve, reject) => | 44 let testData = new Promise((resolve, reject) => |
| 44 { | 45 { |
| 45 const fs = require("fs"); | 46 const fs = require("fs"); |
| 46 const path = require("path"); | 47 const path = require("path"); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 { | 84 { |
| 84 if (a.key < b.key) | 85 if (a.key < b.key) |
| 85 return -1; | 86 return -1; |
| 86 else if (a.key > b.key) | 87 else if (a.key > b.key) |
| 87 return 1; | 88 return 1; |
| 88 return 0; | 89 return 0; |
| 89 }); | 90 }); |
| 90 return sections; | 91 return sections; |
| 91 } | 92 } |
| 92 | 93 |
| 93 function testReadWrite(test, withExternal) | 94 function testReadWrite(test, withExternal, withEmptySpecial) |
| 94 { | 95 { |
| 95 test.ok(!FilterStorage.initialized, "Uninitialized before the first load"); | 96 test.ok(!FilterStorage.initialized, "Uninitialized before the first load"); |
| 96 | 97 |
| 97 return testData.then(data => | 98 return testData.then(data => |
| 98 { | 99 { |
| 99 IO._setFileContents(FilterStorage.sourceFile, data); | 100 IO._setFileContents(FilterStorage.sourceFile, data); |
| 100 return FilterStorage.loadFromDisk(); | 101 return FilterStorage.loadFromDisk(); |
| 101 }).then(() => | 102 }).then(() => |
| 102 { | 103 { |
| 103 test.ok(FilterStorage.initialized, "Initialize after the first load"); | 104 test.ok(FilterStorage.initialized, "Initialize after the first load"); |
| 104 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion
, "File format version"); | 105 test.equal(FilterStorage.fileProperties.version, FilterStorage.formatVersion
, "File format version"); |
| 105 | 106 |
| 106 if (withExternal) | 107 if (withExternal) |
| 107 { | 108 { |
| 108 { | 109 { |
| 109 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); | 110 let subscription = new ExternalSubscription("~external~external subscrip
tion ID", "External subscription"); |
| 110 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; | 111 subscription.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; |
| 111 FilterStorage.addSubscription(subscription); | 112 FilterStorage.addSubscription(subscription); |
| 112 } | 113 } |
| 113 | 114 |
| 114 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio
n => subscription instanceof ExternalSubscription); | 115 let externalSubscriptions = FilterStorage.subscriptions.filter(subscriptio
n => subscription instanceof ExternalSubscription); |
| 115 test.equal(externalSubscriptions.length, 1, "Number of external subscripti
ons after updateExternalSubscription"); | 116 test.equal(externalSubscriptions.length, 1, "Number of external subscripti
ons after updateExternalSubscription"); |
| 116 | 117 |
| 117 test.equal(externalSubscriptions[0].url, "~external~external subscription
ID", "ID of external subscription"); | 118 test.equal(externalSubscriptions[0].url, "~external~external subscription
ID", "ID of external subscription"); |
| 118 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters
in external subscription"); | 119 test.equal(externalSubscriptions[0].filters.length, 2, "Number of filters
in external subscription"); |
| 119 } | 120 } |
| 120 | 121 |
| 122 if (withEmptySpecial) |
| 123 { |
| 124 let specialSubscription = |
| 125 SpecialSubscription.createForFilter(Filter.fromText("!foo")); |
| 126 FilterStorage.addSubscription(specialSubscription); |
| 127 |
| 128 FilterStorage.removeFilter(Filter.fromText("!foo"), specialSubscription); |
| 129 |
| 130 test.equal(specialSubscription.filters.length, 0, |
| 131 "No filters in special subscription"); |
| 132 test.ok(FilterStorage.subscriptions.indexOf(specialSubscription) != -1, |
| 133 "Empty special subscription still in storage"); |
| 134 } |
| 135 |
| 121 return FilterStorage.saveToDisk(); | 136 return FilterStorage.saveToDisk(); |
| 122 }).then(() => testData).then(expected => | 137 }).then(() => testData).then(expected => |
| 123 { | 138 { |
| 124 test.deepEqual(canonize(IO._getFileContents(FilterStorage.sourceFile)), | 139 test.deepEqual(canonize(IO._getFileContents(FilterStorage.sourceFile)), |
| 125 canonize(expected), "Read/write result"); | 140 canonize(expected), "Read/write result"); |
| 126 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 141 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| 127 } | 142 } |
| 128 | 143 |
| 129 exports.testReadAndSaveToFile = function(test) | 144 exports.testReadAndSaveToFile = function(test) |
| 130 { | 145 { |
| 131 testReadWrite(test, false); | 146 testReadWrite(test, false); |
| 132 }; | 147 }; |
| 133 | 148 |
| 134 exports.testReadAndSaveToFileWithExternalSubscription = function(test) | 149 exports.testReadAndSaveToFileWithExternalSubscription = function(test) |
| 135 { | 150 { |
| 136 testReadWrite(test, true); | 151 testReadWrite(test, true); |
| 137 }; | 152 }; |
| 138 | 153 |
| 154 exports.testReadAndSaveToFileWithEmptySpecial = function(test) |
| 155 { |
| 156 testReadWrite(test, false, true); |
| 157 }; |
| 158 |
| 139 exports.testImportExport = function(test) | 159 exports.testImportExport = function(test) |
| 140 { | 160 { |
| 141 testData.then(lines => | 161 testData.then(lines => |
| 142 { | 162 { |
| 143 if (lines.length && lines[lines.length - 1] == "") | 163 if (lines.length && lines[lines.length - 1] == "") |
| 144 lines.pop(); | 164 lines.pop(); |
| 145 | 165 |
| 146 let importer = FilterStorage.importData(); | 166 let importer = FilterStorage.importData(); |
| 147 for (let line of lines) | 167 for (let line of lines) |
| 148 importer(line); | 168 importer(line); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 return FilterStorage.restoreBackup(1); | 263 return FilterStorage.restoreBackup(1); |
| 244 }).then(() => | 264 }).then(() => |
| 245 { | 265 { |
| 246 test.equal(FilterStorage.subscriptions[0].filters.length, 1, "Filter count a
fter restoring backup"); | 266 test.equal(FilterStorage.subscriptions[0].filters.length, 1, "Filter count a
fter restoring backup"); |
| 247 return FilterStorage.loadFromDisk(); | 267 return FilterStorage.loadFromDisk(); |
| 248 }).then(() => | 268 }).then(() => |
| 249 { | 269 { |
| 250 test.equal(FilterStorage.subscriptions[0].filters.length, 1, "Filter count a
fter reloading"); | 270 test.equal(FilterStorage.subscriptions[0].filters.length, 1, "Filter count a
fter reloading"); |
| 251 }).catch(unexpectedError.bind(test)).then(() => test.done()); | 271 }).catch(unexpectedError.bind(test)).then(() => test.done()); |
| 252 }; | 272 }; |
| OLD | NEW |