Index: qunit/tests/indexedDBBackup.js |
diff --git a/qunit/tests/indexedDBBackup.js b/qunit/tests/indexedDBBackup.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ad37730d80635e7b6c14cf4871b515fd1bb61bb8 |
--- /dev/null |
+++ b/qunit/tests/indexedDBBackup.js |
@@ -0,0 +1,142 @@ |
+"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.setBackupInterval(writeInterval); |
+ }, |
+ afterEach() |
+ { |
+ Object.defineProperty( |
+ browser.storage.local, "set", |
+ {value: this._storageLocalSet, enumerable: true}); |
Sebastian Noack
2018/09/04 21:00:01
Nit: See previous comment(s) about wrapping argume
geo
2018/09/05 14:04:34
Done.
|
+ IndexedDBBackup.setBackupInterval(); |
+ } |
+ }); |
+ |
+ 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"); |
+ |
+ FilterStorage.addSubscription(subscription); |
+ } |
+ }, |
+ { |
+ 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(() => |
+ { |
+ let step = steps.shift(); |
+ saveTimes.push(performance.now()); |
+ step.check(data["file:indexedDB-backup"]); |
+ step.done(); |
+ resolve(); |
+ }, writeTime); |
+ }); |
+ }; |
+ |
+ Object.defineProperty( |
+ browser.storage.local, "set", |
+ {value: mockSave, enumerable: true}); |
+ |
+ FilterStorage.addSubscription(specialSubscription); |
+ } |
+} |