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