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 // This file contains download browser tests that are known to be runnable | 5 // This file contains download browser tests that are known to be runnable |
6 // in a pure content context. Over time tests should be migrated here. | 6 // in a pure content context. Over time tests should be migrated here. |
7 | 7 |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() | 170 DownloadFileWithDelayFactory::DownloadFileWithDelayFactory() |
171 : waiting_(false) {} | 171 : waiting_(false) {} |
172 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} | 172 DownloadFileWithDelayFactory::~DownloadFileWithDelayFactory() {} |
173 | 173 |
174 DownloadFile* DownloadFileWithDelayFactory::CreateFile( | 174 DownloadFile* DownloadFileWithDelayFactory::CreateFile( |
175 DownloadCreateInfo* info, | 175 DownloadCreateInfo* info, |
176 scoped_ptr<content::ByteStreamReader> stream, | 176 scoped_ptr<content::ByteStreamReader> stream, |
177 DownloadManager* download_manager, | 177 DownloadManager* download_manager, |
178 bool calculate_hash, | 178 bool calculate_hash, |
179 const net::BoundNetLog& bound_net_log) { | 179 const net::BoundNetLog& bound_net_log) { |
180 | |
181 return new DownloadFileWithDelay( | 180 return new DownloadFileWithDelay( |
182 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), | 181 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), |
183 download_manager, calculate_hash, | 182 download_manager, calculate_hash, |
184 scoped_ptr<content::PowerSaveBlocker>( | 183 scoped_ptr<content::PowerSaveBlocker>( |
185 new content::PowerSaveBlocker( | 184 new content::PowerSaveBlocker( |
186 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | 185 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, |
187 "Download in progress")).Pass(), | 186 "Download in progress")).Pass(), |
188 bound_net_log, this); | 187 bound_net_log, this); |
189 } | 188 } |
190 | 189 |
(...skipping 26 matching lines...) Expand all Loading... | |
217 void DownloadFileWithDelayFactory::WaitForSomeCallback() { | 216 void DownloadFileWithDelayFactory::WaitForSomeCallback() { |
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
219 | 218 |
220 if (rename_callbacks_.empty() && detach_callbacks_.empty()) { | 219 if (rename_callbacks_.empty() && detach_callbacks_.empty()) { |
221 waiting_ = true; | 220 waiting_ = true; |
222 RunMessageLoop(); | 221 RunMessageLoop(); |
223 waiting_ = false; | 222 waiting_ = false; |
224 } | 223 } |
225 } | 224 } |
226 | 225 |
226 class CountingDownloadFile : public DownloadFileImpl { | |
benjhayden
2012/10/09 18:29:05
Want to put this in content/public/test?
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
Not until there's some need for it beyond this fil
| |
227 public: | |
228 CountingDownloadFile( | |
229 const DownloadCreateInfo* info, | |
230 scoped_ptr<content::ByteStreamReader> stream, | |
231 DownloadRequestHandleInterface* request_handle, | |
232 scoped_refptr<content::DownloadManager> download_manager, | |
233 bool calculate_hash, | |
234 scoped_ptr<content::PowerSaveBlocker> power_save_blocker, | |
235 const net::BoundNetLog& bound_net_log) | |
236 : DownloadFileImpl(info, stream.Pass(), request_handle, download_manager, | |
237 calculate_hash, power_save_blocker.Pass(), | |
238 bound_net_log) {} | |
239 | |
240 virtual ~CountingDownloadFile() { | |
241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
242 active_files_--; | |
243 } | |
244 | |
245 virtual content::DownloadInterruptReason Initialize() OVERRIDE { | |
246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
247 active_files_++; | |
248 return DownloadFileImpl::Initialize(); | |
249 } | |
250 | |
251 static void GetNumberActiveFiles(int* result) { | |
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
253 *result = active_files_; | |
254 } | |
255 | |
256 // Can be called on any thread, and will block (running message loop) | |
257 // until data is returned. | |
258 static int GetNumberActiveFilesFromFileThread() { | |
259 int result; | |
benjhayden
2012/10/09 18:29:05
Can you just '= -1'? Un-initialized variables make
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
Whoops. Sure. Done.
| |
260 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | |
261 base::Bind(&CountingDownloadFile::GetNumberActiveFiles, &result), | |
262 MessageLoop::current()->QuitClosure()); | |
263 MessageLoop::current()->Run(); | |
264 return result; | |
265 } | |
266 | |
267 private: | |
268 static int active_files_; | |
269 }; | |
270 | |
271 int CountingDownloadFile::active_files_ = 0; | |
272 | |
273 class CountingDownloadFileFactory : public DownloadFileFactory { | |
benjhayden
2012/10/09 18:29:05
Templatize DownloadFileFactory?
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
Templatize? I'm not sure what you mean? What wou
benjhayden
2012/10/09 20:01:47
Ah, sorry, I was wondering if the pattern of a DFF
| |
274 public: | |
275 CountingDownloadFileFactory() {} | |
276 virtual ~CountingDownloadFileFactory() {} | |
277 | |
278 // DownloadFileFactory interface. | |
279 virtual content::DownloadFile* CreateFile( | |
280 DownloadCreateInfo* info, | |
281 scoped_ptr<content::ByteStreamReader> stream, | |
282 DownloadManager* download_manager, | |
283 bool calculate_hash, | |
284 const net::BoundNetLog& bound_net_log) OVERRIDE { | |
285 return new CountingDownloadFile( | |
286 info, stream.Pass(), new DownloadRequestHandle(info->request_handle), | |
287 download_manager, calculate_hash, | |
288 scoped_ptr<content::PowerSaveBlocker>( | |
289 new content::PowerSaveBlocker( | |
290 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension, | |
291 "Download in progress")).Pass(), | |
292 bound_net_log); | |
293 } | |
294 }; | |
295 | |
227 } // namespace | 296 } // namespace |
228 | 297 |
229 class DownloadContentTest : public ContentBrowserTest { | 298 class DownloadContentTest : public ContentBrowserTest { |
230 protected: | 299 protected: |
231 virtual void SetUpOnMainThread() OVERRIDE { | 300 virtual void SetUpOnMainThread() OVERRIDE { |
232 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); | 301 ASSERT_TRUE(downloads_directory_.CreateUniqueTempDir()); |
233 | 302 |
234 ShellDownloadManagerDelegate* delegate = | 303 ShellDownloadManagerDelegate* delegate = |
235 static_cast<ShellDownloadManagerDelegate*>( | 304 static_cast<ShellDownloadManagerDelegate*>( |
236 shell()->web_contents()->GetBrowserContext() | 305 shell()->web_contents()->GetBrowserContext() |
(...skipping 19 matching lines...) Expand all Loading... | |
256 } | 325 } |
257 | 326 |
258 // Create a DownloadTestObserverInProgress that will wait for the | 327 // Create a DownloadTestObserverInProgress that will wait for the |
259 // specified number of downloads to start. | 328 // specified number of downloads to start. |
260 DownloadTestObserver* CreateInProgressWaiter( | 329 DownloadTestObserver* CreateInProgressWaiter( |
261 Shell* shell, int num_downloads) { | 330 Shell* shell, int num_downloads) { |
262 DownloadManager* download_manager = DownloadManagerForShell(shell); | 331 DownloadManager* download_manager = DownloadManagerForShell(shell); |
263 return new DownloadTestObserverInProgress(download_manager, num_downloads); | 332 return new DownloadTestObserverInProgress(download_manager, num_downloads); |
264 } | 333 } |
265 | 334 |
335 // Note: Cannot be used with other alternative DownloadFileFactorys | |
benjhayden
2012/10/09 18:29:05
How likely is it that we'd want to use Counting* w
Randy Smith (Not in Mondays)
2012/10/09 19:54:59
It's my usual metric: I don't want to add complexi
| |
336 void SetupEnsureNoPendingDownloads() { | |
337 GetDownloadFileManager()->SetFileFactoryForTesting( | |
338 scoped_ptr<content::DownloadFileFactory>( | |
339 new CountingDownloadFileFactory()).Pass()); | |
340 } | |
341 | |
266 bool EnsureNoPendingDownloads() { | 342 bool EnsureNoPendingDownloads() { |
267 bool result = true; | 343 bool result = true; |
268 BrowserThread::PostTask( | 344 BrowserThread::PostTask( |
269 BrowserThread::IO, FROM_HERE, | 345 BrowserThread::IO, FROM_HERE, |
270 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 346 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); |
271 MessageLoop::current()->Run(); | 347 MessageLoop::current()->Run(); |
272 return result && DownloadManager::EnsureNoPendingDownloadsForTesting(); | 348 return result && |
349 (CountingDownloadFile::GetNumberActiveFilesFromFileThread() == 0); | |
273 } | 350 } |
274 | 351 |
275 void DownloadAndWait(Shell* shell, const GURL& url) { | 352 void DownloadAndWait(Shell* shell, const GURL& url) { |
276 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); | 353 scoped_ptr<DownloadTestObserver> observer(CreateWaiter(shell, 1)); |
277 NavigateToURL(shell, url); | 354 NavigateToURL(shell, url); |
278 observer->WaitForFinished(); | 355 observer->WaitForFinished(); |
279 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); | 356 EXPECT_EQ(1u, observer->NumDownloadsSeenInState(DownloadItem::COMPLETE)); |
280 } | 357 } |
281 | 358 |
282 // Checks that |path| is has |file_size| bytes, and matches the |value| | 359 // Checks that |path| is has |file_size| bytes, and matches the |value| |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 // DownloadItems are created in the IN_PROGRESS state and made known | 412 // DownloadItems are created in the IN_PROGRESS state and made known |
336 // to the DownloadManager immediately, so any ModelChanged event on | 413 // to the DownloadManager immediately, so any ModelChanged event on |
337 // the DownloadManager after navigation would allow the observer to | 414 // the DownloadManager after navigation would allow the observer to |
338 // return. However, the only ModelChanged() event the code will | 415 // return. However, the only ModelChanged() event the code will |
339 // currently fire is in OnCreateDownloadEntryComplete, at which | 416 // currently fire is in OnCreateDownloadEntryComplete, at which |
340 // point the download item will be in the state we need. | 417 // point the download item will be in the state we need. |
341 // The right way to fix this is to create finer grained states on the | 418 // The right way to fix this is to create finer grained states on the |
342 // DownloadItem, and wait for the state that indicates the item has been | 419 // DownloadItem, and wait for the state that indicates the item has been |
343 // entered in the history and made visible in the UI. | 420 // entered in the history and made visible in the UI. |
344 | 421 |
422 SetupEnsureNoPendingDownloads(); | |
423 | |
345 // Create a download, wait until it's started, and confirm | 424 // Create a download, wait until it's started, and confirm |
346 // we're in the expected state. | 425 // we're in the expected state. |
347 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); | 426 scoped_ptr<DownloadTestObserver> observer(CreateInProgressWaiter(shell(), 1)); |
348 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 427 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
349 observer->WaitForFinished(); | 428 observer->WaitForFinished(); |
350 | 429 |
351 std::vector<DownloadItem*> downloads; | 430 std::vector<DownloadItem*> downloads; |
352 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 431 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
353 ASSERT_EQ(1u, downloads.size()); | 432 ASSERT_EQ(1u, downloads.size()); |
354 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); | 433 ASSERT_EQ(DownloadItem::IN_PROGRESS, downloads[0]->GetState()); |
355 | 434 |
356 // Cancel the download and wait for download system quiesce. | 435 // Cancel the download and wait for download system quiesce. |
357 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); | 436 downloads[0]->Delete(DownloadItem::DELETE_DUE_TO_USER_DISCARD); |
358 scoped_refptr<DownloadTestFlushObserver> flush_observer( | 437 scoped_refptr<DownloadTestFlushObserver> flush_observer( |
359 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); | 438 new DownloadTestFlushObserver(DownloadManagerForShell(shell()))); |
360 flush_observer->WaitForFlush(); | 439 flush_observer->WaitForFlush(); |
361 | 440 |
362 // Get the important info from other threads and check it. | 441 // Get the important info from other threads and check it. |
363 EXPECT_TRUE(EnsureNoPendingDownloads()); | 442 EXPECT_TRUE(EnsureNoPendingDownloads()); |
364 } | 443 } |
365 | 444 |
366 // Check that downloading multiple (in this case, 2) files does not result in | 445 // Check that downloading multiple (in this case, 2) files does not result in |
367 // corrupted files. | 446 // corrupted files. |
368 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { | 447 IN_PROC_BROWSER_TEST_F(DownloadContentTest, MultiDownload) { |
448 SetupEnsureNoPendingDownloads(); | |
449 | |
369 // Create a download, wait until it's started, and confirm | 450 // Create a download, wait until it's started, and confirm |
370 // we're in the expected state. | 451 // we're in the expected state. |
371 scoped_ptr<DownloadTestObserver> observer1( | 452 scoped_ptr<DownloadTestObserver> observer1( |
372 CreateInProgressWaiter(shell(), 1)); | 453 CreateInProgressWaiter(shell(), 1)); |
373 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); | 454 NavigateToURL(shell(), GURL(URLRequestSlowDownloadJob::kUnknownSizeUrl)); |
374 observer1->WaitForFinished(); | 455 observer1->WaitForFinished(); |
375 | 456 |
376 std::vector<DownloadItem*> downloads; | 457 std::vector<DownloadItem*> downloads; |
377 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); | 458 DownloadManagerForShell(shell())->GetAllDownloads(&downloads); |
378 ASSERT_EQ(1u, downloads.size()); | 459 ASSERT_EQ(1u, downloads.size()); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
519 file_factory->GetAllRenameCallbacks(&callbacks); | 600 file_factory->GetAllRenameCallbacks(&callbacks); |
520 ASSERT_TRUE(callbacks.empty()); | 601 ASSERT_TRUE(callbacks.empty()); |
521 file_factory->GetAllDetachCallbacks(&callbacks); | 602 file_factory->GetAllDetachCallbacks(&callbacks); |
522 ASSERT_EQ(1u, callbacks.size()); | 603 ASSERT_EQ(1u, callbacks.size()); |
523 callbacks[0].Run(); | 604 callbacks[0].Run(); |
524 callbacks.clear(); | 605 callbacks.clear(); |
525 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); | 606 EXPECT_EQ(DownloadItem::COMPLETE, items[0]->GetState()); |
526 } | 607 } |
527 | 608 |
528 } // namespace content | 609 } // namespace content |
OLD | NEW |