Index: content/browser/resource_protocol_handler_unittest.cc |
diff --git a/content/browser/resource_protocol_handler_unittest.cc b/content/browser/resource_protocol_handler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8694c3e3e2662e8836a84f6d5a8d4ba7fcff6513 |
--- /dev/null |
+++ b/content/browser/resource_protocol_handler_unittest.cc |
@@ -0,0 +1,83 @@ |
+// 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_handler.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; |
+ return ContentClient::GetDataResource(resource_id, scale_factor); |
+ } |
+}; |
+ |
+class ResourceProtocolHandlerTest : public ::testing::Test { |
+ protected: |
+ ResourceProtocolHandlerTest() : 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(ResourceProtocolHandlerTest, ValidResource) { |
+ StartRequest(kHelloWorldPath); |
+ ASSERT_TRUE(request_->status().is_success()); |
+ EXPECT_EQ(kHelloWorld, delegate_.data_received()); |
+} |
+ |
+TEST_F(ResourceProtocolHandlerTest, InvalidResource) { |
+ StartRequest(kInvalidPath); |
+ EXPECT_TRUE(delegate_.request_failed()); |
+ EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request_->status().error()); |
+} |
+ |
+} // namespace |
+} // namespace content |