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.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 "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using base::ASCIIToUTF16
;
16 using testing::Property
;
18 class MockPasswordUIView
: public PasswordUIView
{
20 explicit MockPasswordUIView(Profile
* profile
)
21 : profile_(profile
), password_manager_presenter_(this) {
22 password_manager_presenter_
.Initialize();
24 virtual ~MockPasswordUIView() {}
25 virtual Profile
* GetProfile() OVERRIDE
;
26 #if !defined(OS_ANDROID)
27 virtual gfx::NativeWindow
GetNativeWindow() OVERRIDE
;
29 MOCK_METHOD2(ShowPassword
, void(size_t, const base::string16
&));
30 MOCK_METHOD2(SetPasswordList
,
31 void(const ScopedVector
<autofill::PasswordForm
>&, bool));
32 MOCK_METHOD1(SetPasswordExceptionList
,
33 void(const ScopedVector
<autofill::PasswordForm
>&));
34 PasswordManagerPresenter
* GetPasswordManagerPresenter() {
35 return &password_manager_presenter_
;
40 PasswordManagerPresenter password_manager_presenter_
;
42 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView
);
45 #if !defined(OS_ANDROID)
46 gfx::NativeWindow
MockPasswordUIView::GetNativeWindow() { return NULL
; }
48 Profile
* MockPasswordUIView::GetProfile() { return profile_
; }
50 class PasswordManagerPresenterTest
: public testing::Test
{
52 PasswordManagerPresenterTest() {}
54 virtual ~PasswordManagerPresenterTest() {}
55 virtual void SetUp() OVERRIDE
{
56 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse(
57 &profile_
, MockPasswordStore::Build
);
58 mock_controller_
.reset(new MockPasswordUIView(&profile_
));
60 void AddPasswordEntry(const GURL
& origin
,
61 const std::string
& user_name
,
62 const std::string
& password
);
63 void AddPasswordException(const GURL
& origin
);
65 MockPasswordUIView
* GetUIController() { return mock_controller_
.get(); }
68 TestingProfile profile_
;
69 scoped_ptr
<MockPasswordUIView
> mock_controller_
;
71 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest
);
74 void PasswordManagerPresenterTest::AddPasswordEntry(
76 const std::string
& user_name
,
77 const std::string
& password
) {
78 autofill::PasswordForm
* form
= new autofill::PasswordForm();
79 form
->origin
= origin
;
80 form
->username_element
= base::ASCIIToUTF16("Email");
81 form
->username_value
= base::ASCIIToUTF16(user_name
);
82 form
->password_element
= base::ASCIIToUTF16("Passwd");
83 form
->password_value
= base::ASCIIToUTF16(password
);
84 mock_controller_
->GetPasswordManagerPresenter()->password_list_
88 void PasswordManagerPresenterTest::AddPasswordException(const GURL
& origin
) {
89 autofill::PasswordForm
* form
= new autofill::PasswordForm();
90 form
->origin
= origin
;
91 mock_controller_
->GetPasswordManagerPresenter()->password_exception_list_
95 void PasswordManagerPresenterTest::UpdateLists() {
96 mock_controller_
->GetPasswordManagerPresenter()->SetPasswordList();
97 mock_controller_
->GetPasswordManagerPresenter()->SetPasswordExceptionList();
102 TEST_F(PasswordManagerPresenterTest
, UIControllerIsCalled
) {
106 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u)),
108 EXPECT_CALL(*GetUIController(),
109 SetPasswordExceptionList(Property(
110 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u))));
112 GURL
pass_origin("http://abc1.com");
113 AddPasswordEntry(pass_origin
, "test@gmail.com", "test");
117 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u)),
119 EXPECT_CALL(*GetUIController(),
120 SetPasswordExceptionList(Property(
121 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u))));
123 GURL
except_origin("http://abc2.com");
124 AddPasswordException(except_origin
);
128 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u)),
130 EXPECT_CALL(*GetUIController(),
131 SetPasswordExceptionList(Property(
132 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u))));
134 GURL
pass_origin2("http://example.com");
135 AddPasswordEntry(pass_origin2
, "test@gmail.com", "test");
139 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(2u)),
141 EXPECT_CALL(*GetUIController(),
142 SetPasswordExceptionList(Property(
143 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u))));