OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 // Directory that contains all the files used in tests. All files are created |
| 6 // and cleaned up on C++ side of the test. Initially, all files will have |
| 7 // content kTestFileContent. |
| 8 var kTestDirPath = 'mount/test_dir/'; |
| 9 // Test file that will remain unchanged during tests. Used to test read-only |
| 10 // functionality. |
| 11 var kTestFilePath = 'mount/test_dir/test_file.foo'; |
| 12 // File that will be used to test delete operation. |
| 13 var kTestFileToDelete = 'mount/test_dir/test_file_to_delete.foo'; |
| 14 // File that will be used to test write operations. |
| 15 var kMutableTestFile = 'mount/test_dir/mutable_test_file.foo'; |
| 16 // File that should be created while testing create operation. |
| 17 // Initialy does not exist. |
| 18 var kTestFileToCreate = 'mount/test_dir/new_test_file.foo'; |
| 19 // File that will be moved when testing move operation. |
| 20 var kTestFileToMove = 'mount/test_dir/test_file_to_move.foo'; |
| 21 // Data that will be written when testing write operation. |
| 22 var kWriteData = '!!!'; |
| 23 // Name of target file in copy operation test. |
| 24 // The file will be located in the test dir. |
| 25 var kCopiedFileName = 'copied_file.foo'; |
| 26 // Name of target file in move operation test. |
| 27 // The file will be located in the test dir. |
| 28 var kMovedFileName = 'moved_file.foo'; |
| 29 // Initial content of the test files. |
| 30 var kTestFileContent = "hello, world!"; |
| 31 |
| 32 function TestRunner() { |
| 33 this.fileSystem_ = undefined; |
| 34 |
| 35 // The entry set in |runGetDirTest|. |
| 36 this.testDirEntry = undefined; |
| 37 // Read only file under |kTestFilePath|. |
| 38 this.testFileEntry = undefined; |
| 39 // Last entry set in |runGetFileTest|. |
| 40 this.lastFileEntry = undefined; |
| 41 } |
| 42 |
| 43 // Sets |testFileEntry| to |lastFileEntry|. |
| 44 TestRunner.prototype.rememberLastFileEntry = function() { |
| 45 chrome.test.assertTrue(!!this.lastFileEntry, 'Cannot set null entry.'); |
| 46 this.testFileEntry = this.lastFileEntry; |
| 47 }; |
| 48 |
| 49 // Gets local filesystem used in tests. |
| 50 TestRunner.prototype.init = function() { |
| 51 // Get local FS. |
| 52 console.log('Requesting local file system...'); |
| 53 chrome.fileBrowserPrivate.requestLocalFileSystem( |
| 54 this.onFileSystemFetched_.bind(this)); |
| 55 }; |
| 56 |
| 57 TestRunner.prototype.onFileSystemFetched_ = function(fs) { |
| 58 chrome.test.assertTrue(!!fs, 'Error getting file system.'); |
| 59 |
| 60 this.fileSystem_ = fs; |
| 61 chrome.test.succeed(); |
| 62 }; |
| 63 |
| 64 TestRunner.prototype.runGetFileTest = function(filePath, create, callback) { |
| 65 self = this; |
| 66 this.fileSystem_.root.getFile(filePath, {create: create}, |
| 67 function(entry) { |
| 68 self.lastFileEntry = entry; |
| 69 callback('getting file', null); |
| 70 }, |
| 71 callback.bind(null, 'getting file')); |
| 72 }; |
| 73 |
| 74 TestRunner.prototype.runGetDirTest = function(filePath, create, callback) { |
| 75 self = this; |
| 76 this.fileSystem_.root.getDirectory(filePath, {create: create}, |
| 77 function(entry) { |
| 78 self.testDirEntry = entry; |
| 79 callback('getting directory', null); |
| 80 }, |
| 81 callback.bind(null, 'getting directory')); |
| 82 }; |
| 83 |
| 84 TestRunner.prototype.runReadFileTest = function(entry, expectedText, callback) { |
| 85 readFile(entry, |
| 86 function(text) { |
| 87 chrome.test.assertEq(expectedText, text, 'Unexpected file content'); |
| 88 callback('reading file', null); |
| 89 }, |
| 90 callback.bind(null, 'reading file')); |
| 91 }; |
| 92 |
| 93 TestRunner.prototype.runWriteFileTest = function(entry, callback) { |
| 94 var self = this; |
| 95 entry.createWriter( |
| 96 function(writer) { |
| 97 writer.onerror = function () { |
| 98 callback('writing to file', writer.error); |
| 99 }; |
| 100 writer.onwrite = function(e) { |
| 101 callback('writing to file', null); |
| 102 }; |
| 103 |
| 104 writer.write(new Blob([kWriteData], {'type': 'text/plain'})); |
| 105 }, |
| 106 callback.bind(null, 'creating writer')); |
| 107 }; |
| 108 |
| 109 TestRunner.prototype.runDeleteFileTest = function(entry, callback) { |
| 110 entry.remove( |
| 111 function() { |
| 112 callback('removing file', null); |
| 113 }, |
| 114 callback.bind(null, 'removing file')); |
| 115 }; |
| 116 |
| 117 TestRunner.prototype.runCopyFileTest = function(sourceEntry, |
| 118 targetDir, |
| 119 targetName, |
| 120 callback) { |
| 121 var self = this; |
| 122 sourceEntry.copyTo(targetDir, targetName, |
| 123 function (entry) { |
| 124 callback('copying file', null); |
| 125 }, |
| 126 callback.bind(null, 'copying file')); |
| 127 }; |
| 128 |
| 129 TestRunner.prototype.runMoveFileTest = function(sourceEntry, |
| 130 targetDir, |
| 131 targetName, |
| 132 callback) { |
| 133 var self = this; |
| 134 sourceEntry.moveTo(targetDir, targetName, |
| 135 function (entry) { |
| 136 callback('moving file', null); |
| 137 }, |
| 138 callback.bind(null, 'moving file')); |
| 139 }; |
| 140 |
| 141 function errorCodeToString(errorCode) { |
| 142 switch (errorCode) { |
| 143 case FileError.QUOTA_EXCEEDED_ERR: |
| 144 return 'QUOTA_EXCEEDED_ERR'; |
| 145 case FileError.NOT_FOUND_ERR: |
| 146 return 'NOT_FOUND_ERR'; |
| 147 case FileError.SECURITY_ERR: |
| 148 return 'SECURITY_ERR'; |
| 149 case FileError.INVALID_MODIFICATION_ERR: |
| 150 return 'INVALID_MODIFICATION_ERR'; |
| 151 case FileError.INVALID_STATE_ERR: |
| 152 return 'INVALID_STATE_ERR'; |
| 153 default: |
| 154 return 'Unknown Error'; |
| 155 } |
| 156 } |
| 157 |
| 158 // Checks operation results and ends the test. |
| 159 // If the oiperation is supposed to fail, |expectedErrorCode| should be null. |
| 160 // |error| is error returned by the operation. It is null if the operation |
| 161 // succeeded. |
| 162 // |testName| and |operation| parameters are used to make debugging strings |
| 163 // displayed on test failure clearer and more helpful. |
| 164 function verifyTestResult(testName, expectedErrorCode, operation, error) { |
| 165 if (!error && !expectedErrorCode) { |
| 166 chrome.test.succeed(); |
| 167 return; |
| 168 } |
| 169 |
| 170 if (expectedErrorCode) { |
| 171 chrome.test.assertTrue(!!error, |
| 172 testName + ' unexpectedly succeeded. ' + |
| 173 'Expected error: ' + errorCodeToString(expectedErrorCode)); |
| 174 } |
| 175 |
| 176 chrome.test.assertEq(expectedErrorCode, error.code, |
| 177 testName + ' got unexpected error.'+ |
| 178 'Expected error: ' + errorCodeToString(expectedErrorCode) + '. ' + |
| 179 'Got error' + errorCodeToString(error.code) + ' while ' + operation); |
| 180 |
| 181 chrome.test.succeed(); |
| 182 } |
| 183 |
| 184 chrome.test.runTests([ |
| 185 function initTests() { |
| 186 testRunner = new TestRunner(); |
| 187 testRunner.init(); |
| 188 }, |
| 189 function getFile() { |
| 190 testRunner.runGetFileTest(kTestFilePath, false, |
| 191 verifyTestResult.bind(null, 'Get Test File', null)); |
| 192 }, |
| 193 function saveFile() { |
| 194 testRunner.rememberLastFileEntry(); |
| 195 chrome.test.succeed(); |
| 196 }, |
| 197 function getDirectory() { |
| 198 testRunner.runGetDirTest(kTestDirPath, false, |
| 199 verifyTestResult.bind(null, 'Get Directory', null)); |
| 200 }, |
| 201 function readFile() { |
| 202 chrome.test.assertTrue(!!testRunner.testFileEntry, |
| 203 'Test file entry should have been created by previousd tests.'); |
| 204 testRunner.runReadFileTest(testRunner.testFileEntry, kTestFileContent, |
| 205 verifyTestResult.bind(null, 'Read File', null)); |
| 206 }, |
| 207 function copyFile() { |
| 208 chrome.test.assertTrue(!!testRunner.testFileEntry, |
| 209 'Test file entry should have been created by previousd tests.'); |
| 210 chrome.test.assertTrue(!!testRunner.testDirEntry, |
| 211 'Test dir entry should have been created by previousd tests.'); |
| 212 testRunner.runCopyFileTest( |
| 213 testRunner.testFileEntry, |
| 214 testRunner.testDirEntry, |
| 215 kCopiedFileName, |
| 216 verifyTestResult.bind(null, 'Copy File', FileError.SECURITY_ERR)); |
| 217 }, |
| 218 function getFileToMove() { |
| 219 testRunner.lastFileEntry = undefined; |
| 220 testRunner.runGetFileTest(kTestFileToMove, false, |
| 221 verifyTestResult.bind(null, 'Get File To Move', null)); |
| 222 }, |
| 223 function moveFile() { |
| 224 chrome.test.assertTrue(!!testRunner.lastFileEntry, |
| 225 'File entry to move should have been created by previousd tests.'); |
| 226 chrome.test.assertTrue(!!testRunner.testDirEntry, |
| 227 'Test dir entry should have been created by previousd tests.'); |
| 228 testRunner.runMoveFileTest( |
| 229 testRunner.lastFileEntry, |
| 230 testRunner.testDirEntry, |
| 231 kCopiedFileName, |
| 232 verifyTestResult.bind(null, 'Move File', FileError.SECURITY_ERR)); |
| 233 }, |
| 234 function getFileToWrite() { |
| 235 testRunner.lastFileEntry = undefined; |
| 236 testRunner.runGetFileTest(kMutableTestFile, false, |
| 237 verifyTestResult.bind(null, 'Get File To Write', null)); |
| 238 }, |
| 239 function writeFile() { |
| 240 chrome.test.assertTrue(!!testRunner.lastFileEntry, |
| 241 'File entry to write should have been created by previousd tests.'); |
| 242 testRunner.runWriteFileTest(testRunner.lastFileEntry, |
| 243 verifyTestResult.bind(null, 'Write File', FileError.SECURITY_ERR)); |
| 244 }, |
| 245 function createFile() { |
| 246 testRunner.runGetFileTest(kTestFileToCreate, true, |
| 247 verifyTestResult.bind(null, 'Create File', FileError.SECURITY_ERR)); |
| 248 }, |
| 249 function getFileToDelete() { |
| 250 testRunner.lastFileEntry = undefined; |
| 251 testRunner.runGetFileTest(kTestFileToDelete, false, |
| 252 verifyTestResult.bind(null, 'Get File To Delete', null)); |
| 253 }, |
| 254 function deleteFile() { |
| 255 chrome.test.assertTrue(!!testRunner.lastFileEntry, |
| 256 'File entry to delete should have been created by previousd tests.'); |
| 257 testRunner.runDeleteFileTest(testRunner.lastFileEntry, |
| 258 verifyTestResult.bind(null, 'Delete File', FileError.SECURITY_ERR)); |
| 259 } |
| 260 ]); |
OLD | NEW |