OLD | NEW |
1 "use strict"; | 1 "use strict"; |
2 | 2 |
3 { | 3 { |
4 const {IO} = require("io"); | 4 const {IO} = require("io"); |
5 const info = require("info"); | 5 const info = require("info"); |
| 6 const {IndexedDBBackup} = require("../../lib/indexedDBBackup"); |
6 | 7 |
7 const testFileNames = { | 8 const testFileNames = { |
8 testData: "testData", | 9 testData: "testData", |
| 10 stat: "stat", |
9 simpleCheck: "simpleCheck", | 11 simpleCheck: "simpleCheck", |
10 write: "writeCheck", | 12 write: "writeCheck", |
11 read: "readCheck", | 13 read: "readCheck", |
| 14 readBackup: "readBackup", |
12 rename: "renameCheck" | 15 rename: "renameCheck" |
13 }; | 16 }; |
14 const testData = { | 17 const testData = { |
15 fileName: "file:" + testFileNames.testData, | 18 fileName: "file:" + testFileNames.testData, |
16 content: [1, 2, 3], | 19 content: [1, 2, 3], |
17 lastModified: Date.now() | 20 lastModified: Date.now() |
18 }; | 21 }; |
19 | 22 |
| 23 let _backupName = "test"; |
| 24 let _storageData = new Map(); |
| 25 |
| 26 IndexedDBBackup.getBackupData = () => |
| 27 { |
| 28 return new Promise((resolve, reject) => |
| 29 { |
| 30 if (_storageData.size) |
| 31 { |
| 32 resolve(_storageData.get(_backupName)); |
| 33 } |
| 34 else |
| 35 reject({type: "NoSuchFile"}); |
| 36 }); |
| 37 }; |
| 38 |
20 let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; | 39 let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; |
21 | 40 |
22 QUnit.module("Microsoft Edge filter storage", { | 41 QUnit.module("Microsoft Edge filter storage", { |
23 beforeEach() | 42 beforeEach(assert) |
24 { | 43 { |
25 return prePopulateStorage(); | 44 prePopulateStorage(assert); |
26 }, | 45 }, |
27 afterEach() | 46 afterEach(assert) |
28 { | 47 { |
29 return clearStorage(); | 48 _storageData.clear(); |
| 49 clearStorage(assert); |
30 } | 50 } |
31 }); | 51 }); |
32 | 52 |
33 testEdge("statFile", assert => | 53 testEdge("statFile", assert => |
34 { | 54 { |
35 const noFileMsg = "returns correct value if file doesn't exist"; | 55 const noFileMsg = "returns correct value if file doesn't exist" + |
36 const fileExistsMsg = "returns correct value if file exists"; | 56 " and there is no backup"; |
| 57 const fileExistsMsg = "returns correct value if file exists in indexedDB"; |
37 | 58 |
38 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise, | 59 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise, |
39 "returns a promise"); | 60 "returns a promise"); |
40 | 61 |
41 asyncReadHelper( | 62 asyncReadHelper( |
42 IO.statFile, | 63 IO.statFile, |
43 testFileNames.testData, | 64 testFileNames.testData, |
44 {exists: true, lastModified: testData.lastModified}, | 65 {exists: true, lastModified: testData.lastModified}, |
45 fileExistsMsg, | 66 fileExistsMsg, |
46 assert); | 67 assert); |
47 | 68 |
48 asyncReadHelper( | 69 asyncReadHelper( |
49 IO.statFile, | 70 IO.statFile, |
50 testFileNames.simpleCheck, | 71 testFileNames.simpleCheck, |
51 {exists: false}, | 72 {exists: false}, |
52 noFileMsg, | 73 noFileMsg, |
53 assert); | 74 assert); |
54 }); | 75 }); |
55 | 76 |
| 77 testEdge("restore backup", assert => |
| 78 { |
| 79 let backupData = { |
| 80 content: ["backup data"], |
| 81 lastModified: Date.now() |
| 82 }; |
| 83 let readFromFileMessage = "readFromFile return correct value," + |
| 84 " if a data restore is performed"; |
| 85 _storageData.set(_backupName, backupData); |
| 86 |
| 87 asyncReadHelper( |
| 88 IO.statFile, |
| 89 testFileNames.stat, |
| 90 {exists: true, lastModified: backupData.lastModified}, |
| 91 "statFile return correct value, if a data restore is performed", |
| 92 assert); |
| 93 |
| 94 callsListeners( |
| 95 testFileNames.readBackup, |
| 96 assert, backupData.content, |
| 97 readFromFileMessage); |
| 98 }); |
| 99 |
56 testEdge("writeToFile", assert => | 100 testEdge("writeToFile", assert => |
57 { | 101 { |
58 ok(IO.writeToFile(testFileNames.simpleCheck, ["test"]) instanceof Promise, | 102 ok(IO.writeToFile(testFileNames.simpleCheck, ["test"]) instanceof Promise, |
59 "returns a promise"); | 103 "returns a promise"); |
60 | 104 |
61 writesCorrectValue(assert); | 105 writesCorrectValue(assert); |
62 }); | 106 }); |
63 | 107 |
64 function writesCorrectValue(assert) | 108 function writesCorrectValue(assert) |
65 { | 109 { |
(...skipping 16 matching lines...) Expand all Loading... |
82 deepEqual( | 126 deepEqual( |
83 writeCheck.content, | 127 writeCheck.content, |
84 result.content, | 128 result.content, |
85 "data has the correct content"); | 129 "data has the correct content"); |
86 done(); | 130 done(); |
87 }); | 131 }); |
88 } | 132 } |
89 | 133 |
90 testEdge("readFromFile", assert => | 134 testEdge("readFromFile", assert => |
91 { | 135 { |
92 const noFileMsg = "returns correct value if file doesn't exist"; | 136 const noFileMsg = "returns correct value if file doesn't exist" + |
| 137 " and there is no backup"; |
| 138 const fileExistsMsg = "calls listeners with the correct values"; |
93 | 139 |
94 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise, | 140 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise, |
95 "returns a promise"); | 141 "returns a promise"); |
96 | 142 |
97 asyncReadHelper( | 143 asyncReadHelper( |
98 IO.readFromFile, | 144 IO.readFromFile, |
99 testFileNames.read, | 145 testFileNames.read, |
100 {type: "NoSuchFile"}, | 146 {type: "NoSuchFile"}, |
101 noFileMsg, | 147 noFileMsg, |
102 assert | 148 assert |
103 ); | 149 ); |
104 | 150 |
105 callsListeners(assert); | 151 callsListeners( |
| 152 testFileNames.testData, |
| 153 assert, |
| 154 testData.content, |
| 155 fileExistsMsg); |
106 }); | 156 }); |
107 | 157 |
108 function callsListeners(assert) | 158 function callsListeners(fileName, assert, expected, message) |
109 { | 159 { |
110 let done = assert.async(); | 160 let done = assert.async(); |
111 let called = []; | 161 let called = []; |
112 | 162 |
113 IO.readFromFile(testFileNames.testData, (entry) => called.push(entry)) | 163 IO.readFromFile(fileName, (entry) => called.push(entry)) |
114 .then(() => | 164 .then(() => |
115 { | 165 { |
116 deepEqual( | 166 deepEqual(called, expected, message); |
117 called, | |
118 testData.content, | |
119 "calls listeners with the correct values"); | |
120 done(); | 167 done(); |
121 }); | 168 }); |
122 } | 169 } |
123 | 170 |
124 testEdge("renameFile", assert => | 171 testEdge("renameFile", assert => |
125 { | 172 { |
126 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise, | 173 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise, |
127 "returns a promise"); | 174 "returns a promise"); |
128 | 175 |
129 checkRename(assert); | 176 checkRename(assert); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 { | 216 { |
170 let db; | 217 let db; |
171 let req = indexedDB.open("adblockplus", 1); | 218 let req = indexedDB.open("adblockplus", 1); |
172 req.onsuccess = (event) => | 219 req.onsuccess = (event) => |
173 { | 220 { |
174 db = event.currentTarget.result; | 221 db = event.currentTarget.result; |
175 let store = db | 222 let store = db |
176 .transaction(["file"], "readwrite") | 223 .transaction(["file"], "readwrite") |
177 .objectStore("file"); | 224 .objectStore("file"); |
178 | 225 |
179 store.get(fileName).onsuccess = (evt => | 226 store.get(fileName).onsuccess = evt => |
180 resolve(evt.currentTarget.result) | 227 resolve(evt.currentTarget.result); |
181 ); | |
182 }; | 228 }; |
183 }); | 229 }); |
184 } | 230 } |
185 | 231 |
186 function prePopulateStorage() | 232 function prePopulateStorage(assert) |
187 { | 233 { |
188 return new Promise(resolve => | 234 let done = assert.async(); |
| 235 let db; |
| 236 let req = indexedDB.open("adblockplus", 1); |
| 237 |
| 238 req.onsuccess = (event) => |
189 { | 239 { |
190 let db; | 240 db = event.currentTarget.result; |
191 let req = indexedDB.open("adblockplus", 1); | 241 let store = db |
| 242 .transaction(["file"], "readwrite") |
| 243 .objectStore("file"); |
192 | 244 |
193 req.onsuccess = (event) => | 245 store.put(testData).onsuccess = done; |
| 246 }; |
| 247 } |
| 248 |
| 249 function clearStorage(assert) |
| 250 { |
| 251 let done = assert.async(); |
| 252 let db; |
| 253 let req = indexedDB.open("adblockplus", 1); |
| 254 req.onsuccess = event => |
| 255 { |
| 256 db = event.currentTarget.result; |
| 257 Promise.all(Object.values(testFileNames) |
| 258 .map(fileName => new Promise(resolveFile => |
194 { | 259 { |
195 db = event.currentTarget.result; | |
196 let store = db | 260 let store = db |
197 .transaction(["file"], "readwrite") | 261 .transaction(["file"], "readwrite") |
198 .objectStore("file"); | 262 .objectStore("file"); |
199 | 263 |
200 store.put(testData).onsuccess = resolve; | 264 store.delete("file:" + fileName).onsuccess = resolveFile; |
201 }; | 265 }))) |
202 }); | 266 .then(() => done()); |
203 } | 267 }; |
204 | |
205 function clearStorage() | |
206 { | |
207 return new Promise(resolve => | |
208 { | |
209 let db; | |
210 let req = indexedDB.open("adblockplus", 1); | |
211 | |
212 req.onsuccess = (event) => | |
213 { | |
214 db = event.currentTarget.result; | |
215 let files = Object.keys(testFileNames) | |
216 .map(fileName => new Promise((resolveFile, reject) => | |
217 { | |
218 let store = db | |
219 .transaction(["file"], "readwrite") | |
220 .objectStore("file"); | |
221 | |
222 store.delete("file:" + fileName).onsuccess = resolveFile; | |
223 })); | |
224 | |
225 Promise.all(files).then(resolve); | |
226 }; | |
227 }); | |
228 } | 268 } |
229 } | 269 } |
230 | 270 |
OLD | NEW |