OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #import "chrome/browser/autofill/autofill_address_model_mac.h" |
| 6 #import "chrome/browser/autofill/autofill_address_view_controller_mac.h" |
| 7 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" |
| 8 #import "chrome/browser/autofill/autofill_credit_card_view_controller_mac.h" |
| 9 #import "chrome/browser/autofill/autofill_dialog_controller_mac.h" |
| 10 #include "chrome/browser/autofill/autofill_profile.h" |
| 11 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 12 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 13 #include "chrome/browser/profile.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 class AutoFillDialogObserverTester : public AutoFillDialogObserver { |
| 18 public: |
| 19 AutoFillDialogObserverTester() : hit_(false) {} |
| 20 virtual ~AutoFillDialogObserverTester() {} |
| 21 |
| 22 virtual void OnAutoFillDialogApply( |
| 23 std::vector<AutoFillProfile>* profiles, |
| 24 std::vector<CreditCard>* credit_cards) { |
| 25 hit_ = true; |
| 26 |
| 27 std::vector<AutoFillProfile>::iterator i; |
| 28 profiles_.clear(); |
| 29 for (i = profiles->begin(); i != profiles->end(); ++i) |
| 30 profiles_.push_back(*i); |
| 31 |
| 32 std::vector<CreditCard>::iterator j; |
| 33 credit_cards_.clear(); |
| 34 for (j = credit_cards->begin(); j != credit_cards->end(); ++j) |
| 35 credit_cards_.push_back(*j); |
| 36 } |
| 37 |
| 38 bool hit_; |
| 39 std::vector<AutoFillProfile> profiles_; |
| 40 std::vector<CreditCard> credit_cards_; |
| 41 }; |
| 42 |
| 43 class AutoFillDialogControllerTest : public CocoaTest { |
| 44 public: |
| 45 AutoFillDialogControllerTest() {} |
| 46 |
| 47 void LoadDialog() { |
| 48 controller_ = [AutoFillDialogController |
| 49 controllerWithObserver:&observer_ |
| 50 autoFillProfiles:profiles_ |
| 51 creditCards:credit_cards_]; |
| 52 [controller_ window]; |
| 53 } |
| 54 |
| 55 BrowserTestHelper helper_; |
| 56 AutoFillDialogObserverTester observer_; |
| 57 AutoFillDialogController* controller_; // weak reference |
| 58 std::vector<AutoFillProfile*> profiles_; // weak references within vector |
| 59 std::vector<CreditCard*> credit_cards_; // weak references within vector |
| 60 }; |
| 61 |
| 62 TEST_F(AutoFillDialogControllerTest, SaveButtonInformsObserver) { |
| 63 LoadDialog(); |
| 64 [controller_ save:nil]; |
| 65 ASSERT_TRUE(observer_.hit_); |
| 66 } |
| 67 |
| 68 TEST_F(AutoFillDialogControllerTest, CancelButtonDoesNotInformObserver) { |
| 69 LoadDialog(); |
| 70 [controller_ cancel:nil]; |
| 71 ASSERT_FALSE(observer_.hit_); |
| 72 } |
| 73 |
| 74 TEST_F(AutoFillDialogControllerTest, NoEditsGiveBackOriginalProfile) { |
| 75 AutoFillProfile profile; |
| 76 profiles_.push_back(&profile); |
| 77 LoadDialog(); |
| 78 [controller_ save:nil]; |
| 79 |
| 80 // Should hit our observer. |
| 81 ASSERT_TRUE(observer_.hit_); |
| 82 |
| 83 // Sizes should match. |
| 84 ASSERT_EQ(observer_.profiles_.size(), profiles_.size()); |
| 85 |
| 86 // Contents should match. |
| 87 size_t i = 0; |
| 88 size_t count = profiles_.size(); |
| 89 for (i = 0; i < count; i++) |
| 90 ASSERT_EQ(observer_.profiles_[i], *profiles_[i]); |
| 91 |
| 92 // Contents should not match a different profile. |
| 93 AutoFillProfile differentProfile; |
| 94 differentProfile.set_label(ASCIIToUTF16("different")); |
| 95 differentProfile.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("joe")); |
| 96 for (i = 0; i < count; i++) |
| 97 ASSERT_NE(observer_.profiles_[i], differentProfile); |
| 98 } |
| 99 |
| 100 TEST_F(AutoFillDialogControllerTest, NoEditsGiveBackOriginalCreditCard) { |
| 101 CreditCard creditCard(ASCIIToUTF16("myCC"), 345); |
| 102 credit_cards_.push_back(&creditCard); |
| 103 LoadDialog(); |
| 104 [controller_ save:nil]; |
| 105 |
| 106 // Should hit our observer. |
| 107 ASSERT_TRUE(observer_.hit_); |
| 108 |
| 109 // Sizes should match. |
| 110 ASSERT_EQ(observer_.credit_cards_.size(), credit_cards_.size()); |
| 111 |
| 112 // Contents should match. |
| 113 size_t i = 0; |
| 114 size_t count = credit_cards_.size(); |
| 115 for (i = 0; i < count; i++) |
| 116 ASSERT_EQ(observer_.credit_cards_[i], *credit_cards_[i]); |
| 117 |
| 118 // Contents should not match a different profile. |
| 119 CreditCard differentCreditCard(ASCIIToUTF16("different"), 0); |
| 120 differentCreditCard.SetInfo( |
| 121 AutoFillType(CREDIT_CARD_NUMBER), ASCIIToUTF16("1234")); |
| 122 for (i = 0; i < count; i++) |
| 123 ASSERT_NE(observer_.credit_cards_[i], differentCreditCard); |
| 124 } |
| 125 |
| 126 TEST_F(AutoFillDialogControllerTest, AutoFillDataMutation) { |
| 127 AutoFillProfile profile(ASCIIToUTF16("Home"), 17); |
| 128 profile.SetInfo(AutoFillType(NAME_FIRST), ASCIIToUTF16("David")); |
| 129 profile.SetInfo(AutoFillType(NAME_MIDDLE), ASCIIToUTF16("C")); |
| 130 profile.SetInfo(AutoFillType(NAME_LAST), ASCIIToUTF16("Holloway")); |
| 131 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), |
| 132 ASCIIToUTF16("dhollowa@chromium.org")); |
| 133 profile.SetInfo(AutoFillType(COMPANY_NAME), ASCIIToUTF16("Google Inc.")); |
| 134 profile.SetInfo( |
| 135 AutoFillType(ADDRESS_HOME_LINE1), ASCIIToUTF16("1122 Mountain View Road")); |
| 136 profile.SetInfo(AutoFillType(ADDRESS_HOME_LINE2), ASCIIToUTF16("Suite #1")); |
| 137 profile.SetInfo(AutoFillType(ADDRESS_HOME_CITY), |
| 138 ASCIIToUTF16("Mountain View")); |
| 139 profile.SetInfo(AutoFillType(ADDRESS_HOME_STATE), ASCIIToUTF16("CA")); |
| 140 profile.SetInfo(AutoFillType(ADDRESS_HOME_ZIP), ASCIIToUTF16("94111")); |
| 141 profile.SetInfo(AutoFillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16("USA")); |
| 142 profile.SetInfo(AutoFillType(PHONE_HOME_COUNTRY_CODE), ASCIIToUTF16("01")); |
| 143 profile.SetInfo(AutoFillType(PHONE_HOME_CITY_CODE), ASCIIToUTF16("415")); |
| 144 profile.SetInfo(AutoFillType(PHONE_HOME_NUMBER), ASCIIToUTF16("5552258")); |
| 145 profile.SetInfo(AutoFillType(PHONE_FAX_COUNTRY_CODE), ASCIIToUTF16("02")); |
| 146 profile.SetInfo(AutoFillType(PHONE_FAX_CITY_CODE), ASCIIToUTF16("408")); |
| 147 profile.SetInfo(AutoFillType(PHONE_FAX_NUMBER), ASCIIToUTF16("7172258")); |
| 148 profiles_.push_back(&profile); |
| 149 |
| 150 LoadDialog(); |
| 151 |
| 152 AutoFillAddressModel* am = [[controller_ addressFormViewController] |
| 153 addressModel]; |
| 154 EXPECT_TRUE([[am firstName] isEqualToString:@"David"]); |
| 155 EXPECT_TRUE([[am middleName] isEqualToString:@"C"]); |
| 156 EXPECT_TRUE([[am lastName] isEqualToString:@"Holloway"]); |
| 157 EXPECT_TRUE([[am email] isEqualToString:@"dhollowa@chromium.org"]); |
| 158 EXPECT_TRUE([[am companyName] isEqualToString:@"Google Inc."]); |
| 159 EXPECT_TRUE([[am addressLine1] isEqualToString:@"1122 Mountain View Road"]); |
| 160 EXPECT_TRUE([[am addressLine2] isEqualToString:@"Suite #1"]); |
| 161 EXPECT_TRUE([[am city] isEqualToString:@"Mountain View"]); |
| 162 EXPECT_TRUE([[am state] isEqualToString:@"CA"]); |
| 163 EXPECT_TRUE([[am zip] isEqualToString:@"94111"]); |
| 164 EXPECT_TRUE([[am phoneCountryCode] isEqualToString:@"01"]); |
| 165 EXPECT_TRUE([[am phoneAreaCode] isEqualToString:@"415"]); |
| 166 EXPECT_TRUE([[am phoneNumber] isEqualToString:@"5552258"]); |
| 167 EXPECT_TRUE([[am faxCountryCode] isEqualToString:@"02"]); |
| 168 EXPECT_TRUE([[am faxAreaCode] isEqualToString:@"408"]); |
| 169 EXPECT_TRUE([[am faxNumber] isEqualToString:@"7172258"]); |
| 170 |
| 171 [controller_ save:nil]; |
| 172 |
| 173 ASSERT_TRUE(observer_.hit_); |
| 174 ASSERT_TRUE(observer_.profiles_.size() == 1); |
| 175 } |
| 176 |
| 177 TEST_F(AutoFillDialogControllerTest, CreditCardDataMutation) { |
| 178 CreditCard creditCard(ASCIIToUTF16("myCC"), 345); |
| 179 creditCard.SetInfo(AutoFillType(CREDIT_CARD_NAME), ASCIIToUTF16("DCH")); |
| 180 creditCard.SetInfo( |
| 181 AutoFillType(CREDIT_CARD_NUMBER), ASCIIToUTF16("1234 5678 9101 1121")); |
| 182 creditCard.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("01")); |
| 183 creditCard.SetInfo( |
| 184 AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), ASCIIToUTF16("2012")); |
| 185 creditCard.SetInfo( |
| 186 AutoFillType(CREDIT_CARD_VERIFICATION_CODE), ASCIIToUTF16("222")); |
| 187 credit_cards_.push_back(&creditCard); |
| 188 |
| 189 LoadDialog(); |
| 190 |
| 191 AutoFillCreditCardModel* cm = [[controller_ creditCardFormViewController] |
| 192 creditCardModel]; |
| 193 EXPECT_TRUE([[cm nameOnCard] isEqualToString:@"DCH"]); |
| 194 EXPECT_TRUE([[cm creditCardNumber] isEqualToString:@"1234 5678 9101 1121"]); |
| 195 EXPECT_TRUE([[cm expirationMonth] isEqualToString:@"01"]); |
| 196 EXPECT_TRUE([[cm expirationYear] isEqualToString:@"2012"]); |
| 197 EXPECT_TRUE([[cm cvcCode] isEqualToString:@"222"]); |
| 198 |
| 199 [controller_ save:nil]; |
| 200 |
| 201 ASSERT_TRUE(observer_.hit_); |
| 202 ASSERT_TRUE(observer_.credit_cards_.size() == 1); |
| 203 } |
| 204 |
| 205 } |
OLD | NEW |