| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/resource_scheduler.h" |
| 6 |
| 7 #include "base/message_loop.h" |
| 8 #include "content/browser/browser_thread_impl.h" |
| 9 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 10 #include "content/browser/loader/resource_request_info_impl.h" |
| 11 #include "content/public/browser/resource_controller.h" |
| 12 #include "content/public/browser/resource_throttle.h" |
| 13 #include "net/url_request/url_request.h" |
| 14 #include "net/url_request/url_request_test_util.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webkit/glue/resource_type.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class TestRequestFactory; |
| 23 |
| 24 const int kChildId = 30; |
| 25 const int kRouteId = 75; |
| 26 |
| 27 class TestRequest : public ResourceController { |
| 28 public: |
| 29 TestRequest(scoped_ptr<ResourceThrottle> throttle, |
| 30 scoped_ptr<net::URLRequest> url_request) |
| 31 : started_(false), |
| 32 throttle_(throttle.Pass()), |
| 33 url_request_(url_request.Pass()) { |
| 34 throttle_->set_controller_for_testing(this); |
| 35 } |
| 36 |
| 37 bool started() const { return started_; } |
| 38 |
| 39 void Start() { |
| 40 bool deferred = false; |
| 41 throttle_->WillStartRequest(&deferred); |
| 42 started_ = !deferred; |
| 43 } |
| 44 |
| 45 protected: |
| 46 // ResourceController interface: |
| 47 virtual void Cancel() OVERRIDE {} |
| 48 virtual void CancelAndIgnore() OVERRIDE {} |
| 49 virtual void CancelWithError(int error_code) OVERRIDE {} |
| 50 virtual void Resume() OVERRIDE { started_ = true; } |
| 51 |
| 52 private: |
| 53 bool started_; |
| 54 scoped_ptr<ResourceThrottle> throttle_; |
| 55 scoped_ptr<net::URLRequest> url_request_; |
| 56 }; |
| 57 |
| 58 class CancelingTestRequest : public TestRequest { |
| 59 public: |
| 60 CancelingTestRequest(scoped_ptr<ResourceThrottle> throttle, |
| 61 scoped_ptr<net::URLRequest> url_request) |
| 62 : TestRequest(throttle.Pass(), url_request.Pass()) { |
| 63 } |
| 64 |
| 65 void set_request_to_cancel(scoped_ptr<TestRequest> request_to_cancel) { |
| 66 request_to_cancel_ = request_to_cancel.Pass(); |
| 67 } |
| 68 |
| 69 private: |
| 70 virtual void Resume() OVERRIDE { |
| 71 TestRequest::Resume(); |
| 72 request_to_cancel_.reset(); |
| 73 } |
| 74 |
| 75 scoped_ptr<TestRequest> request_to_cancel_; |
| 76 }; |
| 77 |
| 78 class ResourceSchedulerTest : public testing::Test { |
| 79 protected: |
| 80 ResourceSchedulerTest() |
| 81 : message_loop_(MessageLoop::TYPE_IO), |
| 82 ui_thread_(BrowserThread::UI, &message_loop_) { |
| 83 scheduler_.OnNavigate(kChildId, kRouteId); |
| 84 } |
| 85 |
| 86 virtual ~ResourceSchedulerTest() { |
| 87 } |
| 88 |
| 89 scoped_ptr<net::URLRequest> NewURLRequest(const char* url, |
| 90 net::RequestPriority priority, |
| 91 int route_id = kRouteId) { |
| 92 scoped_ptr<net::URLRequest> url_request( |
| 93 context_.CreateRequest(GURL(url), NULL)); |
| 94 url_request->set_priority(priority); |
| 95 ResourceRequestInfo::AllocateForTesting( |
| 96 url_request.get(), ResourceType::SUB_RESOURCE, NULL, kChildId, |
| 97 route_id); |
| 98 return url_request.Pass(); |
| 99 } |
| 100 |
| 101 TestRequest* NewRequest(const char* url, net::RequestPriority priority, |
| 102 int route_id = kRouteId) { |
| 103 scoped_ptr<net::URLRequest> url_request( |
| 104 NewURLRequest(url, priority, route_id)); |
| 105 scoped_ptr<ResourceThrottle> throttle(scheduler_.ScheduleRequest( |
| 106 kChildId, route_id, url_request.get())); |
| 107 TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass()); |
| 108 request->Start(); |
| 109 return request; |
| 110 } |
| 111 |
| 112 MessageLoop message_loop_; |
| 113 BrowserThreadImpl ui_thread_; |
| 114 ResourceDispatcherHostImpl rdh_; |
| 115 ResourceScheduler scheduler_; |
| 116 net::TestURLRequestContext context_; |
| 117 }; |
| 118 |
| 119 TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) { |
| 120 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOW)); |
| 121 EXPECT_TRUE(request->started()); |
| 122 } |
| 123 |
| 124 TEST_F(ResourceSchedulerTest, LowBlocksUntilIdle) { |
| 125 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 126 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW)); |
| 127 EXPECT_TRUE(high->started()); |
| 128 EXPECT_FALSE(low->started()); |
| 129 high.reset(); |
| 130 EXPECT_TRUE(low->started()); |
| 131 } |
| 132 |
| 133 TEST_F(ResourceSchedulerTest, LowBlocksUntilBodyInserted) { |
| 134 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 135 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW)); |
| 136 EXPECT_TRUE(high->started()); |
| 137 EXPECT_FALSE(low->started()); |
| 138 scheduler_.OnWillInsertBody(kChildId, kRouteId); |
| 139 EXPECT_TRUE(low->started()); |
| 140 } |
| 141 |
| 142 TEST_F(ResourceSchedulerTest, NavigationResetsState) { |
| 143 scheduler_.OnWillInsertBody(kChildId, kRouteId); |
| 144 scheduler_.OnNavigate(kChildId, kRouteId); |
| 145 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 146 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW)); |
| 147 EXPECT_TRUE(high->started()); |
| 148 EXPECT_FALSE(low->started()); |
| 149 } |
| 150 |
| 151 TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) { |
| 152 const int route_id = 0; // Indicates a background request. |
| 153 scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOW, |
| 154 route_id)); |
| 155 EXPECT_TRUE(request->started()); |
| 156 } |
| 157 |
| 158 TEST_F(ResourceSchedulerTest, StartRequestsWhenIdle) { |
| 159 scoped_ptr<TestRequest> high1(NewRequest("http://host/high1", net::HIGHEST)); |
| 160 scoped_ptr<TestRequest> high2(NewRequest("http://host/high2", net::HIGHEST)); |
| 161 scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW)); |
| 162 EXPECT_TRUE(high1->started()); |
| 163 EXPECT_TRUE(high2->started()); |
| 164 EXPECT_FALSE(low->started()); |
| 165 high1.reset(); |
| 166 EXPECT_FALSE(low->started()); |
| 167 high2.reset(); |
| 168 EXPECT_TRUE(low->started()); |
| 169 } |
| 170 |
| 171 TEST_F(ResourceSchedulerTest, EvictedClientsIssuePendingRequests) { |
| 172 ScopedVector<TestRequest> low_requests; |
| 173 ScopedVector<TestRequest> high_requests; |
| 174 for (int i = 0; i < 6; i++) { |
| 175 scheduler_.OnNavigate(kChildId, kRouteId + i); |
| 176 high_requests.push_back( |
| 177 NewRequest("http://host/i", net::HIGHEST, kRouteId + i)); |
| 178 low_requests.push_back( |
| 179 NewRequest("http://host/i", net::LOW, kRouteId + i)); |
| 180 EXPECT_FALSE(low_requests[i]->started()); |
| 181 } |
| 182 EXPECT_TRUE(low_requests[0]->started()); |
| 183 } |
| 184 |
| 185 TEST_F(ResourceSchedulerTest, CancelOtherRequestsWhileResuming) { |
| 186 scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST)); |
| 187 scoped_ptr<TestRequest> low1(NewRequest("http://host/low1", net::LOW)); |
| 188 |
| 189 scoped_ptr<net::URLRequest> url_request( |
| 190 NewURLRequest("http://host/low2", net::LOW)); |
| 191 scoped_ptr<ResourceThrottle> throttle(scheduler_.ScheduleRequest( |
| 192 kChildId, kRouteId, url_request.get())); |
| 193 scoped_ptr<CancelingTestRequest> low2(new CancelingTestRequest( |
| 194 throttle.Pass(), url_request.Pass())); |
| 195 low2->Start(); |
| 196 |
| 197 scoped_ptr<TestRequest> low3(NewRequest("http://host/low3", net::LOW)); |
| 198 low2->set_request_to_cancel(low3.Pass()); |
| 199 scoped_ptr<TestRequest> low4(NewRequest("http://host/low4", net::LOW)); |
| 200 |
| 201 EXPECT_TRUE(high->started()); |
| 202 EXPECT_FALSE(low2->started()); |
| 203 high.reset(); |
| 204 EXPECT_TRUE(low1->started()); |
| 205 EXPECT_TRUE(low2->started()); |
| 206 EXPECT_TRUE(low4->started()); |
| 207 } |
| 208 |
| 209 } // unnamed namespace |
| 210 |
| 211 } // namespace content |
| OLD | NEW |