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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
7 #include "base/bind_helpers.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/time/time.h"
15 #include "components/password_manager/core/browser/password_form_data.h"
16 #include "components/password_manager/core/browser/password_store_change.h"
17 #include "components/password_manager/core/browser/password_store_consumer.h"
18 #include "components/password_manager/core/browser/password_store_default.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using autofill::PasswordForm
;
23 using base::WaitableEvent
;
26 using testing::ElementsAreArray
;
27 using testing::Pointee
;
28 using testing::Property
;
29 using testing::WithArg
;
33 class MockPasswordStoreConsumer
: public PasswordStoreConsumer
{
35 MOCK_METHOD1(OnGetPasswordStoreResults
,
36 void(const std::vector
<PasswordForm
*>&));
39 class MockPasswordStoreObserver
: public PasswordStore::Observer
{
41 MOCK_METHOD1(OnLoginsChanged
,
42 void(const PasswordStoreChangeList
& changes
));
45 } // anonymous namespace
47 class PasswordStoreDefaultTest
: public testing::Test
{
49 virtual void SetUp() OVERRIDE
{
50 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
51 login_db_
.reset(new LoginDatabase());
52 ASSERT_TRUE(login_db_
->Init(temp_dir_
.path().Append(
53 FILE_PATH_LITERAL("login_test"))));
56 virtual void TearDown() OVERRIDE
{
57 ASSERT_TRUE(temp_dir_
.Delete());
60 base::MessageLoopForUI message_loop_
;
61 scoped_ptr
<LoginDatabase
> login_db_
;
62 base::ScopedTempDir temp_dir_
;
65 ACTION(STLDeleteElements0
) {
66 STLDeleteContainerPointers(arg0
.begin(), arg0
.end());
69 TEST_F(PasswordStoreDefaultTest
, NonASCIIData
) {
70 scoped_refptr
<PasswordStoreDefault
> store(new PasswordStoreDefault(
71 base::MessageLoopProxy::current(),
72 base::MessageLoopProxy::current(),
73 login_db_
.release()));
76 // Some non-ASCII password form data.
77 static const PasswordFormData form_data
[] = {
78 { PasswordForm::SCHEME_HTML
,
79 "http://foo.example.com",
80 "http://foo.example.com/origin",
81 "http://foo.example.com/action",
90 // Build the expected forms vector and add the forms to the store.
91 std::vector
<PasswordForm
*> expected_forms
;
92 for (unsigned int i
= 0; i
< ARRAYSIZE_UNSAFE(form_data
); ++i
) {
93 PasswordForm
* form
= CreatePasswordFormFromData(form_data
[i
]);
94 expected_forms
.push_back(form
);
95 store
->AddLogin(*form
);
98 base::MessageLoop::current()->RunUntilIdle();
100 MockPasswordStoreConsumer consumer
;
102 // We expect to get the same data back, even though it's not all ASCII.
103 EXPECT_CALL(consumer
,
104 OnGetPasswordStoreResults(ContainsAllPasswordForms(expected_forms
)))
105 .WillOnce(WithArg
<0>(STLDeleteElements0()));
106 store
->GetAutofillableLogins(&consumer
);
108 base::MessageLoop::current()->RunUntilIdle();
110 STLDeleteElements(&expected_forms
);
114 TEST_F(PasswordStoreDefaultTest
, Notifications
) {
115 scoped_refptr
<PasswordStoreDefault
> store(new PasswordStoreDefault(
116 base::MessageLoopProxy::current(),
117 base::MessageLoopProxy::current(),
118 login_db_
.release()));
121 PasswordFormData form_data
=
122 { PasswordForm::SCHEME_HTML
,
123 "http://bar.example.com",
124 "http://bar.example.com/origin",
125 "http://bar.example.com/action",
132 scoped_ptr
<PasswordForm
> form(CreatePasswordFormFromData(form_data
));
134 MockPasswordStoreObserver observer
;
135 store
->AddObserver(&observer
);
137 const PasswordStoreChange expected_add_changes
[] = {
138 PasswordStoreChange(PasswordStoreChange::ADD
, *form
),
143 OnLoginsChanged(ElementsAreArray(expected_add_changes
)));
145 // Adding a login should trigger a notification.
146 store
->AddLogin(*form
);
147 base::MessageLoop::current()->RunUntilIdle();
149 // Change the password.
150 form
->password_value
= base::ASCIIToUTF16("a different password");
152 const PasswordStoreChange expected_update_changes
[] = {
153 PasswordStoreChange(PasswordStoreChange::UPDATE
, *form
),
158 OnLoginsChanged(ElementsAreArray(expected_update_changes
)));
160 // Updating the login with the new password should trigger a notification.
161 store
->UpdateLogin(*form
);
162 base::MessageLoop::current()->RunUntilIdle();
164 const PasswordStoreChange expected_delete_changes
[] = {
165 PasswordStoreChange(PasswordStoreChange::REMOVE
, *form
),
170 OnLoginsChanged(ElementsAreArray(expected_delete_changes
)));
172 // Deleting the login should trigger a notification.
173 store
->RemoveLogin(*form
);
174 base::MessageLoop::current()->RunUntilIdle();
176 store
->RemoveObserver(&observer
);