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