Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: chrome/test/data/extensions/api_test/downloads/test.js

Issue 8203005: Implement chrome.experimental.downloads.onChanged (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: fix expected_body Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 var downloads = chrome.experimental.downloads; 8 var downloads = chrome.experimental.downloads;
9 9
10 chrome.test.getConfig(function(testConfig) { 10 chrome.test.getConfig(function(testConfig) {
(...skipping 24 matching lines...) Expand all
35 }); 35 });
36 func.apply(null, args); 36 func.apply(null, args);
37 } catch (exception) { 37 } catch (exception) {
38 chrome.test.assertEq(exceptionMessage, exception.message); 38 chrome.test.assertEq(exceptionMessage, exception.message);
39 chrome.test.succeed(); 39 chrome.test.succeed();
40 } 40 }
41 } 41 }
42 42
43 // The "/slow" handler waits a specified amount of time before returning a 43 // The "/slow" handler waits a specified amount of time before returning a
44 // safe file. Specify zero seconds to return quickly. 44 // safe file. Specify zero seconds to return quickly.
45 var SAFE_FAST_URL = getURL('slow?0'); 45 const SAFE_FAST_URL = getURL('slow?0');
46 var NEVER_FINISH_URL = getURL('download-known-size'); 46 const NEVER_FINISH_URL = getURL('download-known-size');
47 var ERROR_GENERIC = downloads.ERROR_GENERIC; 47 const POST_URL = 'files/post/downloads/a_zip_file.zip?expected_body=BODY'
48 var ERROR_INVALID_URL = downloads.ERROR_INVALID_URL; 48 const ERROR_GENERIC = downloads.ERROR_GENERIC;
49 var ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION; 49 const ERROR_INVALID_URL = downloads.ERROR_INVALID_URL;
50 const ERROR_INVALID_OPERATION = downloads.ERROR_INVALID_OPERATION;
50 51
51 chrome.test.runTests([ 52 chrome.test.runTests([
52 // TODO(benjhayden): Test onErased using remove(). 53 // TODO(benjhayden): Test onErased using remove().
54
55 // TODO(benjhayden): Sub-directories depend on http://crbug.com/109443
56 // function downloadSubDirectoryFilename() {
57 // var download_id = getNextId();
58 // var callbackCompleted = chrome.test.callbackAdded();
59 // function myListener(delta) {
60 // if (delta.filename &&
61 // delta.filename.new.indexOf('/foo/slow') !== -1) {
62 // downloads.onChanged.removeListener(myListener);
63 // callbackCompleted();
64 // }
65 // }
66 // downloads.onChanged.addListener(myListener);
67 // downloads.download(
68 // {'url': SAFE_FAST_URL, 'filename': 'foo/slow'},
69 // chrome.test.callback(function(id) {
70 // chrome.test.assertEq(download_id, id);
71 // }));
72 // },
73
74 function downloadSimple() {
75 // Test that we can begin a download.
76 const download_id = getNextId();
77 downloads.download(
78 {'url': SAFE_FAST_URL},
79 chrome.test.callbackPass(function(id) {
80 chrome.test.assertEq(download_id, id);
81 }));
82 },
83
84 function downloadPostSuccess() {
85 // Test the |method| download option.
86 const download_id = getNextId();
87 var changedCompleted = chrome.test.callbackAdded();
88 function changedListener(delta) {
89 // Ignore onChanged events for downloads besides our own, or events that
90 // signal any change besides completion.
91 if ((delta.id == download_id) &&
92 delta.state &&
93 (delta.state.new == downloads.STATE_COMPLETE)) {
94 chrome.test.assertEq(downloads.STATE_COMPLETE, delta.state.new);
95 downloads.search({id: download_id},
96 chrome.test.callback(function(items) {
97 chrome.test.assertEq(1, items.length);
98 chrome.test.assertEq(download_id, items[0].id);
99 const kExpectedSize = 164;
100 chrome.test.assertEq(kExpectedSize, items[0].totalBytes);
101 chrome.test.assertEq(kExpectedSize, items[0].fileSize);
102 chrome.test.assertEq(kExpectedSize, items[0].bytesReceived);
103 }));
104 downloads.onChanged.removeListener(changedListener);
105 changedCompleted();
106 }
107 }
108 downloads.onChanged.addListener(changedListener);
109
110 downloads.download(
111 {'url': getURL(POST_URL),
112 'method': 'POST',
113 'filename': download_id + '.txt',
114 'body': 'BODY'},
115 chrome.test.callbackPass(function(id) {
116 chrome.test.assertEq(download_id, id);
117 }));
118 },
119
120 function downloadPostWouldFailWithoutMethod() {
121 // Test that downloadPostSuccess would fail if the resource requires the
122 // POST method, and chrome fails to propagate the |method| parameter back
123 // to the server.
124 const download_id = getNextId();
125
126 var changedCompleted = chrome.test.callbackAdded();
127 function changedListener(delta) {
128 // Ignore onChanged events for downloads besides our own, or events that
129 // signal any change besides interruption.
130 if ((delta.id == download_id) &&
131 delta.state &&
132 ((delta.state.new == downloads.STATE_INTERRUPTED) ||
133 (delta.state.new == downloads.STATE_COMPLETE))) {
134 // TODO(benjhayden): Figure out why this download isn't interrupted,
135 // why 4XX HTTP errors are not considered interruptions.
Randy Smith (Not in Mondays) 2012/02/13 16:13:08 Could you make sure we have a bug filed on this ap
benjhayden 2012/02/13 19:01:31 Done.
136 downloads.search({id: download_id},
137 chrome.test.callback(function(items) {
138 chrome.test.assertEq(1, items.length);
139 chrome.test.assertEq(download_id, items[0].id);
140 chrome.test.assertEq(0, items[0].totalBytes);
141 }));
142 downloads.onChanged.removeListener(changedListener);
143 changedCompleted();
144 }
145 }
146 downloads.onChanged.addListener(changedListener);
147
148 downloads.download(
149 {'url': getURL(POST_URL),
150 'filename': download_id + '.txt',
151 'body': 'BODY'},
152 chrome.test.callbackPass(function(id) {
153 chrome.test.assertEq(download_id, id);
154 }));
155 },
156
157 function downloadPostWouldFailWithoutBody() {
158 // Test that downloadPostSuccess would fail if the resource requires the
159 // POST method and a request body, and chrome fails to propagate the
160 // |body| parameter back to the server.
161 const download_id = getNextId();
162
163 var changedCompleted = chrome.test.callbackAdded();
164 function changedListener(delta) {
165 // Ignore onChanged events for downloads besides our own, or events that
166 // signal any change besides interruption.
167 if ((delta.id == download_id) &&
168 delta.state &&
169 ((delta.state.new == downloads.STATE_INTERRUPTED) ||
170 (delta.state.new == downloads.STATE_COMPLETE))) {
171 // TODO(benjhayden): Figure out why this download isn't interrupted.
172 downloads.search({id: download_id},
173 chrome.test.callback(function(items) {
174 chrome.test.assertEq(1, items.length);
175 chrome.test.assertEq(download_id, items[0].id);
176 chrome.test.assertEq(0, items[0].totalBytes);
177 }));
178 downloads.onChanged.removeListener(changedListener);
179 changedCompleted();
180 }
181 }
182 downloads.onChanged.addListener(changedListener);
183
184 downloads.download(
185 {'url': getURL(POST_URL),
186 'filename': download_id + '.txt',
187 'method': 'POST'},
188 chrome.test.callbackPass(function(id) {
189 chrome.test.assertEq(download_id, id);
190 }));
191 },
192
193 function downloadHeader() {
194 // Test the |headers| download option.
195 const download_id = getNextId();
196 downloads.download(
197 {'url': SAFE_FAST_URL,
198 'headers': [{'name': 'Foo', 'value': 'bar'}]
199 },
200 chrome.test.callbackPass(function(id) {
201 chrome.test.assertEq(download_id, id);
202 }));
203 },
204
205 function downloadInterrupted() {
206 // Test that cancel()ing an in-progress download causes its state to
207 // transition to interrupted, and test that that state transition is
208 // detectable by an onChanged event listener.
209 const download_id = getNextId();
210
211 var createdCompleted = chrome.test.callbackAdded();
212 function createdListener(created_item) {
213 // Ignore onCreated events for any download besides our own.
214 if (created_item.id != download_id)
215 return;
216 // TODO(benjhayden) Move this cancel() into the download() callback
217 // after ensuring that DownloadItems are created before that callback
218 // is fired.
219 downloads.cancel(download_id, chrome.test.callback(function() {
220 }));
221 downloads.onCreated.removeListener(createdListener);
222 createdCompleted();
223 }
224 downloads.onCreated.addListener(createdListener);
225
226 var changedCompleted = chrome.test.callbackAdded();
227 function changedListener(delta) {
228 // Ignore onChanged events for downloads besides our own, or events that
229 // signal any change besides interruption.
230 if ((delta.id == download_id) &&
231 delta.state &&
232 (delta.state.new == downloads.STATE_INTERRUPTED)) {
233 downloads.onChanged.removeListener(changedListener);
234 changedCompleted();
235 }
236 }
237 downloads.onChanged.addListener(changedListener);
238
239 downloads.download(
240 {'url': NEVER_FINISH_URL},
241 chrome.test.callback(function(id) {
242 chrome.test.assertEq(download_id, id);
243 }));
244 },
245
246
247 function downloadOnChanged() {
248 // Test that download completion is detectable by an onChanged event
249 // listener.
250 const download_id = getNextId();
251 var callbackCompleted = chrome.test.callbackAdded();
252 function myListener(delta) {
253 if (delta.state && delta.state.new == downloads.STATE_COMPLETE) {
254 downloads.onChanged.removeListener(myListener);
255 callbackCompleted();
256 }
257 }
258 downloads.onChanged.addListener(myListener);
259 downloads.download(
260 {"url": getURL("slow?0")},
261 chrome.test.callback(function(id) {
262 chrome.test.assertEq(download_id, id);
263 }));
264 },
265
53 function downloadFilename() { 266 function downloadFilename() {
54 downloads.download( 267 // Test that we can suggest a filename for a new download, and test that
55 {'url': SAFE_FAST_URL, 'filename': 'foo'}, 268 // we can detect filename changes with an onChanged event listener.
56 chrome.test.callbackPass(function(id) { 269 const kFilename = 'owiejtoiwjrfoiwjroiwjroiwjroiwjrfi';
57 chrome.test.assertEq(getNextId(), id); 270 const download_id = getNextId();
58 })); 271 var callbackCompleted = chrome.test.callbackAdded();
59 // TODO(benjhayden): Test the filename using onChanged. 272 function myListener(delta) {
60 }, 273 if (delta.filename && delta.filename.new.indexOf(kFilename) !== -1) {
274 downloads.onChanged.removeListener(myListener);
275 callbackCompleted();
276 }
277 }
278 downloads.onChanged.addListener(myListener);
279 downloads.download(
280 {'url': SAFE_FAST_URL, 'filename': kFilename},
281 chrome.test.callback(function(id) {
282 chrome.test.assertEq(download_id, id);
283 }));
284 },
285
61 function downloadOnCreated() { 286 function downloadOnCreated() {
62 chrome.test.listenOnce(downloads.onCreated, 287 // Test that the onCreated event fires when we start a download.
63 chrome.test.callbackPass(function(item) {})); 288 var download_id = getNextId();
289 var createdCompleted = chrome.test.callbackAdded();
290 function createdListener(item) {
291 if (item.id == download_id) {
292 createdCompleted();
293 downloads.onCreated.removeListener(createdListener);
294 }
295 };
296 downloads.onCreated.addListener(createdListener);
64 downloads.download( 297 downloads.download(
65 {'url': SAFE_FAST_URL}, 298 {'url': SAFE_FAST_URL},
66 function(id) { 299 chrome.test.callback(function(id) {
67 chrome.test.assertEq(getNextId(), id); 300 chrome.test.assertEq(download_id, id);
68 }); 301 }));
69 }, 302 },
70 function downloadSubDirectoryFilename() { 303
71 downloads.download(
72 {'url': SAFE_FAST_URL, 'filename': 'foo/slow'},
73 chrome.test.callbackPass(function(id) {
74 chrome.test.assertEq(getNextId(), id);
75 }));
76 // TODO(benjhayden): Test the filename using onChanged.
77 },
78 function downloadInvalidFilename() { 304 function downloadInvalidFilename() {
305 // Test that we disallow invalid filenames for new downloads.
79 downloads.download( 306 downloads.download(
80 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'}, 307 {'url': SAFE_FAST_URL, 'filename': '../../../../../etc/passwd'},
81 chrome.test.callbackFail(ERROR_GENERIC)); 308 chrome.test.callbackFail(ERROR_GENERIC));
82 // TODO(benjhayden): Give a better error message. 309 },
83 }, 310
84 function downloadEmpty() { 311 function downloadEmpty() {
85 assertThrows(('Invalid value for argument 1. Property \'url\': ' + 312 assertThrows(('Invalid value for argument 1. Property \'url\': ' +
86 'Property is required.'), 313 'Property is required.'),
87 downloads.download, {}); 314 downloads.download, {});
88 }, 315 },
316
89 function downloadInvalidSaveAs() { 317 function downloadInvalidSaveAs() {
90 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' + 318 assertThrows(('Invalid value for argument 1. Property \'saveAs\': ' +
91 'Expected \'boolean\' but got \'string\'.'), 319 'Expected \'boolean\' but got \'string\'.'),
92 downloads.download, 320 downloads.download,
93 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'}); 321 {'url': SAFE_FAST_URL, 'saveAs': 'GOAT'});
94 }, 322 },
323
95 function downloadInvalidHeadersOption() { 324 function downloadInvalidHeadersOption() {
96 assertThrows(('Invalid value for argument 1. Property \'headers\': ' + 325 assertThrows(('Invalid value for argument 1. Property \'headers\': ' +
97 'Expected \'array\' but got \'string\'.'), 326 'Expected \'array\' but got \'string\'.'),
98 downloads.download, 327 downloads.download,
99 {'url': SAFE_FAST_URL, 'headers': 'GOAT'}); 328 {'url': SAFE_FAST_URL, 'headers': 'GOAT'});
100 }, 329 },
330
101 function downloadInvalidURL() { 331 function downloadInvalidURL() {
332 // Test that download() requires a valid url.
102 downloads.download( 333 downloads.download(
103 {'url': 'foo bar'}, 334 {'url': 'foo bar'},
104 chrome.test.callbackFail(ERROR_INVALID_URL)); 335 chrome.test.callbackFail(ERROR_INVALID_URL));
105 }, 336 },
337
106 function downloadInvalidMethod() { 338 function downloadInvalidMethod() {
107 assertThrows(('Invalid value for argument 1. Property \'method\': ' + 339 assertThrows(('Invalid value for argument 1. Property \'method\': ' +
108 'Value must be one of: [GET, POST].'), 340 'Value must be one of: [GET, POST].'),
109 downloads.download, 341 downloads.download,
110 {'url': SAFE_FAST_URL, 'method': 'GOAT'}); 342 {'url': SAFE_FAST_URL, 'method': 'GOAT'});
111 }, 343 },
112 function downloadSimple() { 344
113 downloads.download(
114 {'url': SAFE_FAST_URL},
115 chrome.test.callbackPass(function(id) {
116 chrome.test.assertEq(getNextId(), id);
117 }));
118 },
119 function downloadPost() {
120 downloads.download(
121 {'url': getURL('files/post/downloads/a_zip_file.js'),
122 'method': 'POST',
123 'body': 'WOOHOO'},
124 chrome.test.callbackPass(function(id) {
125 chrome.test.assertEq(getNextId(), id);
126 }));
127 },
128 function downloadHeader() {
129 downloads.download(
130 {'url': SAFE_FAST_URL,
131 'headers': [{'name': 'Foo', 'value': 'bar'}]
132 },
133 chrome.test.callbackPass(function(id) {
134 chrome.test.assertEq(getNextId(), id);
135 }));
136 },
137 function downloadInterrupted() {
138 // TODO(benjhayden): Find a suitable URL and test that this id is
139 // eventually interrupted using onChanged.
140 downloads.download(
141 {'url': SAFE_FAST_URL},
142 chrome.test.callbackPass(function(id) {
143 chrome.test.assertEq(getNextId(), id);
144 }));
145 },
146 function downloadInvalidHeader() { 345 function downloadInvalidHeader() {
346 // Test that download() disallows setting the Cookie header.
147 downloads.download( 347 downloads.download(
148 {'url': SAFE_FAST_URL, 348 {'url': SAFE_FAST_URL,
149 'headers': [{ 'name': 'Cookie', 'value': 'fake'}] 349 'headers': [{ 'name': 'Cookie', 'value': 'fake'}]
150 }, 350 },
151 chrome.test.callbackFail(ERROR_GENERIC)); 351 chrome.test.callbackFail(ERROR_GENERIC));
152 // TODO(benjhayden): Give a better error message. 352 },
153 }, 353
154 function downloadGetFileIconInvalidOptions() { 354 function downloadGetFileIconInvalidOptions() {
155 assertThrows(('Invalid value for argument 2. Property \'cat\': ' + 355 assertThrows(('Invalid value for argument 2. Property \'cat\': ' +
156 'Unexpected property.'), 356 'Unexpected property.'),
157 downloads.getFileIcon, 357 downloads.getFileIcon,
158 -1, {cat: 'mouse'}); 358 -1, {cat: 'mouse'});
159 }, 359 },
360
160 function downloadGetFileIconInvalidSize() { 361 function downloadGetFileIconInvalidSize() {
161 assertThrows(('Invalid value for argument 2. Property \'size\': ' + 362 assertThrows(('Invalid value for argument 2. Property \'size\': ' +
162 'Value must be one of: [16, 32].'), 363 'Value must be one of: [16, 32].'),
163 downloads.getFileIcon, -1, {size: 31}); 364 downloads.getFileIcon, -1, {size: 31});
164 }, 365 },
366
165 function downloadGetFileIconInvalidId() { 367 function downloadGetFileIconInvalidId() {
166 downloads.getFileIcon(-42, {size: 32}, 368 downloads.getFileIcon(-42, {size: 32},
167 chrome.test.callbackFail(ERROR_INVALID_OPERATION)); 369 chrome.test.callbackFail(ERROR_INVALID_OPERATION));
168 }, 370 },
169 function downloadNoComplete() { 371
170 // This is used partly to test cleanUp.
171 downloads.download(
172 {'url': NEVER_FINISH_URL},
173 chrome.test.callbackPass(function(id) {
174 chrome.test.assertEq(getNextId(), id);
175 }));
176 },
177 function downloadPauseInvalidId() { 372 function downloadPauseInvalidId() {
178 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); 373 downloads.pause(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION));
179 }, 374 },
375
180 function downloadPauseInvalidType() { 376 function downloadPauseInvalidType() {
181 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + 377 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' +
182 'but got \'string\'.'), 378 'but got \'string\'.'),
183 downloads.pause, 379 downloads.pause,
184 'foo'); 380 'foo');
185 }, 381 },
382
186 function downloadResumeInvalidId() { 383 function downloadResumeInvalidId() {
187 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION)); 384 downloads.resume(-42, chrome.test.callbackFail(ERROR_INVALID_OPERATION));
188 }, 385 },
386
189 function downloadResumeInvalidType() { 387 function downloadResumeInvalidType() {
190 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + 388 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' +
191 'but got \'string\'.'), 389 'but got \'string\'.'),
192 downloads.resume, 390 downloads.resume,
193 'foo'); 391 'foo');
194 }, 392 },
393
195 function downloadCancelInvalidId() { 394 function downloadCancelInvalidId() {
196 // Canceling a non-existent download is not considered an error. 395 // Canceling a non-existent download is not considered an error.
197 downloads.cancel(-42, chrome.test.callbackPass(function() {})); 396 downloads.cancel(-42, chrome.test.callbackPass(function() {}));
198 }, 397 },
398
199 function downloadCancelInvalidType() { 399 function downloadCancelInvalidType() {
200 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' + 400 assertThrows(('Invalid value for argument 1. Expected \'integer\' ' +
201 'but got \'string\'.'), 401 'but got \'string\'.'),
202 downloads.cancel, 'foo'); 402 downloads.cancel, 'foo');
203 }, 403 },
404
405 function downloadNoComplete() {
406 // This is used partly to test cleanUp.
407 var download_id = getNextId();
408 downloads.download(
409 {'url': NEVER_FINISH_URL},
410 chrome.test.callbackPass(function(id) {
411 chrome.test.assertEq(download_id, id);
412 }));
413 },
414
204 function cleanUp() { 415 function cleanUp() {
205 // cleanUp must come last. It clears out all in-progress downloads 416 // cleanUp must come last. It clears out all in-progress downloads
206 // so the browser can shutdown cleanly. 417 // so the browser can shutdown cleanly.
207 for (var id = 0; id < nextId; ++id) { 418 for (var id = 0; id < nextId; ++id) {
208 downloads.cancel(id, chrome.test.callbackPass(function() {})); 419 downloads.cancel(id, chrome.test.callbackPass(function() {}));
209 } 420 }
210 } 421 }
211 ]); 422 ]);
212 }); 423 });
OLDNEW
« no previous file with comments | « chrome/browser/download/download_extension_apitest.cc ('k') | net/tools/testserver/testserver.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698