Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / passwords / password_manager_presenter_unittest.cc
blobaf7a87da8f72a5f3c60a4b91fee63e05d003bd26
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;
17 using testing::Eq;
18 using testing::Property;
20 class MockPasswordUIView : public PasswordUIView {
21 public:
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;
30 #endif
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_;
41 private:
42 Profile* profile_;
43 PasswordManagerPresenter password_manager_presenter_;
45 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView);
48 #if !defined(OS_ANDROID)
49 gfx::NativeWindow MockPasswordUIView::GetNativeWindow() const { return NULL; }
50 #endif
51 Profile* MockPasswordUIView::GetProfile() { return profile_; }
53 class PasswordManagerPresenterTest : public testing::Test {
54 protected:
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);
67 void UpdateLists();
68 MockPasswordUIView* GetUIController() { return mock_controller_.get(); }
70 private:
71 content::TestBrowserThreadBundle thread_bundle_;
72 TestingProfile profile_;
73 scoped_ptr<MockPasswordUIView> mock_controller_;
75 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest);
78 void PasswordManagerPresenterTest::AddPasswordEntry(
79 const GURL& origin,
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_
89 .push_back(form);
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_
96 .push_back(form);
99 void PasswordManagerPresenterTest::UpdateLists() {
100 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList();
101 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList();
104 namespace {
106 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) {
107 EXPECT_CALL(
108 *GetUIController(),
109 SetPasswordList(
110 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)),
111 testing::_));
112 EXPECT_CALL(*GetUIController(),
113 SetPasswordExceptionList(Property(
114 &ScopedVector<autofill::PasswordForm>::size, Eq(0u))));
115 UpdateLists();
116 GURL pass_origin("http://abc1.com");
117 AddPasswordEntry(pass_origin, "test@gmail.com", "test");
118 EXPECT_CALL(
119 *GetUIController(),
120 SetPasswordList(
121 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)),
122 testing::_));
123 EXPECT_CALL(*GetUIController(),
124 SetPasswordExceptionList(Property(
125 &ScopedVector<autofill::PasswordForm>::size, Eq(0u))));
126 UpdateLists();
127 GURL except_origin("http://abc2.com");
128 AddPasswordException(except_origin);
129 EXPECT_CALL(
130 *GetUIController(),
131 SetPasswordList(
132 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)),
133 testing::_));
134 EXPECT_CALL(*GetUIController(),
135 SetPasswordExceptionList(Property(
136 &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
137 UpdateLists();
138 GURL pass_origin2("http://example.com");
139 AddPasswordEntry(pass_origin2, "test@gmail.com", "test");
140 EXPECT_CALL(
141 *GetUIController(),
142 SetPasswordList(
143 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)),
144 testing::_));
145 EXPECT_CALL(*GetUIController(),
146 SetPasswordExceptionList(Property(
147 &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
148 UpdateLists();
151 } // namespace