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..bb900726cfc116eec9a7bb825f2cc1cc5de93279 |
--- /dev/null |
+++ b/third_party/libaddressinput/chromium/chrome_downloader_impl_unittest.cc |
@@ -0,0 +1,81 @@ |
+// 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" |
Dan Beam
2014/01/08 03:25:49
#include "third_party/libaddressinput/chromium/cpp
Evan Stade
2014/01/09 00:05:12
Done.
|
+ |
+namespace { |
Dan Beam
2014/01/08 03:25:49
nit: not sure we're supposed to anonymously namesp
Evan Stade
2014/01/09 00:05:12
Done.
|
+ |
+const char kFakeUrl[] = "http://example.com"; |
+ |
+class ChromeDownloaderImplTest : public testing::Test { |
+ public: |
+ ChromeDownloaderImplTest() |
+ : success_(false), |
please use gerrit instead
2014/01/08 01:36:26
+2 more spaces of indent.
Evan Stade
2014/01/09 00:05:12
Done.
|
+ 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()); |
+} |
+ |
+} // namespace |