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