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
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/password_manager/mock_password_store_service.h"
7 #include "chrome/browser/password_manager/password_store_factory.h"
8 #include "chrome/browser/ui/passwords/password_manager_presenter.h"
9 #include "chrome/browser/ui/passwords/password_ui_view.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/password_manager/core/browser/mock_password_store.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16
;
17 using testing::Property
;
19 class MockPasswordUIView
: public PasswordUIView
{
21 explicit MockPasswordUIView(Profile
* profile
)
22 : profile_(profile
), password_manager_presenter_(this) {
23 password_manager_presenter_
.Initialize();
25 virtual ~MockPasswordUIView() {}
26 virtual Profile
* GetProfile() OVERRIDE
;
27 #if !defined(OS_ANDROID)
28 virtual gfx::NativeWindow
GetNativeWindow() OVERRIDE
;
30 MOCK_METHOD2(ShowPassword
, void(size_t, const base::string16
&));
31 MOCK_METHOD2(SetPasswordList
,
32 void(const ScopedVector
<autofill::PasswordForm
>&, bool));
33 MOCK_METHOD1(SetPasswordExceptionList
,
34 void(const ScopedVector
<autofill::PasswordForm
>&));
35 PasswordManagerPresenter
* GetPasswordManagerPresenter() {
36 return &password_manager_presenter_
;
41 PasswordManagerPresenter password_manager_presenter_
;
43 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView
);
46 #if !defined(OS_ANDROID)
47 gfx::NativeWindow
MockPasswordUIView::GetNativeWindow() { return NULL
; }
49 Profile
* MockPasswordUIView::GetProfile() { return profile_
; }
51 class PasswordManagerPresenterTest
: public testing::Test
{
53 PasswordManagerPresenterTest() {}
55 virtual ~PasswordManagerPresenterTest() {}
56 virtual void SetUp() OVERRIDE
{
57 PasswordStoreFactory::GetInstance()->SetTestingFactory(
58 &profile_
, MockPasswordStoreService::Build
);
59 mock_controller_
.reset(new MockPasswordUIView(&profile_
));
61 void AddPasswordEntry(const GURL
& origin
,
62 const std::string
& user_name
,
63 const std::string
& password
);
64 void AddPasswordException(const GURL
& origin
);
66 MockPasswordUIView
* GetUIController() { return mock_controller_
.get(); }
69 TestingProfile profile_
;
70 scoped_ptr
<MockPasswordUIView
> mock_controller_
;
72 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest
);
75 void PasswordManagerPresenterTest::AddPasswordEntry(
77 const std::string
& user_name
,
78 const std::string
& password
) {
79 autofill::PasswordForm
* form
= new autofill::PasswordForm();
80 form
->origin
= origin
;
81 form
->username_element
= base::ASCIIToUTF16("Email");
82 form
->username_value
= base::ASCIIToUTF16(user_name
);
83 form
->password_element
= base::ASCIIToUTF16("Passwd");
84 form
->password_value
= base::ASCIIToUTF16(password
);
85 mock_controller_
->GetPasswordManagerPresenter()->password_list_
89 void PasswordManagerPresenterTest::AddPasswordException(const GURL
& origin
) {
90 autofill::PasswordForm
* form
= new autofill::PasswordForm();
91 form
->origin
= origin
;
92 mock_controller_
->GetPasswordManagerPresenter()->password_exception_list_
96 void PasswordManagerPresenterTest::UpdateLists() {
97 mock_controller_
->GetPasswordManagerPresenter()->SetPasswordList();
98 mock_controller_
->GetPasswordManagerPresenter()->SetPasswordExceptionList();
103 TEST_F(PasswordManagerPresenterTest
, UIControllerIsCalled
) {
107 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u)),
109 EXPECT_CALL(*GetUIController(),
110 SetPasswordExceptionList(Property(
111 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u))));
113 GURL
pass_origin("http://abc1.com");
114 AddPasswordEntry(pass_origin
, "test@gmail.com", "test");
118 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u)),
120 EXPECT_CALL(*GetUIController(),
121 SetPasswordExceptionList(Property(
122 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u))));
124 GURL
except_origin("http://abc2.com");
125 AddPasswordException(except_origin
);
129 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u)),
131 EXPECT_CALL(*GetUIController(),
132 SetPasswordExceptionList(Property(
133 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u))));
135 GURL
pass_origin2("http://example.com");
136 AddPasswordEntry(pass_origin2
, "test@gmail.com", "test");
140 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(2u)),
142 EXPECT_CALL(*GetUIController(),
143 SetPasswordExceptionList(Property(
144 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u))));