OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <list> | 5 #include <list> |
6 | 6 |
7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
8 #include "base/test/test_timeouts.h" | 8 #include "base/test/test_timeouts.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/autofill/autofill_download.h" | 10 #include "chrome/browser/autofill/autofill_download.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // AutofillDownloadManager. Then it records responses to different initiated | 57 // AutofillDownloadManager. Then it records responses to different initiated |
58 // requests, which are verified later. To mock network requests | 58 // requests, which are verified later. To mock network requests |
59 // TestURLFetcherFactory is used, which creates URLFetchers that do not | 59 // TestURLFetcherFactory is used, which creates URLFetchers that do not |
60 // go over the wire, but allow calling back HTTP responses directly. | 60 // go over the wire, but allow calling back HTTP responses directly. |
61 // The responses in test are out of order and verify: successful query request, | 61 // The responses in test are out of order and verify: successful query request, |
62 // successful upload request, failed upload request. | 62 // successful upload request, failed upload request. |
63 class AutofillDownloadTest : public AutofillDownloadManager::Observer, | 63 class AutofillDownloadTest : public AutofillDownloadManager::Observer, |
64 public testing::Test { | 64 public testing::Test { |
65 public: | 65 public: |
66 AutofillDownloadTest() | 66 AutofillDownloadTest() |
67 : download_manager_(&profile_), | 67 : download_manager_(&profile_, this), |
68 io_thread_(BrowserThread::IO) { | 68 io_thread_(BrowserThread::IO) { |
69 } | 69 } |
70 | 70 |
71 virtual void SetUp() { | 71 virtual void SetUp() { |
72 base::Thread::Options options; | 72 base::Thread::Options options; |
73 options.message_loop_type = MessageLoop::TYPE_IO; | 73 options.message_loop_type = MessageLoop::TYPE_IO; |
74 io_thread_.StartWithOptions(options); | 74 io_thread_.StartWithOptions(options); |
75 profile_.CreateRequestContext(); | 75 profile_.CreateRequestContext(); |
76 download_manager_.SetObserver(this); | |
77 } | 76 } |
78 | 77 |
79 virtual void TearDown() { | 78 virtual void TearDown() { |
80 profile_.ResetRequestContext(); | 79 profile_.ResetRequestContext(); |
81 io_thread_.Stop(); | 80 io_thread_.Stop(); |
82 download_manager_.SetObserver(NULL); | |
83 } | 81 } |
84 | 82 |
85 void LimitCache(size_t cache_size) { | 83 void LimitCache(size_t cache_size) { |
86 download_manager_.set_max_form_cache_size(cache_size); | 84 download_manager_.set_max_form_cache_size(cache_size); |
87 } | 85 } |
88 | 86 |
89 // AutofillDownloadManager::Observer implementation. | 87 // AutofillDownloadManager::Observer implementation. |
90 virtual void OnLoadedServerPredictions(const std::string& response_xml) { | 88 virtual void OnLoadedServerPredictions( |
| 89 const std::string& response_xml) OVERRIDE { |
91 ResponseData response; | 90 ResponseData response; |
92 response.response = response_xml; | 91 response.response = response_xml; |
93 response.type_of_response = QUERY_SUCCESSFULL; | 92 response.type_of_response = QUERY_SUCCESSFULL; |
94 responses_.push_back(response); | 93 responses_.push_back(response); |
95 } | 94 } |
96 | 95 |
97 virtual void OnUploadedPossibleFieldTypes() { | 96 virtual void OnUploadedPossibleFieldTypes() OVERRIDE { |
98 ResponseData response; | 97 ResponseData response; |
99 response.type_of_response = UPLOAD_SUCCESSFULL; | 98 response.type_of_response = UPLOAD_SUCCESSFULL; |
100 responses_.push_back(response); | 99 responses_.push_back(response); |
101 } | 100 } |
102 | 101 |
103 virtual void OnServerRequestError( | 102 virtual void OnServerRequestError( |
104 const std::string& form_signature, | 103 const std::string& form_signature, |
105 AutofillDownloadManager::AutofillRequestType request_type, | 104 AutofillDownloadManager::AutofillRequestType request_type, |
106 int http_error) { | 105 int http_error) OVERRIDE { |
107 ResponseData response; | 106 ResponseData response; |
108 response.signature = form_signature; | 107 response.signature = form_signature; |
109 response.error = http_error; | 108 response.error = http_error; |
110 response.type_of_response = | 109 response.type_of_response = |
111 request_type == AutofillDownloadManager::REQUEST_QUERY ? | 110 request_type == AutofillDownloadManager::REQUEST_QUERY ? |
112 REQUEST_QUERY_FAILED : REQUEST_UPLOAD_FAILED; | 111 REQUEST_QUERY_FAILED : REQUEST_UPLOAD_FAILED; |
113 responses_.push_back(response); | 112 responses_.push_back(response); |
114 } | 113 } |
115 | 114 |
116 enum TYPE_OF_RESPONSE { | 115 enum ResponseType { |
117 QUERY_SUCCESSFULL, | 116 QUERY_SUCCESSFULL, |
118 UPLOAD_SUCCESSFULL, | 117 UPLOAD_SUCCESSFULL, |
119 REQUEST_QUERY_FAILED, | 118 REQUEST_QUERY_FAILED, |
120 REQUEST_UPLOAD_FAILED, | 119 REQUEST_UPLOAD_FAILED, |
121 }; | 120 }; |
122 | 121 |
123 struct ResponseData { | 122 struct ResponseData { |
124 TYPE_OF_RESPONSE type_of_response; | 123 ResponseType type_of_response; |
125 int error; | 124 int error; |
126 std::string signature; | 125 std::string signature; |
127 std::string response; | 126 std::string response; |
128 ResponseData() : type_of_response(REQUEST_QUERY_FAILED), error(0) { | 127 |
129 } | 128 ResponseData() : type_of_response(REQUEST_QUERY_FAILED), error(0) {} |
130 }; | 129 }; |
131 std::list<ResponseData> responses_; | 130 std::list<ResponseData> responses_; |
132 | 131 |
133 TestingProfile profile_; | 132 TestingProfile profile_; |
134 AutofillDownloadManager download_manager_; | 133 AutofillDownloadManager download_manager_; |
135 | 134 |
136 private: | 135 private: |
137 // |request_context_getter_| must be released on the IO thread. | 136 // |request_context_getter_| must be released on the IO thread. |
138 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } | 137 MessageLoop* io_thread_loop() { return io_thread_.message_loop(); } |
139 BrowserThread io_thread_; | 138 BrowserThread io_thread_; |
(...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 mock_metric_logger)); | 501 mock_metric_logger)); |
503 // No responses yet | 502 // No responses yet |
504 EXPECT_EQ(static_cast<size_t>(0), responses_.size()); | 503 EXPECT_EQ(static_cast<size_t>(0), responses_.size()); |
505 | 504 |
506 fetcher = factory.GetFetcherByID(3); | 505 fetcher = factory.GetFetcherByID(3); |
507 ASSERT_TRUE(fetcher); | 506 ASSERT_TRUE(fetcher); |
508 FakeOnURLFetchComplete(fetcher, 200, std::string(responses[0])); | 507 FakeOnURLFetchComplete(fetcher, 200, std::string(responses[0])); |
509 ASSERT_EQ(static_cast<size_t>(1), responses_.size()); | 508 ASSERT_EQ(static_cast<size_t>(1), responses_.size()); |
510 EXPECT_EQ(responses[0], responses_.front().response); | 509 EXPECT_EQ(responses[0], responses_.front().response); |
511 } | 510 } |
OLD | NEW |