Chromium Code Reviews| Index: content/browser/renderer_host/resource_scheduler_unittest.cc |
| diff --git a/content/browser/renderer_host/resource_scheduler_unittest.cc b/content/browser/renderer_host/resource_scheduler_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..510a303f1919f45e0066591311c34689f38900cc |
| --- /dev/null |
| +++ b/content/browser/renderer_host/resource_scheduler_unittest.cc |
| @@ -0,0 +1,223 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/message_loop.h" |
| +#include "content/browser/renderer_host/resource_handler.h" |
| +#include "content/browser/renderer_host/resource_loader.h" |
| +#include "content/browser/renderer_host/resource_loader_delegate.h" |
| +#include "content/browser/renderer_host/resource_request_info_impl.h" |
| +#include "content/browser/renderer_host/resource_scheduler.h" |
| +#include "content/public/browser/global_request_id.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| +class TestRequestFactory; |
| + |
| +const int kChildId = 30; |
| +const int kRouteId = 75; |
| + |
| +class DummyResourceHandler : public ResourceHandler { |
| + // ResourceHandler interface |
| + virtual bool OnUploadProgress(int request_id, |
| + uint64 position, |
| + uint64 size) OVERRIDE { return true; } |
| + virtual bool OnRequestRedirected(int request_id, const GURL& url, |
| + ResourceResponse* response, |
| + bool* defer) OVERRIDE { return true; } |
| + virtual bool OnResponseStarted(int request_id, |
| + ResourceResponse* response, |
| + bool* defer) OVERRIDE { return true; } |
| + virtual bool OnWillStart(int request_id, |
| + const GURL& url, |
| + bool* defer) OVERRIDE { return true; } |
| + virtual bool OnWillRead(int request_id, |
| + net::IOBuffer** buf, |
| + int* buf_size, |
| + int min_size) OVERRIDE { return true; } |
| + virtual bool OnReadCompleted(int request_id, |
| + int bytes_read, |
| + bool* defer) OVERRIDE { return true; } |
| + virtual bool OnResponseCompleted( |
| + int request_id, |
| + const net::URLRequestStatus& status, |
| + const std::string& security_info) OVERRIDE { return true; } |
| +}; |
| + |
| +class TestRequest : public ResourceLoaderDelegate { |
| + public: |
| + int id() const { return request_id_; } |
| + bool started() const { return started_; } |
| + linked_ptr<ResourceLoader>& loader() { return loader_; } |
| + |
| + // ResourceLoaderDelegate interface |
| + virtual ResourceDispatcherHostLoginDelegate* CreateLoginDelegate( |
| + ResourceLoader* loader, |
| + net::AuthChallengeInfo* auth_info) OVERRIDE { return NULL; } |
| + virtual bool AcceptAuthRequest( |
| + ResourceLoader* loader, |
| + net::AuthChallengeInfo* auth_info) OVERRIDE { return false; } |
| + virtual bool AcceptSSLClientCertificateRequest( |
| + ResourceLoader* loader, |
| + net::SSLCertRequestInfo* cert_info) OVERRIDE { return false; } |
| + virtual bool HandleExternalProtocol( |
| + ResourceLoader* loader, |
| + const GURL& url) OVERRIDE { return false; } |
| + virtual void DidStartRequest(ResourceLoader* loader) OVERRIDE { |
| + started_ = true; |
| + } |
| + virtual void DidReceiveRedirect(ResourceLoader* loader, |
| + const GURL& new_url) OVERRIDE {} |
| + virtual void DidReceiveResponse(ResourceLoader* loader) OVERRIDE {} |
| + virtual void DidFinishLoading(ResourceLoader* loader) OVERRIDE {} |
| + |
| + private: |
| + friend class TestRequestFactory; |
| + TestRequest(int request_id, |
| + scoped_ptr<net::URLRequest> url_request, |
| + scoped_ptr<ResourceHandler> handler) |
| + : request_id_(request_id), |
| + started_(false), |
| + loader_(new ResourceLoader(url_request.Pass(), handler.Pass(), this)) {} |
| + |
| + int request_id_; |
| + bool started_; |
| + linked_ptr<ResourceLoader> loader_; |
| +}; |
| + |
| +class TestRequestFactory { |
| + public: |
| + TestRequestFactory() |
| + : next_request_id_(100) { |
| + } |
| + |
| + TestRequest* NewRequest(const char* url, net::RequestPriority priority) { |
| + scoped_ptr<net::URLRequest> url_request( |
| + context_.CreateRequest(GURL(url), NULL)); |
| + url_request->set_priority(priority); |
| + |
| + ResourceRequestInfoImpl* extra_info = |
| + new ResourceRequestInfoImpl( |
| + PROCESS_TYPE_RENDERER, |
| + kChildId, |
| + kRouteId, |
| + 0, |
| + next_request_id_, |
| + true, |
| + 0, |
| + false, |
| + 0, |
| + ResourceType::SUB_RESOURCE, |
| + PAGE_TRANSITION_LINK, |
| + false, // is download |
| + false, |
| + false, |
| + WebKit::WebReferrerPolicyDefault, |
| + NULL); |
| + extra_info->AssociateWithRequest(url_request.get()); |
| + |
| + scoped_ptr<ResourceHandler> handler(new DummyResourceHandler()); |
| + return new TestRequest( |
| + next_request_id_++, url_request.Pass(), handler.Pass()); |
| + } |
| + |
| + private: |
| + int next_request_id_; |
| + net::TestURLRequestContext context_; |
| +}; |
| + |
| +} // unnamed namespace |
| + |
| +class ResourceSchedulerTest : public testing::Test { |
| + protected: |
| + ResourceSchedulerTest() |
| + : message_loop_(MessageLoop::TYPE_IO) { |
| + scheduler_.OnCreate(kChildId, kRouteId); |
| + } |
| + |
| + virtual ~ResourceSchedulerTest() { |
| + scheduler_.OnDestroy(kChildId, kRouteId); |
| + } |
| + |
| + MessageLoop message_loop_; |
| + ResourceScheduler scheduler_; |
| + TestRequestFactory factory_; |
| +}; |
| + |
| +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.
|
| + scoped_ptr<TestRequest> request( |
| + factory_.NewRequest("http://host/1", net::LOW)); |
| + scoped_ptr<ResourceScheduler::LoadHandle> handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, request->id(), request->loader())); |
| + EXPECT_TRUE(request->started()); |
| +} |
| + |
| +TEST_F(ResourceSchedulerTest, LowBlocksUntilIdle) { |
| + scoped_ptr<TestRequest> high( |
| + factory_.NewRequest("http://host/high", net::HIGHEST)); |
| + scoped_ptr<TestRequest> low( |
| + factory_.NewRequest("http://host/low", net::LOW)); |
| + scoped_ptr<ResourceScheduler::LoadHandle> high_handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, high->id(), high->loader())); |
| + scoped_ptr<ResourceScheduler::LoadHandle> low_handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, low->id(), low->loader())); |
| + EXPECT_TRUE(high->started()); |
| + EXPECT_FALSE(low->started()); |
| + high_handle.reset(); |
| + EXPECT_TRUE(low->started()); |
| +} |
| + |
| +TEST_F(ResourceSchedulerTest, LowBlocksUntilPaint) { |
| + scoped_ptr<TestRequest> high( |
| + factory_.NewRequest("http://host/high", net::HIGHEST)); |
| + scoped_ptr<TestRequest> low( |
| + factory_.NewRequest("http://host/low", net::LOW)); |
| + scoped_ptr<ResourceScheduler::LoadHandle> high_handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, high->id(), high->loader())); |
| + scoped_ptr<ResourceScheduler::LoadHandle> low_handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, low->id(), low->loader())); |
| + EXPECT_TRUE(high->started()); |
| + EXPECT_FALSE(low->started()); |
| + scheduler_.OnPaint(kChildId, kRouteId); |
| + EXPECT_TRUE(low->started()); |
| +} |
| + |
| +TEST_F(ResourceSchedulerTest, NavigationResetsPaintState) { |
| + scoped_ptr<TestRequest> high( |
| + factory_.NewRequest("http://host/high", net::HIGHEST)); |
| + scoped_ptr<TestRequest> low( |
| + factory_.NewRequest("http://host/low", net::LOW)); |
| + scheduler_.OnPaint(kChildId, kRouteId); |
| + scheduler_.OnNavigate(kChildId, kRouteId); |
| + scoped_ptr<ResourceScheduler::LoadHandle> high_handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, high->id(), high->loader())); |
| + scoped_ptr<ResourceScheduler::LoadHandle> low_handle(scheduler_.ScheduleLoad( |
| + kChildId, kRouteId, low->id(), low->loader())); |
| + EXPECT_TRUE(high->started()); |
| + EXPECT_FALSE(low->started()); |
| +} |
| + |
| +TEST_F(ResourceSchedulerTest, NoRouteIdStartsImmediately) { |
| + scoped_ptr<TestRequest> request( |
| + factory_.NewRequest("http://host/1", net::LOW)); |
| + scoped_ptr<ResourceScheduler::LoadHandle> handle(scheduler_.ScheduleLoad( |
| + kChildId, 0, request->id(), request->loader())); |
| + EXPECT_TRUE(request->started()); |
| +} |
| + |
| +TEST_F(ResourceSchedulerTest, DestroyTabBeforeCancelingLoad) { |
| + const int new_route_id = kRouteId +1; |
| + scheduler_.OnCreate(kChildId, new_route_id); |
| + scoped_ptr<TestRequest> request( |
| + factory_.NewRequest("http://host/1", net::LOW)); |
| + scoped_ptr<ResourceScheduler::LoadHandle> handle(scheduler_.ScheduleLoad( |
| + kChildId, new_route_id, request->id(), request->loader())); |
| + scheduler_.OnDestroy(kChildId, new_route_id); |
| + // Note that new_route_id is destroyed before handle goes out of scope. |
| +} |
| + |
| +} // namespace content |