Chromium Code Reviews| 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 "base/message_loop.h" | |
| 6 #include "content/browser/renderer_host/resource_handler.h" | |
| 7 #include "content/browser/renderer_host/resource_loader.h" | |
| 8 #include "content/browser/renderer_host/resource_loader_delegate.h" | |
| 9 #include "content/browser/renderer_host/resource_request_info_impl.h" | |
| 10 #include "content/browser/renderer_host/resource_scheduler.h" | |
| 11 #include "content/public/browser/global_request_id.h" | |
| 12 #include "net/url_request/url_request.h" | |
| 13 #include "net/url_request/url_request_test_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 namespace { | |
| 19 class TestRequestFactory; | |
| 20 | |
| 21 const int kChildId = 30; | |
| 22 const int kRouteId = 75; | |
| 23 | |
| 24 class DummyResourceHandler : public ResourceHandler { | |
| 25 // ResourceHandler interface | |
| 26 virtual bool OnUploadProgress(int request_id, | |
| 27 uint64 position, | |
| 28 uint64 size) OVERRIDE { return true; } | |
| 29 virtual bool OnRequestRedirected(int request_id, const GURL& url, | |
| 30 ResourceResponse* response, | |
| 31 bool* defer) OVERRIDE { return true; } | |
| 32 virtual bool OnResponseStarted(int request_id, | |
| 33 ResourceResponse* response, | |
| 34 bool* defer) OVERRIDE { return true; } | |
| 35 virtual bool OnWillStart(int request_id, | |
| 36 const GURL& url, | |
| 37 bool* defer) OVERRIDE { return true; } | |
| 38 virtual bool OnWillRead(int request_id, | |
| 39 net::IOBuffer** buf, | |
| 40 int* buf_size, | |
| 41 int min_size) OVERRIDE { return true; } | |
| 42 virtual bool OnReadCompleted(int request_id, | |
| 43 int bytes_read, | |
| 44 bool* defer) OVERRIDE { return true; } | |
| 45 virtual bool OnResponseCompleted( | |
| 46 int request_id, | |
| 47 const net::URLRequestStatus& status, | |
| 48 const std::string& security_info) OVERRIDE { return true; } | |
| 49 }; | |
| 50 | |
| 51 class TestRequest : public ResourceLoaderDelegate { | |
| 52 public: | |
| 53 int id() const { return request_id_; } | |
| 54 bool started() const { return started_; } | |
| 55 linked_ptr<ResourceLoader>& loader() { return loader_; } | |
| 56 | |
| 57 // ResourceLoaderDelegate interface | |
| 58 virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( | |
| 59 ResourceLoader* loader, | |
| 60 net::AuthChallengeInfo* auth_info) OVERRIDE { return NULL; } | |
| 61 virtual bool AcceptAuthRequest( | |
| 62 ResourceLoader* loader, | |
| 63 net::AuthChallengeInfo* auth_info) OVERRIDE { return false; } | |
| 64 virtual bool AcceptSSLClientCertificateRequest( | |
| 65 ResourceLoader* loader, | |
| 66 net::SSLCertRequestInfo* cert_info) OVERRIDE { return false; } | |
| 67 virtual bool HandleExternalProtocol( | |
| 68 ResourceLoader* loader, | |
| 69 const GURL& url) OVERRIDE { return false; } | |
| 70 virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE { | |
| 71 started_ = true; | |
| 72 } | |
| 73 virtual void DidReceiveRedirect(ResourceLoader* loader, | |
| 74 const GURL& new_url) OVERRIDE {} | |
| 75 virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE {} | |
| 76 virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE {} | |
| 77 | |
| 78 private: | |
| 79 friend class TestRequestFactory; | |
| 80 TestRequest(int request_id, | |
| 81 scoped_ptr<net::URLRequest> url_request, | |
| 82 scoped_ptr<ResourceHandler> handler) | |
| 83 : request_id_(request_id), | |
| 84 started_(false), | |
| 85 loader_(new ResourceLoader(url_request.Pass(), handler.Pass(), this)) {} | |
| 86 | |
| 87 int request_id_; | |
| 88 bool started_; | |
| 89 linked_ptr<ResourceLoader> loader_; | |
| 90 }; | |
| 91 | |
| 92 class TestRequestFactory { | |
| 93 public: | |
| 94 TestRequestFactory() | |
| 95 : next_request_id_(100) { | |
| 96 } | |
| 97 | |
| 98 TestRequest* NewRequest(const char* url, net::RequestPriority priority) { | |
| 99 scoped_ptr<net::URLRequest> url_request( | |
| 100 context_.CreateRequest(GURL(url), NULL)); | |
| 101 url_request->set_priority(priority); | |
| 102 | |
| 103 ResourceRequestInfoImpl* extra_info = | |
| 104 new ResourceRequestInfoImpl( | |
| 105 PROCESS_TYPE_RENDERER, | |
| 106 kChildId, | |
| 107 kRouteId, | |
| 108 0, | |
| 109 next_request_id_, | |
| 110 true, | |
| 111 0, | |
| 112 false, | |
| 113 0, | |
| 114 ResourceType::SUB_RESOURCE, | |
| 115 PAGE_TRANSITION_LINK, | |
| 116 false, // is download | |
| 117 false, | |
| 118 false, | |
| 119 WebKit::WebReferrerPolicyDefault, | |
| 120 NULL); | |
| 121 extra_info->AssociateWithRequest(url_request.get()); | |
| 122 | |
| 123 scoped_ptr<ResourceHandler> handler(new DummyResourceHandler()); | |
| 124 return new TestRequest( | |
| 125 next_request_id_++, url_request.Pass(), handler.Pass()); | |
| 126 } | |
| 127 | |
| 128 private: | |
| 129 int next_request_id_; | |
| 130 net::TestURLRequestContext context_; | |
| 131 }; | |
| 132 | |
| 133 } // unnamed namespace | |
| 134 | |
| 135 class ResourceSchedulerTest : public testing::Test { | |
| 136 protected: | |
| 137 ResourceSchedulerTest() | |
| 138 : message_loop_(MessageLoop::TYPE_IO) { | |
| 139 scheduler_.OnCreate(kChildId, kRouteId); | |
| 140 } | |
| 141 | |
| 142 virtual ~ResourceSchedulerTest() { | |
| 143 scheduler_.OnDestroy(kChildId, kRouteId); | |
| 144 } | |
| 145 | |
| 146 MessageLoop message_loop_; | |
| 147 ResourceScheduler scheduler_; | |
| 148 TestRequestFactory factory_; | |
| 149 }; | |
| 150 | |
| 151 TEST_F(ResourceSchedulerTest, OneIsolatedRequest) { | |
|
willchan no longer on Chromium
2012/11/21 09:04:30
OneIsolatedLowRequest
James Simonsen
2012/11/27 02:20:51
Done.
| |
| 152 scoped_ptr<TestRequest> request( | |
| 153 factory_.NewRequest("http://host/1", net::LOW)); | |
| 154 scoped_ptr<ResourceScheduler::LoadHandle> handle(scheduler_.ScheduleLoad( | |
| 155 kChildId, kRouteId, request->id(), request->loader())); | |
| 156 EXPECT_TRUE(request->started()); | |
| 157 } | |
| 158 | |
| 159 TEST_F(ResourceSchedulerTest, LowBlocksUntilIdle) { | |
| 160 scoped_ptr<TestRequest> high( | |
| 161 factory_.NewRequest("http://host/high", net::HIGHEST)); | |
| 162 scoped_ptr<TestRequest> low( | |
| 163 factory_.NewRequest("http://host/low", net::LOW)); | |
| 164 scoped_ptr<ResourceScheduler::LoadHandle> high_handle(scheduler_.ScheduleLoad( | |
| 165 kChildId, kRouteId, high->id(), high->loader())); | |
| 166 scoped_ptr<ResourceScheduler::LoadHandle> low_handle(scheduler_.ScheduleLoad( | |
| 167 kChildId, kRouteId, low->id(), low->loader())); | |
| 168 EXPECT_TRUE(high->started()); | |
| 169 EXPECT_FALSE(low->started()); | |
| 170 high_handle.reset(); | |
| 171 EXPECT_TRUE(low->started()); | |
| 172 } | |
| 173 | |
| 174 TEST_F(ResourceSchedulerTest, LowBlocksUntilPaint) { | |
| 175 scoped_ptr<TestRequest> high( | |
| 176 factory_.NewRequest("http://host/high", net::HIGHEST)); | |
| 177 scoped_ptr<TestRequest> low( | |
| 178 factory_.NewRequest("http://host/low", net::LOW)); | |
| 179 scoped_ptr<ResourceScheduler::LoadHandle> high_handle(scheduler_.ScheduleLoad( | |
| 180 kChildId, kRouteId, high->id(), high->loader())); | |
| 181 scoped_ptr<ResourceScheduler::LoadHandle> low_handle(scheduler_.ScheduleLoad( | |
| 182 kChildId, kRouteId, low->id(), low->loader())); | |
| 183 EXPECT_TRUE(high->started()); | |
| 184 EXPECT_FALSE(low->started()); | |
| 185 scheduler_.OnPaint(kChildId, kRouteId); | |
| 186 EXPECT_TRUE(low->started()); | |
| 187 } | |
| 188 | |
| 189 TEST_F(ResourceSchedulerTest, NavigationResetsPaintState) { | |
| 190 scoped_ptr<TestRequest> high( | |
| 191 factory_.NewRequest("http://host/high", net::HIGHEST)); | |
| 192 scoped_ptr<TestRequest> low( | |
| 193 factory_.NewRequest("http://host/low", net::LOW)); | |
| 194 scheduler_.OnPaint(kChildId, kRouteId); | |
| 195 scheduler_.OnNavigate(kChildId, kRouteId); | |
| 196 scoped_ptr<ResourceScheduler::LoadHandle> high_handle(scheduler_.ScheduleLoad( | |
| 197 kChildId, kRouteId, high->id(), high->loader())); | |
| 198 scoped_ptr<ResourceScheduler::LoadHandle> low_handle(scheduler_.ScheduleLoad( | |
| 199 kChildId, kRouteId, low->id(), low->loader())); | |
| 200 EXPECT_TRUE(high->started()); | |
| 201 EXPECT_FALSE(low->started()); | |
| 202 } | |
| 203 | |
| 204 TEST_F(ResourceSchedulerTest, NoRouteIdStartsImmediately) { | |
| 205 scoped_ptr<TestRequest> request( | |
| 206 factory_.NewRequest("http://host/1", net::LOW)); | |
| 207 scoped_ptr<ResourceScheduler::LoadHandle> handle(scheduler_.ScheduleLoad( | |
| 208 kChildId, 0, request->id(), request->loader())); | |
| 209 EXPECT_TRUE(request->started()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(ResourceSchedulerTest, DestroyTabBeforeCancelingLoad) { | |
| 213 const int new_route_id = kRouteId +1; | |
| 214 scheduler_.OnCreate(kChildId, new_route_id); | |
| 215 scoped_ptr<TestRequest> request( | |
| 216 factory_.NewRequest("http://host/1", net::LOW)); | |
| 217 scoped_ptr<ResourceScheduler::LoadHandle> handle(scheduler_.ScheduleLoad( | |
| 218 kChildId, new_route_id, request->id(), request->loader())); | |
| 219 scheduler_.OnDestroy(kChildId, new_route_id); | |
| 220 // Note that new_route_id is destroyed before handle goes out of scope. | |
| 221 } | |
| 222 | |
| 223 } // namespace content | |
| OLD | NEW |