| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 payload, | 28 payload, |
| 29 code, | 29 code, |
| 30 net::URLRequestStatus::SUCCESS); | 30 net::URLRequestStatus::SUCCESS); |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Kicks off the download. | 33 // Kicks off the download. |
| 34 void Download() { | 34 void Download() { |
| 35 scoped_refptr<net::TestURLRequestContextGetter> getter( | 35 scoped_refptr<net::TestURLRequestContextGetter> getter( |
| 36 new net::TestURLRequestContextGetter( | 36 new net::TestURLRequestContextGetter( |
| 37 base::MessageLoopProxy::current())); | 37 base::MessageLoopProxy::current())); |
| 38 ChromeDownloaderImpl impl(getter.get()); | 38 ChromeDownloaderImpl impl(getter); |
| 39 scoped_ptr< ::i18n::addressinput::Downloader::Callback> callback( | 39 impl.Download(url_.spec(), BuildCallback()); |
| 40 ::i18n::addressinput::BuildCallback( | |
| 41 this, &ChromeDownloaderImplTest::OnDownloaded)); | |
| 42 impl.Download(url_.spec(), *callback); | |
| 43 base::MessageLoop::current()->RunUntilIdle(); | 40 base::MessageLoop::current()->RunUntilIdle(); |
| 44 } | 41 } |
| 45 | 42 |
| 46 void set_url(const GURL& url) { url_ = url; } | 43 void set_url(const GURL& url) { url_ = url; } |
| 47 bool success() const { return success_; } | 44 const std::string& data() { return *data_; } |
| 48 bool has_data() const { return !!data_; } | 45 bool success() { return success_; } |
| 49 | 46 |
| 50 const std::string& data() const { | 47 private: |
| 51 DCHECK(data_); | 48 scoped_ptr<ChromeDownloaderImpl::Callback> BuildCallback() { |
| 52 return *data_; | 49 return ::i18n::addressinput::BuildScopedPtrCallback( |
| 50 this, &ChromeDownloaderImplTest::OnDownloaded); |
| 53 } | 51 } |
| 54 | 52 |
| 55 private: | |
| 56 // Callback for when download is finished. | 53 // Callback for when download is finished. |
| 57 void OnDownloaded(bool success, | 54 void OnDownloaded(bool success, |
| 58 const std::string& url, | 55 const std::string& url, |
| 59 std::string* data) { | 56 scoped_ptr<std::string> data) { |
| 60 ASSERT_FALSE(success && data == NULL); | |
| 61 success_ = success; | 57 success_ = success; |
| 62 data_.reset(data); | 58 data_ = data.Pass(); |
| 63 } | 59 } |
| 64 | 60 |
| 65 base::MessageLoop loop_; | 61 base::MessageLoop loop_; |
| 66 net::URLFetcherImplFactory factory_; | 62 net::URLFetcherImplFactory factory_; |
| 67 net::FakeURLFetcherFactory fake_factory_; | 63 net::FakeURLFetcherFactory fake_factory_; |
| 68 GURL url_; | 64 GURL url_; |
| 69 scoped_ptr<std::string> data_; | 65 scoped_ptr<std::string> data_; |
| 70 bool success_; | 66 bool success_; |
| 71 }; | 67 }; |
| 72 | 68 |
| 73 TEST_F(ChromeDownloaderImplTest, Success) { | 69 TEST_F(ChromeDownloaderImplTest, Success) { |
| 74 const char kFakePayload[] = "ham hock"; | 70 const char kFakePayload[] = "ham hock"; |
| 75 set_url(GURL(kFakeUrl)); | 71 set_url(GURL(kFakeUrl)); |
| 76 SetFakeResponse(kFakePayload, net::HTTP_OK); | 72 SetFakeResponse(kFakePayload, net::HTTP_OK); |
| 77 Download(); | 73 Download(); |
| 78 EXPECT_TRUE(success()); | 74 EXPECT_TRUE(success()); |
| 79 EXPECT_EQ(kFakePayload, data()); | 75 EXPECT_EQ(kFakePayload, data()); |
| 80 } | 76 } |
| 81 | 77 |
| 82 TEST_F(ChromeDownloaderImplTest, Failure) { | 78 TEST_F(ChromeDownloaderImplTest, Failure) { |
| 83 const char kFakePayload[] = "ham hock"; | 79 const char kFakePayload[] = "ham hock"; |
| 84 set_url(GURL(kFakeUrl)); | 80 set_url(GURL(kFakeUrl)); |
| 85 SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR); | 81 SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR); |
| 86 Download(); | 82 Download(); |
| 87 EXPECT_FALSE(success()); | 83 EXPECT_FALSE(success()); |
| 88 EXPECT_TRUE(!has_data() || data().empty()); | 84 EXPECT_EQ(std::string(), data()); |
| 89 } | 85 } |
| 90 | 86 |
| 91 TEST_F(ChromeDownloaderImplTest, RejectsInsecureScheme) { | 87 TEST_F(ChromeDownloaderImplTest, RejectsInsecureScheme) { |
| 92 const char kFakePayload[] = "ham hock"; | 88 const char kFakePayload[] = "ham hock"; |
| 93 set_url(GURL(kFakeInsecureUrl)); | 89 set_url(GURL(kFakeInsecureUrl)); |
| 94 SetFakeResponse(kFakePayload, net::HTTP_OK); | 90 SetFakeResponse(kFakePayload, net::HTTP_OK); |
| 95 Download(); | 91 Download(); |
| 96 EXPECT_FALSE(success()); | 92 EXPECT_FALSE(success()); |
| 97 EXPECT_TRUE(!has_data() || data().empty()); | 93 EXPECT_EQ(std::string(), data()); |
| 98 } | 94 } |
| 99 | 95 |
| 100 } // namespace autofill | 96 } // namespace autofill |
| OLD | NEW |