Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / passwords / password_manager_presenter_unittest.cc
blob338688193716dc7233e1751351e1d8988f96c05f
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;
16 using testing::Eq;
17 using testing::Property;
19 class MockPasswordUIView : public PasswordUIView {
20 public:
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() const override;
29 #endif
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_;
39 private:
40 Profile* profile_;
41 PasswordManagerPresenter password_manager_presenter_;
43 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView);
46 #if !defined(OS_ANDROID)
47 gfx::NativeWindow MockPasswordUIView::GetNativeWindow() const { return NULL; }
48 #endif
49 Profile* MockPasswordUIView::GetProfile() { return profile_; }
51 class PasswordManagerPresenterTest : public testing::Test {
52 protected:
53 PasswordManagerPresenterTest() {}
55 ~PasswordManagerPresenterTest() override {}
56 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);
65 void UpdateLists();
66 MockPasswordUIView* GetUIController() { return mock_controller_.get(); }
68 private:
69 TestingProfile profile_;
70 scoped_ptr<MockPasswordUIView> mock_controller_;
72 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest);
75 void PasswordManagerPresenterTest::AddPasswordEntry(
76 const GURL& origin,
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_
86 .push_back(form);
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_
93 .push_back(form);
96 void PasswordManagerPresenterTest::UpdateLists() {
97 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList();
98 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList();
101 namespace {
103 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) {
104 EXPECT_CALL(
105 *GetUIController(),
106 SetPasswordList(
107 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)),
108 testing::_));
109 EXPECT_CALL(*GetUIController(),
110 SetPasswordExceptionList(Property(
111 &ScopedVector<autofill::PasswordForm>::size, Eq(0u))));
112 UpdateLists();
113 GURL pass_origin("http://abc1.com");
114 AddPasswordEntry(pass_origin, "test@gmail.com", "test");
115 EXPECT_CALL(
116 *GetUIController(),
117 SetPasswordList(
118 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)),
119 testing::_));
120 EXPECT_CALL(*GetUIController(),
121 SetPasswordExceptionList(Property(
122 &ScopedVector<autofill::PasswordForm>::size, Eq(0u))));
123 UpdateLists();
124 GURL except_origin("http://abc2.com");
125 AddPasswordException(except_origin);
126 EXPECT_CALL(
127 *GetUIController(),
128 SetPasswordList(
129 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)),
130 testing::_));
131 EXPECT_CALL(*GetUIController(),
132 SetPasswordExceptionList(Property(
133 &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
134 UpdateLists();
135 GURL pass_origin2("http://example.com");
136 AddPasswordEntry(pass_origin2, "test@gmail.com", "test");
137 EXPECT_CALL(
138 *GetUIController(),
139 SetPasswordList(
140 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)),
141 testing::_));
142 EXPECT_CALL(*GetUIController(),
143 SetPasswordExceptionList(Property(
144 &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
145 UpdateLists();
148 } // namespace