1 // Copyright 2014 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 #import "chrome/browser/ui/cocoa/passwords/manage_password_item_view_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/browser/password_manager/mock_password_store_service.h"
12 #include "chrome/browser/password_manager/password_store_factory.h"
13 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
14 #import "chrome/browser/ui/cocoa/passwords/manage_password_item_view_controller.h"
15 #include "chrome/browser/ui/cocoa/passwords/manage_passwords_controller_test.h"
16 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
17 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
18 #include "components/password_manager/core/browser/mock_password_store.h"
19 #include "components/password_manager/core/browser/password_store.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/gtest_mac.h"
23 using namespace testing;
26 NSString* const kItemTestUsername = @"foo";
27 NSString* const kItemTestPassword = @"bar";
30 MATCHER_P(PasswordFormEq, form, "") {
31 return form.username_value == arg.username_value &&
32 form.password_value == arg.password_value;
35 class ManagePasswordItemViewControllerTest
36 : public ManagePasswordsControllerTest {
38 ManagePasswordItemViewControllerTest() {}
39 virtual ~ManagePasswordItemViewControllerTest() {}
41 virtual void SetUp() override {
42 ManagePasswordsControllerTest::SetUp();
43 PasswordStoreFactory::GetInstance()->SetTestingFactory(
44 profile(), MockPasswordStoreService::Build);
45 ui_controller()->SetPendingPassword(credentials());
48 ManagePasswordItemViewController* controller() {
50 controller_.reset([[ManagePasswordItemViewController alloc]
52 passwordForm:ui_controller()->PendingPassword()
53 position:password_manager::ui::FIRST_ITEM]);
55 return controller_.get();
58 autofill::PasswordForm credentials() {
59 autofill::PasswordForm form;
60 form.username_value = base::SysNSStringToUTF16(kItemTestUsername);
61 form.password_value = base::SysNSStringToUTF16(kItemTestPassword);
65 password_manager::MockPasswordStore* mockStore() {
66 password_manager::PasswordStore* store =
67 PasswordStoreFactory::GetForProfile(profile(), Profile::EXPLICIT_ACCESS)
69 password_manager::MockPasswordStore* mockStore =
70 static_cast<password_manager::MockPasswordStore*>(store);
75 base::scoped_nsobject<ManagePasswordItemViewController> controller_;
76 DISALLOW_COPY_AND_ASSIGN(ManagePasswordItemViewControllerTest);
79 TEST_F(ManagePasswordItemViewControllerTest, ManageStateShouldHaveManageView) {
80 model()->set_state(password_manager::ui::MANAGE_STATE);
81 EXPECT_EQ(MANAGE_PASSWORD_ITEM_STATE_MANAGE, [controller() state]);
82 EXPECT_NSEQ([ManagePasswordItemManageView class],
83 [[controller() contentView] class]);
86 TEST_F(ManagePasswordItemViewControllerTest,
87 ClickingDeleteShouldShowUndoViewAndDeletePassword) {
88 EXPECT_CALL(*mockStore(), RemoveLogin(PasswordFormEq(credentials())));
89 model()->set_state(password_manager::ui::MANAGE_STATE);
91 ManagePasswordItemManageView* manageView =
92 base::mac::ObjCCast<ManagePasswordItemManageView>(
93 controller().contentView);
94 [manageView.deleteButton performClick:nil];
96 EXPECT_NSEQ([ManagePasswordItemUndoView class],
97 [controller().contentView class]);
100 TEST_F(ManagePasswordItemViewControllerTest,
101 ClickingUndoShouldShowManageViewAndAddPassword) {
102 EXPECT_CALL(*mockStore(), AddLogin(PasswordFormEq(credentials())));
103 model()->set_state(password_manager::ui::MANAGE_STATE);
105 ManagePasswordItemManageView* manageView =
106 base::mac::ObjCCast<ManagePasswordItemManageView>(
107 controller().contentView);
108 [manageView.deleteButton performClick:nil];
110 ManagePasswordItemUndoView* undoView =
111 base::mac::ObjCCast<ManagePasswordItemUndoView>(controller().contentView);
112 [undoView.undoButton performClick:nil];
114 EXPECT_NSEQ([ManagePasswordItemManageView class],
115 [controller().contentView class]);
118 TEST_F(ManagePasswordItemViewControllerTest,
119 ManageViewShouldHaveCorrectUsernameAndObscuredPassword) {
120 model()->set_state(password_manager::ui::MANAGE_STATE);
121 ManagePasswordItemManageView* manageView =
122 base::mac::ObjCCast<ManagePasswordItemManageView>(
123 [controller() contentView]);
125 // Ensure the fields are populated properly and the password is obscured.
126 EXPECT_NSEQ(kItemTestUsername, manageView.usernameField.stringValue);
127 EXPECT_NSEQ(kItemTestPassword, manageView.passwordField.stringValue);
128 EXPECT_TRUE([[manageView.passwordField cell] echosBullets]);
131 TEST_F(ManagePasswordItemViewControllerTest,
132 PendingStateShouldHavePendingView) {
133 EXPECT_EQ(MANAGE_PASSWORD_ITEM_STATE_PENDING, [controller() state]);
134 EXPECT_NSEQ([ManagePasswordItemPendingView class],
135 [[controller() contentView] class]);
138 TEST_F(ManagePasswordItemViewControllerTest,
139 PendingViewShouldHaveCorrectUsernameAndObscuredPassword) {
140 model()->set_state(password_manager::ui::PENDING_PASSWORD_STATE);
141 ManagePasswordItemPendingView* pendingView =
142 base::mac::ObjCCast<ManagePasswordItemPendingView>(
143 [controller() contentView]);
145 // Ensure the fields are populated properly and the password is obscured.
146 EXPECT_NSEQ(kItemTestUsername, pendingView.usernameField.stringValue);
147 EXPECT_NSEQ(kItemTestPassword, pendingView.passwordField.stringValue);
148 EXPECT_TRUE([[pendingView.passwordField cell] echosBullets]);