Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: qunit/tests/ioIndexedDB.js

Issue 29823569: fixed ioIndexedDB tests (Closed)
Patch Set: Created July 5, 2018, 7:27 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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");
6
5 7
6 const testFileNames = { 8 const testFileNames = {
7 testData: "testData", 9 testData: "testData",
8 simpleCheck: "simpleCheck", 10 simpleCheck: "simpleCheck",
9 write: "writeCheck", 11 write: "writeCheck",
10 read: "readCheck", 12 read: "readCheck",
11 rename: "renameCheck" 13 rename: "renameCheck"
12 }; 14 };
13 const testData = { 15 const testData = {
14 fileName: "file:" + testFileNames.testData, 16 fileName: "file:" + testFileNames.testData,
15 content: [1, 2, 3], 17 content: [1, 2, 3],
16 lastModified: Date.now() 18 lastModified: Date.now()
17 }; 19 };
18 20
19 QUnit.module("IO tests", { 21 let isEdge = info.platform == "edgehtml";
22 let testEdge = isEdge ? QUnit.test : QUnit.skip;
23
24 QUnit.module("Edge filter storage", {
20 beforeEach() 25 beforeEach()
21 { 26 {
22 return prePopulateStorage(); 27 if (isEdge)
kzar 2018/07/05 09:54:15 I don't think this check (and the one in afterEach
piscoi.georgiana 2018/07/09 14:04:31 You are right, I don't know why I was under the im
kzar 2018/07/09 14:17:28 Acknowledged.
28 return prePopulateStorage();
23 }, 29 },
24 afterEach() 30 afterEach()
25 { 31 {
26 return clearStorage(); 32 if (isEdge)
33 return clearStorage();
27 } 34 }
28 }); 35 });
29 36
30 test("statFile", assert => 37 function prePopulateStorage()
38 {
39 return new Promise(resolve =>
40 {
41 let db;
42 let req = indexedDB.open("adblockplus", 1);
43
44 req.onsuccess = event =>
45 {
46 db = event.currentTarget.result;
47 let store = db
48 .transaction(["file"], "readwrite")
49 .objectStore("file");
50
51 store.put(testData).onsuccess = () => resolve();
52 };
53 });
54 }
55
56 function clearStorage()
57 {
58 return new Promise(resolve =>
59 {
60 let db;
61 let req = indexedDB.open("adblockplus", 1);
62
63 req.onsuccess = event =>
64 {
65 db = event.currentTarget.result;
66 let files = Object.values(testFileNames)
67 .map(fileName => new Promise((resolveFile, reject) =>
68 {
69 let store = db
70 .transaction(["file"], "readwrite")
71 .objectStore("file");
72
73 store.delete("file:" + fileName).onsuccess = resolveFile;
74 }));
75
76 Promise.all(files).then(resolve);
77 };
78 });
79 }
80
81 testEdge("statFile", assert => testStatFile(assert));
82 testEdge("readFromFile", assert => testReadFromFile(assert));
83 testEdge("writeToFile", assert => testWriteToFile(assert));
84 testEdge("renameFile", assert => testRenameFile(assert));
85
86 function testStatFile(assert)
31 { 87 {
32 const noFileMsg = "returns correct value if file doesn't exist"; 88 const noFileMsg = "returns correct value if file doesn't exist";
33 const fileExistsMsg = "returns correct value if file exists"; 89 const fileExistsMsg = "returns correct value if file exists";
34 90
35 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise, 91 ok(IO.statFile(testFileNames.simpleCheck) instanceof Promise,
36 "returns a promise"); 92 "returns a promise");
37 93
38 asyncReadHelper( 94 asyncReadHelper(
39 IO.statFile, 95 IO.statFile,
40 testFileNames.testData, 96 testFileNames.testData,
41 {exists: true, lastModified: testData.lastModified}, 97 {exists: true, lastModified: testData.lastModified},
42 fileExistsMsg, 98 fileExistsMsg,
43 assert); 99 assert);
44 100
45 asyncReadHelper( 101 asyncReadHelper(
46 IO.statFile, 102 IO.statFile,
47 testFileNames.simpleCheck, 103 testFileNames.simpleCheck,
48 {exists: false}, 104 {exists: false},
49 noFileMsg, 105 noFileMsg,
50 assert); 106 assert);
51 });
52
53 test("writeToFile", assert =>
kzar 2018/07/05 09:54:15 Seems like a bunch of unrelated changes here? I'd
piscoi.georgiana 2018/07/09 14:04:31 Sorry about that. In the future I'll leave this so
kzar 2018/07/09 14:17:28 Sure, no problem. FWIW I agree that it's good to i
piscoi.georgiana 2018/07/09 16:38:49 Done.
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 } 107 }
86 108
87 test("readFromFile", assert => 109 function testReadFromFile(assert)
88 { 110 {
89 const noFileMsg = "returns correct value if file doesn't exist"; 111 const noFileMsg = "returns correct value if file doesn't exist";
90 112
91 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise, 113 ok(IO.readFromFile(testFileNames.simpleCheck) instanceof Promise,
92 "returns a promise"); 114 "returns a promise");
93 115
94 asyncReadHelper( 116 asyncReadHelper(
95 IO.readFromFile, 117 IO.readFromFile,
96 testFileNames.read, 118 testFileNames.read,
97 {type: "NoSuchFile"}, 119 {type: "NoSuchFile"},
98 noFileMsg, 120 noFileMsg,
99 assert 121 assert
100 ); 122 );
101 123
102 callsListeners(assert); 124 callsListeners(assert);
103 }); 125 }
126
127 function asyncReadHelper(method, fileName, expectedValue, description, assert)
128 {
129 let done = assert.async();
130
131 method(fileName)
132 .then(result =>
133 {
134 deepEqual(result, expectedValue, description);
135 done();
136 })
137 .catch(error =>
138 {
139 deepEqual(error, expectedValue, description);
140 done();
141 });
142 }
104 143
105 function callsListeners(assert) 144 function callsListeners(assert)
106 { 145 {
107 let done = assert.async(); 146 let done = assert.async();
108 let called = []; 147 let called = [];
109 148
110 IO.readFromFile(testFileNames.testData, (entry) => called.push(entry)) 149 IO.readFromFile(testFileNames.testData, entry => called.push(entry))
111 .then(() => 150 .then(() =>
112 { 151 {
113 deepEqual( 152 deepEqual(
114 called, 153 called,
115 testData.content, 154 testData.content,
116 "calls listeners with the correct values"); 155 "calls listeners with the correct values");
117 done(); 156 done();
118 }); 157 });
119 } 158 }
120 159
121 test("renameFile", assert => 160 function testWriteToFile(assert)
161 {
162 ok(IO.writeToFile(testFileNames.simpleCheck, ["test"]) instanceof Promise,
163 "returns a promise");
164
165 writesCorrectValue(assert);
166 }
167
168 function writesCorrectValue(assert)
169 {
170 const writeCheck = {
171 fileName: "file:writeCheck",
172 content: [1, 2, 3],
173 lastModified: Date.now()
174 };
175 let done = assert.async();
176
177 IO.writeToFile(testFileNames.write, writeCheck.content)
178 .then(() => readFromStorage(writeCheck.fileName))
179 .then(result =>
180 {
181 deepEqual(
182 Object.keys(writeCheck),
183 Object.keys(result),
184 "data is written in the correct format");
185
186 deepEqual(
187 writeCheck.content,
188 result.content,
189 "data has the correct content");
190 done();
191 });
192 }
193
194 function testRenameFile(assert)
122 { 195 {
123 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise, 196 ok(IO.renameFile(testFileNames.simpleCheck) instanceof Promise,
124 "returns a promise"); 197 "returns a promise");
125 198
126 checkRename(assert); 199 checkRename(assert);
127 }); 200 }
128 201
129 function checkRename(assert) 202 function checkRename(assert)
130 { 203 {
131 let done = assert.async(); 204 let done = assert.async();
132 const expected = { 205 const expected = {
133 fileName: "file:" + testFileNames.rename, 206 fileName: "file:" + testFileNames.rename,
134 content: testData.content, 207 content: testData.content,
135 lastModified: testData.lastModified 208 lastModified: testData.lastModified
136 }; 209 };
137 210
138 IO.renameFile(testFileNames.testData, testFileNames.rename) 211 IO.renameFile(testFileNames.testData, testFileNames.rename)
139 .then(() => readFromStorage("file:" + testFileNames.rename)) 212 .then(() => readFromStorage("file:" + testFileNames.rename))
140 .then(result => 213 .then(result =>
141 { 214 {
142 deepEqual(result, expected, "overrites file"); 215 deepEqual(result, expected, "overrites file");
143 done(); 216 done();
144 }); 217 });
145 } 218 }
146 219
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) 220 function readFromStorage(fileName)
164 { 221 {
165 return new Promise(resolve => 222 return new Promise(resolve =>
166 { 223 {
167 let db; 224 let db;
168 let req = indexedDB.open("adblockplus", 1); 225 let req = indexedDB.open("adblockplus", 1);
169 req.onsuccess = (event) => 226 req.onsuccess = event =>
170 { 227 {
171 db = event.currentTarget.result; 228 db = event.currentTarget.result;
172 let store = db 229 let store = db
173 .transaction(["file"], "readwrite") 230 .transaction(["file"], "readwrite")
174 .objectStore("file"); 231 .objectStore("file");
175 232
176 store.get(fileName).onsuccess = (evt => 233 store.get(fileName).onsuccess = (evt =>
177 resolve(evt.currentTarget.result) 234 resolve(evt.currentTarget.result)
178 ); 235 );
179 }; 236 };
180 }); 237 });
181 } 238 }
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 } 239 }
227
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld