016faf53bfa6a9f481a034c333a8ae3bbfc7b34d
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / passwords / password_manager_presenter_unittest.cc
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/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 "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 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 #if !defined(OS_ANDROID)
27   virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
28 #endif
29   MOCK_METHOD2(ShowPassword, void(size_t, const base::string16&));
30   MOCK_METHOD2(SetPasswordList,
31                void(const ScopedVector<autofill::PasswordForm>&, bool));
32   MOCK_METHOD1(SetPasswordExceptionList,
33                void(const ScopedVector<autofill::PasswordForm>&));
34   PasswordManagerPresenter* GetPasswordManagerPresenter() {
35     return &password_manager_presenter_;
36   }
37
38  private:
39   Profile* profile_;
40   PasswordManagerPresenter password_manager_presenter_;
41
42   DISALLOW_COPY_AND_ASSIGN(MockPasswordUIView);
43 };
44
45 #if !defined(OS_ANDROID)
46 gfx::NativeWindow MockPasswordUIView::GetNativeWindow() { return NULL; }
47 #endif
48 Profile* MockPasswordUIView::GetProfile() { return profile_; }
49
50 class PasswordManagerPresenterTest : public testing::Test {
51  protected:
52   PasswordManagerPresenterTest() {}
53
54   virtual ~PasswordManagerPresenterTest() {}
55   virtual void SetUp() OVERRIDE {
56     PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse(
57         &profile_, MockPasswordStore::Build);
58     mock_controller_.reset(new MockPasswordUIView(&profile_));
59   }
60   void AddPasswordEntry(const GURL& origin,
61                         const std::string& user_name,
62                         const std::string& password);
63   void AddPasswordException(const GURL& origin);
64   void UpdateLists();
65   MockPasswordUIView* GetUIController() { return mock_controller_.get(); }
66
67  private:
68   TestingProfile profile_;
69   scoped_ptr<MockPasswordUIView> mock_controller_;
70
71   DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest);
72 };
73
74 void PasswordManagerPresenterTest::AddPasswordEntry(
75     const GURL& origin,
76     const std::string& user_name,
77     const std::string& password) {
78   autofill::PasswordForm* form = new autofill::PasswordForm();
79   form->origin = origin;
80   form->username_element = base::ASCIIToUTF16("Email");
81   form->username_value = base::ASCIIToUTF16(user_name);
82   form->password_element = base::ASCIIToUTF16("Passwd");
83   form->password_value = base::ASCIIToUTF16(password);
84   mock_controller_->GetPasswordManagerPresenter()->password_list_
85       .push_back(form);
86 }
87
88 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) {
89   autofill::PasswordForm* form = new autofill::PasswordForm();
90   form->origin = origin;
91   mock_controller_->GetPasswordManagerPresenter()->password_exception_list_
92       .push_back(form);
93 }
94
95 void PasswordManagerPresenterTest::UpdateLists() {
96   mock_controller_->GetPasswordManagerPresenter()->SetPasswordList();
97   mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList();
98 }
99
100 namespace {
101
102 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) {
103   EXPECT_CALL(
104       *GetUIController(),
105       SetPasswordList(
106           Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)),
107           testing::_));
108   EXPECT_CALL(*GetUIController(),
109               SetPasswordExceptionList(Property(
110                   &ScopedVector<autofill::PasswordForm>::size, Eq(0u))));
111   UpdateLists();
112   GURL pass_origin("http://abc1.com");
113   AddPasswordEntry(pass_origin, "test@gmail.com", "test");
114   EXPECT_CALL(
115       *GetUIController(),
116       SetPasswordList(
117           Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)),
118           testing::_));
119   EXPECT_CALL(*GetUIController(),
120               SetPasswordExceptionList(Property(
121                   &ScopedVector<autofill::PasswordForm>::size, Eq(0u))));
122   UpdateLists();
123   GURL except_origin("http://abc2.com");
124   AddPasswordException(except_origin);
125   EXPECT_CALL(
126       *GetUIController(),
127       SetPasswordList(
128           Property(&ScopedVector<autofill::PasswordForm>::size, Eq(1u)),
129           testing::_));
130   EXPECT_CALL(*GetUIController(),
131               SetPasswordExceptionList(Property(
132                   &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
133   UpdateLists();
134   GURL pass_origin2("http://example.com");
135   AddPasswordEntry(pass_origin2, "test@gmail.com", "test");
136   EXPECT_CALL(
137       *GetUIController(),
138       SetPasswordList(
139           Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)),
140           testing::_));
141   EXPECT_CALL(*GetUIController(),
142               SetPasswordExceptionList(Property(
143                   &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
144   UpdateLists();
145 }
146
147 }  // namespace