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_metadata_source.h" | 5 #include "third_party/libaddressinput/chromium/chrome_metadata_source.h" |
6 | 6 |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.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 18 matching lines...) Expand all Loading... |
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 Get() { | 34 void Get() { |
35 scoped_refptr<net::TestURLRequestContextGetter> getter( | 35 scoped_refptr<net::TestURLRequestContextGetter> getter( |
36 new net::TestURLRequestContextGetter( | 36 new net::TestURLRequestContextGetter( |
37 base::ThreadTaskRunnerHandle::Get())); | 37 base::ThreadTaskRunnerHandle::Get())); |
38 ChromeMetadataSource impl(std::string(), getter.get()); | 38 ChromeMetadataSource impl(std::string(), getter.get()); |
39 scoped_ptr< ::i18n::addressinput::Source::Callback> callback( | 39 std::unique_ptr<::i18n::addressinput::Source::Callback> callback( |
40 ::i18n::addressinput::BuildCallback( | 40 ::i18n::addressinput::BuildCallback( |
41 this, &ChromeMetadataSourceTest::OnDownloaded)); | 41 this, &ChromeMetadataSourceTest::OnDownloaded)); |
42 impl.Get(url_.spec(), *callback); | 42 impl.Get(url_.spec(), *callback); |
43 base::MessageLoop::current()->RunUntilIdle(); | 43 base::MessageLoop::current()->RunUntilIdle(); |
44 } | 44 } |
45 | 45 |
46 void set_url(const GURL& url) { url_ = url; } | 46 void set_url(const GURL& url) { url_ = url; } |
47 bool success() const { return success_; } | 47 bool success() const { return success_; } |
48 bool has_data() const { return !!data_; } | 48 bool has_data() const { return !!data_; } |
49 | 49 |
50 const std::string& data() const { | 50 const std::string& data() const { |
51 DCHECK(data_); | 51 DCHECK(data_); |
52 return *data_; | 52 return *data_; |
53 } | 53 } |
54 | 54 |
55 private: | 55 private: |
56 // Callback for when download is finished. | 56 // Callback for when download is finished. |
57 void OnDownloaded(bool success, | 57 void OnDownloaded(bool success, |
58 const std::string& url, | 58 const std::string& url, |
59 std::string* data) { | 59 std::string* data) { |
60 ASSERT_FALSE(success && data == NULL); | 60 ASSERT_FALSE(success && data == NULL); |
61 success_ = success; | 61 success_ = success; |
62 data_.reset(data); | 62 data_.reset(data); |
63 } | 63 } |
64 | 64 |
65 base::MessageLoop loop_; | 65 base::MessageLoop loop_; |
66 net::URLFetcherImplFactory factory_; | 66 net::URLFetcherImplFactory factory_; |
67 net::FakeURLFetcherFactory fake_factory_; | 67 net::FakeURLFetcherFactory fake_factory_; |
68 GURL url_; | 68 GURL url_; |
69 scoped_ptr<std::string> data_; | 69 std::unique_ptr<std::string> data_; |
70 bool success_; | 70 bool success_; |
71 }; | 71 }; |
72 | 72 |
73 TEST_F(ChromeMetadataSourceTest, Success) { | 73 TEST_F(ChromeMetadataSourceTest, Success) { |
74 const char kFakePayload[] = "ham hock"; | 74 const char kFakePayload[] = "ham hock"; |
75 set_url(GURL(kFakeUrl)); | 75 set_url(GURL(kFakeUrl)); |
76 SetFakeResponse(kFakePayload, net::HTTP_OK); | 76 SetFakeResponse(kFakePayload, net::HTTP_OK); |
77 Get(); | 77 Get(); |
78 EXPECT_TRUE(success()); | 78 EXPECT_TRUE(success()); |
79 EXPECT_EQ(kFakePayload, data()); | 79 EXPECT_EQ(kFakePayload, data()); |
(...skipping 11 matching lines...) Expand all Loading... |
91 TEST_F(ChromeMetadataSourceTest, RejectsInsecureScheme) { | 91 TEST_F(ChromeMetadataSourceTest, RejectsInsecureScheme) { |
92 const char kFakePayload[] = "ham hock"; | 92 const char kFakePayload[] = "ham hock"; |
93 set_url(GURL(kFakeInsecureUrl)); | 93 set_url(GURL(kFakeInsecureUrl)); |
94 SetFakeResponse(kFakePayload, net::HTTP_OK); | 94 SetFakeResponse(kFakePayload, net::HTTP_OK); |
95 Get(); | 95 Get(); |
96 EXPECT_FALSE(success()); | 96 EXPECT_FALSE(success()); |
97 EXPECT_TRUE(!has_data() || data().empty()); | 97 EXPECT_TRUE(!has_data() || data().empty()); |
98 } | 98 } |
99 | 99 |
100 } // namespace autofill | 100 } // namespace autofill |
OLD | NEW |