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