Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(325)

Side by Side Diff: chrome/browser/ui/passwords/password_manager_presenter_unittest.cc

Issue 489103004: Allow editing passwords in settings/passwords (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Exclude 2 tests on Android Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/macros.h"
engedy 2014/09/15 14:38:59 Do we need this?
jaekyeom 2014/09/16 07:58:10 Yes, for arraysize().
6 #include "base/memory/ref_counted.h"
engedy 2014/09/15 14:38:59 I think we can drop this, as this must already be
jaekyeom 2014/09/16 07:58:10 Done.
5 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/password_manager/mock_password_store_service.h" 8 #include "chrome/browser/password_manager/mock_password_store_service.h"
7 #include "chrome/browser/password_manager/password_store_factory.h" 9 #include "chrome/browser/password_manager/password_store_factory.h"
8 #include "chrome/browser/ui/passwords/password_manager_presenter.h" 10 #include "chrome/browser/ui/passwords/password_manager_presenter.h"
9 #include "chrome/browser/ui/passwords/password_ui_view.h" 11 #include "chrome/browser/ui/passwords/password_ui_view.h"
10 #include "chrome/test/base/testing_profile.h" 12 #include "chrome/test/base/testing_profile.h"
11 #include "components/password_manager/core/browser/mock_password_store.h" 13 #include "components/password_manager/core/browser/mock_password_store.h"
12 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 using base::ASCIIToUTF16; 17 using base::ASCIIToUTF16;
18 using testing::AllOf;
engedy 2014/09/15 14:38:59 nit: Conventially, we only introduce a using direc
jaekyeom 2014/09/16 07:58:10 Done.
16 using testing::Eq; 19 using testing::Eq;
20 using testing::Field;
17 using testing::Property; 21 using testing::Property;
18 22
19 class MockPasswordUIView : public PasswordUIView { 23 class MockPasswordUIView : public PasswordUIView {
20 public: 24 public:
21 explicit MockPasswordUIView(Profile* profile) 25 explicit MockPasswordUIView(Profile* profile)
22 : profile_(profile), password_manager_presenter_(this) { 26 : profile_(profile), password_manager_presenter_(this) {
23 password_manager_presenter_.Initialize(); 27 password_manager_presenter_.Initialize();
24 } 28 }
25 virtual ~MockPasswordUIView() {} 29 virtual ~MockPasswordUIView() {}
26 virtual Profile* GetProfile() OVERRIDE; 30 virtual Profile* GetProfile() OVERRIDE;
(...skipping 23 matching lines...) Expand all
50 54
51 class PasswordManagerPresenterTest : public testing::Test { 55 class PasswordManagerPresenterTest : public testing::Test {
52 protected: 56 protected:
53 PasswordManagerPresenterTest() {} 57 PasswordManagerPresenterTest() {}
54 58
55 virtual ~PasswordManagerPresenterTest() {} 59 virtual ~PasswordManagerPresenterTest() {}
56 virtual void SetUp() OVERRIDE { 60 virtual void SetUp() OVERRIDE {
57 PasswordStoreFactory::GetInstance()->SetTestingFactory( 61 PasswordStoreFactory::GetInstance()->SetTestingFactory(
58 &profile_, MockPasswordStoreService::Build); 62 &profile_, MockPasswordStoreService::Build);
59 mock_controller_.reset(new MockPasswordUIView(&profile_)); 63 mock_controller_.reset(new MockPasswordUIView(&profile_));
64 mock_store_ = static_cast<password_manager::MockPasswordStore*>(
65 PasswordStoreFactory::GetForProfile(&profile_,
66 Profile::EXPLICIT_ACCESS).get());
60 } 67 }
61 void AddPasswordEntry(const GURL& origin, 68 void AddPasswordEntry(const GURL& origin,
62 const std::string& user_name, 69 const std::string& user_name,
63 const std::string& password); 70 const std::string& password);
64 void AddPasswordException(const GURL& origin); 71 void AddPasswordException(const GURL& origin);
65 void UpdateLists(); 72 void UpdateLists();
66 MockPasswordUIView* GetUIController() { return mock_controller_.get(); } 73 MockPasswordUIView* GetUIController() { return mock_controller_.get(); }
74 PasswordManagerPresenter* GetPasswordManagerPresenter() {
75 return mock_controller_->GetPasswordManagerPresenter();
76 }
77 password_manager::MockPasswordStore* GetPasswordStore() {
78 return mock_store_.get();
79 }
67 80
68 private: 81 private:
69 TestingProfile profile_; 82 TestingProfile profile_;
70 scoped_ptr<MockPasswordUIView> mock_controller_; 83 scoped_ptr<MockPasswordUIView> mock_controller_;
84 scoped_refptr<password_manager::MockPasswordStore> mock_store_;
71 85
72 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest); 86 DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenterTest);
73 }; 87 };
74 88
75 void PasswordManagerPresenterTest::AddPasswordEntry( 89 void PasswordManagerPresenterTest::AddPasswordEntry(
76 const GURL& origin, 90 const GURL& origin,
77 const std::string& user_name, 91 const std::string& user_name,
78 const std::string& password) { 92 const std::string& password) {
79 autofill::PasswordForm* form = new autofill::PasswordForm(); 93 autofill::PasswordForm* form = new autofill::PasswordForm();
80 form->origin = origin; 94 form->origin = origin;
81 form->username_element = base::ASCIIToUTF16("Email"); 95 form->username_element = base::ASCIIToUTF16("Email");
82 form->username_value = base::ASCIIToUTF16(user_name); 96 form->username_value = base::ASCIIToUTF16(user_name);
83 form->password_element = base::ASCIIToUTF16("Passwd"); 97 form->password_element = base::ASCIIToUTF16("Passwd");
84 form->password_value = base::ASCIIToUTF16(password); 98 form->password_value = base::ASCIIToUTF16(password);
85 mock_controller_->GetPasswordManagerPresenter()->password_list_ 99 GetPasswordManagerPresenter()->password_list_.push_back(form);
86 .push_back(form);
87 } 100 }
88 101
89 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) { 102 void PasswordManagerPresenterTest::AddPasswordException(const GURL& origin) {
90 autofill::PasswordForm* form = new autofill::PasswordForm(); 103 autofill::PasswordForm* form = new autofill::PasswordForm();
91 form->origin = origin; 104 form->origin = origin;
92 mock_controller_->GetPasswordManagerPresenter()->password_exception_list_ 105 GetPasswordManagerPresenter()->password_exception_list_.push_back(form);
93 .push_back(form);
94 } 106 }
95 107
96 void PasswordManagerPresenterTest::UpdateLists() { 108 void PasswordManagerPresenterTest::UpdateLists() {
97 mock_controller_->GetPasswordManagerPresenter()->SetPasswordList(); 109 GetPasswordManagerPresenter()->SetPasswordList();
98 mock_controller_->GetPasswordManagerPresenter()->SetPasswordExceptionList(); 110 GetPasswordManagerPresenter()->SetPasswordExceptionList();
99 } 111 }
100 112
101 namespace { 113 namespace {
102 114
103 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) { 115 TEST_F(PasswordManagerPresenterTest, UIControllerIsCalled) {
104 EXPECT_CALL( 116 EXPECT_CALL(
105 *GetUIController(), 117 *GetUIController(),
106 SetPasswordList( 118 SetPasswordList(
107 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)), 119 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(0u)),
108 testing::_)); 120 testing::_));
(...skipping 29 matching lines...) Expand all
138 *GetUIController(), 150 *GetUIController(),
139 SetPasswordList( 151 SetPasswordList(
140 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)), 152 Property(&ScopedVector<autofill::PasswordForm>::size, Eq(2u)),
141 testing::_)); 153 testing::_));
142 EXPECT_CALL(*GetUIController(), 154 EXPECT_CALL(*GetUIController(),
143 SetPasswordExceptionList(Property( 155 SetPasswordExceptionList(Property(
144 &ScopedVector<autofill::PasswordForm>::size, Eq(1u)))); 156 &ScopedVector<autofill::PasswordForm>::size, Eq(1u))));
145 UpdateLists(); 157 UpdateLists();
146 } 158 }
147 159
160 // AddPassword and UpdatePassword are never called on Android.
161 #if !defined(OS_ANDROID)
162 TEST_F(PasswordManagerPresenterTest, CallAddPassword) {
163 GURL basic_origin("http://host.com");
164 base::string16 username = base::ASCIIToUTF16("username");
165 base::string16 password = base::ASCIIToUTF16("password");
166 EXPECT_CALL(
167 *GetPasswordStore(),
168 AddLogin(AllOf(
169 Field(&autofill::PasswordForm::signon_realm, Eq(basic_origin.spec())),
170 Field(&autofill::PasswordForm::origin, Eq(basic_origin)),
171 Field(&autofill::PasswordForm::username_value, Eq(username)),
172 Field(&autofill::PasswordForm::password_value, Eq(password)),
173 Field(&autofill::PasswordForm::ssl_valid, Eq(false)))));
174 GetPasswordManagerPresenter()->AddPassword(basic_origin, username, password);
175
176 GURL complex_origin("https://foo:bar@host.com/path?query=v#ref");
engedy 2014/09/15 14:38:59 Please add a custom port as well (and update the e
jaekyeom 2014/09/16 07:58:10 Done.
177 EXPECT_CALL(
178 *GetPasswordStore(),
179 AddLogin(AllOf(
180 Field(&autofill::PasswordForm::signon_realm, Eq("https://host.com/")),
181 Field(&autofill::PasswordForm::origin,
182 Eq(GURL("https://host.com/path"))),
183 Field(&autofill::PasswordForm::username_value, Eq(username)),
184 Field(&autofill::PasswordForm::password_value, Eq(password)),
185 Field(&autofill::PasswordForm::ssl_valid, Eq(true)))));
186 GetPasswordManagerPresenter()->AddPassword(complex_origin,
187 username,
188 password);
189 }
190
191 TEST_F(PasswordManagerPresenterTest, CallUpdatePassword) {
192 GURL origin1("http://host.com");
193 std::string username1 = "username";
engedy 2014/09/15 14:38:59 nit: Conventionally, we use const char kUsername1[
jaekyeom 2014/09/16 07:58:10 Done.
194 AddPasswordEntry(origin1, username1, "password");
195 GURL origin2("https://example.com");
196 std::string username2 = "testname";
197 AddPasswordEntry(origin2, username2, "abcd");
198
199 base::string16 new_password = base::ASCIIToUTF16("testpassword");
200 EXPECT_CALL(
201 *GetPasswordStore(),
202 UpdateLogin(AllOf(
203 Field(&autofill::PasswordForm::origin, Eq(origin1)),
204 Field(&autofill::PasswordForm::username_value,
205 Eq(base::ASCIIToUTF16(username1))),
206 Field(&autofill::PasswordForm::password_value,
207 Eq(new_password)))));
208 GetPasswordManagerPresenter()->UpdatePassword(0, new_password);
209
210 base::string16 new_password_again = base::ASCIIToUTF16("testpassword_again");
211 EXPECT_CALL(
212 *GetPasswordStore(),
213 UpdateLogin(AllOf(
214 Field(&autofill::PasswordForm::origin, Eq(origin1)),
215 Field(&autofill::PasswordForm::username_value,
216 Eq(base::ASCIIToUTF16(username1))),
217 Field(&autofill::PasswordForm::password_value,
218 Eq(new_password_again)))));
219 GetPasswordManagerPresenter()->UpdatePassword(0, new_password_again);
220
221 base::string16 another_password = base::ASCIIToUTF16("mypassword");
222 EXPECT_CALL(
223 *GetPasswordStore(),
224 UpdateLogin(AllOf(
225 Field(&autofill::PasswordForm::origin, Eq(origin2)),
226 Field(&autofill::PasswordForm::username_value,
227 Eq(base::ASCIIToUTF16(username2))),
228 Field(&autofill::PasswordForm::password_value,
229 Eq(another_password)))));
230 GetPasswordManagerPresenter()->UpdatePassword(1, another_password);
231 }
232 #endif // !defined(OS_ANDROID)
233
234 TEST(PasswordManagerPresenterTestSimple, CallCheckOriginValidityForAdding) {
235 static const char* const kValidOrigins[] = {
236 "http://host.com",
237 "http://host.com/path",
238 "https://host.com",
239 "https://foo:bar@host.com/path?query=v#ref"
engedy 2014/09/15 14:38:59 Please also add an URL with a custom a port specif
jaekyeom 2014/09/16 07:58:10 Done.
240 };
241 for (size_t i = 0; i < arraysize(kValidOrigins); ++i) {
242 EXPECT_TRUE(PasswordManagerPresenter::CheckOriginValidityForAdding(
engedy 2014/09/15 14:38:59 Could you please add: SCOPED_TRACE(kValidOrigin
jaekyeom 2014/09/16 07:58:10 Done.
243 GURL(kValidOrigins[i])));
244 }
245
246 static const char* const kInvalidOrigins[] = {
247 "noscheme",
248 "invalidscheme:host.com",
249 "ftp://ftp.host.com",
250 "about:test"
251 };
252 for (size_t i = 0; i < arraysize(kInvalidOrigins); ++i) {
253 EXPECT_FALSE(PasswordManagerPresenter::CheckOriginValidityForAdding(
engedy 2014/09/15 14:38:59 Same here.
jaekyeom 2014/09/16 07:58:10 Done.
254 GURL(kInvalidOrigins[i])));
255 }
256 }
257
148 } // namespace 258 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698