| 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" |
| 11 |
| 12 static const char kFakeUrl[] = "http://example.com"; |
| 13 |
| 14 class ChromeDownloaderImplTest : public testing::Test { |
| 15 public: |
| 16 ChromeDownloaderImplTest() |
| 17 : success_(false), |
| 18 fake_factory_(&factory_) {} |
| 19 virtual ~ChromeDownloaderImplTest() {} |
| 20 |
| 21 protected: |
| 22 // Sets the response for the download. |
| 23 void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) { |
| 24 fake_factory_.SetFakeResponse(GURL(kFakeUrl), |
| 25 payload, |
| 26 code, |
| 27 net::URLRequestStatus::SUCCESS); |
| 28 } |
| 29 |
| 30 // Kicks off the download. |
| 31 void Download() { |
| 32 net::TestURLRequestContextGetter* getter = |
| 33 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); |
| 34 ChromeDownloaderImpl impl(getter); |
| 35 impl.Download(kFakeUrl, BuildCallback()); |
| 36 base::MessageLoop::current()->RunUntilIdle(); |
| 37 } |
| 38 |
| 39 const std::string& data() { return data_; } |
| 40 bool success() { return success_; } |
| 41 |
| 42 private: |
| 43 scoped_ptr<ChromeDownloaderImpl::Callback> BuildCallback() { |
| 44 return ::i18n::addressinput::BuildCallback( |
| 45 this, &ChromeDownloaderImplTest::OnDownloaded); |
| 46 } |
| 47 |
| 48 // Callback for when download is finished. |
| 49 void OnDownloaded(bool success, |
| 50 const std::string& url, |
| 51 const std::string& data) { |
| 52 success_ = success; |
| 53 data_ = data; |
| 54 } |
| 55 |
| 56 base::MessageLoop loop_; |
| 57 net::URLFetcherImplFactory factory_; |
| 58 net::FakeURLFetcherFactory fake_factory_; |
| 59 std::string data_; |
| 60 bool success_; |
| 61 }; |
| 62 |
| 63 TEST_F(ChromeDownloaderImplTest, Success) { |
| 64 const char kFakePayload[] = "ham hock"; |
| 65 SetFakeResponse(kFakePayload, net::HTTP_OK); |
| 66 Download(); |
| 67 EXPECT_TRUE(success()); |
| 68 EXPECT_EQ(kFakePayload, data()); |
| 69 } |
| 70 |
| 71 TEST_F(ChromeDownloaderImplTest, Failure) { |
| 72 const char kFakePayload[] = "ham hock"; |
| 73 SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR); |
| 74 Download(); |
| 75 EXPECT_FALSE(success()); |
| 76 EXPECT_EQ(std::string(), data()); |
| 77 } |
| OLD | NEW |