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 'use strict'; |
| 6 |
| 7 var fileSystemId; |
| 8 var fileSystem; |
| 9 |
| 10 /** |
| 11 * @type {Object} |
| 12 * @const |
| 13 */ |
| 14 var TESTING_ROOT = Object.freeze({ |
| 15 isDirectory: true, |
| 16 name: '', |
| 17 size: 0, |
| 18 modificationTime: new Date(2013, 3, 27, 9, 38, 14) |
| 19 }); |
| 20 |
| 21 /** |
| 22 * @type {Object} |
| 23 * @const |
| 24 */ |
| 25 var TESTING_FILE = Object.freeze({ |
| 26 isDirectory: false, |
| 27 name: 'tiramisu.txt', |
| 28 size: 4096, |
| 29 modificationTime: new Date(2014, 4, 28, 10, 39, 15) |
| 30 }); |
| 31 |
| 32 /** |
| 33 * Returns metadata for a requested entry. |
| 34 * |
| 35 * @param {number} inFileSystemId ID of the file system. |
| 36 * @param {string} entryPath Path of the requested entry. |
| 37 * @param {function(Object)} onSuccess Success callback with metadata passed |
| 38 * an argument. |
| 39 * @param {function(string}} onError Error callback with an error code. |
| 40 */ |
| 41 function onGetMetadataRequested( |
| 42 inFileSystemId, entryPath, onSuccess, onError) { |
| 43 if (inFileSystemId != fileSystemId) { |
| 44 onError('SECURITY_ERROR'); // enum ProviderError. |
| 45 return; |
| 46 } |
| 47 |
| 48 if (entryPath == '/') { |
| 49 onSuccess(TESTING_ROOT); |
| 50 return; |
| 51 } |
| 52 |
| 53 if (entryPath == '/' + TESTING_FILE.name) { |
| 54 onSuccess(TESTING_FILE); |
| 55 return; |
| 56 } |
| 57 |
| 58 onError('NOT_FOUND'); // enum ProviderError. |
| 59 } |
| 60 |
| 61 /** |
| 62 * Sets up the tests. Called once per all test cases. In case of a failure, |
| 63 * the callback is not called. |
| 64 * |
| 65 * @param {function()} callback Success callback. |
| 66 */ |
| 67 function setUp(callback) { |
| 68 chrome.fileSystemProvider.mount('chocolate.zip', function(id) { |
| 69 fileSystemId = id; |
| 70 chrome.fileSystemProvider.onGetMetadataRequested.addListener( |
| 71 onGetMetadataRequested); |
| 72 var volumeId = |
| 73 'provided:' + chrome.runtime.id + '-' + fileSystemId + '-user'; |
| 74 |
| 75 chrome.fileBrowserPrivate.requestFileSystem( |
| 76 volumeId, |
| 77 function(inFileSystem) { |
| 78 chrome.test.assertTrue(!!inFileSystem); |
| 79 |
| 80 fileSystem = inFileSystem; |
| 81 callback(); |
| 82 }); |
| 83 }, function() { |
| 84 chrome.test.fail(); |
| 85 }); |
| 86 } |
| 87 |
| 88 /** |
| 89 * Runs all of the test cases, one by one. |
| 90 */ |
| 91 function runTests() { |
| 92 chrome.test.runTests([ |
| 93 // Read metadata of the root. |
| 94 function getFileMetadataSuccess() { |
| 95 var onSuccess = chrome.test.callbackPass(function() {}); |
| 96 fileSystem.root.getMetadata( |
| 97 function(metadata) { |
| 98 chrome.test.assertEq(TESTING_ROOT.size, metadata.size); |
| 99 chrome.test.assertEq( |
| 100 TESTING_ROOT.modificationTime.toString(), |
| 101 metadata.modificationTime.toString()); |
| 102 onSuccess(); |
| 103 }, function(error) { |
| 104 chrome.test.fail(error.name); |
| 105 }); |
| 106 }, |
| 107 // Read metadata of an existing testing file. |
| 108 function getFileMetadataSuccess() { |
| 109 var onSuccess = chrome.test.callbackPass(function() {}); |
| 110 fileSystem.root.getFile( |
| 111 TESTING_FILE.name, |
| 112 {create: false}, |
| 113 function(fileEntry) { |
| 114 chrome.test.assertEq(TESTING_FILE.name, fileEntry.name); |
| 115 chrome.test.assertEq( |
| 116 TESTING_FILE.isDirectory, fileEntry.isDirectory); |
| 117 fileEntry.getMetadata(function(metadata) { |
| 118 chrome.test.assertEq(TESTING_FILE.size, metadata.size); |
| 119 chrome.test.assertEq( |
| 120 TESTING_FILE.modificationTime.toString(), |
| 121 metadata.modificationTime.toString()); |
| 122 onSuccess(); |
| 123 }, function(error) { |
| 124 chrome.test.fail(error.name); |
| 125 }); |
| 126 }, |
| 127 function(error) { |
| 128 chrome.test.fail(error.name); |
| 129 }); |
| 130 }, |
| 131 // Read metadata of a directory which does not exist, what should return an |
| 132 // error. DirectoryEntry.getDirectory() causes fetching metadata. |
| 133 function getFileMetadataNotFound() { |
| 134 var onSuccess = chrome.test.callbackPass(function() {}); |
| 135 fileSystem.root.getDirectory( |
| 136 'cranberries', |
| 137 {create: false}, |
| 138 function(dirEntry) { |
| 139 chrome.test.fail(); |
| 140 }, |
| 141 function(error) { |
| 142 chrome.test.assertEq('NotFoundError', error.name); |
| 143 onSuccess(); |
| 144 }); |
| 145 }, |
| 146 // Read metadata of a file using getDirectory(). An error should be returned |
| 147 // because of type mismatching. DirectoryEntry.getDirectory() causes |
| 148 // fetching metadata. |
| 149 function getFileMetadataWrongType() { |
| 150 var onSuccess = chrome.test.callbackPass(function() {}); |
| 151 fileSystem.root.getDirectory( |
| 152 TESTING_FILE.name, |
| 153 {create: false}, |
| 154 function(fileEntry) { |
| 155 chrome.test.fail(); |
| 156 }, |
| 157 function(error) { |
| 158 chrome.test.assertEq('TypeMismatchError', error.name); |
| 159 onSuccess(); |
| 160 }); |
| 161 } |
| 162 ]); |
| 163 } |
| 164 |
| 165 // Setup and run all of the test cases. |
| 166 setUp(runTests); |
OLD | NEW |