OLD | NEW |
(Empty) | |
| 1 <script> |
| 2 |
| 3 /* |
| 4 This component extension test does the following: |
| 5 |
| 6 1. Creates a txt file on the local file system with some random text. |
| 7 2. Finds a registered task (file item context menu) and invokes it with url |
| 8 of the test file. |
| 9 3. Listens for a message from context menu handler and makes sure its payload |
| 10 matches the random text from the test file. |
| 11 */ |
| 12 |
| 13 // The ID of this extension. |
| 14 var fileBrowserExtensionId = "ddammdhioacbehjngdmkjcjbnfginlla"; |
| 15 |
| 16 var fileSystem = null; |
| 17 var testDirName = "tmp/test_dir_" + Math.floor(Math.random()*10000); |
| 18 var testFileName = "test_file_" + Math.floor(Math.random()*10000)+".txt"; |
| 19 var fileUrl = "filesystem:chrome-extension://" + fileBrowserExtensionId + |
| 20 "/local/" + testDirName + "/" + testFileName; |
| 21 |
| 22 var randomText = "random file text " + Math.floor(Math.random()*10000); |
| 23 |
| 24 function onFileSystemFetched(fs) { |
| 25 if (!fs) { |
| 26 errorCallback(chrome.extensions.lastError); |
| 27 return; |
| 28 } |
| 29 fileSystem = fs; |
| 30 console.log("DONE requesting local filesystem: " + fileSystem.name); |
| 31 console.log("Creating directory : " + testDirName); |
| 32 fileSystem.root.getDirectory(testDirName, {create:true}, |
| 33 directoryCreateCallback, errorCallback); |
| 34 } |
| 35 |
| 36 function directoryCreateCallback(directory) { |
| 37 console.log("DONE creating directory: " + directory.fullPath); |
| 38 directory.getFile(testFileName, {create:true}, fileCreatedCallback, |
| 39 errorCallback); |
| 40 } |
| 41 |
| 42 function fileCreatedCallback(fileEntry) { |
| 43 console.log("DONE creating file: " + fileEntry.fullPath); |
| 44 fileEntry.createWriter(onGetFileWriter); |
| 45 } |
| 46 |
| 47 function onGetFileWriter(writer) { |
| 48 // Start |
| 49 console.log("Got file writter"); |
| 50 writer.onerror = errorCallback; |
| 51 writer.onwrite = onFileWriteCompleted; |
| 52 var bb = new BlobBuilder(); |
| 53 bb.append(randomText); |
| 54 writer.write(bb.getBlob('text/plain')); |
| 55 } |
| 56 |
| 57 function onFileWriteCompleted(e) { |
| 58 // Start |
| 59 console.log("DONE writing file content"); |
| 60 console.log("Get registered tasks now..."); |
| 61 chrome.fileBrowserPrivate.getFileTasks([fileUrl], onGetTasks); |
| 62 |
| 63 } |
| 64 |
| 65 function onGetTasks(tasks) { |
| 66 console.log("Tasks: "); |
| 67 console.log(tasks); |
| 68 if (!tasks || !tasks.length) { |
| 69 chrome.test.fail("No tasks registered"); |
| 70 return; |
| 71 } |
| 72 console.log("DONE fetching tasks: " + tasks[0].taskId); |
| 73 chrome.fileBrowserPrivate.executeTask(tasks[0].taskId, [fileUrl]); |
| 74 } |
| 75 |
| 76 function errorCallback(e) { |
| 77 var msg = ''; |
| 78 if (!e.code) { |
| 79 msg = e.message; |
| 80 } else { |
| 81 switch (e.code) { |
| 82 case FileError.QUOTA_EXCEEDED_ERR: |
| 83 msg = 'QUOTA_EXCEEDED_ERR'; |
| 84 break; |
| 85 case FileError.NOT_FOUND_ERR: |
| 86 msg = 'NOT_FOUND_ERR'; |
| 87 break; |
| 88 case FileError.SECURITY_ERR: |
| 89 msg = 'SECURITY_ERR'; |
| 90 break; |
| 91 case FileError.INVALID_MODIFICATION_ERR: |
| 92 msg = 'INVALID_MODIFICATION_ERR'; |
| 93 break; |
| 94 case FileError.INVALID_STATE_ERR: |
| 95 msg = 'INVALID_STATE_ERR'; |
| 96 break; |
| 97 default: |
| 98 msg = 'Unknown Error'; |
| 99 break; |
| 100 }; |
| 101 } |
| 102 chrome.test.fail("Got unexpected error: " + msg); |
| 103 console.log('Error: ' + msg); |
| 104 alert('Error: ' + msg); |
| 105 } |
| 106 |
| 107 // For simple requests: |
| 108 chrome.extension.onRequestExternal.addListener( |
| 109 function(request, sender, sendResponse) { |
| 110 if (request.fileContent && request.fileContent == randomText) { |
| 111 sendResponse({success: true}); |
| 112 chrome.test.succeed(); |
| 113 } else { |
| 114 sendResponse({success: false}); |
| 115 console.log('Unexpected message received'); |
| 116 console.log(request); |
| 117 chrome.test.fail("Got unexpected message"); |
| 118 } |
| 119 }); |
| 120 |
| 121 chrome.test.runTests([function tab() { |
| 122 // Get local FS, create dir with a file in it. |
| 123 console.log("Requesting local file system..."); |
| 124 chrome.fileBrowserPrivate.requestLocalFileSystem(onFileSystemFetched); |
| 125 }]); |
| 126 |
| 127 |
| 128 </script> |
| 129 <html> |
| 130 <body><h2>chrome.fileBrowserPrivate.* tests</h2></body> |
| 131 </html> |
OLD | NEW |