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_manager.h" | 10 #include "chrome/browser/autofill/autofill_manager.h" |
11 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 11 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
12 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" | 12 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" |
13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
14 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/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 "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
18 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
19 #include "webkit/forms/form_data.h" | 19 #include "webkit/forms/form_data.h" |
20 #include "webkit/forms/form_field.h" | 20 #include "webkit/forms/form_field.h" |
21 | 21 |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 using testing::_; | 23 using testing::_; |
24 using webkit::forms::FormData; | 24 using webkit::forms::FormData; |
25 using webkit::forms::FormField; | 25 using webkit::forms::FormField; |
26 using WebKit::WebAutofillClient; | 26 using WebKit::WebAutofillClient; |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
| 30 // A constant value to use as the Autofill query ID. |
| 31 const int kQueryId = 5; |
| 32 |
| 33 // A constant value to use as an Autofill profile ID. |
| 34 const int kAutofillProfileId = 1; |
| 35 |
30 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { | 36 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { |
31 public: | 37 public: |
32 MockAutofillExternalDelegate(TabContents* tab_contents, | 38 MockAutofillExternalDelegate(TabContents* tab_contents, |
33 AutofillManager* autofill_manger) | 39 AutofillManager* autofill_manger) |
34 : TestAutofillExternalDelegate(tab_contents, autofill_manger) {} | 40 : TestAutofillExternalDelegate(tab_contents, autofill_manger) {} |
35 ~MockAutofillExternalDelegate() {} | 41 ~MockAutofillExternalDelegate() {} |
36 | 42 |
37 MOCK_METHOD4(ApplyAutofillSuggestions, void( | 43 MOCK_METHOD4(ApplyAutofillSuggestions, void( |
38 const std::vector<string16>& autofill_values, | 44 const std::vector<string16>& autofill_values, |
39 const std::vector<string16>& autofill_labels, | 45 const std::vector<string16>& autofill_labels, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 | 87 |
82 virtual void SetUp() OVERRIDE { | 88 virtual void SetUp() OVERRIDE { |
83 TabContentsTestHarness::SetUp(); | 89 TabContentsTestHarness::SetUp(); |
84 autofill_manager_ = new MockAutofillManager(tab_contents()); | 90 autofill_manager_ = new MockAutofillManager(tab_contents()); |
85 external_delegate_.reset(new MockAutofillExternalDelegate( | 91 external_delegate_.reset(new MockAutofillExternalDelegate( |
86 tab_contents(), | 92 tab_contents(), |
87 autofill_manager_)); | 93 autofill_manager_)); |
88 } | 94 } |
89 | 95 |
90 protected: | 96 protected: |
| 97 // Set up the expectation for a platform specific OnQuery call and then |
| 98 // execute it with the given QueryId. |
| 99 void IssueOnQuery(int query_id) { |
| 100 const FormData form; |
| 101 FormField field; |
| 102 field.is_focusable = true; |
| 103 field.should_autocomplete = true; |
| 104 const gfx::Rect bounds; |
| 105 |
| 106 EXPECT_CALL(*external_delegate_, |
| 107 OnQueryPlatformSpecific(query_id, form, field, bounds)); |
| 108 |
| 109 // This should call OnQueryPlatform specific. |
| 110 external_delegate_->OnQuery(query_id, form, field, bounds, false); |
| 111 } |
| 112 |
91 scoped_refptr<MockAutofillManager> autofill_manager_; | 113 scoped_refptr<MockAutofillManager> autofill_manager_; |
92 scoped_ptr<MockAutofillExternalDelegate> external_delegate_; | 114 scoped_ptr<MockAutofillExternalDelegate> external_delegate_; |
93 | 115 |
94 private: | 116 private: |
95 content::TestBrowserThread ui_thread_; | 117 content::TestBrowserThread ui_thread_; |
96 | 118 |
97 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegateUnitTest); | 119 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegateUnitTest); |
98 }; | 120 }; |
99 | 121 |
100 // Test that our external delegate called the virtual methods at the right time. | 122 // Test that our external delegate called the virtual methods at the right time. |
101 TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) { | 123 TEST_F(AutofillExternalDelegateUnitTest, TestExternalDelegateVirtualCalls) { |
102 const int kQueryId = 5; | 124 IssueOnQuery(kQueryId); |
103 const FormData form; | |
104 FormField field; | |
105 field.is_focusable = true; | |
106 field.should_autocomplete = true; | |
107 const gfx::Rect bounds; | |
108 | 125 |
| 126 // The enums must be cast to ints to prevent compile errors on linux_rel. |
109 EXPECT_CALL(*external_delegate_, | 127 EXPECT_CALL(*external_delegate_, |
110 OnQueryPlatformSpecific(kQueryId, form, field, bounds)); | 128 ApplyAutofillSuggestions(_, _, _, testing::ElementsAre( |
111 | 129 kAutofillProfileId, |
112 // This should call OnQueryPlatform specific. | 130 static_cast<int>(WebAutofillClient::MenuItemIDSeparator), |
113 external_delegate_->OnQuery(kQueryId, form, field, bounds, false); | 131 static_cast<int>( |
114 | 132 WebAutofillClient::MenuItemIDAutofillOptions)))); |
115 EXPECT_CALL(*external_delegate_, ApplyAutofillSuggestions(_, _, _, _)); | |
116 | 133 |
117 // This should call ApplyAutofillSuggestions. | 134 // This should call ApplyAutofillSuggestions. |
118 std::vector<string16> autofill_item; | 135 std::vector<string16> autofill_item; |
119 autofill_item.push_back(string16()); | 136 autofill_item.push_back(string16()); |
120 std::vector<int> autofill_ids; | 137 std::vector<int> autofill_ids; |
121 autofill_ids.push_back(1); | 138 autofill_ids.push_back(kAutofillProfileId); |
122 external_delegate_->OnSuggestionsReturned(kQueryId, | 139 external_delegate_->OnSuggestionsReturned(kQueryId, |
123 autofill_item, | 140 autofill_item, |
124 autofill_item, | 141 autofill_item, |
125 autofill_item, | 142 autofill_item, |
126 autofill_ids); | 143 autofill_ids); |
127 | 144 |
128 | 145 |
129 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); | 146 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); |
130 | 147 |
131 // Called by DidAutofillSuggestions, add expectation to remove warning. | 148 // Called by DidAutofillSuggestions, add expectation to remove warning. |
132 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)); | 149 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)); |
133 | 150 |
134 // This should trigger a call to hide the popup since | 151 // This should trigger a call to hide the popup since |
135 // we've selected an option. | 152 // we've selected an option. |
136 external_delegate_->DidAcceptAutofillSuggestions(autofill_item[0], | 153 external_delegate_->DidAcceptAutofillSuggestions(autofill_item[0], |
137 autofill_ids[0], 0); | 154 autofill_ids[0], 0); |
138 } | 155 } |
139 | 156 |
| 157 // Test that data list elements for a node will appear in the Autofill popup. |
| 158 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) { |
| 159 IssueOnQuery(kQueryId); |
| 160 |
| 161 std::vector<string16> data_list_items; |
| 162 data_list_items.push_back(string16()); |
| 163 std::vector<int> data_list_ids; |
| 164 data_list_ids.push_back(WebAutofillClient::MenuItemIDDataListEntry); |
| 165 |
| 166 external_delegate_->SetCurrentDataListValues(data_list_items, |
| 167 data_list_items, |
| 168 data_list_items, |
| 169 data_list_ids); |
| 170 |
| 171 // The enums must be cast to ints to prevent compile errors on linux_rel. |
| 172 EXPECT_CALL(*external_delegate_, |
| 173 ApplyAutofillSuggestions( |
| 174 _, _, _, testing::ElementsAre( |
| 175 static_cast<int>( |
| 176 WebAutofillClient::MenuItemIDDataListEntry), |
| 177 static_cast<int>(WebAutofillClient::MenuItemIDSeparator), |
| 178 kAutofillProfileId, |
| 179 static_cast<int>(WebAutofillClient::MenuItemIDSeparator), |
| 180 static_cast<int>( |
| 181 WebAutofillClient::MenuItemIDAutofillOptions)))); |
| 182 |
| 183 // This should call ApplyAutofillSuggestions. |
| 184 std::vector<string16> autofill_item; |
| 185 autofill_item.push_back(string16()); |
| 186 std::vector<int> autofill_ids; |
| 187 autofill_ids.push_back(kAutofillProfileId); |
| 188 external_delegate_->OnSuggestionsReturned(kQueryId, |
| 189 autofill_item, |
| 190 autofill_item, |
| 191 autofill_item, |
| 192 autofill_ids); |
| 193 |
| 194 // Try calling OnSuggestionsReturned with no Autofill values and ensure |
| 195 // the datalist items are still shown. |
| 196 // The enum must be cast to an int to prevent compile errors on linux_rel. |
| 197 EXPECT_CALL(*external_delegate_, |
| 198 ApplyAutofillSuggestions( |
| 199 _, _, _, testing::ElementsAre( |
| 200 static_cast<int>( |
| 201 WebAutofillClient::MenuItemIDDataListEntry)))); |
| 202 |
| 203 autofill_item = std::vector<string16>(); |
| 204 autofill_ids = std::vector<int>(); |
| 205 external_delegate_->OnSuggestionsReturned(kQueryId, |
| 206 autofill_item, |
| 207 autofill_item, |
| 208 autofill_item, |
| 209 autofill_ids); |
| 210 } |
| 211 |
140 // Test that the Autofill delegate doesn't try and fill a form with a | 212 // Test that the Autofill delegate doesn't try and fill a form with a |
141 // negative unique id. | 213 // negative unique id. |
142 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) { | 214 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) { |
143 // Ensure it doesn't try to preview the negative id. | 215 // Ensure it doesn't try to preview the negative id. |
144 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); | 216 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); |
| 217 EXPECT_CALL(*external_delegate_, ClearPreviewedForm()).Times(1); |
145 external_delegate_->SelectAutofillSuggestionAtIndex(-1); | 218 external_delegate_->SelectAutofillSuggestionAtIndex(-1); |
146 | 219 |
147 // Ensure it doesn't try to fill the form in with the negative id. | 220 // Ensure it doesn't try to fill the form in with the negative id. |
148 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); | 221 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); |
149 external_delegate_->DidAcceptAutofillSuggestions(string16(), -1, 0); | 222 external_delegate_->DidAcceptAutofillSuggestions(string16(), -1, 0); |
150 } | 223 } |
151 | 224 |
152 // Test that the ClearPreview IPC is only sent the form was being previewed | 225 // Test that the ClearPreview IPC is only sent the form was being previewed |
153 // (i.e. it isn't autofilling a password). | 226 // (i.e. it isn't autofilling a password). |
154 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) { | 227 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) { |
(...skipping 11 matching lines...) Expand all Loading... |
166 external_delegate_->SelectAutofillSuggestionAtIndex(1); | 239 external_delegate_->SelectAutofillSuggestionAtIndex(1); |
167 } | 240 } |
168 | 241 |
169 // Test that the popup is hidden once we are done editing the autofill field. | 242 // Test that the popup is hidden once we are done editing the autofill field. |
170 TEST_F(AutofillExternalDelegateUnitTest, | 243 TEST_F(AutofillExternalDelegateUnitTest, |
171 ExternalDelegateHidePopupAfterEditing) { | 244 ExternalDelegateHidePopupAfterEditing) { |
172 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); | 245 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); |
173 | 246 |
174 external_delegate_->DidEndTextFieldEditing(); | 247 external_delegate_->DidEndTextFieldEditing(); |
175 } | 248 } |
OLD | NEW |