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