Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / passwords / credential_item_view_unittest.mm
blob8845b8fa5b15aeb33c2df684b765fe354714c45c
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* upperLabel;
17 @property(nonatomic, readonly) NSTextField* lowerLabel;
18 @property(nonatomic, readonly) NSImageView* avatarView;
19 @end
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;
30 @end
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;
53 @end
55 namespace {
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) {
61   if (!left || !right)
62     return left == 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");
73   return credential;
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");
81   return credential;
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.icon_url = GURL("http://sandwich.com/pastrami.jpg");
89   return credential;
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.icon_url = GURL("http://pasta.com/amatriciana.png");
98   return credential;
101 // Tests for CredentialItemViewTest.
102 class CredentialItemViewTest : public CocoaTest {
103  protected:
104   void SetUp() override {
105     delegate_.reset([[CredentialItemTestDelegate alloc] init]);
106   }
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_PASSWORD
117                        style:password_manager_mac::CredentialItemStyle::
118                                  ACCOUNT_CHOOSER
119                     delegate:delegate()] autorelease];
120   }
122  private:
123   base::scoped_nsobject<CredentialItemTestDelegate> delegate_;
126 TEST_F(CredentialItemViewTest, BasicCredential) {
127   autofill::PasswordForm form(BasicCredential());
128   CredentialItemView* item = view(form);
130   EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
131               [item upperLabel].stringValue);
132   EXPECT_EQ(nil, [item lowerLabel]);
133   EXPECT_FALSE([delegate() didFetchAvatar]);
134   EXPECT_TRUE(
135       ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
138 TEST_F(CredentialItemViewTest, CredentialWithName) {
139   autofill::PasswordForm form(CredentialWithName());
140   CredentialItemView* item = view(form);
142   EXPECT_NSEQ(base::SysUTF16ToNSString(form.display_name),
143               [item upperLabel].stringValue);
144   EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
145               [item lowerLabel].stringValue);
146   EXPECT_FALSE([delegate() didFetchAvatar]);
147   EXPECT_TRUE(
148       ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
151 TEST_F(CredentialItemViewTest, CredentialWithAvatar) {
152   autofill::PasswordForm form(CredentialWithAvatar());
153   CredentialItemView* item = view(form);
155   EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
156               [item upperLabel].stringValue);
157   EXPECT_EQ(nil, [item lowerLabel]);
158   EXPECT_TRUE([delegate() didFetchAvatar]);
159   EXPECT_EQ(form.icon_url, [delegate() fetchedAvatarURL]);
160   EXPECT_EQ(item, [delegate() viewForFetchedAvatar]);
161   EXPECT_TRUE(
162       ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
164   [item updateAvatar:nil];
165   EXPECT_FALSE([item avatarView].image);
168 TEST_F(CredentialItemViewTest, CredentialWithNameAndAvatar) {
169   autofill::PasswordForm form(CredentialWithNameAndAvatar());
170   CredentialItemView* item = view(form);
172   EXPECT_NSEQ(base::SysUTF16ToNSString(form.display_name),
173               [item upperLabel].stringValue);
174   EXPECT_NSEQ(base::SysUTF16ToNSString(form.username_value),
175               [item lowerLabel].stringValue);
176   EXPECT_TRUE([delegate() didFetchAvatar]);
177   EXPECT_EQ(form.icon_url, [delegate() fetchedAvatarURL]);
178   EXPECT_EQ(item, [delegate() viewForFetchedAvatar]);
179   EXPECT_TRUE(
180       ImagesEqual([CredentialItemView defaultAvatar], [item avatarView].image));
182   [item updateAvatar:nil];
183   EXPECT_FALSE([item avatarView].image);
186 }  // namespace