Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: chrome/browser/autofill/autofill_external_delegate_unittest.cc

Issue 10443084: Add Datalist Support to New Autofill UI (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Better Data List Tests Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_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/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.
Ilya Sherman 2012/06/07 22:56:05 nit: "Id" -> either "id" or "ID"
csharp 2012/06/08 14:44:54 Done.
31 const int kQueryId = 5;
32
33 // A constant vale to use as an Autofill profile Id.
Ilya Sherman 2012/06/07 22:56:05 nit: "vale" -> "value"
csharp 2012/06/08 14:44:54 Done.
34 const int kAutofillProfileId = 1;
35
30 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { 36 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate {
31 public: 37 public:
32 MockAutofillExternalDelegate(TabContentsWrapper* wrapper, 38 MockAutofillExternalDelegate(TabContentsWrapper* wrapper,
33 AutofillManager* autofill_manger) 39 AutofillManager* autofill_manger)
34 : TestAutofillExternalDelegate(wrapper, autofill_manger) {} 40 : TestAutofillExternalDelegate(wrapper, 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
81 87
82 virtual void SetUp() OVERRIDE { 88 virtual void SetUp() OVERRIDE {
83 TabContentsWrapperTestHarness::SetUp(); 89 TabContentsWrapperTestHarness::SetUp();
84 autofill_manager_ = new MockAutofillManager(contents_wrapper()); 90 autofill_manager_ = new MockAutofillManager(contents_wrapper());
85 external_delegate_.reset(new MockAutofillExternalDelegate( 91 external_delegate_.reset(new MockAutofillExternalDelegate(
86 contents_wrapper(), 92 contents_wrapper(),
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
109 EXPECT_CALL(*external_delegate_, 126 EXPECT_CALL(*external_delegate_,
110 OnQueryPlatformSpecific(kQueryId, form, field, bounds)); 127 ApplyAutofillSuggestions(_, _, _, testing::ElementsAre(
111 128 kAutofillProfileId,
112 // This should call OnQueryPlatform specific. 129 WebAutofillClient::MenuItemIDSeparator,
113 external_delegate_->OnQuery(kQueryId, form, field, bounds, false); 130 WebAutofillClient::MenuItemIDAutofillOptions)));
114
115 EXPECT_CALL(*external_delegate_, ApplyAutofillSuggestions(_, _, _, _));
116 131
117 // This should call ApplyAutofillSuggestions. 132 // This should call ApplyAutofillSuggestions.
118 std::vector<string16> autofill_item; 133 std::vector<string16> autofill_item;
119 autofill_item.push_back(string16()); 134 autofill_item.push_back(string16());
120 std::vector<int> autofill_ids; 135 std::vector<int> autofill_ids;
121 autofill_ids.push_back(1); 136 autofill_ids.push_back(kAutofillProfileId);
122 external_delegate_->OnSuggestionsReturned(kQueryId, 137 external_delegate_->OnSuggestionsReturned(kQueryId,
123 autofill_item, 138 autofill_item,
124 autofill_item, 139 autofill_item,
125 autofill_item, 140 autofill_item,
126 autofill_ids); 141 autofill_ids);
127 142
128 143
129 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); 144 EXPECT_CALL(*external_delegate_, HideAutofillPopup());
130 145
131 // Called by DidAutofillSuggestions, add expectation to remove warning. 146 // Called by DidAutofillSuggestions, add expectation to remove warning.
132 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)); 147 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _));
133 148
134 // This should trigger a call to hide the popup since 149 // This should trigger a call to hide the popup since
135 // we've selected an option. 150 // we've selected an option.
136 external_delegate_->DidAcceptAutofillSuggestions(autofill_item[0], 151 external_delegate_->DidAcceptAutofillSuggestions(autofill_item[0],
137 autofill_ids[0], 0); 152 autofill_ids[0], 0);
138 } 153 }
139 154
155 // Test that data list elements for a node will appear in the Autofill popup.
156 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateDataList) {
157 IssueOnQuery(kQueryId);
158
159 std::vector<string16> data_list_items;
160 data_list_items.push_back(string16());
161 std::vector<int> data_list_ids;
162 data_list_ids.push_back(WebAutofillClient::MenuItemIDDataListEntry);
163
164 external_delegate_->SetCurrentDataListValues(data_list_items,
165 data_list_items,
166 data_list_items,
167 data_list_ids);
168
169 EXPECT_CALL(*external_delegate_,
170 ApplyAutofillSuggestions(
171 _, _, _, testing::ElementsAre(
172 WebAutofillClient::MenuItemIDDataListEntry,
173 WebAutofillClient::MenuItemIDSeparator,
174 kAutofillProfileId,
175 WebAutofillClient::MenuItemIDSeparator,
176 WebAutofillClient::MenuItemIDAutofillOptions)));
177
178 // This should call ApplyAutofillSuggestions.
179 std::vector<string16> autofill_item;
180 autofill_item.push_back(string16());
181 std::vector<int> autofill_ids;
182 autofill_ids.push_back(kAutofillProfileId);
183 external_delegate_->OnSuggestionsReturned(kQueryId,
184 autofill_item,
185 autofill_item,
186 autofill_item,
187 autofill_ids);
188
189 // Try calling OnSuggestionsReturned with no Autofill values and ensure
190 // the datalist items are still shown.
191 EXPECT_CALL(*external_delegate_,
192 ApplyAutofillSuggestions(
193 _, _, _, testing::ElementsAre(
194 WebAutofillClient::MenuItemIDDataListEntry)));
195
196 autofill_item = std::vector<string16>();
197 autofill_ids = std::vector<int>();
198 external_delegate_->OnSuggestionsReturned(kQueryId,
199 autofill_item,
200 autofill_item,
201 autofill_item,
202 autofill_ids);
203 }
204
140 // Test that the Autofill delegate doesn't try and fill a form with a 205 // Test that the Autofill delegate doesn't try and fill a form with a
141 // negative unique id. 206 // negative unique id.
142 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) { 207 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateInvalidUniqueId) {
143 // Ensure it doesn't try to preview the negative id. 208 // Ensure it doesn't try to preview the negative id.
144 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); 209 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0);
210 EXPECT_CALL(*external_delegate_, ClearPreviewedForm()).Times(1);
145 external_delegate_->SelectAutofillSuggestionAtIndex(-1); 211 external_delegate_->SelectAutofillSuggestionAtIndex(-1);
146 212
147 // Ensure it doesn't try to fill the form in with the negative id. 213 // Ensure it doesn't try to fill the form in with the negative id.
148 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0); 214 EXPECT_CALL(*autofill_manager_, OnFillAutofillFormData(_, _, _, _)).Times(0);
149 external_delegate_->DidAcceptAutofillSuggestions(string16(), -1, 0); 215 external_delegate_->DidAcceptAutofillSuggestions(string16(), -1, 0);
150 } 216 }
151 217
152 // Test that the ClearPreview IPC is only sent the form was being previewed 218 // Test that the ClearPreview IPC is only sent the form was being previewed
153 // (i.e. it isn't autofilling a password). 219 // (i.e. it isn't autofilling a password).
154 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) { 220 TEST_F(AutofillExternalDelegateUnitTest, ExternalDelegateClearPreviewedForm) {
(...skipping 11 matching lines...) Expand all
166 external_delegate_->SelectAutofillSuggestionAtIndex(1); 232 external_delegate_->SelectAutofillSuggestionAtIndex(1);
167 } 233 }
168 234
169 // Test that the popup is hidden once we are done editing the autofill field. 235 // Test that the popup is hidden once we are done editing the autofill field.
170 TEST_F(AutofillExternalDelegateUnitTest, 236 TEST_F(AutofillExternalDelegateUnitTest,
171 ExternalDelegateHidePopupAfterEditing) { 237 ExternalDelegateHidePopupAfterEditing) {
172 EXPECT_CALL(*external_delegate_, HideAutofillPopup()); 238 EXPECT_CALL(*external_delegate_, HideAutofillPopup());
173 239
174 external_delegate_->DidEndTextFieldEditing(); 240 external_delegate_->DidEndTextFieldEditing();
175 } 241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698