| OLD | NEW |
| (Empty) |
| 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.h" | |
| 7 #include "chrome/browser/password_manager/password_store_factory.h" | |
| 8 #include "chrome/browser/ui/password/password_ui_view.h" | |
| 9 #include "chrome/browser/ui/webui/options/password_manager_presenter.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" | |
| 13 | |
| 14 using base::ASCIIToUTF16; | |
| 15 using testing::Eq; | |
| 16 using testing::Property; | |
| 17 | |
| 18 class MockPasswordUIView : public passwords_ui::PasswordUIView { | |
| 19 public: | |
| 20 explicit MockPasswordUIView(Profile* profile) | |
| 21 : profile_(profile), password_manager_presenter_(this) { | |
| 22 password_manager_presenter_.Initialize(); | |
| 23 } | |
| 24 virtual ~MockPasswordUIView() {} | |
| 25 virtual Profile* GetProfile() OVERRIDE; | |
| 26 MOCK_METHOD2(ShowPassword, void(size_t, const string16&)); | |
| 27 MOCK_METHOD2(SetPasswordList, | |
| 28 void(const ScopedVector<autofill::PasswordForm>&, bool)); | |
| 29 MOCK_METHOD1(SetPasswordExceptionList, | |
| 30 void(const ScopedVector<autofill::PasswordForm>&)); | |
| 31 options::PasswordManagerPresenter* GetPasswordManagerPresenter() { | |
| 32 return &password_manager_presenter_; | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 Profile* profile_; | |
| 37 options::PasswordManagerPresenter password_manager_presenter_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView); | |
| 40 }; | |
| 41 | |
| 42 Profile* MockPasswordUIView::GetProfile() { return profile_; } | |
| 43 | |
| 44 namespace options { | |
| 45 | |
| 46 class PasswordManagerPresenterTest : public testing::Test { | |
| 47 protected: | |
| 48 PasswordManagerPresenterTest() {} | |
| 49 | |
| 50 virtual ~PasswordManagerPresenterTest() {} | |
| 51 virtual void SetUp() OVERRIDE { | |
| 52 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( | |
| 53 &profile_, MockPasswordStore::Build); | |
| 54 mock_controller_.reset(new MockPasswordUIView(&profile_)); | |
| 55 } | |
| 56 void AddPasswordEntry(const GURL& origin, | |
| 57 const std::string& user_name, | |
| 58 const std::string& password); | |
| 59 void AddPasswordException(const GURL& origin); | |
| 60 void UpdateLists(); | |
| 61 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } | |
| 62 | |
| 63 private: | |
| 64 TestingProfile profile_; | |
| 65 scoped_ptr<MockPasswordUIView> mock_controller_; | |
| 66 | |
| 67 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); | |
| 68 }; | |
| 69 | |
| 70 void PasswordManagerPresenterTest::AddPasswordEntry( | |
| 71 const GURL& origin, | |
| 72 const std::string& user_name, | |
| 73 const std::string& password) { | |
| 74 autofill::PasswordForm* form = new autofill::PasswordForm(); | |
| 75 form->origin = origin; | |
| 76 form->username_element = ASCIIToUTF16("Email"); | |
| 77 form->username_value = ASCIIToUTF16(user_name); | |
| 78 form->password_element = ASCIIToUTF16("Passwd"); | |
| 79 form->password_value = ASCIIToUTF16(password); | |
| 80 mock_controller_->GetPasswordManagerPresenter()->password_list_ | |
| 81 .push_back(form); | |
| 82 } | |
| 83 | |
| 84 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) { | |
| 85 autofill::PasswordForm* form = new autofill::PasswordForm(); | |
| 86 form->origin = origin; | |
| 87 mock_controller_->GetPasswordManagerPresenter()->password_exception_list_ | |
| 88 .push_back(form); | |
| 89 } | |
| 90 | |
| 91 void PasswordManagerPresenterTest::UpdateLists() { | |
| 92 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); | |
| 93 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); | |
| 94 } | |
| 95 | |
| 96 namespace { | |
| 97 | |
| 98 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { | |
| 99 EXPECT_CALL( | |
| 100 *GetUIController(), | |
| 101 SetPasswordList( | |
| 102 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)), true)); | |
| 103 EXPECT_CALL(*GetUIController(), | |
| 104 SetPasswordExceptionList(Property( | |
| 105 &ScopedVector<autofill::PasswordForm>::size, Eq(0u)))); | |
| 106 UpdateLists(); | |
| 107 GURL pass_origin("http://abc1.com"); | |
| 108 AddPasswordEntry(pass_origin, "test@gmail.com", "test"); | |
| 109 EXPECT_CALL( | |
| 110 *GetUIController(), | |
| 111 SetPasswordList( | |
| 112 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)), true)); | |
| 113 EXPECT_CALL(*GetUIController(), | |
| 114 SetPasswordExceptionList(Property( | |
| 115 &ScopedVector<autofill::PasswordForm>::size, Eq(0u)))); | |
| 116 UpdateLists(); | |
| 117 GURL except_origin("http://abc2.com"); | |
| 118 AddPasswordException(except_origin); | |
| 119 EXPECT_CALL( | |
| 120 *GetUIController(), | |
| 121 SetPasswordList( | |
| 122 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)), true)); | |
| 123 EXPECT_CALL(*GetUIController(), | |
| 124 SetPasswordExceptionList(Property( | |
| 125 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); | |
| 126 UpdateLists(); | |
| 127 GURL pass_origin2("http://example.com"); | |
| 128 AddPasswordEntry(pass_origin2, "test@gmail.com", "test"); | |
| 129 EXPECT_CALL( | |
| 130 *GetUIController(), | |
| 131 SetPasswordList( | |
| 132 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)), true)); | |
| 133 EXPECT_CALL(*GetUIController(), | |
| 134 SetPasswordExceptionList(Property( | |
| 135 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); | |
| 136 UpdateLists(); | |
| 137 } | |
| 138 | |
| 139 } // namespace | |
| 140 } // namespace options | |
| OLD | NEW |