| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * Dummy private APIs. |
| 7 */ |
| 8 var chrome; |
| 9 |
| 10 /** |
| 11 * Callbacks registered by setTimeout. |
| 12 * @type {Array.<function>} |
| 13 */ |
| 14 var timeoutCallbacks; |
| 15 |
| 16 |
| 17 // Set up the test components. |
| 18 function setUp() { |
| 19 } |
| 20 |
| 21 /** |
| 22 * Creates a subdirectory within a temporary file system for testing. |
| 23 * @param {string} directoryName Name of the test directory to create. Must be |
| 24 * unique within this test suite. |
| 25 */ |
| 26 function makeTestFilesystemRoot(directoryName) { |
| 27 function makeTestFilesystem() { |
| 28 return new Promise(function(resolve, reject) { |
| 29 window.webkitRequestFileSystem( |
| 30 window.TEMPORARY, |
| 31 1024*1024, |
| 32 resolve, |
| 33 reject); |
| 34 }); |
| 35 } |
| 36 |
| 37 return makeTestFilesystem() |
| 38 .then( |
| 39 // Create a directory, pretend that's the root. |
| 40 function(fs) { |
| 41 return new Promise(function(resolve, reject) { |
| 42 fs.root.getDirectory( |
| 43 directoryName, |
| 44 { |
| 45 create: true, |
| 46 exclusive: true |
| 47 }, |
| 48 resolve, |
| 49 reject); |
| 50 }); |
| 51 }); |
| 52 } |
| 53 |
| 54 /** |
| 55 * Creates a set of files in the given directory. |
| 56 * @param {!Array<!Array|string>} filenames A (potentially nested) array of |
| 57 * strings, reflecting a directory structure. |
| 58 * @param <!DirectoryEntry> The root of the directory tree. |
| 59 */ |
| 60 function populateDir(filenames, dir) { |
| 61 return Promise.all( |
| 62 filenames.map(function(filename) { |
| 63 if (filename instanceof Array) { |
| 64 return new Promise(function(resolve, reject) { |
| 65 dir.getDirectory(filename[0], {create: true}, resolve, reject); |
| 66 }).then(populateDir.bind(null, filename)); |
| 67 } else { |
| 68 return new Promise(function(resolve, reject) { |
| 69 dir.getFile(filename, {create: true}, resolve, reject); |
| 70 }); |
| 71 } |
| 72 })); |
| 73 } |
| 74 |
| 75 /** |
| 76 * Verifies that scanning an empty filesystem produces an empty list. |
| 77 */ |
| 78 function testEmptyList(errorIf) { |
| 79 var scanner = new MediaScanner([]); |
| 80 scanner.getFiles().then(function(files) { |
| 81 errorIf(files.length !== 0); |
| 82 }); |
| 83 } |
| 84 |
| 85 /** |
| 86 * Verifies that scanning a simple single-level directory structure works. |
| 87 */ |
| 88 function testSingleLevel(errorIf) { |
| 89 var filenames = [ |
| 90 'foo', |
| 91 'foo.jpg', |
| 92 'bar.gif', |
| 93 'baz.avi', |
| 94 'foo.mp3', |
| 95 'bar.txt' |
| 96 ]; |
| 97 var expectedFiles = [ |
| 98 '/testSingleLevel/foo.jpg', |
| 99 '/testSingleLevel/bar.gif', |
| 100 '/testSingleLevel/baz.avi' |
| 101 ]; |
| 102 makeTestFilesystemRoot('testSingleLevel') |
| 103 .then( |
| 104 /** |
| 105 * Creates the test directory and populates it. |
| 106 * @param {!DirectoryEntry} root |
| 107 */ |
| 108 function(root) { |
| 109 populateDir(filenames, root); |
| 110 return root; |
| 111 }) |
| 112 .then( |
| 113 /** |
| 114 * Scans the directory. |
| 115 * @param {!DirectoryEntry} root |
| 116 */ |
| 117 function(root) { |
| 118 var scanner = new MediaScanner([root]); |
| 119 return scanner.getFiles(); |
| 120 }) |
| 121 .then( |
| 122 /** |
| 123 * Verifies the results of the media scan. |
| 124 * @param {!Array.<!FileEntry>} scanResults |
| 125 */ |
| 126 function(scanResults) { |
| 127 assertEquals(expectedFiles.length, scanResults.length); |
| 128 scanResults.forEach(function(result) { |
| 129 // Verify that the scanner only returns files. |
| 130 assertTrue(result.isFile, result.fullPath + ' is not a file'); |
| 131 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, |
| 132 result.fullPath + ' not found in control set'); |
| 133 }); |
| 134 // Signal test completion with no errors. |
| 135 errorIf(false); |
| 136 }) |
| 137 .catch( |
| 138 function(e) { |
| 139 // Catch failures and print them. |
| 140 console.error(e); |
| 141 errorIf(e); |
| 142 }); |
| 143 } |
| 144 |
| 145 function testMultiLevel(errorIf) { |
| 146 var filenames = [ |
| 147 'foo.jpg', |
| 148 'bar', |
| 149 [ |
| 150 'foo.0', |
| 151 'bar.0.jpg' |
| 152 ], |
| 153 [ |
| 154 'foo.1', |
| 155 'bar.1.gif', |
| 156 [ |
| 157 'foo.1.0', |
| 158 'bar.1.0.avi' |
| 159 ] |
| 160 ] |
| 161 ]; |
| 162 var expectedFiles = [ |
| 163 '/testMultiLevel/foo.jpg', |
| 164 '/testMultiLevel/foo.0/bar.0.jpg', |
| 165 '/testMultiLevel/foo.1/bar.1.gif', |
| 166 '/testMultiLevel/foo.1/foo.1.0/bar.1.0.avi' |
| 167 ]; |
| 168 |
| 169 makeTestFilesystemRoot('testMultiLevel') |
| 170 .then( |
| 171 /** |
| 172 * Creates the test directory and populates it. |
| 173 * @param {!DirectoryEntry} root |
| 174 */ |
| 175 function(root) { |
| 176 populateDir(filenames, root); |
| 177 return root; |
| 178 }) |
| 179 .then( |
| 180 /** |
| 181 * Scans the directory. |
| 182 * @param {!DirectoryEntry} root |
| 183 */ |
| 184 function(root) { |
| 185 var scanner = new MediaScanner([root]); |
| 186 return scanner.getFiles(); |
| 187 }) |
| 188 .then( |
| 189 /** |
| 190 * Verifies the results of the media scan. |
| 191 * @param {!Array.<!FileEntry>} scanResults |
| 192 */ |
| 193 function(scanResults) { |
| 194 assertEquals(expectedFiles.length, scanResults.length); |
| 195 scanResults.forEach(function(result) { |
| 196 // Verify that the scanner only returns files. |
| 197 assertTrue(result.isFile, result.fullPath + ' is not a file'); |
| 198 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, |
| 199 result.fullPath + ' not found in control set'); |
| 200 }); |
| 201 // Signal test completion with no errors. |
| 202 errorIf(false); |
| 203 }) |
| 204 .catch( |
| 205 function(e) { |
| 206 // Catch failures and print them. |
| 207 console.error(e); |
| 208 errorIf(e); |
| 209 }); |
| 210 |
| 211 errorIf(false); |
| 212 } |
| 213 |
| 214 function testMultipleDirectories(errorIf) { |
| 215 var filenames = [ |
| 216 'foo', |
| 217 'bar', |
| 218 [ |
| 219 'foo.0', |
| 220 'bar.0.jpg' |
| 221 ], |
| 222 [ |
| 223 'foo.1', |
| 224 'bar.1.jpg', |
| 225 ] |
| 226 ]; |
| 227 // Expected file paths from the scan. We're scanning the two subdirectories |
| 228 // only. |
| 229 var expectedFiles = [ |
| 230 '/testMultipleDirectories/foo.0/bar.0.jpg', |
| 231 '/testMultipleDirectories/foo.1/bar.1.jpg' |
| 232 ]; |
| 233 |
| 234 var getDirectory = function(root, dirname) { |
| 235 return new Promise(function(resolve, reject) { |
| 236 root.getDirectory( |
| 237 dirname, {create: false}, resolve, reject); |
| 238 }); |
| 239 }; |
| 240 makeTestFilesystemRoot('testMultipleDirectories') |
| 241 .then( |
| 242 /** |
| 243 * Creates the test directory and populates it. |
| 244 * @param {!DirectoryEntry} root |
| 245 */ |
| 246 function(root) { |
| 247 populateDir(filenames, root); |
| 248 return root; |
| 249 }) |
| 250 .then( |
| 251 /** |
| 252 * Scans the directories. |
| 253 * @param {!DirectoryEntry} root |
| 254 */ |
| 255 function(root) { |
| 256 return Promise.all(['foo.0', 'foo.1'].map( |
| 257 getDirectory.bind(null, root))).then( |
| 258 function(directories) { |
| 259 var scanner = new MediaScanner(directories); |
| 260 return scanner.getFiles(); |
| 261 }); |
| 262 }) |
| 263 .then( |
| 264 /** |
| 265 * Verifies the results of the media scan. |
| 266 * @param {!Array.<!FileEntry>} scanResults |
| 267 */ |
| 268 function(scanResults) { |
| 269 assertEquals(expectedFiles.length, scanResults.length); |
| 270 scanResults.forEach(function(result) { |
| 271 // Verify that the scanner only returns files. |
| 272 assertTrue(result.isFile, result.fullPath + ' is not a file'); |
| 273 assertTrue(expectedFiles.indexOf(result.fullPath) != -1, |
| 274 result.fullPath + ' not found in control set'); |
| 275 }); |
| 276 // Signal test completion with no errors. |
| 277 errorIf(false); |
| 278 }) |
| 279 .catch( |
| 280 function(e) { |
| 281 // Catch failures and print them. |
| 282 console.error(e); |
| 283 errorIf(e); |
| 284 }); |
| 285 } |
| OLD | NEW |