| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 "use strict"; | 1 "use strict"; |
| 2 | 2 |
| 3 { | 3 { |
| 4 const {IndexedDBBackup} = require("../../lib/indexedDBBackup"); | 4 const {IndexedDBBackup} = require("../../lib/indexedDBBackup"); |
| 5 const info = require("info"); | 5 const info = require("info"); |
| 6 const {FilterStorage} = require("../../adblockpluscore/lib/filterStorage"); | |
| 6 const {Filter} = require("../../adblockpluscore/lib/filterClasses"); | 7 const {Filter} = require("../../adblockpluscore/lib/filterClasses"); |
| 7 const {Subscription, SpecialSubscription} = | 8 const {Subscription, SpecialSubscription} = |
| 8 require("../../adblockpluscore/lib/subscriptionClasses"); | 9 require("../../adblockpluscore/lib/subscriptionClasses"); |
| 9 | 10 |
| 10 let writeTime = 0; | 11 let backupDelay = 100; |
| 11 let writeInterval = 100; | 12 let subscription = Subscription.fromObject({ |
| 13 title: "test", | |
| 14 url: "test.com", | |
| 15 homepage: "example.com", | |
| 16 lastSuccess: 8, | |
| 17 disabled: false, | |
| 18 lastDownload: 12, | |
| 19 lastCheck: 16, | |
| 20 softExpiration: 18, | |
| 21 expires: 20, | |
| 22 downloadStatus: "done", | |
| 23 errors: 3, | |
| 24 version: 24, | |
| 25 downloadCount: 1, | |
| 26 requiredVersion: "0.6" | |
| 27 }); | |
| 28 let filter = Filter.fromText("example.com"); | |
| 29 let specialSubscription = SpecialSubscription.createForFilter(filter); | |
| 12 | 30 |
| 13 let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; | 31 let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; |
| 14 | 32 |
| 15 QUnit.module("Microsoft Edge indexedDB backup", { | 33 QUnit.module("Microsoft Edge indexedDB backup", { |
| 34 beforeEach() | |
| 35 { | |
| 36 this._storageLocalSet = browser.storage.local.set; | |
| 37 IndexedDBBackup.setBackupInterval(backupDelay); | |
| 38 }, | |
| 16 afterEach() | 39 afterEach() |
| 17 { | 40 { |
| 18 IndexedDBBackup.init(); | 41 Object.defineProperty( |
| 42 browser.storage.local, "set", | |
| 43 {value: this._storageLocalSet, enumerable: true} | |
| 44 ); | |
| 45 IndexedDBBackup.setBackupInterval(); | |
| 19 } | 46 } |
| 20 }); | 47 }); |
| 21 | 48 |
| 22 testEdge("Data serialization", () => | 49 testEdge("Backup creation", assert => |
| 23 { | 50 { |
| 24 let subscription = Subscription.fromObject({ | |
| 25 title: "test", | |
| 26 url: "test.com", | |
| 27 homepage: "example.com", | |
| 28 disabled: false, | |
| 29 lastSuccess: 8, | |
| 30 lastDownload: 12, | |
| 31 lastCheck: 16, | |
| 32 softExpiration: 18, | |
| 33 expires: 20, | |
| 34 downloadStatus: "done", | |
| 35 errors: 3, | |
| 36 version: 24, | |
| 37 downloadCount: 1, | |
| 38 requiredVersion: "0.6" | |
| 39 }); | |
| 40 let filter = Filter.fromText("example.com"); | |
| 41 let specialSubscription = SpecialSubscription.createForFilter(filter); | |
| 42 | |
| 43 let expectedFormat = [ | |
| 44 "", | |
| 45 "[Subscription]", | |
| 46 `homepage=${subscription.homepage}`, | |
| 47 `title=${subscription.title}`, | |
| 48 `url=${subscription.url}`, | |
| 49 `disabled=${subscription.disabled}`, | |
| 50 "", | |
| 51 "[Subscription]", | |
| 52 `url=${specialSubscription.url}`, | |
| 53 "defaults=blocking", | |
| 54 "", | |
| 55 "[Subscription filters]", | |
| 56 "example.com" | |
| 57 ]; | |
| 58 let actualFormat = IndexedDBBackup.serialize([ | |
| 59 subscription, | |
| 60 specialSubscription | |
| 61 ]); | |
| 62 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.
| |
| 63 }); | |
| 64 | |
| 65 testEdge("Write time smaller than backup interval", assert => | |
| 66 { | |
| 67 writeTime = 10; | |
| 68 testSaveSteps(assert); | |
| 69 }); | |
| 70 | |
| 71 testEdge("Write time greater than backup interval", assert => | |
| 72 { | |
| 73 writeTime = 150; | |
| 74 testSaveSteps(assert); | 51 testSaveSteps(assert); |
| 75 }); | 52 }); |
| 76 | 53 |
| 77 function testSaveSteps(assert) | 54 function testSaveSteps(assert) |
| 78 { | 55 { |
| 79 let start = performance.now(); | 56 let start = performance.now(); |
| 80 let saveTimes = []; | 57 let saveTimes = []; |
| 58 | |
| 81 let steps = [ | 59 let steps = [ |
| 82 { | 60 { |
| 83 done: assert.async(), | 61 done: assert.async(), |
| 84 check() | 62 check(data) |
| 85 { | 63 { |
| 86 ok(start - saveTimes[0].start < writeInterval, | 64 let expectedFormat = [ |
| 87 "first change is proccessed imediatelly"); | 65 "[Subscription]", |
| 66 `url=${specialSubscription.url}`, | |
| 67 "defaults=blocking", | |
| 68 "[Subscription filters]", | |
| 69 "example.com", | |
| 70 "[Subscription]", | |
| 71 "homepage=example.com", | |
| 72 "title=test", | |
| 73 "url=test.com", | |
| 74 "disabled=false" | |
| 75 ]; | |
| 76 | |
| 77 ok( | |
| 78 saveTimes[0] - start >= backupDelay, | |
| 79 "first write is deferred" | |
| 80 ); | |
| 81 deepEqual( | |
| 82 data.content, | |
| 83 expectedFormat, | |
| 84 "saved data has the correct information" | |
| 85 ); | |
| 86 | |
| 87 FilterStorage.removeSubscription(subscription); | |
| 88 FilterStorage.removeSubscription(specialSubscription); | |
| 88 } | 89 } |
| 89 }, | 90 }, |
| 90 { | 91 { |
| 91 done: assert.async(), | 92 done: assert.async(), |
| 92 check() | 93 check(data) |
| 93 { | 94 { |
| 94 ok(saveTimes[1].start - saveTimes[0].end >= writeInterval, | 95 ok( |
| 95 "next change is after the time limit"); | 96 saveTimes[1] - saveTimes[0] >= backupDelay, |
| 97 "next changes are saved after the write delay" | |
| 98 ); | |
| 99 deepEqual( | |
| 100 data.content, [], "saved data has the correct information" | |
| 101 ); | |
| 96 } | 102 } |
| 97 } | 103 } |
| 98 ]; | 104 ]; |
| 99 let mockSave = () => | 105 let mockSave = data => |
| 100 { | 106 { |
| 101 saveTimes.push({start: performance.now()}); | 107 let step = steps.shift(); |
| 102 return new Promise(resolve => | 108 |
| 109 saveTimes.push(performance.now()); | |
| 110 | |
| 111 setTimeout(() => | |
|
geo
2018/09/05 15:59:08
I've reintroduced this timeout because without it,
| |
| 103 { | 112 { |
| 104 setTimeout(() => | 113 step.check(data["file:indexedDB-backup"]); |
| 105 { | 114 step.done(); |
| 106 saveTimes[saveTimes.length - 1].end = performance.now(); | 115 }, 0); |
| 107 let step = steps.shift(); | |
| 108 step.check(); | |
| 109 step.done(); | |
| 110 resolve(); | |
| 111 }, writeTime); | |
| 112 }); | |
| 113 }; | 116 }; |
| 114 | 117 |
| 115 IndexedDBBackup.init(writeInterval, mockSave); | 118 Object.defineProperty( |
| 116 IndexedDBBackup.scheduleBackup(); | 119 browser.storage.local, "set", |
| 117 setTimeout(IndexedDBBackup.scheduleBackup, writeTime / 2); | 120 {value: mockSave, enumerable: true} |
| 118 setTimeout(IndexedDBBackup.scheduleBackup, writeInterval); | 121 ); |
| 122 | |
| 123 FilterStorage.addSubscription(specialSubscription); | |
| 124 FilterStorage.addSubscription(subscription); | |
| 119 } | 125 } |
| 120 } | 126 } |
| LEFT | RIGHT |