Chromium Code Reviews| Index: content/browser/resource_protocol_unittest.cc |
| diff --git a/content/browser/resource_protocol_unittest.cc b/content/browser/resource_protocol_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a70c923f5d6c3b4779ebc3672ad46b6aaf916951 |
| --- /dev/null |
| +++ b/content/browser/resource_protocol_unittest.cc |
| @@ -0,0 +1,84 @@ |
| +// Copyright 2014 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/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "content/browser/resource_protocol.h" |
| +#include "content/public/common/content_client.h" |
| +#include "content/public/common/url_constants.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_job_factory_impl.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| +namespace { |
| + |
| +const char kHelloWorldHost[] = "hello-world"; |
| +const char kHelloWorldPath[] = "/hello.txt"; |
| +const char kInvalidPath[] = "/invalid.txt"; |
| +const int kHelloWorldResourceId = 42; |
| +const char kHelloWorld[] = "Hello, world!"; |
| +const char kTextPlain[] = "text/plain"; |
| + |
| +class HelloWorldContentClient : public ContentClient { |
| + public: |
| + base::StringPiece GetDataResource( |
| + int resource_id, |
| + ui::ScaleFactor scale_factor) const override { |
| + if (resource_id == kHelloWorldResourceId) |
| + return kHelloWorld; |
| + else |
|
Tom Sepez
2014/11/17 18:09:11
nit: else after return.
jbroman
2014/11/17 18:35:16
Done.
|
| + return ContentClient::GetDataResource(resource_id, scale_factor); |
| + } |
| +}; |
| + |
| +class ResourceProtocolTest : public ::testing::Test { |
| + protected: |
| + ResourceProtocolTest() : context_(true) { |
| + scoped_ptr<ResourceProtocolHandler> handler( |
| + new ResourceProtocolHandler(&content_client_)); |
| + handler->RegisterResource(kHelloWorldHost, kHelloWorldPath, |
| + kHelloWorldResourceId, ui::SCALE_FACTOR_NONE, |
| + kTextPlain); |
| + job_factory_.SetProtocolHandler(kResourceScheme, handler.release()); |
| + context_.set_job_factory(&job_factory_); |
| + context_.Init(); |
| + } |
| + |
| + void StartRequest(const char* path) { |
| + GURL url(base::StringPrintf("%s://%s%s", kResourceScheme, kHelloWorldHost, |
| + path)); |
| + request_ = |
| + context_.CreateRequest(url, net::DEFAULT_PRIORITY, &delegate_, nullptr); |
| + request_->Start(); |
| + EXPECT_TRUE(request_->is_pending()); |
| + base::RunLoop().Run(); |
| + EXPECT_FALSE(request_->is_pending()); |
| + } |
| + |
| + base::MessageLoop message_loop_; |
| + HelloWorldContentClient content_client_; |
| + net::URLRequestJobFactoryImpl job_factory_; |
| + net::TestURLRequestContext context_; |
| + net::TestDelegate delegate_; |
| + scoped_ptr<net::URLRequest> request_; |
| +}; |
| + |
| +TEST_F(ResourceProtocolTest, ValidResource) { |
| + StartRequest(kHelloWorldPath); |
| + ASSERT_TRUE(request_->status().is_success()); |
| + EXPECT_EQ(kHelloWorld, delegate_.data_received()); |
| +} |
| + |
| +TEST_F(ResourceProtocolTest, InvalidResource) { |
| + StartRequest(kInvalidPath); |
| + EXPECT_TRUE(delegate_.request_failed()); |
| + EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
| +} |
| + |
| +} // namespace |
| +} // namespace content |