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