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. 4 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" 14 15using base::ASCIIToUTF16; 16using testing::Eq; 17using testing::Property; 18 19class MockPasswordUIView : public PasswordUIView { 20 public: 21 explicit MockPasswordUIView(Profile* profile) 22 : profile_(profile), password_manager_presenter_(this) { 23 password_manager_presenter_.Initialize(); 24 } 25 virtual ~MockPasswordUIView() {} 26 virtual Profile* GetProfile() OVERRIDE; 27#if !defined(OS_ANDROID) 28 virtual gfx::NativeWindow GetNativeWindow() 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_; 37 } 38 39 private: 40 Profile* profile_; 41 PasswordManagerPresenter password_manager_presenter_; 42 43 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView); 44}; 45 46#if !defined(OS_ANDROID) 47gfx::NativeWindow MockPasswordUIView::GetNativeWindow() { return NULL; } 48#endif 49Profile* MockPasswordUIView::GetProfile() { return profile_; } 50 51class PasswordManagerPresenterTest : public testing::Test { 52 protected: 53 PasswordManagerPresenterTest() {} 54 55 virtual ~PasswordManagerPresenterTest() {} 56 virtual void SetUp() OVERRIDE { 57 PasswordStoreFactory::GetInstance()->SetTestingFactory( 58 &profile_, MockPasswordStoreService::Build); 59 mock_controller_.reset(new MockPasswordUIView(&profile_)); 60 } 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(); } 67 68 private: 69 TestingProfile profile_; 70 scoped_ptr<MockPasswordUIView> mock_controller_; 71 72 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); 73}; 74 75void 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); 87} 88 89void 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); 94} 95 96void PasswordManagerPresenterTest::UpdateLists() { 97 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); 98 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); 99} 100 101namespace { 102 103TEST_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(); 146} 147 148} // namespace 149