1 // Copyright 2015 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/credential_item_view.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
10 #include "testing/gtest_mac.h"
11 #import "third_party/ocmock/OCMock/OCMock.h"
12 #include "third_party/ocmock/gtest_support.h"
13 #include "ui/gfx/image/image.h"
15 @interface CredentialItemView(Testing)
16 @property(nonatomic, readonly) NSTextField* nameLabel;
17 @property(nonatomic, readonly) NSTextField* usernameLabel;
18 @property(nonatomic, readonly) NSImageView* avatarView;
21 // A test implementation of a CredentialItemDelegate to stub out interactions.
22 @interface CredentialItemTestDelegate : NSObject<CredentialItemDelegate>
23 @property(nonatomic, readonly) BOOL didFetchAvatar;
24 @property(nonatomic, readonly) GURL fetchedAvatarURL;
25 @property(nonatomic, readonly) CredentialItemView* viewForFetchedAvatar;
26 @property(nonatomic, readonly) BOOL didSelectPasswordForm;
27 @property(nonatomic, readonly) autofill::PasswordForm selectedPasswordForm;
28 @property(nonatomic, readonly)
29 password_manager::CredentialType selectedCredentialType;
32 @implementation CredentialItemTestDelegate
33 @synthesize didFetchAvatar = didFetchAvatar_;
34 @synthesize fetchedAvatarURL = fetchedAvatarURL_;
35 @synthesize viewForFetchedAvatar = viewForFetchedAvatar_;
36 @synthesize didSelectPasswordForm = didSelectPasswordForm_;
37 @synthesize selectedPasswordForm = selectedPasswordForm_;
38 @synthesize selectedCredentialType = selectedCredentialType_;
40 - (void)fetchAvatar:(const GURL&)avatarURL forView:(CredentialItemView*)view {
41 didFetchAvatar_ = YES;
42 fetchedAvatarURL_ = avatarURL;
43 viewForFetchedAvatar_ = view;
46 - (void)selectPasswordForm:(const autofill::PasswordForm&)passwordForm
47 credentialType:(password_manager::CredentialType)credentialType {
48 didSelectPasswordForm_ = YES;
49 selectedPasswordForm_ = passwordForm;
50 selectedCredentialType_ = credentialType;
57 // Determines whether |left| and |right| have the same data representation.
58 // Necessary because [CredentialItemView defaultAvatar] does some ImageSkia
59 // stuff that creates new NSImage instances.
60 bool ImagesEqual(NSImage* left, NSImage* right) {
64 gfx::Image leftImage([left copy]);
65 gfx::Image rightImage([right copy]);
66 return leftImage.As1xPNGBytes()->Equals(rightImage.As1xPNGBytes());
69 // Returns a PasswordForm with only a username.
70 autofill::PasswordForm BasicCredential() {
71 autofill::PasswordForm credential;
72 credential.username_value = base::ASCIIToUTF16("taco");
76 // Returns a PasswordForm with a username and display name.
77 autofill::PasswordForm CredentialWithName() {
78 autofill::PasswordForm credential;
79 credential.username_value = base::ASCIIToUTF16("pizza");
80 credential.display_name = base::ASCIIToUTF16("margherita pizza");
84 // Returns a PasswordForm with a username and avatar URL.
85 autofill::PasswordForm CredentialWithAvatar() {
86 autofill::PasswordForm credential;
87 credential.username_value = base::ASCIIToUTF16("sandwich");
88 credential.avatar_url = GURL("http://sandwich.com/pastrami.jpg");
92 // Returns a PasswordForm with a username, display name, and avatar URL.
93 autofill::PasswordForm CredentialWithNameAndAvatar() {
94 autofill::PasswordForm credential;
95 credential.username_value = base::ASCIIToUTF16("noodle");
96 credential.display_name = base::ASCIIToUTF16("pasta amatriciana");
97 credential.avatar_url = GURL("http://pasta.com/amatriciana.png");
101 // Tests for CredentialItemViewTest.
102 class CredentialItemViewTest : public CocoaTest {
104 void SetUp() override {
105 delegate_.reset([[CredentialItemTestDelegate alloc] init]);
108 // Returns a delegate for testing.
109 CredentialItemTestDelegate* delegate() { return delegate_.get(); }
111 // Returns an autoreleased view populated from |form|.
112 CredentialItemView* view(const autofill::PasswordForm& form) {
113 return [[[CredentialItemView alloc]
114 initWithPasswordForm:form
115 credentialType:password_manager::CredentialType::
116 CREDENTIAL_TYPE_LOCAL
117 delegate:delegate()] autorelease];
121 base::scoped_nsobject<CredentialItemTestDelegate> delegate_;
124 TEST_F(CredentialItemViewTest, BasicCredential) {
125 autofill::PasswordForm form(BasicCredential());
126 CredentialItemView* item = view(form);
128 EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
129 [item usernameLabel].stringValue);
130 EXPECT_EQ(nil, [item nameLabel]);
131 EXPECT_FALSE([delegate() didFetchAvatar]);
133 ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
136 TEST_F(CredentialItemViewTest, CredentialWithName) {
137 autofill::PasswordForm form(CredentialWithName());
138 CredentialItemView* item = view(form);
140 EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
141 [item usernameLabel].stringValue);
142 EXPECT_NSEQ(base::SysUTF16ToNSString(form.display_name),
143 [item nameLabel].stringValue);
144 EXPECT_FALSE([delegate() didFetchAvatar]);
146 ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
149 TEST_F(CredentialItemViewTest, CredentialWithAvatar) {
150 autofill::PasswordForm form(CredentialWithAvatar());
151 CredentialItemView* item = view(form);
153 EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
154 [item usernameLabel].stringValue);
155 EXPECT_EQ(nil, [item nameLabel]);
156 EXPECT_TRUE([delegate() didFetchAvatar]);
157 EXPECT_EQ(form.avatar_url, [delegate() fetchedAvatarURL]);
158 EXPECT_EQ(item, [delegate() viewForFetchedAvatar]);
160 ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
162 [item updateAvatar:nil];
163 EXPECT_FALSE([item avatarView].image);
166 TEST_F(CredentialItemViewTest, CredentialWithNameAndAvatar) {
167 autofill::PasswordForm form(CredentialWithNameAndAvatar());
168 CredentialItemView* item = view(form);
170 EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
171 [item usernameLabel].stringValue);
172 EXPECT_NSEQ(base::SysUTF16ToNSString(form.display_name),
173 [item nameLabel].stringValue);
174 EXPECT_TRUE([delegate() didFetchAvatar]);
175 EXPECT_EQ(form.avatar_url, [delegate() fetchedAvatarURL]);
176 EXPECT_EQ(item, [delegate() viewForFetchedAvatar]);
178 ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
180 [item updateAvatar:nil];
181 EXPECT_FALSE([item avatarView].image);