| 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 "content/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 CreateFetcher(test_server.GetURL( | 319 CreateFetcher(test_server.GetURL( |
| 320 std::string(kTestServerFilePrefix) + kFileToFetch)); | 320 std::string(kTestServerFilePrefix) + kFileToFetch)); |
| 321 | 321 |
| 322 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 322 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
| 323 | 323 |
| 324 MessageLoop::current()->RunAllPending(); | 324 MessageLoop::current()->RunAllPending(); |
| 325 ASSERT_FALSE(file_util::PathExists(temp_file_)) | 325 ASSERT_FALSE(file_util::PathExists(temp_file_)) |
| 326 << temp_file_.value() << " not removed."; | 326 << temp_file_.value() << " not removed."; |
| 327 } | 327 } |
| 328 | 328 |
| 329 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We | |
| 330 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call | |
| 331 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit | |
| 332 // confusing in that case because GTest doesn't know about the refcounting. | |
| 333 // It's less confusing to just do it this way. | |
| 334 class FetcherWrapperTask : public Task { | |
| 335 public: | |
| 336 FetcherWrapperTask(URLFetcherTest* test, const GURL& url) | |
| 337 : test_(test), url_(url) { } | |
| 338 virtual void Run() { | |
| 339 test_->CreateFetcher(url_); | |
| 340 } | |
| 341 | |
| 342 private: | |
| 343 URLFetcherTest* test_; | |
| 344 GURL url_; | |
| 345 }; | |
| 346 | |
| 347 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 329 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
| 348 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); | 330 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::POST, this); |
| 349 fetcher_->SetRequestContext(new TestURLRequestContextGetter( | 331 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 350 io_message_loop_proxy())); | 332 io_message_loop_proxy())); |
| 351 fetcher_->SetUploadData("application/x-www-form-urlencoded", | 333 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 352 "bobsyeruncle"); | 334 "bobsyeruncle"); |
| 353 fetcher_->Start(); | 335 fetcher_->Start(); |
| 354 } | 336 } |
| 355 | 337 |
| 356 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { | 338 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 546 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 565 ASSERT_TRUE(test_server.Start()); | 547 ASSERT_TRUE(test_server.Start()); |
| 566 | 548 |
| 567 // Create a separate thread that will create the URLFetcher. The current | 549 // Create a separate thread that will create the URLFetcher. The current |
| 568 // (main) thread will do the IO, and when the fetch is complete it will | 550 // (main) thread will do the IO, and when the fetch is complete it will |
| 569 // terminate the main thread's message loop; then the other thread's | 551 // terminate the main thread's message loop; then the other thread's |
| 570 // message loop will be shut down automatically as the thread goes out of | 552 // message loop will be shut down automatically as the thread goes out of |
| 571 // scope. | 553 // scope. |
| 572 base::Thread t("URLFetcher test thread"); | 554 base::Thread t("URLFetcher test thread"); |
| 573 ASSERT_TRUE(t.Start()); | 555 ASSERT_TRUE(t.Start()); |
| 574 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, | 556 t.message_loop()->PostTask( |
| 575 test_server.GetURL("defaultresponse"))); | 557 FROM_HERE, |
| 558 base::Bind(&URLFetcherTest::CreateFetcher, |
| 559 base::Unretained(this), |
| 560 test_server.GetURL("defaultresponse"))); |
| 576 | 561 |
| 577 MessageLoop::current()->Run(); | 562 MessageLoop::current()->Run(); |
| 578 } | 563 } |
| 579 | 564 |
| 580 #if defined(OS_MACOSX) | 565 #if defined(OS_MACOSX) |
| 581 // SIGSEGV on Mac: http://crbug.com/60426 | 566 // SIGSEGV on Mac: http://crbug.com/60426 |
| 582 TEST_F(URLFetcherPostTest, DISABLED_Basic) { | 567 TEST_F(URLFetcherPostTest, DISABLED_Basic) { |
| 583 #else | 568 #else |
| 584 TEST_F(URLFetcherPostTest, Basic) { | 569 TEST_F(URLFetcherPostTest, Basic) { |
| 585 #endif | 570 #endif |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 719 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); | 704 manager, "", 200, 3, 2000, 2.0, 0.0, 4000)); |
| 720 manager->OverrideEntryForTests(url, entry); | 705 manager->OverrideEntryForTests(url, entry); |
| 721 | 706 |
| 722 // Create a separate thread that will create the URLFetcher. The current | 707 // Create a separate thread that will create the URLFetcher. The current |
| 723 // (main) thread will do the IO, and when the fetch is complete it will | 708 // (main) thread will do the IO, and when the fetch is complete it will |
| 724 // terminate the main thread's message loop; then the other thread's | 709 // terminate the main thread's message loop; then the other thread's |
| 725 // message loop will be shut down automatically as the thread goes out of | 710 // message loop will be shut down automatically as the thread goes out of |
| 726 // scope. | 711 // scope. |
| 727 base::Thread t("URLFetcher test thread"); | 712 base::Thread t("URLFetcher test thread"); |
| 728 ASSERT_TRUE(t.Start()); | 713 ASSERT_TRUE(t.Start()); |
| 729 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 714 t.message_loop()->PostTask( |
| 715 FROM_HERE, |
| 716 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); |
| 730 | 717 |
| 731 MessageLoop::current()->Run(); | 718 MessageLoop::current()->Run(); |
| 732 | 719 |
| 733 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 720 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
| 734 } | 721 } |
| 735 | 722 |
| 736 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { | 723 TEST_F(URLFetcherCancelTest, CancelWhileDelayedStartTaskPending) { |
| 737 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 724 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 738 ASSERT_TRUE(test_server.Start()); | 725 ASSERT_TRUE(test_server.Start()); |
| 739 | 726 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 750 manager->OverrideEntryForTests(url, entry); | 737 manager->OverrideEntryForTests(url, entry); |
| 751 // Fake that a request has just started. | 738 // Fake that a request has just started. |
| 752 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); | 739 entry->ReserveSendingTimeForNextRequest(base::TimeTicks()); |
| 753 | 740 |
| 754 // The next request we try to send will be delayed by ~4 seconds. | 741 // The next request we try to send will be delayed by ~4 seconds. |
| 755 // The slower the test runs, the less the delay will be (since it takes the | 742 // The slower the test runs, the less the delay will be (since it takes the |
| 756 // time difference from now). | 743 // time difference from now). |
| 757 | 744 |
| 758 base::Thread t("URLFetcher test thread"); | 745 base::Thread t("URLFetcher test thread"); |
| 759 ASSERT_TRUE(t.Start()); | 746 ASSERT_TRUE(t.Start()); |
| 760 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 747 t.message_loop()->PostTask( |
| 748 FROM_HERE, |
| 749 base::Bind(&URLFetcherTest::CreateFetcher, base::Unretained(this), url)); |
| 761 | 750 |
| 762 MessageLoop::current()->Run(); | 751 MessageLoop::current()->Run(); |
| 763 | 752 |
| 764 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 753 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
| 765 } | 754 } |
| 766 | 755 |
| 767 TEST_F(URLFetcherMultipleAttemptTest, SameData) { | 756 TEST_F(URLFetcherMultipleAttemptTest, SameData) { |
| 768 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 757 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 769 ASSERT_TRUE(test_server.Start()); | 758 ASSERT_TRUE(test_server.Start()); |
| 770 | 759 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 785 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. | 774 // Tests to make sure CancelAll() will successfully cancel existing URLFetchers. |
| 786 TEST_F(URLFetcherTest, CancelAll) { | 775 TEST_F(URLFetcherTest, CancelAll) { |
| 787 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 776 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 788 ASSERT_TRUE(test_server.Start()); | 777 ASSERT_TRUE(test_server.Start()); |
| 789 EXPECT_EQ(0, GetNumFetcherCores()); | 778 EXPECT_EQ(0, GetNumFetcherCores()); |
| 790 | 779 |
| 791 CreateFetcher(test_server.GetURL("defaultresponse")); | 780 CreateFetcher(test_server.GetURL("defaultresponse")); |
| 792 io_message_loop_proxy()->PostTaskAndReply( | 781 io_message_loop_proxy()->PostTaskAndReply( |
| 793 FROM_HERE, | 782 FROM_HERE, |
| 794 base::Bind(&CancelAllOnIO), | 783 base::Bind(&CancelAllOnIO), |
| 795 base::Bind(&MessageLoop::Quit, | 784 MessageLoop::QuitClosure()); |
| 796 base::Unretained(MessageLoop::current()))); | |
| 797 MessageLoop::current()->Run(); | 785 MessageLoop::current()->Run(); |
| 798 EXPECT_EQ(0, GetNumFetcherCores()); | 786 EXPECT_EQ(0, GetNumFetcherCores()); |
| 799 delete fetcher_; | 787 delete fetcher_; |
| 800 } | 788 } |
| 801 | 789 |
| 802 } // namespace. | 790 } // namespace. |
| OLD | NEW |