Index: qunit/tests/indexedDBBackup.js |
diff --git a/qunit/tests/indexedDBBackup.js b/qunit/tests/indexedDBBackup.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0003a0550b14b7e535a8664d9daa4f8bab415157 |
--- /dev/null |
+++ b/qunit/tests/indexedDBBackup.js |
@@ -0,0 +1,140 @@ |
+"use strict"; |
+ |
+{ |
+ const {IndexedDBBackup} = require("../../lib/indexedDBBackup"); |
+ const info = require("info"); |
+ const {FilterStorage} = require("../../adblockpluscore/lib/filterStorage"); |
+ const {Filter} = require("../../adblockpluscore/lib/filterClasses"); |
+ const {Subscription, SpecialSubscription} = |
+ require("../../adblockpluscore/lib/subscriptionClasses"); |
+ |
+ let writeTime = 0; |
+ let writeInterval = 100; |
+ let subscription = Subscription.fromObject({ |
+ title: "test", |
+ url: "test.com", |
+ homepage: "example.com", |
+ lastSuccess: 8, |
+ disabled: false, |
+ lastDownload: 12, |
+ lastCheck: 16, |
+ softExpiration: 18, |
+ expires: 20, |
+ downloadStatus: "done", |
+ errors: 3, |
+ version: 24, |
+ downloadCount: 1, |
+ requiredVersion: "0.6" |
+ }); |
+ let filter = Filter.fromText("example.com"); |
+ let specialSubscription = SpecialSubscription.createForFilter(filter); |
+ |
+ let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; |
+ |
+ QUnit.module("Microsoft Edge indexedDB backup", { |
+ beforeEach() |
+ { |
+ this._storageLocalSet = browser.storage.local.set; |
+ IndexedDBBackup.init(writeInterval); |
+ }, |
+ afterEach() |
+ { |
+ Object.defineProperty( |
+ browser.storage.local, "set", |
+ {value: this._storageLocalSet, enumerable: true}); |
+ IndexedDBBackup.init(); |
+ } |
+ }); |
+ |
+ testEdge("Write time smaller than backup interval", assert => |
+ { |
+ writeTime = 10; |
+ testSaveSteps(assert); |
+ }); |
+ |
+ testEdge("Write time greater than backup interval", assert => |
+ { |
+ writeTime = 150; |
+ testSaveSteps(assert); |
+ }); |
+ |
+ function testSaveSteps(assert) |
+ { |
+ let start = performance.now(); |
+ let saveTimes = []; |
+ |
+ let steps = [ |
+ { |
+ done: assert.async(), |
+ check(data) |
+ { |
+ let expectedFormat = [ |
+ "[Subscription]", |
+ `url=${specialSubscription.url}`, |
+ "defaults=blocking", |
+ "[Subscription filters]", |
+ "example.com" |
+ ]; |
+ ok(start - data.lastModified < writeInterval, |
+ "first change is proccessed immediately"); |
+ deepEqual( |
+ data.content, expectedFormat, "data is formatted correctly"); |
+ } |
+ }, |
+ { |
+ done: assert.async(), |
+ check(data) |
+ { |
+ let expectedFormat = [ |
+ "[Subscription]", |
+ `url=${specialSubscription.url}`, |
+ "defaults=blocking", |
+ "[Subscription filters]", |
+ "example.com", |
+ "[Subscription]", |
+ "homepage=example.com", |
+ "title=test", |
+ "url=test.com", |
+ "disabled=false" |
+ ]; |
+ ok(data.lastModified - saveTimes[0] >= writeInterval, |
+ "next change is after the time limit"); |
+ deepEqual( |
+ data.content, expectedFormat, "data is formatted correctly"); |
+ |
+ FilterStorage.removeSubscription(subscription); |
+ FilterStorage.removeSubscription(specialSubscription); |
+ } |
+ }, |
+ { |
+ done: assert.async(), |
+ check(data) |
+ { |
+ deepEqual( |
+ data.content, [], "multiple changes are treated in one write"); |
+ } |
+ } |
+ ]; |
+ let mockSave = (data) => |
+ { |
+ return new Promise(resolve => |
+ { |
+ setTimeout(() => |
+ { |
+ saveTimes.push(performance.now()); |
+ let step = steps.shift(); |
+ step.check(data["file:indexedDB-backup"]); |
+ step.done(); |
+ resolve(); |
+ }, writeTime); |
+ }); |
+ }; |
+ |
+ Object.defineProperty( |
+ browser.storage.local, "set", |
+ {value: mockSave, enumerable: true}); |
+ |
+ FilterStorage.addSubscription(specialSubscription); |
+ FilterStorage.addSubscription(subscription); |
+ } |
+} |