| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/string16.h" | 9 #include "base/string16.h" |
| 10 #include "chrome/browser/autofill/autofill_external_delegate.h" | |
| 11 #include "chrome/browser/autofill/autofill_manager.h" | 10 #include "chrome/browser/autofill/autofill_manager.h" |
| 11 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 12 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" | 12 #include "chrome/browser/ui/tab_contents/test_tab_contents_wrapper.h" |
| 13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/test/test_browser_thread.h" | 14 #include "content/test/test_browser_thread.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "ui/gfx/rect.h" | 17 #include "ui/gfx/rect.h" |
| 18 #include "webkit/forms/form_data.h" | 18 #include "webkit/forms/form_data.h" |
| 19 #include "webkit/forms/form_field.h" | 19 #include "webkit/forms/form_field.h" |
| 20 | 20 |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 using testing::_; | 22 using testing::_; |
| 23 using webkit::forms::FormData; | 23 using webkit::forms::FormData; |
| 24 using webkit::forms::FormField; | 24 using webkit::forms::FormField; |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 class MockAutofillExternalDelegate : public AutofillExternalDelegate { | 28 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { |
| 29 public: | 29 public: |
| 30 explicit MockAutofillExternalDelegate(TabContentsWrapper* wrapper, | 30 MockAutofillExternalDelegate(TabContentsWrapper* wrapper, |
| 31 AutofillManager* autofill_manager) | 31 AutofillManager* autofill_manger) |
| 32 : AutofillExternalDelegate(wrapper, autofill_manager) {} | 32 : TestAutofillExternalDelegate(wrapper, autofill_manger) {} |
| 33 virtual ~MockAutofillExternalDelegate() {} | 33 ~MockAutofillExternalDelegate() {} |
| 34 | |
| 35 virtual void HideAutofillPopup() OVERRIDE {} | |
| 36 | 34 |
| 37 MOCK_METHOD5(ApplyAutofillSuggestions, void( | 35 MOCK_METHOD5(ApplyAutofillSuggestions, void( |
| 38 const std::vector<string16>& autofill_values, | 36 const std::vector<string16>& autofill_values, |
| 39 const std::vector<string16>& autofill_labels, | 37 const std::vector<string16>& autofill_labels, |
| 40 const std::vector<string16>& autofill_icons, | 38 const std::vector<string16>& autofill_icons, |
| 41 const std::vector<int>& autofill_unique_ids, | 39 const std::vector<int>& autofill_unique_ids, |
| 42 int separator_index)); | 40 int separator_index)); |
| 43 | 41 |
| 44 MOCK_METHOD4(OnQueryPlatformSpecific, | 42 MOCK_METHOD4(OnQueryPlatformSpecific, |
| 45 void(int query_id, | 43 void(int query_id, |
| 46 const webkit::forms::FormData& form, | 44 const webkit::forms::FormData& form, |
| 47 const webkit::forms::FormField& field, | 45 const webkit::forms::FormField& field, |
| 48 const gfx::Rect& bounds)); | 46 const gfx::Rect& bounds)); |
| 49 | 47 |
| 48 MOCK_METHOD0(HideAutofillPopup, void()); |
| 49 |
| 50 private: | 50 private: |
| 51 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate); | 51 virtual void HideAutofillPopupInternal() {}; |
| 52 }; |
| 53 |
| 54 class MockAutofillManager : public AutofillManager { |
| 55 public: |
| 56 explicit MockAutofillManager(TabContentsWrapper* tab_contents) |
| 57 : AutofillManager(tab_contents) {} |
| 58 ~MockAutofillManager() {} |
| 59 |
| 60 MOCK_METHOD4(OnFillAutofillFormData, |
| 61 void(int query_id, |
| 62 const webkit::forms::FormData& form, |
| 63 const webkit::forms::FormField& field, |
| 64 int unique_id)); |
| 52 }; | 65 }; |
| 53 | 66 |
| 54 } // namespace | 67 } // namespace |
| 55 | 68 |
| 56 class AutofillExternalDelegateTest : public TabContentsWrapperTestHarness { | 69 class AutofillExternalDelegateUnitTest : public TabContentsWrapperTestHarness { |
| 57 public: | 70 public: |
| 58 AutofillExternalDelegateTest() | 71 AutofillExternalDelegateUnitTest() |
| 59 : ui_thread_(BrowserThread::UI, &message_loop_) {} | 72 : ui_thread_(BrowserThread::UI, &message_loop_) {} |
| 60 virtual ~AutofillExternalDelegateTest() {} | 73 virtual ~AutofillExternalDelegateUnitTest() {} |
| 61 | 74 |
| 62 virtual void SetUp() OVERRIDE { | 75 virtual void SetUp() OVERRIDE { |
| 63 TabContentsWrapperTestHarness::SetUp(); | 76 TabContentsWrapperTestHarness::SetUp(); |
| 64 autofill_manager_ = new AutofillManager(contents_wrapper()); | 77 autofill_manager_ = new MockAutofillManager(contents_wrapper()); |
| 78 external_delegate_.reset(new MockAutofillExternalDelegate( |
| 79 contents_wrapper(), |
| 80 autofill_manager_)); |
| 65 } | 81 } |
| 66 | 82 |
| 67 protected: | 83 protected: |
| 68 scoped_refptr<AutofillManager> autofill_manager_; | 84 scoped_refptr<MockAutofillManager> autofill_manager_; |
| 85 scoped_ptr<MockAutofillExternalDelegate> external_delegate_; |
| 69 | 86 |
| 70 private: | 87 private: |
| 71 content::TestBrowserThread ui_thread_; | 88 content::TestBrowserThread ui_thread_; |
| 72 | 89 |
| 73 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegateTest); | 90 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegateUnitTest); |
| 74 }; | 91 }; |
| 75 | 92 |
| 76 // Test that our external delegate called the virtual methods at the right time. | 93 // Test that our external delegate called the virtual methods at the right time. |
| 77 TEST_F(AutofillExternalDelegateTest, TestExternalDelegateVirtualCalls) { | 94 TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) { |
| 78 MockAutofillExternalDelegate external_delegate(contents_wrapper(), | |
| 79 autofill_manager_); | |
| 80 const int kQueryId = 5; | 95 const int kQueryId = 5; |
| 81 const FormData form; | 96 const FormData form; |
| 82 FormField field; | 97 FormField field; |
| 83 field.is_focusable = true; | 98 field.is_focusable = true; |
| 84 field.should_autocomplete = true; | 99 field.should_autocomplete = true; |
| 85 const gfx::Rect bounds; | 100 const gfx::Rect bounds; |
| 86 | 101 |
| 87 EXPECT_CALL(external_delegate, | 102 EXPECT_CALL(*external_delegate_, |
| 88 OnQueryPlatformSpecific(kQueryId, form, field, bounds)); | 103 OnQueryPlatformSpecific(kQueryId, form, field, bounds)); |
| 89 | 104 |
| 90 // This should call OnQueryPlatform specific. | 105 // This should call OnQueryPlatform specific. |
| 91 external_delegate.OnQuery(kQueryId, form, field, bounds, false); | 106 external_delegate_->OnQuery(kQueryId, form, field, bounds, false); |
| 92 | 107 |
| 93 | 108 EXPECT_CALL(*external_delegate_, ApplyAutofillSuggestions(_, _, _, _, _)); |
| 94 EXPECT_CALL(external_delegate, ApplyAutofillSuggestions(_, _, _, _, _)); | |
| 95 | 109 |
| 96 // This should call ApplyAutofillSuggestions. | 110 // This should call ApplyAutofillSuggestions. |
| 97 std::vector<string16> autofill_item; | 111 std::vector<string16> autofill_item; |
| 98 autofill_item.push_back(string16()); | 112 autofill_item.push_back(string16()); |
| 99 std::vector<int> autofill_ids; | 113 std::vector<int> autofill_ids; |
| 100 autofill_ids.push_back(1); | 114 autofill_ids.push_back(1); |
| 101 external_delegate.OnSuggestionsReturned(kQueryId, | 115 external_delegate_->OnSuggestionsReturned(kQueryId, |
| 102 autofill_item, | 116 autofill_item, |
| 103 autofill_item, | 117 autofill_item, |
| 104 autofill_item, | 118 autofill_item, |
| 105 autofill_ids); | 119 autofill_ids); |
| 120 |
| 121 |
| 122 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); |
| 123 |
| 124 // This should trigger a call to hide the popup since |
| 125 // we've selected an option. |
| 126 external_delegate_->DidAcceptAutofillSuggestions(autofill_item[0], |
| 127 autofill_ids[0], 0); |
| 106 } | 128 } |
| 129 |
| 130 // Test that the Autofill delegate doesn't try and fill a form with a |
| 131 // negative unique id. |
| 132 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) { |
| 133 // Ensure it doesn't try to preview the negative id. |
| 134 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); |
| 135 external_delegate_->SelectAutofillSuggestionAtIndex(-1, 0); |
| 136 |
| 137 // Ensure it doesn't try to fill the form in with the negative id. |
| 138 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); |
| 139 external_delegate_->DidAcceptAutofillSuggestions(string16(), -1, 0); |
| 140 } |
| OLD | NEW |