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