OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 { |
| 4 const {IO} = require("io"); |
| 5 |
| 6 const testFileNames = { |
| 7 testData: "testData", |
| 8 simpleCheck: "simpleCheck", |
| 9 write: "writeCheck", |
| 10 read: "readCheck", |
| 11 rename: "renameCheck" |
| 12 }; |
| 13 const testData = { |
| 14 fileName: "file:" + testFileNames.testData, |
| 15 content: [1, 2, 3], |
| 16 lastModified: Date.now() |
| 17 }; |
| 18 |
| 19 QUnit.module("IO tests", { |
| 20 beforeEach() |
| 21 { |
| 22 return prePopulateStorage(); |
| 23 }, |
| 24 afterEach() |
| 25 { |
| 26 return clearStorage(); |
| 27 } |
| 28 }); |
| 29 |
| 30 test("statFile", assert => |
| 31 { |
| 32 const noFileMsg = "returns correct value if file doesn't exist"; |
| 33 const fileExistsMsg = "returns correct value if file exists"; |
| 34 |
| 35 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise, |
| 36 "returns a promise"); |
| 37 |
| 38 asyncReadHelper( |
| 39 IO.statFile, |
| 40 testFileNames.testData, |
| 41 {exists: true, lastModified: testData.lastModified}, |
| 42 fileExistsMsg, |
| 43 assert); |
| 44 |
| 45 asyncReadHelper( |
| 46 IO.statFile, |
| 47 testFileNames.simpleCheck, |
| 48 {exists: false}, |
| 49 noFileMsg, |
| 50 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 } |
| 86 |
| 87 test("readFromFile", assert => |
| 88 { |
| 89 const noFileMsg = "returns correct value if file doesn't exist"; |
| 90 |
| 91 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise, |
| 92 "returns a promise"); |
| 93 |
| 94 asyncReadHelper( |
| 95 IO.readFromFile, |
| 96 testFileNames.read, |
| 97 {type: "NoSuchFile"}, |
| 98 noFileMsg, |
| 99 assert |
| 100 ); |
| 101 |
| 102 callsListeners(assert); |
| 103 }); |
| 104 |
| 105 function callsListeners(assert) |
| 106 { |
| 107 let done = assert.async(); |
| 108 let called = []; |
| 109 |
| 110 IO.readFromFile(testFileNames.testData, (entry) => called.push(entry)) |
| 111 .then(() => |
| 112 { |
| 113 deepEqual( |
| 114 called, |
| 115 testData.content, |
| 116 "calls listeners with the correct values"); |
| 117 done(); |
| 118 }); |
| 119 } |
| 120 |
| 121 test("renameFile", assert => |
| 122 { |
| 123 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise, |
| 124 "returns a promise"); |
| 125 |
| 126 checkRename(assert); |
| 127 }); |
| 128 |
| 129 function checkRename(assert) |
| 130 { |
| 131 let done = assert.async(); |
| 132 const expected = { |
| 133 fileName: "file:" + testFileNames.rename, |
| 134 content: testData.content, |
| 135 lastModified: testData.lastModified |
| 136 }; |
| 137 |
| 138 IO.renameFile(testFileNames.testData, testFileNames.rename) |
| 139 .then(() => readFromStorage("file:" + testFileNames.rename)) |
| 140 .then(result => |
| 141 { |
| 142 deepEqual(result, expected, "overrites file"); |
| 143 done(); |
| 144 }); |
| 145 } |
| 146 |
| 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) |
| 164 { |
| 165 return new Promise(resolve => |
| 166 { |
| 167 let db; |
| 168 let req = indexedDB.open("adblockplus", 1); |
| 169 req.onsuccess = (event) => |
| 170 { |
| 171 db = event.currentTarget.result; |
| 172 let store = db |
| 173 .transaction(["file"], "readwrite") |
| 174 .objectStore("file"); |
| 175 |
| 176 store.get(fileName).onsuccess = (evt => |
| 177 resolve(evt.currentTarget.result) |
| 178 ); |
| 179 }; |
| 180 }); |
| 181 } |
| 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 } |
| 227 |
OLD | NEW |