OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 chrome.test.getConfig(function(testConfig) { | 8 chrome.test.getConfig(function(testConfig) { |
9 function getURL(path) { | 9 function getURL(path) { |
10 return "http://localhost:" + testConfig.testServer.port + "/" + path; | 10 return "http://localhost:" + testConfig.testServer.port + "/" + path; |
11 } | 11 } |
| 12 |
12 var nextId = 0; | 13 var nextId = 0; |
13 function getNextId() { | 14 function getNextId() { |
14 return nextId++; | 15 return nextId++; |
15 } | 16 } |
16 | 17 |
17 // There can be only one assertThrows per test function. | 18 // There can be only one assertThrows per test function. |
18 function assertThrows(func, arg, expected_message) { | 19 function assertThrows(func, arg, expected_message) { |
19 try { | 20 try { |
20 func(arg, function() { | 21 func(arg, function() { |
21 // Don't use chrome.test.callbackFail because it requires the | 22 // Don't use chrome.test.callbackFail because it requires the |
22 // callback to be called. | 23 // callback to be called. |
23 chrome.test.fail("Failed to throw exception (" + | 24 chrome.test.fail("Failed to throw exception (" + |
24 expected_message + ")"); | 25 expected_message + ")"); |
25 }); | 26 }); |
26 } catch (exception) { | 27 } catch (exception) { |
27 chrome.test.assertEq(expected_message, exception.message); | 28 chrome.test.assertEq(expected_message, exception.message); |
28 chrome.test.succeed(); | 29 chrome.test.succeed(); |
29 } | 30 } |
30 } | 31 } |
31 | 32 |
| 33 // The "/slow" handler waits a specified amount of time before returning a |
| 34 // safe file. Specify zero seconds to return quickly. |
| 35 var SAFE_FAST_URL = getURL("slow?0"); |
| 36 |
32 chrome.test.runTests([ | 37 chrome.test.runTests([ |
| 38 // TODO(benjhayden): Test onErased using remove(). |
33 function downloadFilename() { | 39 function downloadFilename() { |
34 chrome.experimental.downloads.download( | 40 chrome.experimental.downloads.download( |
35 {"url": getURL("pass"), "filename": "pass"}, | 41 {"url": SAFE_FAST_URL, "filename": "foo"}, |
36 chrome.test.callbackPass(function(id) { | 42 chrome.test.callbackPass(function(id) { |
37 chrome.test.assertEq(getNextId(), id); | 43 chrome.test.assertEq(getNextId(), id); |
38 })); | 44 })); |
39 // TODO(benjhayden): Test the filename using onChanged. | 45 // TODO(benjhayden): Test the filename using onChanged. |
40 }, | 46 }, |
| 47 function downloadOnCreated() { |
| 48 chrome.test.listenOnce(chrome.experimental.downloads.onCreated, |
| 49 chrome.test.callbackPass(function(item) { |
| 50 })); |
| 51 chrome.experimental.downloads.download( |
| 52 {"url": SAFE_FAST_URL}, |
| 53 function(id) { |
| 54 chrome.test.assertEq(getNextId(), id); |
| 55 }); |
| 56 }, |
41 function downloadSubDirectoryFilename() { | 57 function downloadSubDirectoryFilename() { |
42 chrome.experimental.downloads.download( | 58 chrome.experimental.downloads.download( |
43 {"url": getURL("pass"), "filename": "foo/pass"}, | 59 {"url": SAFE_FAST_URL, "filename": "foo/slow"}, |
44 chrome.test.callbackPass(function(id) { | 60 chrome.test.callbackPass(function(id) { |
45 chrome.test.assertEq(getNextId(), id); | 61 chrome.test.assertEq(getNextId(), id); |
46 })); | 62 })); |
47 // TODO(benjhayden): Test the filename using onChanged. | 63 // TODO(benjhayden): Test the filename using onChanged. |
48 }, | 64 }, |
49 function downloadInvalidFilename() { | 65 function downloadInvalidFilename() { |
50 chrome.experimental.downloads.download( | 66 chrome.experimental.downloads.download( |
51 {"url": getURL("pass"), "filename": "../../../../../etc/passwd"}, | 67 {"url": SAFE_FAST_URL, "filename": "../../../../../etc/passwd"}, |
52 chrome.test.callbackFail("I'm afraid I can't do that.")); | 68 chrome.test.callbackFail("I'm afraid I can't do that.")); |
53 // TODO(benjhayden): Give a better error message. | 69 // TODO(benjhayden): Give a better error message. |
54 }, | 70 }, |
55 function downloadEmpty() { | 71 function downloadEmpty() { |
56 assertThrows(chrome.experimental.downloads.download, {}, | 72 assertThrows(chrome.experimental.downloads.download, {}, |
57 ("Invalid value for argument 1. Property 'url': " + | 73 ("Invalid value for argument 1. Property 'url': " + |
58 "Property is required.")); | 74 "Property is required.")); |
59 }, | 75 }, |
60 function downloadInvalidSaveAs() { | 76 function downloadInvalidSaveAs() { |
61 assertThrows(chrome.experimental.downloads.download, | 77 assertThrows(chrome.experimental.downloads.download, |
62 {"url": getURL("pass"), "saveAs": "GOAT"}, | 78 {"url": SAFE_FAST_URL, "saveAs": "GOAT"}, |
63 ("Invalid value for argument 1. Property 'saveAs': " + | 79 ("Invalid value for argument 1. Property 'saveAs': " + |
64 "Expected 'boolean' but got 'string'.")); | 80 "Expected 'boolean' but got 'string'.")); |
65 }, | 81 }, |
66 function downloadInvalidHeadersOption() { | 82 function downloadInvalidHeadersOption() { |
67 assertThrows(chrome.experimental.downloads.download, | 83 assertThrows(chrome.experimental.downloads.download, |
68 {"url": getURL("pass"), "headers": "GOAT"}, | 84 {"url": SAFE_FAST_URL, "headers": "GOAT"}, |
69 ("Invalid value for argument 1. Property 'headers': " + | 85 ("Invalid value for argument 1. Property 'headers': " + |
70 "Expected 'array' but got 'string'.")); | 86 "Expected 'array' but got 'string'.")); |
71 }, | 87 }, |
72 function downloadInvalidURL() { | 88 function downloadInvalidURL() { |
73 chrome.experimental.downloads.download( | 89 chrome.experimental.downloads.download( |
74 {"url": "foo bar"}, | 90 {"url": "foo bar"}, |
75 chrome.test.callbackFail("Invalid URL")); | 91 chrome.test.callbackFail("Invalid URL")); |
76 }, | 92 }, |
77 function downloadInvalidMethod() { | 93 function downloadInvalidMethod() { |
78 assertThrows(chrome.experimental.downloads.download, | 94 assertThrows(chrome.experimental.downloads.download, |
79 {"url": getURL("pass"), "method": "GOAT"}, | 95 {"url": SAFE_FAST_URL, "method": "GOAT"}, |
80 ("Invalid value for argument 1. Property 'method': " + | 96 ("Invalid value for argument 1. Property 'method': " + |
81 "Value must be one of: [GET, POST].")); | 97 "Value must be one of: [GET, POST].")); |
82 }, | 98 }, |
83 function downloadSimple() { | 99 function downloadSimple() { |
84 chrome.experimental.downloads.download( | 100 chrome.experimental.downloads.download( |
85 {"url": getURL("pass")}, | 101 {"url": SAFE_FAST_URL}, |
86 chrome.test.callbackPass(function(id) { | 102 chrome.test.callbackPass(function(id) { |
87 chrome.test.assertEq(getNextId(), id); | 103 chrome.test.assertEq(getNextId(), id); |
88 })); | 104 })); |
89 }, | 105 }, |
90 function downloadHeader() { | 106 function downloadHeader() { |
91 chrome.experimental.downloads.download( | 107 chrome.experimental.downloads.download( |
92 {"url": getURL("pass"), | 108 {"url": SAFE_FAST_URL, |
93 "headers": [{"name": "Foo", "value": "bar"}]}, | 109 "headers": [{"name": "Foo", "value": "bar"}]}, |
94 chrome.test.callbackPass(function(id) { | 110 chrome.test.callbackPass(function(id) { |
95 chrome.test.assertEq(getNextId(), id); | 111 chrome.test.assertEq(getNextId(), id); |
96 })); | 112 })); |
97 }, | 113 }, |
98 function downloadInterrupted() { | 114 function downloadInterrupted() { |
| 115 // TODO(benjhayden): Find a suitable URL and test that this id is |
| 116 // eventually interrupted using onChanged. |
99 chrome.experimental.downloads.download( | 117 chrome.experimental.downloads.download( |
100 {"url": getURL("compressedfiles/Picture_1.doc?L")}, | 118 {"url": SAFE_FAST_URL}, |
101 chrome.test.callbackPass(function(id) { | 119 chrome.test.callbackPass(function(id) { |
102 chrome.test.assertEq(getNextId(), id); | 120 chrome.test.assertEq(getNextId(), id); |
103 // TODO(benjhayden): Test that this id is eventually interrupted using | |
104 // onChanged. | |
105 })); | 121 })); |
106 }, | 122 }, |
107 function downloadInvalidHeader() { | 123 function downloadInvalidHeader() { |
108 chrome.experimental.downloads.download( | 124 chrome.experimental.downloads.download( |
109 {"url": getURL("pass"), | 125 {"url": SAFE_FAST_URL, |
110 "headers": [{"name": "Cookie", "value": "fake"}]}, | 126 "headers": [{"name": "Cookie", "value": "fake"}]}, |
111 chrome.test.callbackFail("I'm afraid I can't do that.")); | 127 chrome.test.callbackFail("I'm afraid I can't do that.")); |
112 // TODO(benjhayden): Give a better error message. | 128 // TODO(benjhayden): Give a better error message. |
113 } | 129 } |
114 ]); | 130 ]); |
115 }); | 131 }); |
OLD | NEW |