| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var EXTENSION_ID = 'kidcpjlbjdmcnmccjhjdckhbngnhnepk'; | 5 var EXTENSION_ID = 'kidcpjlbjdmcnmccjhjdckhbngnhnepk'; |
| 6 var FILE_CONTENTS = 'hello from test extension.'; | 6 var FILE_CONTENTS = 'hello from test extension.'; |
| 7 | 7 |
| 8 function errorCallback(error) { | 8 function errorCallback(error) { |
| 9 var msg = ''; | 9 var msg = ''; |
| 10 if (!error.code) { | 10 if (!error.code) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 break; | 28 break; |
| 29 default: | 29 default: |
| 30 msg = 'Unknown Error'; | 30 msg = 'Unknown Error'; |
| 31 break; | 31 break; |
| 32 }; | 32 }; |
| 33 } | 33 } |
| 34 console.log(msg); | 34 console.log(msg); |
| 35 chrome.test.fail(msg); | 35 chrome.test.fail(msg); |
| 36 } | 36 } |
| 37 | 37 |
| 38 function ensureFileExists(entry, successCallback, errorCallback) { |
| 39 entry.filesystem.root.getFile(entry.fullPath, |
| 40 {create: true}, |
| 41 successCallback, |
| 42 errorCallback); |
| 43 } |
| 44 |
| 38 function writeToFile(entry) { | 45 function writeToFile(entry) { |
| 39 entry.createWriter(function(writer) { | 46 entry.createWriter(function(writer) { |
| 40 writer.onerror = function(e) { | 47 writer.onerror = function(e) { |
| 41 errorCallback(writer.error); | 48 errorCallback(writer.error); |
| 42 }; | 49 }; |
| 43 writer.onwrite = chrome.test.succeed; | 50 writer.onwrite = chrome.test.succeed; |
| 44 | 51 |
| 45 var bb = new WebKitBlobBuilder(); | 52 var bb = new WebKitBlobBuilder(); |
| 46 bb.append(FILE_CONTENTS); | 53 bb.append(FILE_CONTENTS); |
| 47 writer.write(bb.getBlob('text/plain')); | 54 writer.write(bb.getBlob('text/plain')); |
| 48 }, errorCallback); | 55 }, errorCallback); |
| 49 } | 56 } |
| 50 | 57 |
| 51 chrome.test.runTests([ | 58 chrome.test.runTests([ |
| 52 function selectionSuccessful() { | 59 function selectionSuccessful() { |
| 53 // The test will call selectFile function and expect it to succeed. | 60 // The test will call selectFile function and expect it to succeed. |
| 54 // When it gets the file entry, it verifies that the permissions given in | 61 // When it gets the file entry, it verifies that the permissions given in |
| 55 // the method allow the extension to read/write to selected file. | 62 // the method allow the extension to read/write to selected file. |
| 56 chrome.fileBrowserHandler.selectFile({ suggestedName: 'some_file_name.txt'}, | 63 chrome.fileBrowserHandler.selectFile({ suggestedName: 'some_file_name.txt'}, |
| 57 function(result) { | 64 function(result) { |
| 58 chrome.test.assertTrue(!!result); | 65 chrome.test.assertTrue(!!result); |
| 59 chrome.test.assertTrue(result.success); | 66 chrome.test.assertTrue(result.success); |
| 60 chrome.test.assertTrue(!!result.entry); | 67 chrome.test.assertTrue(!!result.entry); |
| 61 | 68 |
| 62 writeToFile(result.entry); | 69 ensureFileExists(result.entry, writeToFile, errorCallback); |
| 63 }); | 70 }); |
| 64 }, | 71 }, |
| 65 function selectionFails() { | 72 function selectionFails() { |
| 66 // The test expects that selectFile returns failure with an empty entry. | 73 // The test expects that selectFile returns failure with an empty entry. |
| 67 chrome.fileBrowserHandler.selectFile({ suggestedName: 'fail' }, | 74 chrome.fileBrowserHandler.selectFile({ suggestedName: 'fail' }, |
| 68 function(result) { | 75 function(result) { |
| 69 chrome.test.assertTrue(!!result); | 76 chrome.test.assertTrue(!!result); |
| 70 // Entry should be set iff operation succeeded. | 77 // Entry should be set iff operation succeeded. |
| 71 chrome.test.assertEq(false, result.success); | 78 chrome.test.assertEq(false, result.success); |
| 72 chrome.test.assertTrue(result.entry == null); | 79 chrome.test.assertTrue(result.entry == null); |
| 73 chrome.test.succeed(); | 80 chrome.test.succeed(); |
| 74 }); | 81 }); |
| 75 }]); | 82 }]); |
| OLD | NEW |