OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
47 | 47 |
48 void Quit(); | 48 void Quit(); |
49 void RunThreads(); | 49 void RunThreads(); |
50 void RunThreadsUntilIdle(); | 50 void RunThreadsUntilIdle(); |
51 | 51 |
52 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); | 52 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); |
53 | 53 |
54 protected: | 54 protected: |
55 scoped_ptr<CrxDownloader> crx_downloader_; | 55 scoped_ptr<CrxDownloader> crx_downloader_; |
56 | 56 |
57 CrxDownloader::DownloadCallback callback_; | |
58 | |
57 int crx_context_; | 59 int crx_context_; |
58 int error_; | 60 int error_; |
59 base::FilePath response_; | 61 base::FilePath response_; |
60 | 62 |
61 int num_calls_; | 63 int num_calls_; |
62 | 64 |
63 // A magic value for the context to be used in the tests. | 65 // A magic value for the context to be used in the tests. |
64 static const int kExpectedContext = 0xaabb; | 66 static const int kExpectedContext = 0xaabb; |
65 | 67 |
66 private: | 68 private: |
67 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 69 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
68 scoped_refptr<net::TestURLRequestContextGetter> context_; | 70 scoped_refptr<net::TestURLRequestContextGetter> context_; |
69 content::TestBrowserThreadBundle thread_bundle_; | 71 content::TestBrowserThreadBundle thread_bundle_; |
70 base::Closure quit_closure_; | 72 base::Closure quit_closure_; |
71 }; | 73 }; |
72 | 74 |
73 const int CrxDownloaderTest::kExpectedContext; | 75 const int CrxDownloaderTest::kExpectedContext; |
74 | 76 |
75 CrxDownloaderTest::CrxDownloaderTest() | 77 CrxDownloaderTest::CrxDownloaderTest() |
76 : crx_context_(0), | 78 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, |
79 base::Unretained(this), | |
80 kExpectedContext)), | |
81 crx_context_(0), | |
77 error_(0), | 82 error_(0), |
78 num_calls_(0), | 83 num_calls_(0), |
79 blocking_task_runner_(BrowserThread::GetBlockingPool()-> | 84 blocking_task_runner_(BrowserThread::GetBlockingPool()-> |
80 GetSequencedTaskRunnerWithShutdownBehavior( | 85 GetSequencedTaskRunnerWithShutdownBehavior( |
81 BrowserThread::GetBlockingPool()->GetSequenceToken(), | 86 BrowserThread::GetBlockingPool()->GetSequenceToken(), |
82 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 87 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
83 context_(new net::TestURLRequestContextGetter( | 88 context_(new net::TestURLRequestContextGetter( |
84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), | 89 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), |
85 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 90 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
86 } | 91 } |
87 | 92 |
88 CrxDownloaderTest::~CrxDownloaderTest() { | 93 CrxDownloaderTest::~CrxDownloaderTest() { |
89 context_ = NULL; | 94 context_ = NULL; |
90 } | 95 } |
91 | 96 |
92 void CrxDownloaderTest::SetUp() { | 97 void CrxDownloaderTest::SetUp() { |
93 num_calls_ = 0; | 98 num_calls_ = 0; |
94 crx_downloader_.reset(CrxDownloader::Create( | 99 crx_downloader_.reset(CrxDownloader::Create( |
95 false, // Do not use the background downloader in these tests. | 100 false, // Do not use the background downloader in these tests. |
96 context_.get(), | 101 context_.get(), |
97 blocking_task_runner_, | 102 blocking_task_runner_)); |
98 base::Bind(&CrxDownloaderTest::DownloadComplete, | |
99 base::Unretained(this), | |
100 kExpectedContext))); | |
101 } | 103 } |
102 | 104 |
103 void CrxDownloaderTest::TearDown() { | 105 void CrxDownloaderTest::TearDown() { |
104 } | 106 } |
105 | 107 |
106 void CrxDownloaderTest::Quit() { | 108 void CrxDownloaderTest::Quit() { |
107 if (!quit_closure_.is_null()) | 109 if (!quit_closure_.is_null()) |
108 quit_closure_.Run(); | 110 quit_closure_.Run(); |
109 } | 111 } |
110 | 112 |
(...skipping 19 matching lines...) Expand all Loading... | |
130 RunThreadsUntilIdle(); | 132 RunThreadsUntilIdle(); |
131 } | 133 } |
132 | 134 |
133 void CrxDownloaderTest::RunThreadsUntilIdle() { | 135 void CrxDownloaderTest::RunThreadsUntilIdle() { |
134 base::RunLoop().RunUntilIdle(); | 136 base::RunLoop().RunUntilIdle(); |
135 } | 137 } |
136 | 138 |
137 // Tests that starting a download without a url results in an error. | 139 // Tests that starting a download without a url results in an error. |
138 TEST_F(CrxDownloaderTest, NoUrl) { | 140 TEST_F(CrxDownloaderTest, NoUrl) { |
139 std::vector<GURL> urls; | 141 std::vector<GURL> urls; |
140 crx_downloader_->StartDownload(urls); | 142 crx_downloader_->StartDownload(urls, callback_); |
141 | 143 |
142 RunThreadsUntilIdle(); | 144 RunThreadsUntilIdle(); |
143 EXPECT_EQ(-1, error_); | 145 EXPECT_EQ(-1, error_); |
144 EXPECT_EQ(kExpectedContext, crx_context_); | 146 EXPECT_EQ(kExpectedContext, crx_context_); |
145 | 147 |
146 EXPECT_EQ(1, num_calls_); | 148 EXPECT_EQ(1, num_calls_); |
147 } | 149 } |
148 | 150 |
149 // Tests that downloading from one url is successful. | 151 // Tests that downloading from one url is successful. |
150 TEST_F(CrxDownloaderTest, OneUrl) { | 152 TEST_F(CrxDownloaderTest, OneUrl) { |
151 const GURL expected_crx_url = | 153 const GURL expected_crx_url = |
152 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 154 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
153 | 155 |
154 GetInterceptor interceptor; | 156 GetInterceptor interceptor; |
155 interceptor.SetResponse( | 157 interceptor.SetResponse( |
156 expected_crx_url, | 158 expected_crx_url, |
157 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 159 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
158 | 160 |
159 crx_downloader_->StartDownloadFromUrl(expected_crx_url); | 161 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); |
160 | 162 |
161 RunThreads(); | 163 RunThreads(); |
162 EXPECT_EQ(1, interceptor.GetHitCount()); | 164 EXPECT_EQ(1, interceptor.GetHitCount()); |
163 EXPECT_EQ(0, error_); | 165 EXPECT_EQ(0, error_); |
164 EXPECT_EQ(kExpectedContext, crx_context_); | 166 EXPECT_EQ(kExpectedContext, crx_context_); |
165 | 167 |
166 EXPECT_EQ(1, num_calls_); | 168 EXPECT_EQ(1, num_calls_); |
167 } | 169 } |
168 | 170 |
169 // Tests that specifying from two urls has no side effects. Expect a successful | 171 // Tests that specifying from two urls has no side effects. Expect a successful |
(...skipping 10 matching lines...) Expand all Loading... | |
180 | 182 |
181 GetInterceptor interceptor; | 183 GetInterceptor interceptor; |
182 interceptor.SetResponse( | 184 interceptor.SetResponse( |
183 expected_crx_url, | 185 expected_crx_url, |
184 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 186 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
185 | 187 |
186 std::vector<GURL> urls; | 188 std::vector<GURL> urls; |
187 urls.push_back(expected_crx_url); | 189 urls.push_back(expected_crx_url); |
188 urls.push_back(expected_crx_url); | 190 urls.push_back(expected_crx_url); |
189 | 191 |
190 crx_downloader_->StartDownload(urls); | 192 crx_downloader_->StartDownload(urls, callback_); |
191 | 193 |
192 RunThreads(); | 194 RunThreads(); |
193 EXPECT_EQ(1, interceptor.GetHitCount()); | 195 EXPECT_EQ(1, interceptor.GetHitCount()); |
194 EXPECT_EQ(0, error_); | 196 EXPECT_EQ(0, error_); |
195 EXPECT_EQ(kExpectedContext, crx_context_); | 197 EXPECT_EQ(kExpectedContext, crx_context_); |
196 | 198 |
197 EXPECT_EQ(1, num_calls_); | 199 EXPECT_EQ(1, num_calls_); |
198 } | 200 } |
199 | 201 |
200 // Tests that an invalid host results in a download error. | 202 // Tests that an invalid host results in a download error. |
201 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { | 203 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { |
202 const GURL expected_crx_url = | 204 const GURL expected_crx_url = |
203 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 205 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
204 | 206 |
205 GetInterceptor interceptor; | 207 GetInterceptor interceptor; |
206 interceptor.SetResponse( | 208 interceptor.SetResponse( |
207 expected_crx_url, | 209 expected_crx_url, |
208 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 210 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
209 | 211 |
210 crx_downloader_->StartDownloadFromUrl( | 212 crx_downloader_->StartDownloadFromUrl( |
211 GURL("http://no.such.host" | 213 GURL("http://no.such.host" |
212 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); | 214 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), |
215 callback_); | |
213 | 216 |
214 RunThreads(); | 217 RunThreads(); |
215 EXPECT_EQ(0, interceptor.GetHitCount()); | 218 EXPECT_EQ(0, interceptor.GetHitCount()); |
216 EXPECT_NE(0, error_); | 219 EXPECT_NE(0, error_); |
217 EXPECT_EQ(kExpectedContext, crx_context_); | 220 EXPECT_EQ(kExpectedContext, crx_context_); |
218 | 221 |
219 EXPECT_EQ(1, num_calls_); | 222 EXPECT_EQ(1, num_calls_); |
220 } | 223 } |
221 | 224 |
222 // Tests that an invalid path results in a download error. | 225 // Tests that an invalid path results in a download error. |
223 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { | 226 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { |
224 const GURL expected_crx_url = | 227 const GURL expected_crx_url = |
225 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 228 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
226 | 229 |
227 GetInterceptor interceptor; | 230 GetInterceptor interceptor; |
228 interceptor.SetResponse( | 231 interceptor.SetResponse( |
229 expected_crx_url, | 232 expected_crx_url, |
230 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 233 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
231 | 234 |
232 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file")); | 235 crx_downloader_->StartDownloadFromUrl( |
236 GURL("http://localhost/no/such/file"), | |
237 callback_); | |
233 | 238 |
234 RunThreads(); | 239 RunThreads(); |
235 EXPECT_EQ(0, interceptor.GetHitCount()); | 240 EXPECT_EQ(0, interceptor.GetHitCount()); |
236 EXPECT_NE(0, error_); | 241 EXPECT_NE(0, error_); |
237 EXPECT_EQ(kExpectedContext, crx_context_); | 242 EXPECT_EQ(kExpectedContext, crx_context_); |
238 | 243 |
239 EXPECT_EQ(1, num_calls_); | 244 EXPECT_EQ(1, num_calls_); |
240 } | 245 } |
241 | 246 |
242 // Tests that the fallback to a valid url is successful. | 247 // Tests that the fallback to a valid url is successful. |
243 // This test is flaky on Android. crbug.com/329883 | 248 // This test is flaky on Android. crbug.com/329883 |
244 #if defined(OS_ANDROID) | 249 #if defined(OS_ANDROID) |
245 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid | 250 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid |
246 #else | 251 #else |
247 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid | 252 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid |
248 #endif | 253 #endif |
249 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { | 254 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { |
250 const GURL expected_crx_url = | 255 const GURL expected_crx_url = |
251 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 256 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
252 | 257 |
253 GetInterceptor interceptor; | 258 GetInterceptor interceptor; |
254 interceptor.SetResponse( | 259 interceptor.SetResponse( |
255 expected_crx_url, | 260 expected_crx_url, |
256 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 261 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
257 | 262 |
258 std::vector<GURL> urls; | 263 std::vector<GURL> urls; |
259 urls.push_back(GURL("http://localhost/no/such/file")); | 264 urls.push_back(GURL("http://localhost/no/such/file")); |
260 urls.push_back(expected_crx_url); | 265 urls.push_back(expected_crx_url); |
261 | 266 |
262 crx_downloader_->StartDownload(urls); | 267 crx_downloader_->StartDownload(urls, callback_); |
263 | 268 |
264 RunThreads(); | 269 RunThreads(); |
265 EXPECT_EQ(1, interceptor.GetHitCount()); | 270 EXPECT_EQ(1, interceptor.GetHitCount()); |
266 EXPECT_EQ(0, error_); | 271 EXPECT_EQ(0, error_); |
267 EXPECT_EQ(kExpectedContext, crx_context_); | 272 EXPECT_EQ(kExpectedContext, crx_context_); |
268 | 273 |
269 EXPECT_EQ(1, num_calls_); | 274 EXPECT_EQ(1, num_calls_); |
270 } | 275 } |
271 | 276 |
272 // Tests that the download succeeds if the first url is correct and the | 277 // Tests that the download succeeds if the first url is correct and the |
273 // second bad url does not have a side-effect. | 278 // second bad url does not have a side-effect. |
274 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { | 279 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { |
275 const GURL expected_crx_url = | 280 const GURL expected_crx_url = |
276 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 281 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
277 | 282 |
278 GetInterceptor interceptor; | 283 GetInterceptor interceptor; |
279 interceptor.SetResponse( | 284 interceptor.SetResponse( |
280 expected_crx_url, | 285 expected_crx_url, |
281 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 286 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
282 | 287 |
283 std::vector<GURL> urls; | 288 std::vector<GURL> urls; |
284 urls.push_back(expected_crx_url); | 289 urls.push_back(expected_crx_url); |
285 urls.push_back(GURL("http://localhost/no/such/file")); | 290 urls.push_back(GURL("http://localhost/no/such/file")); |
286 | 291 |
287 crx_downloader_->StartDownload(urls); | 292 crx_downloader_->StartDownload(urls, callback_); |
288 | 293 |
289 RunThreads(); | 294 RunThreads(); |
290 EXPECT_EQ(1, interceptor.GetHitCount()); | 295 EXPECT_EQ(1, interceptor.GetHitCount()); |
291 EXPECT_EQ(0, error_); | 296 EXPECT_EQ(0, error_); |
292 EXPECT_EQ(kExpectedContext, crx_context_); | 297 EXPECT_EQ(kExpectedContext, crx_context_); |
293 | 298 |
294 EXPECT_EQ(1, num_calls_); | 299 EXPECT_EQ(1, num_calls_); |
295 } | 300 } |
296 | 301 |
297 // Tests that the download fails if both urls are bad. | 302 // Tests that the download fails if both urls are bad. |
298 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { | 303 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { |
299 const GURL expected_crx_url = | 304 const GURL expected_crx_url = |
300 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | 305 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
301 | 306 |
302 GetInterceptor interceptor; | 307 GetInterceptor interceptor; |
303 interceptor.SetResponse( | 308 interceptor.SetResponse( |
304 expected_crx_url, | 309 expected_crx_url, |
305 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); | 310 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx")); |
306 | 311 |
307 std::vector<GURL> urls; | 312 std::vector<GURL> urls; |
308 urls.push_back(GURL("http://localhost/no/such/file")); | 313 urls.push_back(GURL("http://localhost/no/such/file")); |
309 urls.push_back(GURL("http://no.such.host/" | 314 urls.push_back(GURL("http://no.such.host/" |
310 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); | 315 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); |
311 | 316 |
312 crx_downloader_->StartDownload(urls); | 317 crx_downloader_->StartDownload(urls, callback_); |
313 | 318 |
314 RunThreads(); | 319 RunThreads(); |
315 EXPECT_EQ(0, interceptor.GetHitCount()); | 320 EXPECT_EQ(0, interceptor.GetHitCount()); |
316 EXPECT_NE(0, error_); | 321 EXPECT_NE(0, error_); |
317 EXPECT_EQ(kExpectedContext, crx_context_); | 322 EXPECT_EQ(kExpectedContext, crx_context_); |
318 | 323 |
319 EXPECT_EQ(1, num_calls_); | 324 EXPECT_EQ(1, num_calls_); |
320 } | 325 } |
321 | 326 |
322 } // namespace component_updater | 327 } // namespace component_updater |
323 | |
waffles
2014/04/24 23:27:53
I'm glad you got rid of this. Now if only we could
| |
OLD | NEW |