| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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" |
| 11 #include "chrome/browser/autofill/autofill_metrics.h" |
| 11 #include "chrome/common/net/test_url_fetcher_factory.h" | 12 #include "chrome/common/net/test_url_fetcher_factory.h" |
| 12 #include "chrome/test/testing_profile.h" | 13 #include "chrome/test/testing_profile.h" |
| 13 #include "net/url_request/url_request_status.h" | 14 #include "net/url_request/url_request_status.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" |
| 16 #include "webkit/glue/form_data.h" | 18 #include "webkit/glue/form_data.h" |
| 17 | 19 |
| 18 using webkit_glue::FormData; | 20 using webkit_glue::FormData; |
| 19 using WebKit::WebInputElement; | 21 using WebKit::WebInputElement; |
| 20 | 22 |
| 23 namespace { |
| 24 |
| 25 class MockAutoFillMetrics : public AutoFillMetrics { |
| 26 public: |
| 27 MOCK_CONST_METHOD1(Log, void(ServerQueryMetric metric)); |
| 28 }; |
| 29 |
| 30 } // namespace |
| 31 |
| 21 // This tests AutoFillDownloadManager. AutoFillDownloadTestHelper implements | 32 // This tests AutoFillDownloadManager. AutoFillDownloadTestHelper implements |
| 22 // AutoFillDownloadManager::Observer and creates an instance of | 33 // AutoFillDownloadManager::Observer and creates an instance of |
| 23 // AutoFillDownloadManager. Then it records responses to different initiated | 34 // AutoFillDownloadManager. Then it records responses to different initiated |
| 24 // requests, which are verified later. To mock network requests | 35 // requests, which are verified later. To mock network requests |
| 25 // TestURLFetcherFactory is used, which creates URLFetchers that do not | 36 // TestURLFetcherFactory is used, which creates URLFetchers that do not |
| 26 // go over the wire, but allow calling back HTTP responses directly. | 37 // go over the wire, but allow calling back HTTP responses directly. |
| 27 // The responses in test are out of order and verify: successful query request, | 38 // The responses in test are out of order and verify: successful query request, |
| 28 // successful upload request, failed upload request. | 39 // successful upload request, failed upload request. |
| 29 class AutoFillDownloadTestHelper : public AutoFillDownloadManager::Observer { | 40 class AutoFillDownloadTestHelper : public AutoFillDownloadManager::Observer { |
| 30 public: | 41 public: |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 std::string response; | 90 std::string response; |
| 80 ResponseData() : type_of_response(REQUEST_QUERY_FAILED), error(0) { | 91 ResponseData() : type_of_response(REQUEST_QUERY_FAILED), error(0) { |
| 81 } | 92 } |
| 82 }; | 93 }; |
| 83 std::list<AutoFillDownloadTestHelper::ResponseData> responses_; | 94 std::list<AutoFillDownloadTestHelper::ResponseData> responses_; |
| 84 | 95 |
| 85 TestingProfile profile; | 96 TestingProfile profile; |
| 86 AutoFillDownloadManager download_manager; | 97 AutoFillDownloadManager download_manager; |
| 87 }; | 98 }; |
| 88 | 99 |
| 89 namespace { | |
| 90 | |
| 91 TEST(AutoFillDownloadTest, QueryAndUploadTest) { | 100 TEST(AutoFillDownloadTest, QueryAndUploadTest) { |
| 92 MessageLoopForUI message_loop; | 101 MessageLoopForUI message_loop; |
| 93 // Create and register factory. | 102 // Create and register factory. |
| 94 AutoFillDownloadTestHelper helper; | 103 AutoFillDownloadTestHelper helper; |
| 95 TestURLFetcherFactory factory; | 104 TestURLFetcherFactory factory; |
| 96 URLFetcher::set_factory(&factory); | 105 URLFetcher::set_factory(&factory); |
| 97 | 106 |
| 98 FormData form; | 107 FormData form; |
| 99 form.method = ASCIIToUTF16("post"); | 108 form.method = ASCIIToUTF16("post"); |
| 100 form.fields.push_back(webkit_glue::FormField(ASCIIToUTF16("username"), | 109 form.fields.push_back(webkit_glue::FormField(ASCIIToUTF16("username"), |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 form.fields.push_back(webkit_glue::FormField(string16(), | 175 form.fields.push_back(webkit_glue::FormField(string16(), |
| 167 ASCIIToUTF16("Submit"), | 176 ASCIIToUTF16("Submit"), |
| 168 string16(), | 177 string16(), |
| 169 ASCIIToUTF16("submit"), | 178 ASCIIToUTF16("submit"), |
| 170 0, | 179 0, |
| 171 false)); | 180 false)); |
| 172 form_structure = new FormStructure(form); | 181 form_structure = new FormStructure(form); |
| 173 form_structures.push_back(form_structure); | 182 form_structures.push_back(form_structure); |
| 174 | 183 |
| 175 // Request with id 0. | 184 // Request with id 0. |
| 176 EXPECT_TRUE(helper.download_manager.StartQueryRequest(form_structures)); | 185 MockAutoFillMetrics mock_metric_logger; |
| 186 EXPECT_CALL(mock_metric_logger, Log(AutoFillMetrics::QUERY_SENT)).Times(1); |
| 187 EXPECT_TRUE(helper.download_manager.StartQueryRequest(form_structures, |
| 188 mock_metric_logger)); |
| 177 // Set upload to 100% so requests happen. | 189 // Set upload to 100% so requests happen. |
| 178 helper.download_manager.SetPositiveUploadRate(1.0); | 190 helper.download_manager.SetPositiveUploadRate(1.0); |
| 179 helper.download_manager.SetNegativeUploadRate(1.0); | 191 helper.download_manager.SetNegativeUploadRate(1.0); |
| 180 // Request with id 1. | 192 // Request with id 1. |
| 181 EXPECT_TRUE(helper.download_manager.StartUploadRequest(*(form_structures[0]), | 193 EXPECT_TRUE(helper.download_manager.StartUploadRequest(*(form_structures[0]), |
| 182 true)); | 194 true)); |
| 183 // Request with id 2. | 195 // Request with id 2. |
| 184 EXPECT_TRUE(helper.download_manager.StartUploadRequest(*(form_structures[1]), | 196 EXPECT_TRUE(helper.download_manager.StartUploadRequest(*(form_structures[1]), |
| 185 false)); | 197 false)); |
| 186 | 198 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 helper.download_manager.SetNegativeUploadRate(0.0); | 263 helper.download_manager.SetNegativeUploadRate(0.0); |
| 252 // No actual requests for the next two calls, as we set upload rate to 0%. | 264 // No actual requests for the next two calls, as we set upload rate to 0%. |
| 253 EXPECT_FALSE(helper.download_manager.StartUploadRequest(*(form_structures[0]), | 265 EXPECT_FALSE(helper.download_manager.StartUploadRequest(*(form_structures[0]), |
| 254 true)); | 266 true)); |
| 255 EXPECT_FALSE(helper.download_manager.StartUploadRequest(*(form_structures[1]), | 267 EXPECT_FALSE(helper.download_manager.StartUploadRequest(*(form_structures[1]), |
| 256 false)); | 268 false)); |
| 257 fetcher = factory.GetFetcherByID(3); | 269 fetcher = factory.GetFetcherByID(3); |
| 258 EXPECT_EQ(NULL, fetcher); | 270 EXPECT_EQ(NULL, fetcher); |
| 259 | 271 |
| 260 // Request with id 3. | 272 // Request with id 3. |
| 261 EXPECT_TRUE(helper.download_manager.StartQueryRequest(form_structures)); | 273 EXPECT_CALL(mock_metric_logger, Log(AutoFillMetrics::QUERY_SENT)).Times(1); |
| 274 EXPECT_TRUE(helper.download_manager.StartQueryRequest(form_structures, |
| 275 mock_metric_logger)); |
| 262 fetcher = factory.GetFetcherByID(3); | 276 fetcher = factory.GetFetcherByID(3); |
| 263 ASSERT_TRUE(fetcher); | 277 ASSERT_TRUE(fetcher); |
| 264 fetcher->set_backoff_delay( | 278 fetcher->set_backoff_delay( |
| 265 base::TimeDelta::FromMilliseconds(TestTimeouts::action_max_timeout_ms())); | 279 base::TimeDelta::FromMilliseconds(TestTimeouts::action_max_timeout_ms())); |
| 266 fetcher->delegate()->OnURLFetchComplete(fetcher, GURL(), URLRequestStatus(), | 280 fetcher->delegate()->OnURLFetchComplete(fetcher, GURL(), URLRequestStatus(), |
| 267 500, ResponseCookies(), | 281 500, ResponseCookies(), |
| 268 std::string(responses[0])); | 282 std::string(responses[0])); |
| 269 EXPECT_EQ(AutoFillDownloadTestHelper::REQUEST_QUERY_FAILED, | 283 EXPECT_EQ(AutoFillDownloadTestHelper::REQUEST_QUERY_FAILED, |
| 270 helper.responses_.front().type_of_response); | 284 helper.responses_.front().type_of_response); |
| 271 EXPECT_EQ(500, helper.responses_.front().error); | 285 EXPECT_EQ(500, helper.responses_.front().error); |
| 272 // Expected response on non-query request is an empty string. | 286 // Expected response on non-query request is an empty string. |
| 273 EXPECT_EQ(std::string(), helper.responses_.front().response); | 287 EXPECT_EQ(std::string(), helper.responses_.front().response); |
| 274 helper.responses_.pop_front(); | 288 helper.responses_.pop_front(); |
| 275 | 289 |
| 276 // Query requests should be ignored for the next 10 seconds. | 290 // Query requests should be ignored for the next 10 seconds. |
| 277 EXPECT_FALSE(helper.download_manager.StartQueryRequest(form_structures)); | 291 EXPECT_CALL(mock_metric_logger, Log(AutoFillMetrics::QUERY_SENT)).Times(0); |
| 292 EXPECT_FALSE(helper.download_manager.StartQueryRequest(form_structures, |
| 293 mock_metric_logger)); |
| 278 fetcher = factory.GetFetcherByID(4); | 294 fetcher = factory.GetFetcherByID(4); |
| 279 EXPECT_EQ(NULL, fetcher); | 295 EXPECT_EQ(NULL, fetcher); |
| 280 | 296 |
| 281 // Set upload to 100% so requests happen. | 297 // Set upload to 100% so requests happen. |
| 282 helper.download_manager.SetPositiveUploadRate(1.0); | 298 helper.download_manager.SetPositiveUploadRate(1.0); |
| 283 // Request with id 4. | 299 // Request with id 4. |
| 284 EXPECT_TRUE(helper.download_manager.StartUploadRequest(*(form_structures[0]), | 300 EXPECT_TRUE(helper.download_manager.StartUploadRequest(*(form_structures[0]), |
| 285 true)); | 301 true)); |
| 286 fetcher = factory.GetFetcherByID(4); | 302 fetcher = factory.GetFetcherByID(4); |
| 287 ASSERT_TRUE(fetcher); | 303 ASSERT_TRUE(fetcher); |
| 288 fetcher->set_backoff_delay( | 304 fetcher->set_backoff_delay( |
| 289 base::TimeDelta::FromMilliseconds(TestTimeouts::action_max_timeout_ms())); | 305 base::TimeDelta::FromMilliseconds(TestTimeouts::action_max_timeout_ms())); |
| 290 fetcher->delegate()->OnURLFetchComplete(fetcher, GURL(), URLRequestStatus(), | 306 fetcher->delegate()->OnURLFetchComplete(fetcher, GURL(), URLRequestStatus(), |
| 291 503, ResponseCookies(), | 307 503, ResponseCookies(), |
| 292 std::string(responses[2])); | 308 std::string(responses[2])); |
| 293 EXPECT_EQ(AutoFillDownloadTestHelper::REQUEST_UPLOAD_FAILED, | 309 EXPECT_EQ(AutoFillDownloadTestHelper::REQUEST_UPLOAD_FAILED, |
| 294 helper.responses_.front().type_of_response); | 310 helper.responses_.front().type_of_response); |
| 295 EXPECT_EQ(503, helper.responses_.front().error); | 311 EXPECT_EQ(503, helper.responses_.front().error); |
| 296 helper.responses_.pop_front(); | 312 helper.responses_.pop_front(); |
| 297 | 313 |
| 298 // Upload requests should be ignored for the next 10 seconds. | 314 // Upload requests should be ignored for the next 10 seconds. |
| 299 EXPECT_FALSE(helper.download_manager.StartUploadRequest(*(form_structures[0]), | 315 EXPECT_FALSE(helper.download_manager.StartUploadRequest(*(form_structures[0]), |
| 300 true)); | 316 true)); |
| 301 fetcher = factory.GetFetcherByID(5); | 317 fetcher = factory.GetFetcherByID(5); |
| 302 EXPECT_EQ(NULL, fetcher); | 318 EXPECT_EQ(NULL, fetcher); |
| 303 | 319 |
| 304 // Make sure consumer of URLFetcher does the right thing. | 320 // Make sure consumer of URLFetcher does the right thing. |
| 305 URLFetcher::set_factory(NULL); | 321 URLFetcher::set_factory(NULL); |
| 306 } | 322 } |
| 307 | |
| 308 } // namespace | |
| OLD | NEW |