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); |
18 | 22 |
19 chrome.test.getConfig(function(testConfig) { | 23 chrome.test.getConfig(function(testConfig) { |
20 function getURL(path) { | 24 function getURL(path) { |
21 return 'http://localhost:' + testConfig.testServer.port + '/' + path; | 25 return 'http://localhost:' + testConfig.testServer.port + '/' + path; |
22 } | 26 } |
23 | 27 |
24 var nextId = 0; | 28 var nextId = 0; |
25 function getNextId() { | 29 function getNextId() { |
26 return nextId++; | 30 return nextId++; |
27 } | 31 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 // callbackCompleted(); | 91 // callbackCompleted(); |
88 // } | 92 // } |
89 // downloads.onChanged.addListener(myListener); | 93 // downloads.onChanged.addListener(myListener); |
90 // downloads.download( | 94 // downloads.download( |
91 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, | 95 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'}, |
92 // chrome.test.callback(function(id) { | 96 // chrome.test.callback(function(id) { |
93 // chrome.test.assertEq(downloadId, id); | 97 // chrome.test.assertEq(downloadId, id); |
94 // })); | 98 // })); |
95 // }, | 99 // }, |
96 | 100 |
| 101 function downloadBlob() { |
| 102 // Test that we can begin a download for a blob. |
| 103 var downloadId = getNextId(); |
| 104 console.debug(downloadId); |
| 105 function getBlobURL(data, filename, callback) { |
| 106 var dirname = '' + Math.random(); |
| 107 function fileSystemError(operation, data) { |
| 108 return function(fileError) { |
| 109 callback(null, {operation: operation, |
| 110 data: data, |
| 111 code: fileError.code}); |
| 112 } |
| 113 } |
| 114 window.requestFileSystem(TEMPORARY, 5*1024*1024, function(fs) { |
| 115 fs.root.getDirectory(dirname, {create: true, exclusive: true}, |
| 116 function(dirEntry) { |
| 117 dirEntry.getFile(filename, {create: true, exclusive: true}, |
| 118 function(fileEntry) { |
| 119 fileEntry.createWriter(function(fileWriter) { |
| 120 fileWriter.onwriteend = function(e) { |
| 121 callback(fileEntry.toURL(), null); |
| 122 }; |
| 123 fileWriter.onerror = function(e) { |
| 124 callback(null, ('Write failed: ' + e.toString())); |
| 125 }; |
| 126 var bb = new window.BlobBuilder(); |
| 127 bb.append(data); |
| 128 fileWriter.write(bb.getBlob()); |
| 129 }, fileSystemError('createWriter')); |
| 130 }, fileSystemError('getFile', filename)); |
| 131 }, fileSystemError('getDirectory', dirname)); |
| 132 }, fileSystemError('requestFileSystem')); |
| 133 } |
| 134 |
| 135 getBlobURL('Lorem ipsum', downloadId + '.txt', |
| 136 chrome.test.callback(function(blobUrl, blobError) { |
| 137 if (blobError) |
| 138 throw blobError; |
| 139 console.debug(blobUrl); |
| 140 downloads.download( |
| 141 {'url': blobUrl}, |
| 142 chrome.test.callback(function(id) { |
| 143 console.debug(id); |
| 144 chrome.test.assertEq(downloadId, id); |
| 145 })); |
| 146 })); |
| 147 }, |
| 148 |
97 function downloadSimple() { | 149 function downloadSimple() { |
98 // Test that we can begin a download. | 150 // Test that we can begin a download. |
99 var downloadId = getNextId(); | 151 var downloadId = getNextId(); |
100 console.debug(downloadId); | 152 console.debug(downloadId); |
101 downloads.download( | 153 downloads.download( |
102 {'url': SAFE_FAST_URL}, | 154 {'url': SAFE_FAST_URL}, |
103 chrome.test.callback(function(id) { | 155 chrome.test.callback(function(id) { |
104 chrome.test.assertEq(downloadId, id); | 156 chrome.test.assertEq(downloadId, id); |
105 })); | 157 })); |
106 }, | 158 }, |
(...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
925 // Valid file URLs are valid URLs. | 977 // Valid file URLs are valid URLs. |
926 var downloadId = getNextId(); | 978 var downloadId = getNextId(); |
927 console.debug(downloadId); | 979 console.debug(downloadId); |
928 downloads.download( | 980 downloads.download( |
929 {'url': 'file:///'}, | 981 {'url': 'file:///'}, |
930 chrome.test.callback(function(id) { | 982 chrome.test.callback(function(id) { |
931 chrome.test.assertEq(downloadId, id); | 983 chrome.test.assertEq(downloadId, id); |
932 })); | 984 })); |
933 }, | 985 }, |
934 | 986 |
935 // TODO(benjhayden): Set up a test ftp server. | 987 function downloadInvalidURL7() { |
| 988 // Test that download() rejects javascript urls. |
| 989 downloads.download( |
| 990 {'url': 'javascript:document.write("hello");'}, |
| 991 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); |
| 992 }, |
| 993 |
| 994 function downloadInvalidURL8() { |
| 995 // Test that download() rejects javascript urls. |
| 996 downloads.download( |
| 997 {'url': 'javascript:return false;'}, |
| 998 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); |
| 999 }, |
| 1000 |
| 1001 function downloadInvalidURL9() { |
| 1002 // Test that download() rejects otherwise-valid URLs that fail the host |
| 1003 // permissions check. |
| 1004 downloads.download( |
| 1005 {'url': 'ftp://example.com/example.txt'}, |
| 1006 chrome.test.callbackFail(downloads.ERROR_INVALID_URL)); |
| 1007 }, |
| 1008 |
| 1009 // TODO(benjhayden): Set up a test ftp server, add ftp://localhost* to |
| 1010 // permissions, maybe update downloadInvalidURL9. |
936 // function downloadAllowFTPURLs() { | 1011 // function downloadAllowFTPURLs() { |
937 // // Valid ftp URLs are valid URLs. | 1012 // // Valid ftp URLs are valid URLs. |
938 // var downloadId = getNextId(); | 1013 // var downloadId = getNextId(); |
939 // console.debug(downloadId); | 1014 // console.debug(downloadId); |
940 // downloads.download( | 1015 // downloads.download( |
941 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'}, | 1016 // {'url': 'ftp://localhost:' + testConfig.testServer.port + '/'}, |
942 // chrome.test.callback(function(id) { | 1017 // chrome.test.callback(function(id) { |
943 // chrome.test.assertEq(downloadId, id); | 1018 // chrome.test.assertEq(downloadId, id); |
944 // })); | 1019 // })); |
945 // }, | 1020 // }, |
946 | 1021 |
947 function downloadInvalidURL7() { | |
948 // Test that download() rejects javascript urls. | |
949 downloads.download( | |
950 {'url': 'javascript:document.write("hello");'}, | |
951 chrome.test.callbackFail('net::ERR_ACCESS_DENIED')); | |
952 }, | |
953 | |
954 function downloadInvalidURL8() { | |
955 // Test that download() rejects javascript urls. | |
956 downloads.download( | |
957 {'url': 'javascript:return false;'}, | |
958 chrome.test.callbackFail('net::ERR_ACCESS_DENIED')); | |
959 }, | |
960 | |
961 function downloadInvalidMethod() { | 1022 function downloadInvalidMethod() { |
962 assertThrows(('Invalid value for argument 1. Property \'method\': ' + | 1023 assertThrows(('Invalid value for argument 1. Property \'method\': ' + |
963 'Value must be one of: [GET, POST].'), | 1024 'Value must be one of: [GET, POST].'), |
964 downloads.download, | 1025 downloads.download, |
965 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); | 1026 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); |
966 }, | 1027 }, |
967 | 1028 |
968 function downloadInvalidHeader() { | 1029 function downloadInvalidHeader() { |
969 // Test that download() disallows setting the Cookie header. | 1030 // Test that download() disallows setting the Cookie header. |
970 downloads.download( | 1031 downloads.download( |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1059 }, | 1120 }, |
1060 | 1121 |
1061 function callNotifyPass() { | 1122 function callNotifyPass() { |
1062 chrome.test.notifyPass(); | 1123 chrome.test.notifyPass(); |
1063 setTimeout(chrome.test.callback(function() { | 1124 setTimeout(chrome.test.callback(function() { |
1064 console.debug(''); | 1125 console.debug(''); |
1065 }), 0); | 1126 }), 0); |
1066 } | 1127 } |
1067 ]); | 1128 ]); |
1068 }); | 1129 }); |
OLD | NEW |