| Index: third_party/libaddressinput/chromium/chrome_downloader_impl_unittest.cc
|
| diff --git a/third_party/libaddressinput/chromium/chrome_downloader_impl_unittest.cc b/third_party/libaddressinput/chromium/chrome_downloader_impl_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..b95be93329d8ba2fa9b17c606c7bb81a92b9f457
|
| --- /dev/null
|
| +++ b/third_party/libaddressinput/chromium/chrome_downloader_impl_unittest.cc
|
| @@ -0,0 +1,77 @@
|
| +// Copyright 2013 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 "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
|
| +
|
| +#include "base/message_loop/message_loop_proxy.h"
|
| +#include "net/url_request/test_url_fetcher_factory.h"
|
| +#include "net/url_request/url_request_test_util.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +static const char kFakeUrl[] = "http://example.com";
|
| +
|
| +class ChromeDownloaderImplTest : public testing::Test {
|
| + public:
|
| + ChromeDownloaderImplTest()
|
| + : success_(false),
|
| + fake_factory_(&factory_) {}
|
| + virtual ~ChromeDownloaderImplTest() {}
|
| +
|
| + protected:
|
| + // Sets the response for the download.
|
| + void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) {
|
| + fake_factory_.SetFakeResponse(GURL(kFakeUrl),
|
| + payload,
|
| + code,
|
| + net::URLRequestStatus::SUCCESS);
|
| + }
|
| +
|
| + // Kicks off the download.
|
| + void Download() {
|
| + net::TestURLRequestContextGetter* getter =
|
| + new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
|
| + ChromeDownloaderImpl impl(getter);
|
| + impl.Download(kFakeUrl, BuildCallback());
|
| + base::MessageLoop::current()->RunUntilIdle();
|
| + }
|
| +
|
| + const std::string& data() { return data_; }
|
| + bool success() { return success_; }
|
| +
|
| + private:
|
| + scoped_ptr<ChromeDownloaderImpl::Callback> BuildCallback() {
|
| + return ::i18n::addressinput::BuildCallback(
|
| + this, &ChromeDownloaderImplTest::OnDownloaded);
|
| + }
|
| +
|
| + // Callback for when download is finished.
|
| + void OnDownloaded(bool success,
|
| + const std::string& url,
|
| + const std::string& data) {
|
| + success_ = success;
|
| + data_ = data;
|
| + }
|
| +
|
| + base::MessageLoop loop_;
|
| + net::URLFetcherImplFactory factory_;
|
| + net::FakeURLFetcherFactory fake_factory_;
|
| + std::string data_;
|
| + bool success_;
|
| +};
|
| +
|
| +TEST_F(ChromeDownloaderImplTest, Success) {
|
| + const char kFakePayload[] = "ham hock";
|
| + SetFakeResponse(kFakePayload, net::HTTP_OK);
|
| + Download();
|
| + EXPECT_TRUE(success());
|
| + EXPECT_EQ(kFakePayload, data());
|
| +}
|
| +
|
| +TEST_F(ChromeDownloaderImplTest, Failure) {
|
| + const char kFakePayload[] = "ham hock";
|
| + SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR);
|
| + Download();
|
| + EXPECT_FALSE(success());
|
| + EXPECT_EQ(std::string(), data());
|
| +}
|
|
|