Chromium Code Reviews| 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 // downloads api test | 5 // downloads api test |
| 6 // browser_tests.exe --gtest_filter=DownloadsApiTest.Downloads | 6 // browser_tests.exe --gtest_filter=DownloadsApiTest.Downloads |
| 7 | 7 |
| 8 // Comment this out to enable debugging. | 8 // Comment this out to enable debugging. |
| 9 console.debug = function() {}; | 9 console.debug = function() {}; |
| 10 | 10 |
| 11 function debugObject(obj) { | 11 function debugObject(obj) { |
| 12 for (var property in obj) { | 12 for (var property in obj) { |
| 13 console.debug(property + ': ' + obj[property]); | 13 console.debug(property + ': ' + obj[property]); |
| 14 } | 14 } |
| 15 } | 15 } |
| 16 | 16 |
| 17 var downloads = chrome.experimental.downloads; | 17 var downloads = chrome.experimental.downloads; |
| 18 window.requestFileSystem = (window.requestFileSystem || | |
| 19 window.webkitRequestFileSystem); | |
| 20 window.BlobBuilder = (window.BlobBuilder || | |
| 21 window.WebKitBlobBuilder || | |
| 22 window.MozBlobBuilder); | |
|
ericu
2012/05/03 17:14:55
Why MozBlobBuilder? This won't run on Firefox.
benjhayden
2012/05/03 19:38:35
Done.
| |
| 18 | 23 |
| 19 chrome.test.getConfig(function(testConfig) { | 24 chrome.test.getConfig(function(testConfig) { |
| 20 function getURL(path) { | 25 function getURL(path) { |
| 21 return 'http://localhost:' + testConfig.testServer.port + '/' + path; | 26 return 'http://localhost:' + testConfig.testServer.port + '/' + path; |
| 22 } | 27 } |
| 23 | 28 |
| 24 var nextId = 0; | 29 var nextId = 0; |
| 25 function getNextId() { | 30 function getNextId() { |
| 26 return nextId++; | 31 return nextId++; |
| 27 } | 32 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 // Test that we can begin a download. | 103 // Test that we can begin a download. |
| 99 var downloadId = getNextId(); | 104 var downloadId = getNextId(); |
| 100 console.debug(downloadId); | 105 console.debug(downloadId); |
| 101 downloads.download( | 106 downloads.download( |
| 102 {'url': SAFE_FAST_URL}, | 107 {'url': SAFE_FAST_URL}, |
| 103 chrome.test.callback(function(id) { | 108 chrome.test.callback(function(id) { |
| 104 chrome.test.assertEq(downloadId, id); | 109 chrome.test.assertEq(downloadId, id); |
| 105 })); | 110 })); |
| 106 }, | 111 }, |
| 107 | 112 |
| 113 function downloadBlob() { | |
| 114 // Test that we can begin a download for a blob. | |
| 115 var downloadId = getNextId(); | |
| 116 console.debug(downloadId); | |
| 117 function getBlobURL(data, filename, callback) { | |
| 118 var dirname = '' + Math.random(); | |
| 119 function fileSystemError(operation, data) { | |
| 120 return function(fileError) { | |
| 121 callback(null, {operation: operation, | |
| 122 data: data, | |
| 123 code: fileError.code}); | |
| 124 } | |
| 125 } | |
| 126 window.requestFileSystem(TEMPORARY, 5*1024*1024, function(fs) { | |
| 127 fs.root.getDirectory(dirname, {create: true, exclusive: true}, | |
| 128 function(dirEntry) { | |
| 129 dirEntry.getFile(filename, {create: true, exclusive: true}, | |
| 130 function(fileEntry) { | |
| 131 fileEntry.createWriter(function(fileWriter) { | |
| 132 fileWriter.onwriteend = function(e) { | |
| 133 callback(fileEntry.toURL(), null); | |
| 134 }; | |
| 135 fileWriter.onerror = function(e) { | |
| 136 callback(null, ('Write failed: ' + e.toString())); | |
| 137 }; | |
| 138 var bb = new window.BlobBuilder(); | |
| 139 bb.append(data); | |
| 140 fileWriter.write(bb.getBlob()); | |
| 141 }, fileSystemError('createWriter')); | |
| 142 }, fileSystemError('getFile', filename)); | |
| 143 }, fileSystemError('getDirectory', dirname)); | |
| 144 }, fileSystemError('requestFileSystem')); | |
| 145 } | |
| 146 | |
| 147 getBlobURL('Lorem ipsum', downloadId + '.txt', | |
| 148 chrome.test.callback(function(blobUrl, blobError) { | |
| 149 if (blobError) | |
| 150 throw blobError; | |
| 151 console.debug(blobUrl); | |
| 152 downloads.download( | |
| 153 {'url': blobUrl}, | |
| 154 chrome.test.callback(function(id) { | |
| 155 console.debug(id); | |
| 156 chrome.test.assertEq(downloadId, id); | |
| 157 })); | |
| 158 })); | |
| 159 }, | |
| 160 | |
| 108 function downloadOnChanged() { | 161 function downloadOnChanged() { |
| 109 // Test that download completion is detectable by an onChanged event | 162 // Test that download completion is detectable by an onChanged event |
| 110 // listener. | 163 // listener. |
| 111 var downloadId = getNextId(); | 164 var downloadId = getNextId(); |
| 112 console.debug(downloadId); | 165 console.debug(downloadId); |
| 113 var callbackCompleted = chrome.test.callbackAdded(); | 166 var callbackCompleted = chrome.test.callbackAdded(); |
| 114 function myListener(delta) { | 167 function myListener(delta) { |
| 115 console.debug(delta.id); | 168 console.debug(delta.id); |
| 116 if ((delta.id != downloadId) || | 169 if ((delta.id != downloadId) || |
| 117 !delta.state) | 170 !delta.state) |
| (...skipping 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 723 // Valid file URLs are valid URLs. | 776 // Valid file URLs are valid URLs. |
| 724 var downloadId = getNextId(); | 777 var downloadId = getNextId(); |
| 725 console.debug(downloadId); | 778 console.debug(downloadId); |
| 726 downloads.download( | 779 downloads.download( |
| 727 {'url': 'file:///'}, | 780 {'url': 'file:///'}, |
| 728 chrome.test.callback(function(id) { | 781 chrome.test.callback(function(id) { |
| 729 chrome.test.assertEq(downloadId, id); | 782 chrome.test.assertEq(downloadId, id); |
| 730 })); | 783 })); |
| 731 }, | 784 }, |
| 732 | 785 |
| 733 // TODO(benjhayden): Set up a test ftp server. | 786 function downloadInvalidURL7() { |
| 787 // Test that download() rejects javascript urls. | |
| 788 downloads.download( | |
| 789 {'url': 'javascript:document.write("hello");'}, | |
| 790 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); | |
| 791 }, | |
| 792 | |
| 793 function downloadInvalidURL8() { | |
| 794 // Test that download() rejects javascript urls. | |
| 795 downloads.download( | |
| 796 {'url': 'javascript:return false;'}, | |
| 797 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); | |
| 798 }, | |
| 799 | |
| 800 function downloadInvalidURL9() { | |
| 801 // Test that download() rejects otherwise-valid URLs that fail the host | |
| 802 // permissions check. | |
| 803 downloads.download( | |
| 804 {'url': 'ftp://example.com/example.txt'}, | |
| 805 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); | |
| 806 }, | |
| 807 | |
| 808 // TODO(benjhayden): Set up a test ftp server, add ftp://localhost* to | |
| 809 // permissions, maybe update downloadInvalidURL9. | |
| 734 // function downloadAllowFTPURLs() { | 810 // function downloadAllowFTPURLs() { |
| 735 // // Valid ftp URLs are valid URLs. | 811 // // Valid ftp URLs are valid URLs. |
| 736 // var downloadId = getNextId(); | 812 // var downloadId = getNextId(); |
| 737 // console.debug(downloadId); | 813 // console.debug(downloadId); |
| 738 // downloads.download( | 814 // downloads.download( |
| 739 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'}, | 815 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'}, |
| 740 // chrome.test.callback(function(id) { | 816 // chrome.test.callback(function(id) { |
| 741 // chrome.test.assertEq(downloadId, id); | 817 // chrome.test.assertEq(downloadId, id); |
| 742 // })); | 818 // })); |
| 743 // }, | 819 // }, |
| 744 | 820 |
| 745 function downloadInvalidURL7() { | |
| 746 // Test that download() rejects javascript urls. | |
| 747 downloads.download( | |
| 748 {'url': 'javascript:document.write("hello");'}, | |
| 749 chrome.test.callbackFail('net::ERR_ACCESS_DENIED')); | |
| 750 }, | |
| 751 | |
| 752 function downloadInvalidURL8() { | |
| 753 // Test that download() rejects javascript urls. | |
| 754 downloads.download( | |
| 755 {'url': 'javascript:return false;'}, | |
| 756 chrome.test.callbackFail('net::ERR_ACCESS_DENIED')); | |
| 757 }, | |
| 758 | |
| 759 function downloadInvalidMethod() { | 821 function downloadInvalidMethod() { |
| 760 assertThrows(('Invalid value for argument 1. Property \'method\': ' + | 822 assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
| 761 'Value must be one of: [GET, POST].'), | 823 'Value must be one of: [GET, POST].'), |
| 762 downloads.download, | 824 downloads.download, |
| 763 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); | 825 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
| 764 }, | 826 }, |
| 765 | 827 |
| 766 function downloadInvalidHeader() { | 828 function downloadInvalidHeader() { |
| 767 // Test that download() disallows setting the Cookie header. | 829 // Test that download() disallows setting the Cookie header. |
| 768 downloads.download( | 830 downloads.download( |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 857 }, | 919 }, |
| 858 | 920 |
| 859 function callNotifyPass() { | 921 function callNotifyPass() { |
| 860 chrome.test.notifyPass(); | 922 chrome.test.notifyPass(); |
| 861 setTimeout(chrome.test.callback(function() { | 923 setTimeout(chrome.test.callback(function() { |
| 862 console.debug(''); | 924 console.debug(''); |
| 863 }), 0); | 925 }), 0); |
| 864 } | 926 } |
| 865 ]); | 927 ]); |
| 866 }); | 928 }); |
| OLD | NEW |