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 #include "base/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
6 #include "base/synchronization/waitable_event.h" | 6 #include "base/synchronization/waitable_event.h" |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 #include "chrome/common/net/url_fetcher.h" | 9 #include "chrome/common/net/url_fetcher.h" |
10 #include "net/http/http_response_headers.h" | 10 #include "net/http/http_response_headers.h" |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 350 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
351 ASSERT_TRUE(test_server.Start()); | 351 ASSERT_TRUE(test_server.Start()); |
352 | 352 |
353 // Get a small file. | 353 // Get a small file. |
354 const char* kFileToFetch = "simple.html"; | 354 const char* kFileToFetch = "simple.html"; |
355 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 355 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
356 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); | 356 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); |
357 | 357 |
358 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 358 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
359 | 359 |
360 ASSERT_TRUE(file_util::PathExists(temp_file_)) | |
361 << temp_file_.value() << " not found."; | |
362 | |
363 // Deleting the fetcher should remove the file. | |
364 delete fetcher_; | |
365 | |
366 MessageLoop::current()->RunAllPending(); | |
367 ASSERT_FALSE(file_util::PathExists(temp_file_)) | 360 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
368 << temp_file_.value() << " not removed."; | 361 << temp_file_.value() << " not removed."; |
369 } | 362 } |
370 | 363 |
371 TEST_F(URLFetcherTempFileTest, LargeGet) { | 364 TEST_F(URLFetcherTempFileTest, LargeGet) { |
372 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 365 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
373 ASSERT_TRUE(test_server.Start()); | 366 ASSERT_TRUE(test_server.Start()); |
374 | 367 |
375 // Get a file large enough to require more than one read into | 368 // Get a file large enough to require more than one read into |
376 // URLFetcher::Core's IOBuffer. | 369 // URLFetcher::Core's IOBuffer. |
377 const char* kFileToFetch = "animate1.gif"; | 370 const char* kFileToFetch = "animate1.gif"; |
378 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 371 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
379 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); | 372 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); |
380 | 373 |
381 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 374 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
382 } | 375 } |
383 | 376 |
384 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { | 377 TEST_F(URLFetcherTempFileTest, CanTakeOwnershipOfFile) { |
385 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 378 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
386 ASSERT_TRUE(test_server.Start()); | 379 ASSERT_TRUE(test_server.Start()); |
387 | 380 |
388 // Get a small file. | 381 // Get a small file. |
389 const char* kFileToFetch = "simple.html"; | 382 const char* kFileToFetch = "simple.html"; |
390 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 383 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); |
391 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); | 384 CreateFetcher(test_server.GetURL(kTestServerFilePrefix + kFileToFetch)); |
392 | 385 |
393 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 386 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
394 | 387 |
395 ASSERT_TRUE(file_util::PathExists(temp_file_)) | |
396 << temp_file_.value() << " not found."; | |
397 | |
398 // Deleting the fetcher should remove the file. | |
399 delete fetcher_; | |
400 | |
401 MessageLoop::current()->RunAllPending(); | 388 MessageLoop::current()->RunAllPending(); |
402 ASSERT_FALSE(file_util::PathExists(temp_file_)) | 389 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
403 << temp_file_.value() << " not removed."; | 390 << temp_file_.value() << " not removed."; |
404 } | 391 } |
405 | 392 |
406 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We | 393 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We |
407 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call | 394 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call |
408 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit | 395 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit |
409 // confusing in that case because GTest doesn't know about the refcounting. | 396 // confusing in that case because GTest doesn't know about the refcounting. |
410 // It's less confusing to just do it this way. | 397 // It's less confusing to just do it this way. |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 | 626 |
640 void URLFetcherTempFileTest::OnURLFetchComplete(const URLFetcher* source) { | 627 void URLFetcherTempFileTest::OnURLFetchComplete(const URLFetcher* source) { |
641 EXPECT_TRUE(source->status().is_success()); | 628 EXPECT_TRUE(source->status().is_success()); |
642 EXPECT_EQ(source->response_code(), 200); | 629 EXPECT_EQ(source->response_code(), 200); |
643 | 630 |
644 EXPECT_TRUE(source->GetResponseAsFilePath( | 631 EXPECT_TRUE(source->GetResponseAsFilePath( |
645 take_ownership_of_temp_file_, &temp_file_)); | 632 take_ownership_of_temp_file_, &temp_file_)); |
646 | 633 |
647 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 634 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
648 | 635 |
| 636 delete fetcher_; |
| 637 |
649 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 638 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
650 } | 639 } |
651 | 640 |
652 void URLFetcherTempFileTest::OnURLFetchComplete( | 641 void URLFetcherTempFileTest::OnURLFetchComplete( |
653 const URLFetcher* source, | 642 const URLFetcher* source, |
654 const GURL& url, | 643 const GURL& url, |
655 const net::URLRequestStatus& status, | 644 const net::URLRequestStatus& status, |
656 int response_code, | 645 int response_code, |
657 const net::ResponseCookies& cookies, | 646 const net::ResponseCookies& cookies, |
658 const std::string& data) { | 647 const std::string& data) { |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 FROM_HERE, | 893 FROM_HERE, |
905 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); | 894 new CurriedTask(new MessageLoop::QuitTask(), MessageLoop::current())); |
906 MessageLoop::current()->Run(); | 895 MessageLoop::current()->Run(); |
907 EXPECT_EQ(1, GetNumFetcherCores()); | 896 EXPECT_EQ(1, GetNumFetcherCores()); |
908 URLFetcher::CancelAll(); | 897 URLFetcher::CancelAll(); |
909 EXPECT_EQ(0, GetNumFetcherCores()); | 898 EXPECT_EQ(0, GetNumFetcherCores()); |
910 delete fetcher_; | 899 delete fetcher_; |
911 } | 900 } |
912 | 901 |
913 } // namespace. | 902 } // namespace. |
OLD | NEW |