OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
11 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
12 #include "base/string16.h" | 13 #include "base/string16.h" |
13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "base/time.h" | 16 #include "base/time.h" |
16 #include "base/tuple.h" | 17 #include "base/tuple.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
18 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 19 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
(...skipping 15 matching lines...) Expand all Loading... |
34 #include "components/autofill/browser/credit_card.h" | 35 #include "components/autofill/browser/credit_card.h" |
35 #include "components/autofill/browser/personal_data_manager.h" | 36 #include "components/autofill/browser/personal_data_manager.h" |
36 #include "components/autofill/browser/test_autofill_external_delegate.h" | 37 #include "components/autofill/browser/test_autofill_external_delegate.h" |
37 #include "components/autofill/common/autofill_messages.h" | 38 #include "components/autofill/common/autofill_messages.h" |
38 #include "components/autofill/common/form_data.h" | 39 #include "components/autofill/common/form_data.h" |
39 #include "components/autofill/common/form_field_data.h" | 40 #include "components/autofill/common/form_field_data.h" |
40 #include "components/user_prefs/user_prefs.h" | 41 #include "components/user_prefs/user_prefs.h" |
41 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
42 #include "content/public/test/mock_render_process_host.h" | 43 #include "content/public/test/mock_render_process_host.h" |
43 #include "content/public/test/test_browser_thread.h" | 44 #include "content/public/test/test_browser_thread.h" |
| 45 #include "content/public/test/test_utils.h" |
44 #include "googleurl/src/gurl.h" | 46 #include "googleurl/src/gurl.h" |
45 #include "grit/generated_resources.h" | 47 #include "grit/generated_resources.h" |
46 #include "ipc/ipc_test_sink.h" | 48 #include "ipc/ipc_test_sink.h" |
47 #include "testing/gmock/include/gmock/gmock.h" | 49 #include "testing/gmock/include/gmock/gmock.h" |
48 #include "testing/gtest/include/gtest/gtest.h" | 50 #include "testing/gtest/include/gtest/gtest.h" |
49 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 51 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
50 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" | 52 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" |
51 #include "ui/base/l10n/l10n_util.h" | 53 #include "ui/base/l10n/l10n_util.h" |
52 #include "ui/gfx/rect.h" | 54 #include "ui/gfx/rect.h" |
53 | 55 |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 has_address_fields, true, true); | 479 has_address_fields, true, true); |
478 } | 480 } |
479 | 481 |
480 class TestAutofillManager : public AutofillManager { | 482 class TestAutofillManager : public AutofillManager { |
481 public: | 483 public: |
482 TestAutofillManager(content::WebContents* web_contents, | 484 TestAutofillManager(content::WebContents* web_contents, |
483 autofill::AutofillManagerDelegate* delegate, | 485 autofill::AutofillManagerDelegate* delegate, |
484 TestPersonalDataManager* personal_data) | 486 TestPersonalDataManager* personal_data) |
485 : AutofillManager(web_contents, delegate, personal_data), | 487 : AutofillManager(web_contents, delegate, personal_data), |
486 personal_data_(personal_data), | 488 personal_data_(personal_data), |
487 autofill_enabled_(true), | 489 autofill_enabled_(true) {} |
488 did_finish_async_form_submit_(false), | |
489 message_loop_is_running_(false) { | |
490 } | |
491 virtual ~TestAutofillManager() {} | 490 virtual ~TestAutofillManager() {} |
492 | 491 |
493 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } | 492 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } |
494 | 493 |
495 void set_autofill_enabled(bool autofill_enabled) { | 494 void set_autofill_enabled(bool autofill_enabled) { |
496 autofill_enabled_ = autofill_enabled; | 495 autofill_enabled_ = autofill_enabled; |
497 } | 496 } |
498 | 497 |
499 void set_autocheckout_url_prefix(const std::string& autocheckout_url_prefix) { | 498 void set_autocheckout_url_prefix(const std::string& autocheckout_url_prefix) { |
500 autocheckout_url_prefix_ = autocheckout_url_prefix; | 499 autocheckout_url_prefix_ = autocheckout_url_prefix; |
(...skipping 12 matching lines...) Expand all Loading... |
513 void set_expected_submitted_field_types( | 512 void set_expected_submitted_field_types( |
514 const std::vector<FieldTypeSet>& expected_types) { | 513 const std::vector<FieldTypeSet>& expected_types) { |
515 expected_submitted_field_types_ = expected_types; | 514 expected_submitted_field_types_ = expected_types; |
516 } | 515 } |
517 | 516 |
518 virtual void UploadFormDataAsyncCallback( | 517 virtual void UploadFormDataAsyncCallback( |
519 const FormStructure* submitted_form, | 518 const FormStructure* submitted_form, |
520 const base::TimeTicks& load_time, | 519 const base::TimeTicks& load_time, |
521 const base::TimeTicks& interaction_time, | 520 const base::TimeTicks& interaction_time, |
522 const base::TimeTicks& submission_time) OVERRIDE { | 521 const base::TimeTicks& submission_time) OVERRIDE { |
523 if (message_loop_is_running_) { | 522 message_loop_runner_->Quit(); |
524 MessageLoop::current()->Quit(); | |
525 message_loop_is_running_ = false; | |
526 } else { | |
527 did_finish_async_form_submit_ = true; | |
528 } | |
529 | 523 |
530 // If we have expected field types set, make sure they match. | 524 // If we have expected field types set, make sure they match. |
531 if (!expected_submitted_field_types_.empty()) { | 525 if (!expected_submitted_field_types_.empty()) { |
532 ASSERT_EQ(expected_submitted_field_types_.size(), | 526 ASSERT_EQ(expected_submitted_field_types_.size(), |
533 submitted_form->field_count()); | 527 submitted_form->field_count()); |
534 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) { | 528 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) { |
535 SCOPED_TRACE( | 529 SCOPED_TRACE( |
536 base::StringPrintf( | 530 base::StringPrintf( |
537 "Field %d with value %s", static_cast<int>(i), | 531 "Field %d with value %s", static_cast<int>(i), |
538 UTF16ToUTF8(submitted_form->field(i)->value).c_str())); | 532 UTF16ToUTF8(submitted_form->field(i)->value).c_str())); |
539 const FieldTypeSet& possible_types = | 533 const FieldTypeSet& possible_types = |
540 submitted_form->field(i)->possible_types(); | 534 submitted_form->field(i)->possible_types(); |
541 EXPECT_EQ(expected_submitted_field_types_[i].size(), | 535 EXPECT_EQ(expected_submitted_field_types_[i].size(), |
542 possible_types.size()); | 536 possible_types.size()); |
543 for (FieldTypeSet::const_iterator it = | 537 for (FieldTypeSet::const_iterator it = |
544 expected_submitted_field_types_[i].begin(); | 538 expected_submitted_field_types_[i].begin(); |
545 it != expected_submitted_field_types_[i].end(); ++it) { | 539 it != expected_submitted_field_types_[i].end(); ++it) { |
546 EXPECT_TRUE(possible_types.count(*it)) | 540 EXPECT_TRUE(possible_types.count(*it)) |
547 << "Expected type: " << AutofillType::FieldTypeToString(*it); | 541 << "Expected type: " << AutofillType::FieldTypeToString(*it); |
548 } | 542 } |
549 } | 543 } |
550 } | 544 } |
551 | 545 |
552 AutofillManager::UploadFormDataAsyncCallback(submitted_form, | 546 AutofillManager::UploadFormDataAsyncCallback(submitted_form, |
553 load_time, | 547 load_time, |
554 interaction_time, | 548 interaction_time, |
555 submission_time); | 549 submission_time); |
556 } | 550 } |
557 | 551 |
| 552 // Resets the MessageLoopRunner so that it can wait for an asynchronous form |
| 553 // submission to complete. |
| 554 void ResetMessageLoopRunner() { |
| 555 message_loop_runner_ = new content::MessageLoopRunner(); |
| 556 } |
| 557 |
558 // Wait for the asynchronous OnFormSubmitted() call to complete. | 558 // Wait for the asynchronous OnFormSubmitted() call to complete. |
559 void WaitForAsyncFormSubmit() { | 559 void WaitForAsyncFormSubmit() { |
560 if (!did_finish_async_form_submit_) { | 560 message_loop_runner_->Run(); |
561 // TODO(isherman): It seems silly to need this variable. Is there some | |
562 // way I can just query the message loop's state? | |
563 message_loop_is_running_ = true; | |
564 MessageLoop::current()->Run(); | |
565 } else { | |
566 did_finish_async_form_submit_ = false; | |
567 } | |
568 } | 561 } |
569 | 562 |
570 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE { | 563 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE { |
571 submitted_form_signature_ = submitted_form.FormSignature(); | 564 submitted_form_signature_ = submitted_form.FormSignature(); |
572 } | 565 } |
573 | 566 |
574 virtual void SendPasswordGenerationStateToRenderer( | 567 virtual void SendPasswordGenerationStateToRenderer( |
575 content::RenderViewHost* host, bool enabled) OVERRIDE { | 568 content::RenderViewHost* host, bool enabled) OVERRIDE { |
576 sent_states_.push_back(enabled); | 569 sent_states_.push_back(enabled); |
577 } | 570 } |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 } | 619 } |
627 | 620 |
628 private: | 621 private: |
629 // Weak reference. | 622 // Weak reference. |
630 TestPersonalDataManager* personal_data_; | 623 TestPersonalDataManager* personal_data_; |
631 | 624 |
632 bool autofill_enabled_; | 625 bool autofill_enabled_; |
633 std::vector<std::pair<WebFormElement::AutocompleteResult, FormData> > | 626 std::vector<std::pair<WebFormElement::AutocompleteResult, FormData> > |
634 request_autocomplete_results_; | 627 request_autocomplete_results_; |
635 | 628 |
636 bool did_finish_async_form_submit_; | 629 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
637 bool message_loop_is_running_; | |
638 | 630 |
639 std::string autocheckout_url_prefix_; | 631 std::string autocheckout_url_prefix_; |
640 std::string submitted_form_signature_; | 632 std::string submitted_form_signature_; |
641 std::vector<FieldTypeSet> expected_submitted_field_types_; | 633 std::vector<FieldTypeSet> expected_submitted_field_types_; |
642 std::vector<bool> sent_states_; | 634 std::vector<bool> sent_states_; |
643 | 635 |
644 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); | 636 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); |
645 }; | 637 }; |
646 | 638 |
647 } // namespace | 639 } // namespace |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
724 | 716 |
725 void FormsSeen(const std::vector<FormData>& forms) { | 717 void FormsSeen(const std::vector<FormData>& forms) { |
726 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), false); | 718 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), false); |
727 } | 719 } |
728 | 720 |
729 void PartialFormsSeen(const std::vector<FormData>& forms) { | 721 void PartialFormsSeen(const std::vector<FormData>& forms) { |
730 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), true); | 722 autofill_manager_->OnFormsSeen(forms, base::TimeTicks(), true); |
731 } | 723 } |
732 | 724 |
733 void FormSubmitted(const FormData& form) { | 725 void FormSubmitted(const FormData& form) { |
| 726 autofill_manager_->ResetMessageLoopRunner(); |
734 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now())) | 727 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now())) |
735 autofill_manager_->WaitForAsyncFormSubmit(); | 728 autofill_manager_->WaitForAsyncFormSubmit(); |
736 } | 729 } |
737 | 730 |
738 void FillAutofillFormData(int query_id, | 731 void FillAutofillFormData(int query_id, |
739 const FormData& form, | 732 const FormData& form, |
740 const FormFieldData& field, | 733 const FormFieldData& field, |
741 int unique_id) { | 734 int unique_id) { |
742 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id); | 735 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id); |
743 } | 736 } |
(...skipping 2589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3333 CreateTestAddressFormData(&form); | 3326 CreateTestAddressFormData(&form); |
3334 std::vector<FormData> forms(1, form); | 3327 std::vector<FormData> forms(1, form); |
3335 FormsSeen(forms); | 3328 FormsSeen(forms); |
3336 const FormFieldData& field = form.fields[0]; | 3329 const FormFieldData& field = form.fields[0]; |
3337 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() | 3330 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() |
3338 | 3331 |
3339 autofill_manager_->SetExternalDelegate(NULL); | 3332 autofill_manager_->SetExternalDelegate(NULL); |
3340 } | 3333 } |
3341 | 3334 |
3342 } // namespace autofill | 3335 } // namespace autofill |
OLD | NEW |