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