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 "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::ASCIIToUTF16
;
18 using testing::Property
;
20 class MockPasswordUIView
: public PasswordUIView
{
22 explicit MockPasswordUIView(Profile
* profile
)
23 : profile_(profile
), password_manager_presenter_(this) {
24 password_manager_presenter_
.Initialize();
26 ~MockPasswordUIView() override
{}
27 Profile
* GetProfile() override
;
28 #if !defined(OS_ANDROID)
29 gfx::NativeWindow
GetNativeWindow() const override
;
31 MOCK_METHOD4(ShowPassword
, void(
32 size_t, const std::string
&, const std::string
&, const base::string16
&));
33 MOCK_METHOD2(SetPasswordList
,
34 void(const ScopedVector
<autofill::PasswordForm
>&, bool));
35 MOCK_METHOD1(SetPasswordExceptionList
,
36 void(const ScopedVector
<autofill::PasswordForm
>&));
37 PasswordManagerPresenter
* GetPasswordManagerPresenter() {
38 return &password_manager_presenter_
;
43 PasswordManagerPresenter password_manager_presenter_
;
45 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView
);
48 #if !defined(OS_ANDROID)
49 gfx::NativeWindow
MockPasswordUIView::GetNativeWindow() const { return NULL
; }
51 Profile
* MockPasswordUIView::GetProfile() { return profile_
; }
53 class PasswordManagerPresenterTest
: public testing::Test
{
55 PasswordManagerPresenterTest() {}
57 ~PasswordManagerPresenterTest() override
{}
58 void SetUp() override
{
59 PasswordStoreFactory::GetInstance()->SetTestingFactory(
60 &profile_
, MockPasswordStoreService::Build
);
61 mock_controller_
.reset(new MockPasswordUIView(&profile_
));
63 void AddPasswordEntry(const GURL
& origin
,
64 const std::string
& user_name
,
65 const std::string
& password
);
66 void AddPasswordException(const GURL
& origin
);
68 MockPasswordUIView
* GetUIController() { return mock_controller_
.get(); }
71 content::TestBrowserThreadBundle thread_bundle_
;
72 TestingProfile profile_
;
73 scoped_ptr
<MockPasswordUIView
> mock_controller_
;
75 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest
);
78 void PasswordManagerPresenterTest::AddPasswordEntry(
80 const std::string
& user_name
,
81 const std::string
& password
) {
82 autofill::PasswordForm
* form
= new autofill::PasswordForm();
83 form
->origin
= origin
;
84 form
->username_element
= base::ASCIIToUTF16("Email");
85 form
->username_value
= base::ASCIIToUTF16(user_name
);
86 form
->password_element
= base::ASCIIToUTF16("Passwd");
87 form
->password_value
= base::ASCIIToUTF16(password
);
88 mock_controller_
->GetPasswordManagerPresenter()->password_list_
92 void PasswordManagerPresenterTest::AddPasswordException(const GURL
& origin
) {
93 autofill::PasswordForm
* form
= new autofill::PasswordForm();
94 form
->origin
= origin
;
95 mock_controller_
->GetPasswordManagerPresenter()->password_exception_list_
99 void PasswordManagerPresenterTest::UpdateLists() {
100 mock_controller_
->GetPasswordManagerPresenter()->SetPasswordList();
101 mock_controller_
->GetPasswordManagerPresenter()->SetPasswordExceptionList();
106 TEST_F(PasswordManagerPresenterTest
, UIControllerIsCalled
) {
110 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u)),
112 EXPECT_CALL(*GetUIController(),
113 SetPasswordExceptionList(Property(
114 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u))));
116 GURL
pass_origin("http://abc1.com");
117 AddPasswordEntry(pass_origin
, "test@gmail.com", "test");
121 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u)),
123 EXPECT_CALL(*GetUIController(),
124 SetPasswordExceptionList(Property(
125 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(0u))));
127 GURL
except_origin("http://abc2.com");
128 AddPasswordException(except_origin
);
132 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u)),
134 EXPECT_CALL(*GetUIController(),
135 SetPasswordExceptionList(Property(
136 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u))));
138 GURL
pass_origin2("http://example.com");
139 AddPasswordEntry(pass_origin2
, "test@gmail.com", "test");
143 Property(&ScopedVector
<autofill::PasswordForm
>::size
, Eq(2u)),
145 EXPECT_CALL(*GetUIController(),
146 SetPasswordExceptionList(Property(
147 &ScopedVector
<autofill::PasswordForm
>::size
, Eq(1u))));