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 |
32 chrome.test.runTests([ | 33 chrome.test.runTests([ |
34 // TODO(benjhayden): Test onErased using remove(). | |
33 function downloadFilename() { | 35 function downloadFilename() { |
34 chrome.experimental.downloads.download( | 36 chrome.experimental.downloads.download( |
35 {"url": getURL("pass"), "filename": "pass"}, | 37 {"url": getURL("slow?0"), "filename": "foo"}, |
Mihai Parparita -not on Chrome
2011/10/18 19:59:37
Can you add a comment why your'e using this URL (i
benjhayden
2011/10/18 20:32:56
Done.
/download-[un]known-size stalls the request
| |
36 chrome.test.callbackPass(function(id) { | 38 chrome.test.callbackPass(function(id) { |
37 chrome.test.assertEq(getNextId(), id); | 39 chrome.test.assertEq(getNextId(), id); |
38 })); | 40 })); |
39 // TODO(benjhayden): Test the filename using onChanged. | 41 // TODO(benjhayden): Test the filename using onChanged. |
40 }, | 42 }, |
43 function downloadOnCreated() { | |
44 function myListener(item) { | |
45 // Remove the listener so that we don't call chrome.test.succeed | |
Mihai Parparita -not on Chrome
2011/10/18 19:59:37
You can use chrome.test.listenOnce instead.
benjhayden
2011/10/18 20:32:56
Done.
| |
46 // spuriously for following test functions. Avoid | |
47 // chrome.test.callbackPass for event handlers so that we can use our | |
48 // own explicit version of the pendingCallbacks counter if necessary. | |
49 chrome.experimental.downloads.onCreated.removeListener(myListener); | |
50 chrome.test.succeed(); | |
51 }; | |
52 chrome.experimental.downloads.onCreated.addListener(myListener); | |
53 chrome.experimental.downloads.download( | |
54 {"url": getURL("slow?0")}, | |
55 function(id) { | |
56 chrome.test.assertEq(getNextId(), id); | |
57 }); | |
58 }, | |
41 function downloadSubDirectoryFilename() { | 59 function downloadSubDirectoryFilename() { |
42 chrome.experimental.downloads.download( | 60 chrome.experimental.downloads.download( |
43 {"url": getURL("pass"), "filename": "foo/pass"}, | 61 {"url": getURL("slow?0"), "filename": "foo/slow"}, |
44 chrome.test.callbackPass(function(id) { | 62 chrome.test.callbackPass(function(id) { |
45 chrome.test.assertEq(getNextId(), id); | 63 chrome.test.assertEq(getNextId(), id); |
46 })); | 64 })); |
47 // TODO(benjhayden): Test the filename using onChanged. | 65 // TODO(benjhayden): Test the filename using onChanged. |
48 }, | 66 }, |
49 function downloadInvalidFilename() { | 67 function downloadInvalidFilename() { |
50 chrome.experimental.downloads.download( | 68 chrome.experimental.downloads.download( |
51 {"url": getURL("pass"), "filename": "../../../../../etc/passwd"}, | 69 {"url": getURL("slow?0"), "filename": "../../../../../etc/passwd"}, |
52 chrome.test.callbackFail("I'm afraid I can't do that.")); | 70 chrome.test.callbackFail("I'm afraid I can't do that.")); |
53 // TODO(benjhayden): Give a better error message. | 71 // TODO(benjhayden): Give a better error message. |
54 }, | 72 }, |
55 function downloadEmpty() { | 73 function downloadEmpty() { |
56 assertThrows(chrome.experimental.downloads.download, {}, | 74 assertThrows(chrome.experimental.downloads.download, {}, |
57 ("Invalid value for argument 1. Property 'url': " + | 75 ("Invalid value for argument 1. Property 'url': " + |
58 "Property is required.")); | 76 "Property is required.")); |
59 }, | 77 }, |
60 function downloadInvalidSaveAs() { | 78 function downloadInvalidSaveAs() { |
61 assertThrows(chrome.experimental.downloads.download, | 79 assertThrows(chrome.experimental.downloads.download, |
62 {"url": getURL("pass"), "saveAs": "GOAT"}, | 80 {"url": getURL("slow?0"), "saveAs": "GOAT"}, |
63 ("Invalid value for argument 1. Property 'saveAs': " + | 81 ("Invalid value for argument 1. Property 'saveAs': " + |
64 "Expected 'boolean' but got 'string'.")); | 82 "Expected 'boolean' but got 'string'.")); |
65 }, | 83 }, |
66 function downloadInvalidHeadersOption() { | 84 function downloadInvalidHeadersOption() { |
67 assertThrows(chrome.experimental.downloads.download, | 85 assertThrows(chrome.experimental.downloads.download, |
68 {"url": getURL("pass"), "headers": "GOAT"}, | 86 {"url": getURL("slow?0"), "headers": "GOAT"}, |
69 ("Invalid value for argument 1. Property 'headers': " + | 87 ("Invalid value for argument 1. Property 'headers': " + |
70 "Expected 'array' but got 'string'.")); | 88 "Expected 'array' but got 'string'.")); |
71 }, | 89 }, |
72 function downloadInvalidURL() { | 90 function downloadInvalidURL() { |
73 chrome.experimental.downloads.download( | 91 chrome.experimental.downloads.download( |
74 {"url": "foo bar"}, | 92 {"url": "foo bar"}, |
75 chrome.test.callbackFail("Invalid URL")); | 93 chrome.test.callbackFail("Invalid URL")); |
76 }, | 94 }, |
77 function downloadInvalidMethod() { | 95 function downloadInvalidMethod() { |
78 assertThrows(chrome.experimental.downloads.download, | 96 assertThrows(chrome.experimental.downloads.download, |
79 {"url": getURL("pass"), "method": "GOAT"}, | 97 {"url": getURL("slow?0"), "method": "GOAT"}, |
80 ("Invalid value for argument 1. Property 'method': " + | 98 ("Invalid value for argument 1. Property 'method': " + |
81 "Value must be one of: [GET, POST].")); | 99 "Value must be one of: [GET, POST].")); |
82 }, | 100 }, |
83 function downloadSimple() { | 101 function downloadSimple() { |
84 chrome.experimental.downloads.download( | 102 chrome.experimental.downloads.download( |
85 {"url": getURL("pass")}, | 103 {"url": getURL("slow?0")}, |
86 chrome.test.callbackPass(function(id) { | 104 chrome.test.callbackPass(function(id) { |
87 chrome.test.assertEq(getNextId(), id); | 105 chrome.test.assertEq(getNextId(), id); |
88 })); | 106 })); |
89 }, | 107 }, |
90 function downloadHeader() { | 108 function downloadHeader() { |
91 chrome.experimental.downloads.download( | 109 chrome.experimental.downloads.download( |
92 {"url": getURL("pass"), | 110 {"url": getURL("slow?0"), |
93 "headers": [{"name": "Foo", "value": "bar"}]}, | 111 "headers": [{"name": "Foo", "value": "bar"}]}, |
94 chrome.test.callbackPass(function(id) { | 112 chrome.test.callbackPass(function(id) { |
95 chrome.test.assertEq(getNextId(), id); | 113 chrome.test.assertEq(getNextId(), id); |
96 })); | 114 })); |
97 }, | 115 }, |
98 function downloadInterrupted() { | 116 function downloadInterrupted() { |
117 // TODO(benjhayden): Find a suitable URL and test that this id is | |
118 // eventually interrupted using onChanged. | |
99 chrome.experimental.downloads.download( | 119 chrome.experimental.downloads.download( |
100 {"url": getURL("compressedfiles/Picture_1.doc?L")}, | 120 {"url": getURL("slow?0")}, |
101 chrome.test.callbackPass(function(id) { | 121 chrome.test.callbackPass(function(id) { |
102 chrome.test.assertEq(getNextId(), id); | 122 chrome.test.assertEq(getNextId(), id); |
103 // TODO(benjhayden): Test that this id is eventually interrupted using | |
104 // onChanged. | |
105 })); | 123 })); |
106 }, | 124 }, |
107 function downloadInvalidHeader() { | 125 function downloadInvalidHeader() { |
108 chrome.experimental.downloads.download( | 126 chrome.experimental.downloads.download( |
109 {"url": getURL("pass"), | 127 {"url": getURL("slow?0"), |
110 "headers": [{"name": "Cookie", "value": "fake"}]}, | 128 "headers": [{"name": "Cookie", "value": "fake"}]}, |
111 chrome.test.callbackFail("I'm afraid I can't do that.")); | 129 chrome.test.callbackFail("I'm afraid I can't do that.")); |
112 // TODO(benjhayden): Give a better error message. | 130 // TODO(benjhayden): Give a better error message. |
113 } | 131 } |
114 ]); | 132 ]); |
115 }); | 133 }); |
OLD | NEW |