Chromium Code Reviews| Index: mojo/fetcher/network_fetcher_unittest.cc |
| diff --git a/mojo/fetcher/network_fetcher_unittest.cc b/mojo/fetcher/network_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..66ff9d5c634104160efe0d091f83cafb7edf06cd |
| --- /dev/null |
| +++ b/mojo/fetcher/network_fetcher_unittest.cc |
| @@ -0,0 +1,170 @@ |
| +// Copyright 2015 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/at_exit.h" |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/run_loop.h" |
| +#include "mojo/fetcher/network_fetcher.h" |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "mojo/runner/context.h" |
| +#include "mojo/services/network/public/interfaces/url_loader.mojom.h" |
| +#include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace mojo { |
| +namespace fetcher { |
| +namespace { |
| + |
| +const char k200Request[] = "http://request_expect_200"; |
| +const char k404Request[] = "http://request_expect_404"; |
| +const char k504Request[] = "http://request_expect_504"; |
| +const char kErrorRequest[] = "http://request_expect_error"; |
| + |
| +class TestURLLoaderImpl : public URLLoader { |
| + public: |
| + explicit TestURLLoaderImpl(InterfaceRequest<URLLoader> request) |
| + : binding_(this, request.Pass()) {} |
| + ~TestURLLoaderImpl() override {} |
| + |
| + private: |
| + // URLLoader implementation. |
| + void Start(URLRequestPtr request, |
| + const Callback<void(URLResponsePtr)>& callback) override { |
| + URLResponsePtr response(URLResponse::New()); |
| + response->url = request->url; |
| + if (request->url == std::string(k200Request)) { |
| + response->mime_type = "text/html"; |
| + response->status_code = 200; |
| + } else if (request->url == std::string(k404Request)) { |
| + response->mime_type = "text/html"; |
| + response->status_code = 404; |
| + } else if (request->url == std::string(k504Request)) { |
| + response->mime_type = "text/html"; |
| + response->status_code = 504; |
| + } else { |
| + response->error = NetworkError::New(); |
| + response->error->code = -2; |
| + } |
| + callback.Run(response.Pass()); |
| + } |
| + void FollowRedirect(const Callback<void(URLResponsePtr)>& callback) override { |
| + NOTREACHED(); |
| + } |
| + void QueryStatus( |
| + const Callback<void(URLLoaderStatusPtr)>& callback) override { |
| + NOTREACHED(); |
| + } |
| + |
| + StrongBinding<URLLoader> binding_; |
| + DISALLOW_COPY_AND_ASSIGN(TestURLLoaderImpl); |
| +}; |
| + |
| +class TestURLLoaderFactoryImpl : public URLLoaderFactory { |
| + public: |
| + explicit TestURLLoaderFactoryImpl(InterfaceRequest<URLLoaderFactory> request) |
| + : binding_(this, request.Pass()) {} |
| + ~TestURLLoaderFactoryImpl() override {} |
| + |
| + private: |
| + // URLLoaderFactory implementation. |
| + void CreateURLLoader(InterfaceRequest<URLLoader> loader) override { |
| + new TestURLLoaderImpl(loader.Pass()); |
| + } |
| + |
| + StrongBinding<URLLoaderFactory> binding_; |
| + DISALLOW_COPY_AND_ASSIGN(TestURLLoaderFactoryImpl); |
| +}; |
| + |
| +class FetchCallbackHelper { |
| + public: |
| + FetchCallbackHelper() : run_loop_(nullptr) {} |
| + ~FetchCallbackHelper() {} |
| + |
| + shell::Fetcher::FetchCallback GetCallback() { |
| + return base::Bind(&FetchCallbackHelper::CallbackHandler, |
| + base::Unretained(this)); |
| + } |
| + |
| + void WaitForCallback() { |
| + base::RunLoop run_loop; |
| + run_loop_ = &run_loop; |
|
sky
2015/09/16 01:14:45
nit: base::AutoReset
yzshen1
2015/09/16 05:24:54
Done.
|
| + run_loop_->Run(); |
| + run_loop_ = nullptr; |
| + } |
| + |
| + shell::Fetcher* fetcher() const { return fetcher_.get(); } |
| + |
| + private: |
| + void CallbackHandler(scoped_ptr<shell::Fetcher> fetcher) { |
| + fetcher_ = fetcher.Pass(); |
| + if (run_loop_) |
| + run_loop_->Quit(); |
| + } |
| + |
| + base::RunLoop* run_loop_; |
|
sky
2015/09/16 01:14:45
document what where this comes from (especially ow
yzshen1
2015/09/16 05:24:54
Done.
|
| + scoped_ptr<shell::Fetcher> fetcher_; |
| + DISALLOW_COPY_AND_ASSIGN(FetchCallbackHelper); |
| +}; |
| + |
| +class NetworkFetcherTest : public testing::Test { |
| + public: |
| + NetworkFetcherTest() {} |
| + ~NetworkFetcherTest() override {} |
| + |
| + protected: |
| + // Overridden from testing::Test: |
| + void SetUp() override { |
| + runner::Context::EnsureEmbedderIsInitialized(); |
| + // Automatically destroyed when |url_loader_factory_| is closed. |
| + new TestURLLoaderFactoryImpl(GetProxy(&url_loader_factory_)); |
| + } |
| + |
| + // When |expect_fetch_success| is false, |expected_status_code| is ignored. |
| + void TestFetchURL(const std::string& url, |
| + bool expect_fetch_success, |
| + uint32_t expected_status_code) { |
| + FetchCallbackHelper helper; |
| + |
| + URLRequestPtr request(URLRequest::New()); |
| + request->url = url; |
| + new NetworkFetcher(true, request.Pass(), url_loader_factory_.get(), |
| + helper.GetCallback()); |
| + helper.WaitForCallback(); |
| + |
| + if (!expect_fetch_success) { |
| + ASSERT_FALSE(helper.fetcher()); |
| + } else { |
| + ASSERT_TRUE(helper.fetcher()); |
| + URLResponsePtr response = helper.fetcher()->AsURLResponse(nullptr, 0); |
| + ASSERT_TRUE(response); |
| + EXPECT_EQ(url, response->url); |
| + EXPECT_EQ(expected_status_code, response->status_code); |
| + } |
| + } |
| + |
| + private: |
| + base::ShadowingAtExitManager at_exit_; |
| + base::MessageLoop loop_; |
| + URLLoaderFactoryPtr url_loader_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NetworkFetcherTest); |
| +}; |
| + |
| +TEST_F(NetworkFetcherTest, FetchSucceeded) { |
| + TestFetchURL(k200Request, true, 200u); |
|
sky
2015/09/16 01:14:45
I would separate each into its own test. That way
yzshen1
2015/09/16 05:24:54
Done.
|
| + TestFetchURL(k404Request, true, 404u); |
| + TestFetchURL(k504Request, true, 504u); |
| +} |
| + |
| +TEST_F(NetworkFetcherTest, FetchFailed) { |
| + TestFetchURL(kErrorRequest, false, 0u); |
| +} |
| + |
| +} // namespace |
| +} // namespace fetcher |
| +} // namespace mojo |