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

Side by Side Diff: components/password_manager/core/browser/password_autofill_manager_unittest.cc

Issue 772253003: Create an autofill Suggestion class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 6 years 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
« no previous file with comments | « components/password_manager/core/browser/password_autofill_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/password_manager/core/browser/password_autofill_manager.h" 5 #include "components/password_manager/core/browser/password_autofill_manager.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "components/autofill/core/browser/popup_item_ids.h" 10 #include "components/autofill/core/browser/popup_item_ids.h"
11 #include "components/autofill/core/browser/suggestion_test_helpers.h"
11 #include "components/autofill/core/browser/test_autofill_client.h" 12 #include "components/autofill/core/browser/test_autofill_client.h"
12 #include "components/autofill/core/browser/test_autofill_driver.h" 13 #include "components/autofill/core/browser/test_autofill_driver.h"
13 #include "components/autofill/core/common/autofill_constants.h" 14 #include "components/autofill/core/common/autofill_constants.h"
14 #include "components/autofill/core/common/form_field_data.h" 15 #include "components/autofill/core/common/form_field_data.h"
15 #include "components/autofill/core/common/password_form_fill_data.h" 16 #include "components/autofill/core/common/password_form_fill_data.h"
16 #include "components/password_manager/core/browser/stub_password_manager_client. h" 17 #include "components/password_manager/core/browser/stub_password_manager_client. h"
17 #include "components/password_manager/core/browser/stub_password_manager_driver. h" 18 #include "components/password_manager/core/browser/stub_password_manager_driver. h"
18 #include "components/strings/grit/components_strings.h" 19 #include "components/strings/grit/components_strings.h"
19 #include "testing/gmock/include/gmock/gmock.h" 20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/geometry/rect_f.h" 23 #include "ui/gfx/geometry/rect_f.h"
23 24
24 // The name of the username/password element in the form. 25 // The name of the username/password element in the form.
25 const char kUsernameName[] = "username"; 26 const char kUsernameName[] = "username";
26 const char kInvalidUsername[] = "no-username"; 27 const char kInvalidUsername[] = "no-username";
27 const char kPasswordName[] = "password"; 28 const char kPasswordName[] = "password";
28 29
29 const char kAliceUsername[] = "alice"; 30 const char kAliceUsername[] = "alice";
30 const char kAlicePassword[] = "password"; 31 const char kAlicePassword[] = "password";
31 32
33 using autofill::Suggestion;
34 using autofill::SuggestionVectorIdsAre;
35 using autofill::SuggestionVectorValuesAre;
32 using testing::_; 36 using testing::_;
33 37
34 namespace autofill { 38 namespace autofill {
35 class AutofillPopupDelegate; 39 class AutofillPopupDelegate;
36 } 40 }
37 41
38 namespace password_manager { 42 namespace password_manager {
39 43
40 namespace { 44 namespace {
41 45
42 class MockPasswordManagerDriver : public StubPasswordManagerDriver { 46 class MockPasswordManagerDriver : public StubPasswordManagerDriver {
43 public: 47 public:
44 MOCK_METHOD2(FillSuggestion, 48 MOCK_METHOD2(FillSuggestion,
45 void(const base::string16&, const base::string16&)); 49 void(const base::string16&, const base::string16&));
46 MOCK_METHOD2(PreviewSuggestion, 50 MOCK_METHOD2(PreviewSuggestion,
47 void(const base::string16&, const base::string16&)); 51 void(const base::string16&, const base::string16&));
48 }; 52 };
49 53
50 class TestPasswordManagerClient : public StubPasswordManagerClient { 54 class TestPasswordManagerClient : public StubPasswordManagerClient {
51 public: 55 public:
52 MockPasswordManagerDriver* mock_driver() { return &driver_; } 56 MockPasswordManagerDriver* mock_driver() { return &driver_; }
53 57
54 private: 58 private:
55 MockPasswordManagerDriver driver_; 59 MockPasswordManagerDriver driver_;
56 }; 60 };
57 61
58 class MockAutofillClient : public autofill::TestAutofillClient { 62 class MockAutofillClient : public autofill::TestAutofillClient {
59 public: 63 public:
60 MOCK_METHOD7(ShowAutofillPopup, 64 MOCK_METHOD4(ShowAutofillPopup,
61 void(const gfx::RectF& element_bounds, 65 void(const gfx::RectF& element_bounds,
62 base::i18n::TextDirection text_direction, 66 base::i18n::TextDirection text_direction,
63 const std::vector<base::string16>& values, 67 const std::vector<Suggestion>& suggestions,
64 const std::vector<base::string16>& labels,
65 const std::vector<base::string16>& icons,
66 const std::vector<int>& identifiers,
67 base::WeakPtr<autofill::AutofillPopupDelegate> delegate)); 68 base::WeakPtr<autofill::AutofillPopupDelegate> delegate));
68 MOCK_METHOD0(HideAutofillPopup, void()); 69 MOCK_METHOD0(HideAutofillPopup, void());
69 }; 70 };
70 71
71 } // namespace 72 } // namespace
72 73
73 class PasswordAutofillManagerTest : public testing::Test { 74 class PasswordAutofillManagerTest : public testing::Test {
74 protected: 75 protected:
75 PasswordAutofillManagerTest() 76 PasswordAutofillManagerTest()
76 : test_username_(base::ASCIIToUTF16(kAliceUsername)), 77 : test_username_(base::ASCIIToUTF16(kAliceUsername)),
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data); 182 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data);
182 183
183 EXPECT_CALL(*client->mock_driver(), 184 EXPECT_CALL(*client->mock_driver(),
184 FillSuggestion(test_username_, test_password_)); 185 FillSuggestion(test_username_, test_password_));
185 186
186 // The enums must be cast to ints to prevent compile errors on linux_rel. 187 // The enums must be cast to ints to prevent compile errors on linux_rel.
187 EXPECT_CALL(*autofill_client, 188 EXPECT_CALL(*autofill_client,
188 ShowAutofillPopup( 189 ShowAutofillPopup(
189 _, 190 _,
190 _, 191 _,
191 _, 192 SuggestionVectorIdsAre(testing::ElementsAre(
192 _, 193 autofill::POPUP_ITEM_ID_PASSWORD_ENTRY)),
193 _,
194 testing::ElementsAre(autofill::POPUP_ITEM_ID_PASSWORD_ENTRY),
195 _)); 194 _));
196 password_autofill_manager_->OnShowPasswordSuggestions( 195 password_autofill_manager_->OnShowPasswordSuggestions(
197 dummy_key, base::i18n::RIGHT_TO_LEFT, base::string16(), false, 196 dummy_key, base::i18n::RIGHT_TO_LEFT, base::string16(), false,
198 element_bounds); 197 element_bounds);
199 198
200 // Accepting a suggestion should trigger a call to hide the popup. 199 // Accepting a suggestion should trigger a call to hide the popup.
201 EXPECT_CALL(*autofill_client, HideAutofillPopup()); 200 EXPECT_CALL(*autofill_client, HideAutofillPopup());
202 password_autofill_manager_->DidAcceptSuggestion( 201 password_autofill_manager_->DidAcceptSuggestion(
203 test_username_, autofill::POPUP_ITEM_ID_PASSWORD_ENTRY); 202 test_username_, autofill::POPUP_ITEM_ID_PASSWORD_ENTRY);
204 } 203 }
(...skipping 23 matching lines...) Expand all
228 other_names.push_back(other_username); 227 other_names.push_back(other_username);
229 data.other_possible_usernames[usernames_key] = other_names; 228 data.other_possible_usernames[usernames_key] = other_names;
230 229
231 int dummy_key = 0; 230 int dummy_key = 0;
232 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data); 231 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data);
233 232
234 // First, simulate displaying suggestions matching an empty prefix. 233 // First, simulate displaying suggestions matching an empty prefix.
235 EXPECT_CALL(*autofill_client, 234 EXPECT_CALL(*autofill_client,
236 ShowAutofillPopup( 235 ShowAutofillPopup(
237 element_bounds, _, 236 element_bounds, _,
238 testing::UnorderedElementsAre( 237 SuggestionVectorValuesAre(testing::UnorderedElementsAre(
239 test_username_, additional_username, other_username), 238 test_username_, additional_username, other_username)),
240 _, _, _, _)); 239 _));
241 password_autofill_manager_->OnShowPasswordSuggestions( 240 password_autofill_manager_->OnShowPasswordSuggestions(
242 dummy_key, base::i18n::RIGHT_TO_LEFT, base::string16(), false, 241 dummy_key, base::i18n::RIGHT_TO_LEFT, base::string16(), false,
243 element_bounds); 242 element_bounds);
244 243
245 // Now simulate displaying suggestions matching "John". 244 // Now simulate displaying suggestions matching "John".
246 EXPECT_CALL(*autofill_client, 245 EXPECT_CALL(*autofill_client,
247 ShowAutofillPopup(element_bounds, _, 246 ShowAutofillPopup(
248 testing::UnorderedElementsAre( 247 element_bounds, _,
249 additional_username, other_username), 248 SuggestionVectorValuesAre(testing::UnorderedElementsAre(
250 _, _, _, _)); 249 additional_username,
250 other_username)),
251 _));
251 password_autofill_manager_->OnShowPasswordSuggestions( 252 password_autofill_manager_->OnShowPasswordSuggestions(
252 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("John"), false, 253 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("John"), false,
253 element_bounds); 254 element_bounds);
254 255
255 // Finally, simulate displaying all suggestions, without any prefix matching. 256 // Finally, simulate displaying all suggestions, without any prefix matching.
256 EXPECT_CALL(*autofill_client, 257 EXPECT_CALL(*autofill_client,
257 ShowAutofillPopup( 258 ShowAutofillPopup(
258 element_bounds, _, 259 element_bounds, _,
259 testing::UnorderedElementsAre( 260 SuggestionVectorValuesAre(testing::UnorderedElementsAre(
260 test_username_, additional_username, other_username), 261 test_username_, additional_username, other_username)),
261 _, _, _, _)); 262 _));
262 password_autofill_manager_->OnShowPasswordSuggestions( 263 password_autofill_manager_->OnShowPasswordSuggestions(
263 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("xyz"), true, 264 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("xyz"), true,
264 element_bounds); 265 element_bounds);
265 } 266 }
266 267
267 TEST_F(PasswordAutofillManagerTest, FillSuggestionPasswordField) { 268 TEST_F(PasswordAutofillManagerTest, FillSuggestionPasswordField) {
268 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); 269 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient);
269 scoped_ptr<MockAutofillClient> autofill_client(new MockAutofillClient); 270 scoped_ptr<MockAutofillClient> autofill_client(new MockAutofillClient);
270 InitializePasswordAutofillManager(client.get(), autofill_client.get()); 271 InitializePasswordAutofillManager(client.get(), autofill_client.get());
271 272
(...skipping 18 matching lines...) Expand all
290 int dummy_key = 0; 291 int dummy_key = 0;
291 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data); 292 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data);
292 293
293 // Simulate displaying suggestions matching a username and specifying that the 294 // Simulate displaying suggestions matching a username and specifying that the
294 // field is a password field. 295 // field is a password field.
295 base::string16 title = l10n_util::GetStringUTF16( 296 base::string16 title = l10n_util::GetStringUTF16(
296 IDS_AUTOFILL_PASSWORD_FIELD_SUGGESTIONS_TITLE); 297 IDS_AUTOFILL_PASSWORD_FIELD_SUGGESTIONS_TITLE);
297 EXPECT_CALL(*autofill_client, 298 EXPECT_CALL(*autofill_client,
298 ShowAutofillPopup( 299 ShowAutofillPopup(
299 element_bounds, _, 300 element_bounds, _,
300 testing::UnorderedElementsAre(title, test_username_), 301 SuggestionVectorValuesAre(testing::UnorderedElementsAre(
301 _, _, _, _)); 302 title,
303 test_username_)),
304 _));
302 password_autofill_manager_->OnShowPasswordSuggestions( 305 password_autofill_manager_->OnShowPasswordSuggestions(
303 dummy_key, base::i18n::RIGHT_TO_LEFT, test_username_, 306 dummy_key, base::i18n::RIGHT_TO_LEFT, test_username_,
304 autofill::IS_PASSWORD_FIELD, element_bounds); 307 autofill::IS_PASSWORD_FIELD, element_bounds);
305 } 308 }
306 309
307 } // namespace password_manager 310 } // namespace password_manager
OLDNEW
« no previous file with comments | « components/password_manager/core/browser/password_autofill_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698