Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop_proxy.h" | |
| 8 #include "net/url_request/test_url_fetcher_factory.h" | |
| 9 #include "net/url_request/url_request_test_util.h" | |
| 10 #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.
| |
| 11 | |
| 12 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.
| |
| 13 | |
| 14 const char kFakeUrl[] = "http://example.com"; | |
| 15 | |
| 16 class ChromeDownloaderImplTest : public testing::Test { | |
| 17 public: | |
| 18 ChromeDownloaderImplTest() | |
| 19 : 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.
| |
| 20 fake_factory_(&factory_) {} | |
| 21 virtual ~ChromeDownloaderImplTest() {} | |
| 22 | |
| 23 protected: | |
| 24 // Sets the response for the download. | |
| 25 void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) { | |
| 26 fake_factory_.SetFakeResponse(GURL(kFakeUrl), | |
| 27 payload, | |
| 28 code, | |
| 29 net::URLRequestStatus::SUCCESS); | |
| 30 } | |
| 31 | |
| 32 // Kicks off the download. | |
| 33 void Download() { | |
| 34 net::TestURLRequestContextGetter* getter = | |
| 35 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | |
| 36 ChromeDownloaderImpl impl(getter); | |
| 37 impl.Download(kFakeUrl, BuildCallback()); | |
| 38 base::MessageLoop::current()->RunUntilIdle(); | |
| 39 } | |
| 40 | |
| 41 const std::string& data() { return data_; } | |
| 42 bool success() { return success_; } | |
| 43 | |
| 44 private: | |
| 45 scoped_ptr<ChromeDownloaderImpl::Callback> BuildCallback() { | |
| 46 return ::i18n::addressinput::BuildCallback( | |
| 47 this, &ChromeDownloaderImplTest::OnDownloaded); | |
| 48 } | |
| 49 | |
| 50 // Callback for when download is finished. | |
| 51 void OnDownloaded(bool success, | |
| 52 const std::string& url, | |
| 53 const std::string& data) { | |
| 54 success_ = success; | |
| 55 data_ = data; | |
| 56 } | |
| 57 | |
| 58 base::MessageLoop loop_; | |
| 59 net::URLFetcherImplFactory factory_; | |
| 60 net::FakeURLFetcherFactory fake_factory_; | |
| 61 std::string data_; | |
| 62 bool success_; | |
| 63 }; | |
| 64 | |
| 65 TEST_F(ChromeDownloaderImplTest, Success) { | |
| 66 const char kFakePayload[] = "ham hock"; | |
| 67 SetFakeResponse(kFakePayload, net::HTTP_OK); | |
| 68 Download(); | |
| 69 EXPECT_TRUE(success()); | |
| 70 EXPECT_EQ(kFakePayload, data()); | |
| 71 } | |
| 72 | |
| 73 TEST_F(ChromeDownloaderImplTest, Failure) { | |
| 74 const char kFakePayload[] = "ham hock"; | |
| 75 SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR); | |
| 76 Download(); | |
| 77 EXPECT_FALSE(success()); | |
| 78 EXPECT_EQ(std::string(), data()); | |
| 79 } | |
| 80 | |
| 81 } // namespace | |
| OLD | NEW |