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 | 6 |
6 const testFileNames = { | 7 const testFileNames = { |
7 testData: "testData", | 8 testData: "testData", |
8 simpleCheck: "simpleCheck", | 9 simpleCheck: "simpleCheck", |
9 write: "writeCheck", | 10 write: "writeCheck", |
10 read: "readCheck", | 11 read: "readCheck", |
11 rename: "renameCheck" | 12 rename: "renameCheck" |
12 }; | 13 }; |
13 const testData = { | 14 const testData = { |
14 fileName: "file:" + testFileNames.testData, | 15 fileName: "file:" + testFileNames.testData, |
15 content: [1, 2, 3], | 16 content: [1, 2, 3], |
16 lastModified: Date.now() | 17 lastModified: Date.now() |
17 }; | 18 }; |
18 | 19 |
19 QUnit.module("IO tests", { | 20 let testEdge = info.platform == "edgehtml" ? QUnit.test : QUnit.skip; |
| 21 |
| 22 QUnit.module("Edge filter storage", { |
20 beforeEach() | 23 beforeEach() |
21 { | 24 { |
22 return prePopulateStorage(); | 25 return prePopulateStorage(); |
23 }, | 26 }, |
24 afterEach() | 27 afterEach() |
25 { | 28 { |
26 return clearStorage(); | 29 return clearStorage(); |
27 } | 30 } |
28 }); | 31 }); |
29 | 32 |
30 test("statFile", assert => | 33 function prePopulateStorage() |
| 34 { |
| 35 return new Promise(resolve => |
| 36 { |
| 37 let db; |
| 38 let req = indexedDB.open("adblockplus", 1); |
| 39 |
| 40 req.onsuccess = event => |
| 41 { |
| 42 db = event.currentTarget.result; |
| 43 let store = db |
| 44 .transaction(["file"], "readwrite") |
| 45 .objectStore("file"); |
| 46 |
| 47 store.put(testData).onsuccess = () => resolve(); |
| 48 }; |
| 49 }); |
| 50 } |
| 51 |
| 52 function clearStorage() |
| 53 { |
| 54 return new Promise(resolve => |
| 55 { |
| 56 let db; |
| 57 let req = indexedDB.open("adblockplus", 1); |
| 58 |
| 59 req.onsuccess = event => |
| 60 { |
| 61 db = event.currentTarget.result; |
| 62 let files = Object.values(testFileNames) |
| 63 .map(fileName => new Promise((resolveFile, reject) => |
| 64 { |
| 65 let store = db |
| 66 .transaction(["file"], "readwrite") |
| 67 .objectStore("file"); |
| 68 |
| 69 store.delete("file:" + fileName).onsuccess = resolveFile; |
| 70 })); |
| 71 |
| 72 Promise.all(files).then(resolve); |
| 73 }; |
| 74 }); |
| 75 } |
| 76 |
| 77 testEdge("statFile", assert => testStatFile(assert)); |
| 78 testEdge("readFromFile", assert => testReadFromFile(assert)); |
| 79 testEdge("writeToFile", assert => testWriteToFile(assert)); |
| 80 testEdge("renameFile", assert => testRenameFile(assert)); |
| 81 |
| 82 function testStatFile(assert) |
31 { | 83 { |
32 const noFileMsg = "returns correct value if file doesn't exist"; | 84 const noFileMsg = "returns correct value if file doesn't exist"; |
33 const fileExistsMsg = "returns correct value if file exists"; | 85 const fileExistsMsg = "returns correct value if file exists"; |
34 | 86 |
35 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise, | 87 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise, |
36 "returns a promise"); | 88 "returns a promise"); |
37 | 89 |
38 asyncReadHelper( | 90 asyncReadHelper( |
39 IO.statFile, | 91 IO.statFile, |
40 testFileNames.testData, | 92 testFileNames.testData, |
41 {exists: true, lastModified: testData.lastModified}, | 93 {exists: true, lastModified: testData.lastModified}, |
42 fileExistsMsg, | 94 fileExistsMsg, |
43 assert); | 95 assert); |
44 | 96 |
45 asyncReadHelper( | 97 asyncReadHelper( |
46 IO.statFile, | 98 IO.statFile, |
47 testFileNames.simpleCheck, | 99 testFileNames.simpleCheck, |
48 {exists: false}, | 100 {exists: false}, |
49 noFileMsg, | 101 noFileMsg, |
50 assert); | 102 assert); |
51 }); | |
52 | |
53 test("writeToFile", assert => | |
54 { | |
55 ok(IO.writeToFile(testFileNames.simpleCheck, ["test"]) instanceof Promise, | |
56 "returns a promise"); | |
57 | |
58 writesCorrectValue(assert); | |
59 }); | |
60 | |
61 function writesCorrectValue(assert) | |
62 { | |
63 const writeCheck = { | |
64 fileName: "file:writeCheck", | |
65 content: [1, 2, 3], | |
66 lastModified: Date.now() | |
67 }; | |
68 let done = assert.async(); | |
69 | |
70 IO.writeToFile(testFileNames.write, writeCheck.content) | |
71 .then(() => readFromStorage(writeCheck.fileName)) | |
72 .then(result => | |
73 { | |
74 deepEqual( | |
75 Object.keys(writeCheck), | |
76 Object.keys(result), | |
77 "data is written in the correct format"); | |
78 | |
79 deepEqual( | |
80 writeCheck.content, | |
81 result.content, | |
82 "data has the correct content"); | |
83 done(); | |
84 }); | |
85 } | 103 } |
86 | 104 |
87 test("readFromFile", assert => | 105 function testReadFromFile(assert) |
88 { | 106 { |
89 const noFileMsg = "returns correct value if file doesn't exist"; | 107 const noFileMsg = "returns correct value if file doesn't exist"; |
90 | 108 |
91 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise, | 109 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise, |
92 "returns a promise"); | 110 "returns a promise"); |
93 | 111 |
94 asyncReadHelper( | 112 asyncReadHelper( |
95 IO.readFromFile, | 113 IO.readFromFile, |
96 testFileNames.read, | 114 testFileNames.read, |
97 {type: "NoSuchFile"}, | 115 {type: "NoSuchFile"}, |
98 noFileMsg, | 116 noFileMsg, |
99 assert | 117 assert |
100 ); | 118 ); |
101 | 119 |
102 callsListeners(assert); | 120 callsListeners(assert); |
103 }); | 121 } |
| 122 |
| 123 function asyncReadHelper(method, fileName, expectedValue, description, assert) |
| 124 { |
| 125 let done = assert.async(); |
| 126 |
| 127 method(fileName) |
| 128 .then(result => |
| 129 { |
| 130 deepEqual(result, expectedValue, description); |
| 131 done(); |
| 132 }) |
| 133 .catch(error => |
| 134 { |
| 135 deepEqual(error, expectedValue, description); |
| 136 done(); |
| 137 }); |
| 138 } |
104 | 139 |
105 function callsListeners(assert) | 140 function callsListeners(assert) |
106 { | 141 { |
107 let done = assert.async(); | 142 let done = assert.async(); |
108 let called = []; | 143 let called = []; |
109 | 144 |
110 IO.readFromFile(testFileNames.testData, (entry) => called.push(entry)) | 145 IO.readFromFile(testFileNames.testData, entry => called.push(entry)) |
111 .then(() => | 146 .then(() => |
112 { | 147 { |
113 deepEqual( | 148 deepEqual( |
114 called, | 149 called, |
115 testData.content, | 150 testData.content, |
116 "calls listeners with the correct values"); | 151 "calls listeners with the correct values"); |
117 done(); | 152 done(); |
118 }); | 153 }); |
119 } | 154 } |
120 | 155 |
121 test("renameFile", assert => | 156 function testWriteToFile(assert) |
| 157 { |
| 158 ok(IO.writeToFile(testFileNames.simpleCheck, ["test"]) instanceof Promise, |
| 159 "returns a promise"); |
| 160 |
| 161 writesCorrectValue(assert); |
| 162 } |
| 163 |
| 164 function writesCorrectValue(assert) |
| 165 { |
| 166 const writeCheck = { |
| 167 fileName: "file:writeCheck", |
| 168 content: [1, 2, 3], |
| 169 lastModified: Date.now() |
| 170 }; |
| 171 let done = assert.async(); |
| 172 |
| 173 IO.writeToFile(testFileNames.write, writeCheck.content) |
| 174 .then(() => readFromStorage(writeCheck.fileName)) |
| 175 .then(result => |
| 176 { |
| 177 deepEqual( |
| 178 Object.keys(writeCheck), |
| 179 Object.keys(result), |
| 180 "data is written in the correct format"); |
| 181 |
| 182 deepEqual( |
| 183 writeCheck.content, |
| 184 result.content, |
| 185 "data has the correct content"); |
| 186 done(); |
| 187 }); |
| 188 } |
| 189 |
| 190 function testRenameFile(assert) |
122 { | 191 { |
123 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise, | 192 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise, |
124 "returns a promise"); | 193 "returns a promise"); |
125 | 194 |
126 checkRename(assert); | 195 checkRename(assert); |
127 }); | 196 } |
128 | 197 |
129 function checkRename(assert) | 198 function checkRename(assert) |
130 { | 199 { |
131 let done = assert.async(); | 200 let done = assert.async(); |
132 const expected = { | 201 const expected = { |
133 fileName: "file:" + testFileNames.rename, | 202 fileName: "file:" + testFileNames.rename, |
134 content: testData.content, | 203 content: testData.content, |
135 lastModified: testData.lastModified | 204 lastModified: testData.lastModified |
136 }; | 205 }; |
137 | 206 |
138 IO.renameFile(testFileNames.testData, testFileNames.rename) | 207 IO.renameFile(testFileNames.testData, testFileNames.rename) |
139 .then(() => readFromStorage("file:" + testFileNames.rename)) | 208 .then(() => readFromStorage("file:" + testFileNames.rename)) |
140 .then(result => | 209 .then(result => |
141 { | 210 { |
142 deepEqual(result, expected, "overrites file"); | 211 deepEqual(result, expected, "overrites file"); |
143 done(); | 212 done(); |
144 }); | 213 }); |
145 } | 214 } |
146 | 215 |
147 function asyncReadHelper(method, fileName, expectedValue, description, assert) | |
148 { | |
149 let done = assert.async(); | |
150 method(fileName) | |
151 .then(result => | |
152 { | |
153 deepEqual(result, expectedValue, description); | |
154 done(); | |
155 }) | |
156 .catch(error => | |
157 { | |
158 deepEqual(error, expectedValue, description); | |
159 done(); | |
160 }); | |
161 } | |
162 | |
163 function readFromStorage(fileName) | 216 function readFromStorage(fileName) |
164 { | 217 { |
165 return new Promise(resolve => | 218 return new Promise(resolve => |
166 { | 219 { |
167 let db; | 220 let db; |
168 let req = indexedDB.open("adblockplus", 1); | 221 let req = indexedDB.open("adblockplus", 1); |
169 req.onsuccess = (event) => | 222 req.onsuccess = event => |
170 { | 223 { |
171 db = event.currentTarget.result; | 224 db = event.currentTarget.result; |
172 let store = db | 225 let store = db |
173 .transaction(["file"], "readwrite") | 226 .transaction(["file"], "readwrite") |
174 .objectStore("file"); | 227 .objectStore("file"); |
175 | 228 |
176 store.get(fileName).onsuccess = (evt => | 229 store.get(fileName).onsuccess = (evt => |
177 resolve(evt.currentTarget.result) | 230 resolve(evt.currentTarget.result) |
178 ); | 231 ); |
179 }; | 232 }; |
180 }); | 233 }); |
181 } | 234 } |
182 | |
183 function prePopulateStorage() | |
184 { | |
185 return new Promise(resolve => | |
186 { | |
187 let db; | |
188 let req = indexedDB.open("adblockplus", 1); | |
189 | |
190 req.onsuccess = (event) => | |
191 { | |
192 db = event.currentTarget.result; | |
193 let store = db | |
194 .transaction(["file"], "readwrite") | |
195 .objectStore("file"); | |
196 | |
197 store.put(testData).onsuccess = resolve; | |
198 }; | |
199 }); | |
200 } | |
201 | |
202 function clearStorage() | |
203 { | |
204 return new Promise(resolve => | |
205 { | |
206 let db; | |
207 let req = indexedDB.open("adblockplus", 1); | |
208 | |
209 req.onsuccess = (event) => | |
210 { | |
211 db = event.currentTarget.result; | |
212 let files = Object.keys(testFileNames) | |
213 .map(fileName => new Promise((resolveFile, reject) => | |
214 { | |
215 let store = db | |
216 .transaction(["file"], "readwrite") | |
217 .objectStore("file"); | |
218 | |
219 store.delete("file:" + fileName).onsuccess = resolveFile; | |
220 })); | |
221 | |
222 Promise.all(files).then(resolve); | |
223 }; | |
224 }); | |
225 } | |
226 } | 235 } |
227 | |
OLD | NEW |