Index: qunit/tests/indexedDBBackup.js |
diff --git a/qunit/tests/indexedDBBackup.js b/qunit/tests/indexedDBBackup.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3bd493bbce71fac4833a81bbcb5d95f8d32d3613 |
--- /dev/null |
+++ b/qunit/tests/indexedDBBackup.js |
@@ -0,0 +1,120 @@ |
+"use strict"; |
+ |
+{ |
+ const {IndexedDBBackup} = require("../../lib/indexedDBBackup"); |
+ const info = require("info"); |
+ const {Filter} = require("../../adblockpluscore/lib/filterClasses"); |
+ const {Subscription, SpecialSubscription} = |
+ require("../../adblockpluscore/lib/subscriptionClasses"); |
+ |
+ let writeTime = 0; |
+ let writeInterval = 100; |
+ |
+ let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; |
+ |
+ QUnit.module("Microsoft Edge indexedDB backup", { |
+ afterEach() |
+ { |
+ IndexedDBBackup.init(); |
+ } |
+ }); |
+ |
+ testEdge("Data serialization", () => |
+ { |
+ let subscription = Subscription.fromObject({ |
+ title: "test", |
+ url: "test.com", |
+ homepage: "example.com", |
+ disabled: false, |
+ lastSuccess: 8, |
+ 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 expectedFormat = [ |
+ "", |
+ "[Subscription]", |
+ `homepage=${subscription.homepage}`, |
+ `title=${subscription.title}`, |
+ `url=${subscription.url}`, |
+ `disabled=${subscription.disabled}`, |
+ "", |
+ "[Subscription]", |
+ `url=${specialSubscription.url}`, |
+ "defaults=blocking", |
+ "", |
+ "[Subscription filters]", |
+ "example.com" |
+ ]; |
+ let actualFormat = IndexedDBBackup.serialize([ |
+ subscription, |
+ specialSubscription |
+ ]); |
+ deepEqual(actualFormat, expectedFormat, "formates the data correctly"); |
Sebastian Noack
2018/08/21 20:26:25
Maybe we should test the serialization as part of
geo
2018/08/31 15:49:25
Done.
|
+ }); |
+ |
+ 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() |
+ { |
+ ok(start - saveTimes[0].start < writeInterval, |
+ "first change is proccessed imediatelly"); |
+ } |
+ }, |
+ { |
+ done: assert.async(), |
+ check() |
+ { |
+ ok(saveTimes[1].start - saveTimes[0].end >= writeInterval, |
+ "next change is after the time limit"); |
+ } |
+ } |
+ ]; |
+ let mockSave = () => |
+ { |
+ saveTimes.push({start: performance.now()}); |
+ return new Promise(resolve => |
+ { |
+ setTimeout(() => |
+ { |
+ saveTimes[saveTimes.length - 1].end = performance.now(); |
+ let step = steps.shift(); |
+ step.check(); |
+ step.done(); |
+ resolve(); |
+ }, writeTime); |
+ }); |
+ }; |
+ |
+ IndexedDBBackup.init(writeInterval, mockSave); |
+ IndexedDBBackup.scheduleBackup(); |
+ setTimeout(IndexedDBBackup.scheduleBackup, writeTime / 2); |
+ setTimeout(IndexedDBBackup.scheduleBackup, writeInterval); |
+ } |
+} |