OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Dummy private APIs. | 6 * Dummy private APIs. |
7 */ | 7 */ |
8 var chrome; | 8 var chrome; |
9 | 9 |
10 /** | 10 /** |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 return new Promise(function(resolve, reject) { | 70 return new Promise(function(resolve, reject) { |
71 dir.getFile(filename, {create: true}, resolve, reject); | 71 dir.getFile(filename, {create: true}, resolve, reject); |
72 }); | 72 }); |
73 } | 73 } |
74 })).then(function() { return dir; }); | 74 })).then(function() { return dir; }); |
75 } | 75 } |
76 | 76 |
77 /** | 77 /** |
78 * Verifies that scanning an empty filesystem produces an empty list. | 78 * Verifies that scanning an empty filesystem produces an empty list. |
79 */ | 79 */ |
80 function testEmptyList(errorIf) { | 80 function testEmptyList(callback) { |
81 var scanner = new MediaScanner([]); | 81 var scanner = new MediaScanner([]); |
82 scanner.getFiles().then(function(files) { | 82 reportPromise( |
83 errorIf(files.length !== 0); | 83 scanner.getFiles().then(function(files) { |
84 }); | 84 assertEquals(0, files.length); |
| 85 }), |
| 86 callback); |
85 } | 87 } |
86 | 88 |
87 /** | 89 /** |
88 * Verifies that scanning a simple single-level directory structure works. | 90 * Verifies that scanning a simple single-level directory structure works. |
89 */ | 91 */ |
90 function testSingleLevel(errorIf) { | 92 function testSingleLevel(callback) { |
91 var filenames = [ | 93 var filenames = [ |
92 'foo', | 94 'foo', |
93 'foo.jpg', | 95 'foo.jpg', |
94 'bar.gif', | 96 'bar.gif', |
95 'baz.avi', | 97 'baz.avi', |
96 'foo.mp3', | 98 'foo.mp3', |
97 'bar.txt' | 99 'bar.txt' |
98 ]; | 100 ]; |
99 var expectedFiles = [ | 101 var expectedFiles = [ |
100 '/testSingleLevel/foo.jpg', | 102 '/testSingleLevel/foo.jpg', |
101 '/testSingleLevel/bar.gif', | 103 '/testSingleLevel/bar.gif', |
102 '/testSingleLevel/baz.avi' | 104 '/testSingleLevel/baz.avi' |
103 ]; | 105 ]; |
104 makeTestFilesystemRoot('testSingleLevel') | 106 reportPromise( |
105 .then(populateDir.bind(null, filenames)) | 107 makeTestFilesystemRoot('testSingleLevel') |
106 .then( | 108 .then(populateDir.bind(null, filenames)) |
107 /** | 109 .then( |
108 * Scans the directory. | 110 /** |
109 * @param {!DirectoryEntry} root | 111 * Scans the directory. |
110 */ | 112 * @param {!DirectoryEntry} root |
111 function(root) { | 113 */ |
112 var scanner = new MediaScanner([root]); | 114 function(root) { |
113 return scanner.getFiles(); | 115 var scanner = new MediaScanner([root]); |
114 }) | 116 return scanner.getFiles(); |
115 .then( | 117 }) |
116 /** | 118 .then( |
117 * Verifies the results of the media scan. | 119 /** |
118 * @param {!Array.<!FileEntry>} scanResults | 120 * Verifies the results of the media scan. |
119 */ | 121 * @param {!Array.<!FileEntry>} scanResults |
120 function(scanResults) { | 122 */ |
121 assertEquals(expectedFiles.length, scanResults.length); | 123 function(scanResults) { |
122 scanResults.forEach(function(result) { | 124 assertEquals(expectedFiles.length, scanResults.length); |
123 // Verify that the scanner only returns files. | 125 scanResults.forEach(function(result) { |
124 assertTrue(result.isFile, result.fullPath + ' is not a file'); | 126 // Verify that the scanner only returns files. |
125 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, | 127 assertTrue(result.isFile, result.fullPath + ' is not a file'); |
126 result.fullPath + ' not found in control set'); | 128 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, |
127 }); | 129 result.fullPath + ' not found in control set'); |
128 // Signal test completion with no errors. | 130 }); |
129 errorIf(false); | 131 }), |
130 }) | 132 callback); |
131 .catch( | |
132 function(e) { | |
133 // Catch failures and print them. | |
134 console.error(e); | |
135 errorIf(e); | |
136 }); | |
137 } | 133 } |
138 | 134 |
139 function testMultiLevel(errorIf) { | 135 function testMultiLevel(callback) { |
140 var filenames = [ | 136 var filenames = [ |
141 'foo.jpg', | 137 'foo.jpg', |
142 'bar', | 138 'bar', |
143 [ | 139 [ |
144 'foo.0', | 140 'foo.0', |
145 'bar.0.jpg' | 141 'bar.0.jpg' |
146 ], | 142 ], |
147 [ | 143 [ |
148 'foo.1', | 144 'foo.1', |
149 'bar.1.gif', | 145 'bar.1.gif', |
150 [ | 146 [ |
151 'foo.1.0', | 147 'foo.1.0', |
152 'bar.1.0.avi' | 148 'bar.1.0.avi' |
153 ] | 149 ] |
154 ] | 150 ] |
155 ]; | 151 ]; |
156 var expectedFiles = [ | 152 var expectedFiles = [ |
157 '/testMultiLevel/foo.jpg', | 153 '/testMultiLevel/foo.jpg', |
158 '/testMultiLevel/foo.0/bar.0.jpg', | 154 '/testMultiLevel/foo.0/bar.0.jpg', |
159 '/testMultiLevel/foo.1/bar.1.gif', | 155 '/testMultiLevel/foo.1/bar.1.gif', |
160 '/testMultiLevel/foo.1/foo.1.0/bar.1.0.avi' | 156 '/testMultiLevel/foo.1/foo.1.0/bar.1.0.avi' |
161 ]; | 157 ]; |
162 | 158 |
163 makeTestFilesystemRoot('testMultiLevel') | 159 reportPromise( |
164 .then(populateDir.bind(null, filenames)) | 160 makeTestFilesystemRoot('testMultiLevel') |
165 .then( | 161 .then(populateDir.bind(null, filenames)) |
166 /** | 162 .then( |
167 * Scans the directory. | 163 /** |
168 * @param {!DirectoryEntry} root | 164 * Scans the directory. |
169 */ | 165 * @param {!DirectoryEntry} root |
170 function(root) { | 166 */ |
171 var scanner = new MediaScanner([root]); | 167 function(root) { |
172 return scanner.getFiles(); | 168 var scanner = new MediaScanner([root]); |
173 }) | 169 return scanner.getFiles(); |
174 .then( | 170 }) |
175 /** | 171 .then( |
176 * Verifies the results of the media scan. | 172 /** |
177 * @param {!Array.<!FileEntry>} scanResults | 173 * Verifies the results of the media scan. |
178 */ | 174 * @param {!Array.<!FileEntry>} scanResults |
179 function(scanResults) { | 175 */ |
180 assertEquals(expectedFiles.length, scanResults.length); | 176 function(scanResults) { |
181 scanResults.forEach(function(result) { | 177 assertEquals(expectedFiles.length, scanResults.length); |
182 // Verify that the scanner only returns files. | 178 scanResults.forEach(function(result) { |
183 assertTrue(result.isFile, result.fullPath + ' is not a file'); | 179 // Verify that the scanner only returns files. |
184 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, | 180 assertTrue(result.isFile, result.fullPath + ' is not a file'); |
185 result.fullPath + ' not found in control set'); | 181 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, |
186 }); | 182 result.fullPath + ' not found in control set'); |
187 // Signal test completion with no errors. | 183 }); |
188 errorIf(false); | 184 }), |
189 }) | 185 callback); |
190 .catch( | |
191 function(e) { | |
192 // Catch failures and print them. | |
193 console.error(e); | |
194 errorIf(e); | |
195 }); | |
196 | |
197 errorIf(false); | |
198 } | 186 } |
199 | 187 |
200 function testMultipleDirectories(errorIf) { | 188 function testMultipleDirectories(callback) { |
201 var filenames = [ | 189 var filenames = [ |
202 'foo', | 190 'foo', |
203 'bar', | 191 'bar', |
204 [ | 192 [ |
205 'foo.0', | 193 'foo.0', |
206 'bar.0.jpg' | 194 'bar.0.jpg' |
207 ], | 195 ], |
208 [ | 196 [ |
209 'foo.1', | 197 'foo.1', |
210 'bar.1.jpg', | 198 'bar.1.jpg', |
211 ] | 199 ] |
212 ]; | 200 ]; |
213 // Expected file paths from the scan. We're scanning the two subdirectories | 201 // Expected file paths from the scan. We're scanning the two subdirectories |
214 // only. | 202 // only. |
215 var expectedFiles = [ | 203 var expectedFiles = [ |
216 '/testMultipleDirectories/foo.0/bar.0.jpg', | 204 '/testMultipleDirectories/foo.0/bar.0.jpg', |
217 '/testMultipleDirectories/foo.1/bar.1.jpg' | 205 '/testMultipleDirectories/foo.1/bar.1.jpg' |
218 ]; | 206 ]; |
219 | 207 |
220 var getDirectory = function(root, dirname) { | 208 var getDirectory = function(root, dirname) { |
221 return new Promise(function(resolve, reject) { | 209 return new Promise(function(resolve, reject) { |
222 root.getDirectory( | 210 root.getDirectory( |
223 dirname, {create: false}, resolve, reject); | 211 dirname, {create: false}, resolve, reject); |
224 }); | 212 }); |
225 }; | 213 }; |
226 makeTestFilesystemRoot('testMultipleDirectories') | 214 |
227 .then(populateDir.bind(null, filenames)) | 215 reportPromise( |
228 .then( | 216 makeTestFilesystemRoot('testMultipleDirectories') |
229 /** | 217 .then(populateDir.bind(null, filenames)) |
230 * Scans the directories. | 218 .then( |
231 * @param {!DirectoryEntry} root | 219 /** |
232 */ | 220 * Scans the directories. |
233 function(root) { | 221 * @param {!DirectoryEntry} root |
234 return Promise.all(['foo.0', 'foo.1'].map( | 222 */ |
235 getDirectory.bind(null, root))).then( | 223 function(root) { |
236 function(directories) { | 224 return Promise.all(['foo.0', 'foo.1'].map( |
237 var scanner = new MediaScanner(directories); | 225 getDirectory.bind(null, root))).then( |
238 return scanner.getFiles(); | 226 function(directories) { |
239 }); | 227 var scanner = new MediaScanner(directories); |
240 }) | 228 return scanner.getFiles(); |
241 .then( | 229 }); |
242 /** | 230 }) |
243 * Verifies the results of the media scan. | 231 .then( |
244 * @param {!Array.<!FileEntry>} scanResults | 232 /** |
245 */ | 233 * Verifies the results of the media scan. |
246 function(scanResults) { | 234 * @param {!Array.<!FileEntry>} scanResults |
247 assertEquals(expectedFiles.length, scanResults.length); | 235 */ |
248 scanResults.forEach(function(result) { | 236 function(scanResults) { |
249 // Verify that the scanner only returns files. | 237 assertEquals(expectedFiles.length, scanResults.length); |
250 assertTrue(result.isFile, result.fullPath + ' is not a file'); | 238 scanResults.forEach(function(result) { |
251 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, | 239 // Verify that the scanner only returns files. |
252 result.fullPath + ' not found in control set'); | 240 assertTrue(result.isFile, result.fullPath + ' is not a file'); |
253 }); | 241 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, |
254 // Signal test completion with no errors. | 242 result.fullPath + ' not found in control set'); |
255 errorIf(false); | 243 }); |
256 }) | 244 }), |
257 .catch( | 245 callback); |
258 function(e) { | |
259 // Catch failures and print them. | |
260 console.error(e); | |
261 errorIf(e); | |
262 }); | |
263 } | 246 } |
OLD | NEW |