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

Delta Between Two Patch Sets: qunit/tests/ioIndexedDB.js

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

Powered by Google App Engine
This is Rietveld