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