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