| Index: content/browser/loader/resource_scheduler_unittest.cc
|
| diff --git a/content/browser/loader/resource_scheduler_unittest.cc b/content/browser/loader/resource_scheduler_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..4f8c69324d857ad0f0bb9ea4f71bb78a118ee6c8
|
| --- /dev/null
|
| +++ b/content/browser/loader/resource_scheduler_unittest.cc
|
| @@ -0,0 +1,131 @@
|
| +// 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 "content/browser/loader/resource_scheduler.h"
|
| +
|
| +#include "base/message_loop.h"
|
| +#include "content/public/browser/resource_controller.h"
|
| +#include "content/public/browser/resource_throttle.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 TestRequest : public ResourceController {
|
| + public:
|
| + TestRequest(scoped_ptr<ResourceThrottle> throttle,
|
| + scoped_ptr<net::URLRequest> url_request)
|
| + : started_(false),
|
| + throttle_(throttle.Pass()),
|
| + url_request_(url_request.Pass()) {
|
| + throttle_->set_controller_for_testing(this);
|
| + }
|
| +
|
| + bool started() const { return started_; }
|
| +
|
| + void Start() {
|
| + bool deferred = false;
|
| + throttle_->WillStartRequest(&deferred);
|
| + started_ = !deferred;
|
| + }
|
| +
|
| + private:
|
| + // ResourceController interface:
|
| + virtual void Cancel() OVERRIDE {}
|
| + virtual void CancelAndIgnore() OVERRIDE {}
|
| + virtual void CancelWithError(int error_code) OVERRIDE {}
|
| + virtual void Resume() OVERRIDE { started_ = true; }
|
| +
|
| + bool started_;
|
| + scoped_ptr<ResourceThrottle> throttle_;
|
| + scoped_ptr<net::URLRequest> url_request_;
|
| +};
|
| +
|
| +class ResourceSchedulerTest : public testing::Test {
|
| + protected:
|
| + ResourceSchedulerTest()
|
| + : message_loop_(MessageLoop::TYPE_IO) {
|
| + scheduler_.OnCreate(kChildId, kRouteId);
|
| + }
|
| +
|
| + virtual ~ResourceSchedulerTest() {
|
| + scheduler_.OnDestroy(kChildId, kRouteId);
|
| + }
|
| +
|
| + TestRequest* NewRequest(const char* url, net::RequestPriority priority,
|
| + int route_id = kRouteId) {
|
| + scoped_ptr<net::URLRequest> url_request(
|
| + context_.CreateRequest(GURL(url), NULL));
|
| + url_request->set_priority(priority);
|
| + scoped_ptr<ResourceThrottle> throttle(scheduler_.ScheduleRequest(
|
| + kChildId, route_id, *url_request.get()));
|
| + TestRequest* request = new TestRequest(throttle.Pass(), url_request.Pass());
|
| + request->Start();
|
| + return request;
|
| + }
|
| +
|
| + MessageLoop message_loop_;
|
| + ResourceScheduler scheduler_;
|
| + net::TestURLRequestContext context_;
|
| +};
|
| +
|
| +TEST_F(ResourceSchedulerTest, OneIsolatedLowRequest) {
|
| + scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOW));
|
| + EXPECT_TRUE(request->started());
|
| +}
|
| +
|
| +TEST_F(ResourceSchedulerTest, LowBlocksUntilIdle) {
|
| + scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
|
| + scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
|
| + EXPECT_TRUE(high->started());
|
| + EXPECT_FALSE(low->started());
|
| + high.reset();
|
| + EXPECT_TRUE(low->started());
|
| +}
|
| +
|
| +TEST_F(ResourceSchedulerTest, LowBlocksUntilPaint) {
|
| + scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
|
| + scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
|
| + EXPECT_TRUE(high->started());
|
| + EXPECT_FALSE(low->started());
|
| + scheduler_.OnPaint(kChildId, kRouteId);
|
| + EXPECT_TRUE(low->started());
|
| +}
|
| +
|
| +TEST_F(ResourceSchedulerTest, NavigationResetsPaintState) {
|
| + scheduler_.OnPaint(kChildId, kRouteId);
|
| + scheduler_.OnNavigate(kChildId, kRouteId);
|
| + scoped_ptr<TestRequest> high(NewRequest("http://host/high", net::HIGHEST));
|
| + scoped_ptr<TestRequest> low(NewRequest("http://host/low", net::LOW));
|
| + EXPECT_TRUE(high->started());
|
| + EXPECT_FALSE(low->started());
|
| +}
|
| +
|
| +TEST_F(ResourceSchedulerTest, BackgroundRequestStartsImmediately) {
|
| + const int route_id = 0; // Indicates a background request.
|
| + scoped_ptr<TestRequest> request(NewRequest("http://host/1", net::LOW,
|
| + route_id));
|
| + 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(NewRequest("http://host/1", net::LOW,
|
| + new_route_id));
|
| + scheduler_.OnDestroy(kChildId, new_route_id);
|
| + // Note that |new_route_id| is destroyed before |request| goes out of scope.
|
| +}
|
| +
|
| +} // unnamed namespace
|
| +
|
| +} // namespace content
|
|
|