1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/password_manager/core/browser/test_password_store.h"
7 #include "components/autofill/core/common/password_form.h"
9 namespace password_manager {
11 TestPasswordStore::TestPasswordStore()
12 : PasswordStore(base::MessageLoopProxy::current(),
13 base::MessageLoopProxy::current()) {
16 TestPasswordStore::~TestPasswordStore() {}
18 const TestPasswordStore::PasswordMap& TestPasswordStore::stored_passwords()
20 return stored_passwords_;
23 void TestPasswordStore::Clear() {
24 stored_passwords_.clear();
27 bool TestPasswordStore::IsEmpty() const {
28 // The store is empty, if the sum of all stored passwords across all entries
29 // in |stored_passwords_| is 0.
30 size_t number_of_passwords = 0u;
31 for (PasswordMap::const_iterator it = stored_passwords_.begin();
32 !number_of_passwords && it != stored_passwords_.end();
34 number_of_passwords += it->second.size();
36 return number_of_passwords == 0u;
39 bool TestPasswordStore::FormsAreEquivalent(const autofill::PasswordForm& lhs,
40 const autofill::PasswordForm& rhs) {
41 return lhs.origin == rhs.origin &&
42 lhs.username_element == rhs.username_element &&
43 lhs.username_value == rhs.username_value &&
44 lhs.password_element == rhs.password_element &&
45 lhs.signon_realm == rhs.signon_realm;
48 void TestPasswordStore::WrapModificationTask(ModificationTask task) {
52 PasswordStoreChangeList TestPasswordStore::AddLoginImpl(
53 const autofill::PasswordForm& form) {
54 PasswordStoreChangeList changes;
55 stored_passwords_[form.signon_realm].push_back(form);
56 changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form));
60 PasswordStoreChangeList TestPasswordStore::UpdateLoginImpl(
61 const autofill::PasswordForm& form) {
62 PasswordStoreChangeList changes;
63 std::vector<autofill::PasswordForm>& forms =
64 stored_passwords_[form.signon_realm];
65 for (std::vector<autofill::PasswordForm>::iterator it = forms.begin();
66 it != forms.end(); ++it) {
67 if (FormsAreEquivalent(form, *it)) {
70 PasswordStoreChange(PasswordStoreChange::UPDATE, form));
76 PasswordStoreChangeList TestPasswordStore::RemoveLoginImpl(
77 const autofill::PasswordForm& form) {
78 PasswordStoreChangeList changes;
79 std::vector<autofill::PasswordForm>& forms =
80 stored_passwords_[form.signon_realm];
81 std::vector<autofill::PasswordForm>::iterator it = forms.begin();
82 while (it != forms.end()) {
83 if (FormsAreEquivalent(form, *it)) {
86 PasswordStoreChange(PasswordStoreChange::REMOVE, form));
94 void TestPasswordStore::GetLoginsImpl(
95 const autofill::PasswordForm& form,
96 PasswordStore::AuthorizationPromptPolicy prompt_policy,
97 const PasswordStore::ConsumerCallbackRunner& runner) {
98 std::vector<autofill::PasswordForm*> matched_forms;
99 std::vector<autofill::PasswordForm> forms =
100 stored_passwords_[form.signon_realm];
101 for (std::vector<autofill::PasswordForm>::iterator it = forms.begin();
102 it != forms.end(); ++it) {
103 matched_forms.push_back(new autofill::PasswordForm(*it));
105 runner.Run(matched_forms);
108 PasswordStoreChangeList TestPasswordStore::RemoveLoginsCreatedBetweenImpl(
109 const base::Time& begin, const base::Time& end) {
110 PasswordStoreChangeList changes;
114 bool TestPasswordStore::FillAutofillableLogins(
115 std::vector<autofill::PasswordForm*>* forms) {
119 bool TestPasswordStore::FillBlacklistLogins(
120 std::vector<autofill::PasswordForm*>* forms) {
124 } // namespace password_manager