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 14 matching lines...) Expand all Loading... |
25 // Sets the response for the download. | 25 // Sets the response for the download. |
26 void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) { | 26 void SetFakeResponse(const std::string& payload, net::HttpStatusCode code) { |
27 fake_factory_.SetFakeResponse(url_, | 27 fake_factory_.SetFakeResponse(url_, |
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 scoped_refptr<net::TestURLRequestContextGetter> getter( |
36 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current()); | 36 new net::TestURLRequestContextGetter( |
37 ChromeDownloaderImpl impl(getter); | 37 base::MessageLoopProxy::current())); |
38 impl.Download(url_.spec(), BuildCallback()); | 38 ChromeDownloaderImpl impl(getter.get()); |
| 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 bool success() const { return success_; } |
44 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 } |
45 | 54 |
46 private: | 55 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. | 56 // Callback for when download is finished. |
53 void OnDownloaded(bool success, | 57 void OnDownloaded(bool success, |
54 const std::string& url, | 58 const std::string& url, |
55 scoped_ptr<std::string> data) { | 59 std::string* data) { |
| 60 ASSERT_FALSE(success && data == NULL); |
56 success_ = success; | 61 success_ = success; |
57 data_ = data.Pass(); | 62 data_.reset(data); |
58 } | 63 } |
59 | 64 |
60 base::MessageLoop loop_; | 65 base::MessageLoop loop_; |
61 net::URLFetcherImplFactory factory_; | 66 net::URLFetcherImplFactory factory_; |
62 net::FakeURLFetcherFactory fake_factory_; | 67 net::FakeURLFetcherFactory fake_factory_; |
63 GURL url_; | 68 GURL url_; |
64 scoped_ptr<std::string> data_; | 69 scoped_ptr<std::string> data_; |
65 bool success_; | 70 bool success_; |
66 }; | 71 }; |
67 | 72 |
68 TEST_F(ChromeDownloaderImplTest, Success) { | 73 TEST_F(ChromeDownloaderImplTest, Success) { |
69 const char kFakePayload[] = "ham hock"; | 74 const char kFakePayload[] = "ham hock"; |
70 set_url(GURL(kFakeUrl)); | 75 set_url(GURL(kFakeUrl)); |
71 SetFakeResponse(kFakePayload, net::HTTP_OK); | 76 SetFakeResponse(kFakePayload, net::HTTP_OK); |
72 Download(); | 77 Download(); |
73 EXPECT_TRUE(success()); | 78 EXPECT_TRUE(success()); |
74 EXPECT_EQ(kFakePayload, data()); | 79 EXPECT_EQ(kFakePayload, data()); |
75 } | 80 } |
76 | 81 |
77 TEST_F(ChromeDownloaderImplTest, Failure) { | 82 TEST_F(ChromeDownloaderImplTest, Failure) { |
78 const char kFakePayload[] = "ham hock"; | 83 const char kFakePayload[] = "ham hock"; |
79 set_url(GURL(kFakeUrl)); | 84 set_url(GURL(kFakeUrl)); |
80 SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR); | 85 SetFakeResponse(kFakePayload, net::HTTP_INTERNAL_SERVER_ERROR); |
81 Download(); | 86 Download(); |
82 EXPECT_FALSE(success()); | 87 EXPECT_FALSE(success()); |
83 EXPECT_EQ(std::string(), data()); | 88 EXPECT_TRUE(!has_data() || data().empty()); |
84 } | 89 } |
85 | 90 |
86 TEST_F(ChromeDownloaderImplTest, RejectsInsecureScheme) { | 91 TEST_F(ChromeDownloaderImplTest, RejectsInsecureScheme) { |
87 const char kFakePayload[] = "ham hock"; | 92 const char kFakePayload[] = "ham hock"; |
88 set_url(GURL(kFakeInsecureUrl)); | 93 set_url(GURL(kFakeInsecureUrl)); |
89 SetFakeResponse(kFakePayload, net::HTTP_OK); | 94 SetFakeResponse(kFakePayload, net::HTTP_OK); |
90 Download(); | 95 Download(); |
91 EXPECT_FALSE(success()); | 96 EXPECT_FALSE(success()); |
92 EXPECT_EQ(std::string(), data()); | 97 EXPECT_TRUE(!has_data() || data().empty()); |
93 } | 98 } |
94 | 99 |
95 } // namespace autofill | 100 } // namespace autofill |
OLD | NEW |