| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 #include "base/ref_counted.h" | |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "base/values.h" | |
| 8 #import "chrome/browser/autofill/autofill_address_model_mac.h" | |
| 9 #import "chrome/browser/autofill/autofill_address_sheet_controller_mac.h" | |
| 10 #import "chrome/browser/autofill/autofill_credit_card_model_mac.h" | |
| 11 #import "chrome/browser/autofill/autofill_credit_card_sheet_controller_mac.h" | |
| 12 #import "chrome/browser/autofill/autofill_dialog_controller_mac.h" | |
| 13 #include "chrome/browser/autofill/autofill_profile.h" | |
| 14 #include "chrome/browser/autofill/personal_data_manager.h" | |
| 15 #include "chrome/browser/prefs/pref_service.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/ui/cocoa/browser_test_helper.h" | |
| 18 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | |
| 19 #include "chrome/common/pref_names.h" | |
| 20 #include "chrome/test/testing_browser_process.h" | |
| 21 #include "chrome/test/testing_pref_service.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // Simulated delay (in milliseconds) for web data loading. | |
| 27 const float kWebDataLoadDelayMilliseconds = 10.0; | |
| 28 | |
| 29 // Mock PersonalDataManager that gives back canned profiles and credit cards | |
| 30 // as well as simulating delayed loading of web data using the | |
| 31 // |PersonalDataManager::Observer| interface. | |
| 32 class PersonalDataManagerMock : public PersonalDataManager { | |
| 33 public: | |
| 34 PersonalDataManagerMock() | |
| 35 : observer_(NULL), | |
| 36 test_data_is_loaded_(true) {} | |
| 37 virtual ~PersonalDataManagerMock() {} | |
| 38 | |
| 39 virtual const std::vector<AutoFillProfile*>& web_profiles() { | |
| 40 return test_profiles_; | |
| 41 } | |
| 42 virtual const std::vector<CreditCard*>& credit_cards() { | |
| 43 return test_credit_cards_; | |
| 44 } | |
| 45 virtual bool IsDataLoaded() const { return test_data_is_loaded_; } | |
| 46 virtual void SetObserver(PersonalDataManager::Observer* observer) { | |
| 47 DCHECK(observer); | |
| 48 observer_ = observer; | |
| 49 | |
| 50 // This delay allows the UI loop to run and display intermediate results | |
| 51 // while the data is loading. When notified that the data is available the | |
| 52 // UI updates with the new data. 10ms is a nice short amount of time to | |
| 53 // let the UI thread update but does not slow down the tests too much. | |
| 54 MessageLoop::current()->PostDelayedTask( | |
| 55 FROM_HERE, | |
| 56 new MessageLoop::QuitTask, | |
| 57 kWebDataLoadDelayMilliseconds); | |
| 58 MessageLoop::current()->Run(); | |
| 59 observer_->OnPersonalDataLoaded(); | |
| 60 } | |
| 61 virtual void RemoveObserver(PersonalDataManager::Observer* observer) { | |
| 62 observer_ = NULL; | |
| 63 } | |
| 64 | |
| 65 std::vector<AutoFillProfile*> test_profiles_; | |
| 66 std::vector<CreditCard*> test_credit_cards_; | |
| 67 PersonalDataManager::Observer* observer_; | |
| 68 bool test_data_is_loaded_; | |
| 69 | |
| 70 private: | |
| 71 DISALLOW_COPY_AND_ASSIGN(PersonalDataManagerMock); | |
| 72 }; | |
| 73 | |
| 74 // Mock profile that gives back our own mock |PersonalDataManager|. | |
| 75 class ProfileMock : public TestingProfile { | |
| 76 public: | |
| 77 ProfileMock() { | |
| 78 test_manager_ = new PersonalDataManagerMock; | |
| 79 } | |
| 80 virtual ~ProfileMock() {} | |
| 81 | |
| 82 virtual PersonalDataManager* GetPersonalDataManager() { | |
| 83 return test_manager_.get(); | |
| 84 } | |
| 85 | |
| 86 scoped_refptr<PersonalDataManagerMock> test_manager_; | |
| 87 | |
| 88 private: | |
| 89 DISALLOW_COPY_AND_ASSIGN(ProfileMock); | |
| 90 }; | |
| 91 | |
| 92 // Mock browser that gives back our own |BrowserMock| instance as the profile. | |
| 93 class BrowserMock : public BrowserTestHelper { | |
| 94 public: | |
| 95 BrowserMock() { | |
| 96 test_profile_.reset(new ProfileMock); | |
| 97 } | |
| 98 virtual ~BrowserMock() {} | |
| 99 | |
| 100 // Override of |BrowserTestHelper::profile()|. | |
| 101 virtual TestingProfile* profile() const { | |
| 102 return test_profile_.get(); | |
| 103 } | |
| 104 | |
| 105 scoped_ptr<ProfileMock> test_profile_; | |
| 106 | |
| 107 private: | |
| 108 DISALLOW_COPY_AND_ASSIGN(BrowserMock); | |
| 109 }; | |
| 110 | |
| 111 // Mock observer for the AutoFill settings dialog. | |
| 112 class AutoFillDialogObserverMock : public AutoFillDialogObserver { | |
| 113 public: | |
| 114 AutoFillDialogObserverMock() | |
| 115 : hit_(false) {} | |
| 116 virtual ~AutoFillDialogObserverMock() {} | |
| 117 | |
| 118 virtual void OnAutoFillDialogApply(std::vector<AutoFillProfile>* profiles, | |
| 119 std::vector<CreditCard>* credit_cards) { | |
| 120 hit_ = true; | |
| 121 | |
| 122 std::vector<AutoFillProfile>::iterator i; | |
| 123 profiles_.clear(); | |
| 124 for (i = profiles->begin(); i != profiles->end(); ++i) | |
| 125 profiles_.push_back(*i); | |
| 126 | |
| 127 std::vector<CreditCard>::iterator j; | |
| 128 credit_cards_.clear(); | |
| 129 for (j = credit_cards->begin(); j != credit_cards->end(); ++j) | |
| 130 credit_cards_.push_back(*j); | |
| 131 } | |
| 132 | |
| 133 bool hit_; | |
| 134 std::vector<AutoFillProfile> profiles_; | |
| 135 std::vector<CreditCard> credit_cards_; | |
| 136 | |
| 137 private: | |
| 138 DISALLOW_COPY_AND_ASSIGN(AutoFillDialogObserverMock); | |
| 139 }; | |
| 140 | |
| 141 // Test fixture for setting up and tearing down our dialog controller under | |
| 142 // test. Also provides helper methods to access the source profiles and | |
| 143 // credit card information stored in mock |PersonalDataManager|. | |
| 144 class AutoFillDialogControllerTest : public CocoaTest { | |
| 145 public: | |
| 146 AutoFillDialogControllerTest() | |
| 147 : controller_(nil), | |
| 148 imported_profile_(NULL), | |
| 149 imported_credit_card_(NULL) { | |
| 150 } | |
| 151 | |
| 152 void LoadDialog() { | |
| 153 controller_ = [AutoFillDialogController | |
| 154 controllerWithObserver:&observer_ profile:helper_.profile()]; | |
| 155 [controller_ runModelessDialog]; | |
| 156 } | |
| 157 | |
| 158 std::vector<AutoFillProfile*>& profiles() { | |
| 159 return helper_.test_profile_->test_manager_->test_profiles_; | |
| 160 } | |
| 161 std::vector<CreditCard*>& credit_cards() { | |
| 162 return helper_.test_profile_->test_manager_->test_credit_cards_; | |
| 163 } | |
| 164 | |
| 165 ScopedTestingBrowserProcess browser_process_; | |
| 166 | |
| 167 BrowserMock helper_; | |
| 168 AutoFillDialogObserverMock observer_; | |
| 169 AutoFillDialogController* controller_; // weak reference | |
| 170 AutoFillProfile* imported_profile_; // weak reference | |
| 171 CreditCard* imported_credit_card_; // weak reference | |
| 172 | |
| 173 private: | |
| 174 DISALLOW_COPY_AND_ASSIGN(AutoFillDialogControllerTest); | |
| 175 }; | |
| 176 | |
| 177 TEST_F(AutoFillDialogControllerTest, CloseButtonDoesNotInformObserver) { | |
| 178 LoadDialog(); | |
| 179 [controller_ closeDialog]; | |
| 180 ASSERT_FALSE(observer_.hit_); | |
| 181 } | |
| 182 | |
| 183 TEST_F(AutoFillDialogControllerTest, NoEditsDoNotChangeObserverProfiles) { | |
| 184 AutoFillProfile profile; | |
| 185 profiles().push_back(&profile); | |
| 186 LoadDialog(); | |
| 187 [controller_ closeDialog]; | |
| 188 | |
| 189 // Should not hit our observer. | |
| 190 ASSERT_FALSE(observer_.hit_); | |
| 191 | |
| 192 // Observer should not have profiles. | |
| 193 ASSERT_EQ(0UL, observer_.profiles_.size()); | |
| 194 } | |
| 195 | |
| 196 TEST_F(AutoFillDialogControllerTest, NoEditsDoNotChangeObserverCreditCards) { | |
| 197 CreditCard credit_card; | |
| 198 credit_cards().push_back(&credit_card); | |
| 199 LoadDialog(); | |
| 200 [controller_ closeDialog]; | |
| 201 | |
| 202 // Should not hit our observer. | |
| 203 ASSERT_FALSE(observer_.hit_); | |
| 204 | |
| 205 // Observer should not have credit cards. | |
| 206 ASSERT_EQ(0UL, observer_.credit_cards_.size()); | |
| 207 } | |
| 208 | |
| 209 TEST_F(AutoFillDialogControllerTest, AutoFillDataMutation) { | |
| 210 AutoFillProfile profile; | |
| 211 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("John")); | |
| 212 profile.SetInfo(AutofillType(NAME_MIDDLE), ASCIIToUTF16("C")); | |
| 213 profile.SetInfo(AutofillType(NAME_LAST), ASCIIToUTF16("Smith")); | |
| 214 profile.SetInfo(AutofillType(EMAIL_ADDRESS), | |
| 215 ASCIIToUTF16("john@chromium.org")); | |
| 216 profile.SetInfo(AutofillType(COMPANY_NAME), ASCIIToUTF16("Google Inc.")); | |
| 217 profile.SetInfo(AutofillType(ADDRESS_HOME_LINE1), | |
| 218 ASCIIToUTF16("1122 Mountain View Road")); | |
| 219 profile.SetInfo(AutofillType(ADDRESS_HOME_LINE2), ASCIIToUTF16("Suite #1")); | |
| 220 profile.SetInfo(AutofillType(ADDRESS_HOME_CITY), | |
| 221 ASCIIToUTF16("Mountain View")); | |
| 222 profile.SetInfo(AutofillType(ADDRESS_HOME_STATE), ASCIIToUTF16("CA")); | |
| 223 profile.SetInfo(AutofillType(ADDRESS_HOME_ZIP), ASCIIToUTF16("94111")); | |
| 224 profile.SetInfo(AutofillType(ADDRESS_HOME_COUNTRY), ASCIIToUTF16("USA")); | |
| 225 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), | |
| 226 ASCIIToUTF16("014155552258")); | |
| 227 profile.SetInfo(AutofillType(PHONE_FAX_WHOLE_NUMBER), | |
| 228 ASCIIToUTF16("024087172258")); | |
| 229 profiles().push_back(&profile); | |
| 230 | |
| 231 LoadDialog(); | |
| 232 [controller_ selectAddressAtIndex:0]; | |
| 233 [controller_ editSelection:nil]; | |
| 234 | |
| 235 AutoFillAddressSheetController* sheet = [controller_ addressSheetController]; | |
| 236 ASSERT_TRUE(sheet != nil); | |
| 237 AutoFillAddressModel* am = [sheet addressModel]; | |
| 238 EXPECT_TRUE([[am fullName] isEqualToString:@"John C Smith"]); | |
| 239 EXPECT_TRUE([[am email] isEqualToString:@"john@chromium.org"]); | |
| 240 EXPECT_TRUE([[am companyName] isEqualToString:@"Google Inc."]); | |
| 241 EXPECT_TRUE([[am addressLine1] isEqualToString:@"1122 Mountain View Road"]); | |
| 242 EXPECT_TRUE([[am addressLine2] isEqualToString:@"Suite #1"]); | |
| 243 EXPECT_TRUE([[am addressCity] isEqualToString:@"Mountain View"]); | |
| 244 EXPECT_TRUE([[am addressState] isEqualToString:@"CA"]); | |
| 245 EXPECT_TRUE([[am addressZip] isEqualToString:@"94111"]); | |
| 246 EXPECT_TRUE([[am phoneWholeNumber] isEqualToString:@"014155552258"]); | |
| 247 EXPECT_TRUE([[am faxWholeNumber] isEqualToString:@"024087172258"]); | |
| 248 | |
| 249 [sheet save:nil]; | |
| 250 [controller_ closeDialog]; | |
| 251 | |
| 252 ASSERT_TRUE(observer_.hit_); | |
| 253 ASSERT_TRUE(observer_.profiles_.size() == 1); | |
| 254 | |
| 255 ASSERT_EQ(0, observer_.profiles_[0].Compare(*profiles()[0])); | |
| 256 } | |
| 257 | |
| 258 TEST_F(AutoFillDialogControllerTest, CreditCardDataMutation) { | |
| 259 CreditCard credit_card; | |
| 260 credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("DCH")); | |
| 261 credit_card.SetInfo(AutofillType(CREDIT_CARD_NUMBER), | |
| 262 ASCIIToUTF16("1234 5678 9101 1121")); | |
| 263 credit_card.SetInfo(AutofillType(CREDIT_CARD_EXP_MONTH), ASCIIToUTF16("01")); | |
| 264 credit_card.SetInfo(AutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), | |
| 265 ASCIIToUTF16("2012")); | |
| 266 credit_cards().push_back(&credit_card); | |
| 267 | |
| 268 LoadDialog(); | |
| 269 [controller_ selectCreditCardAtIndex:0]; | |
| 270 [controller_ editSelection:nil]; | |
| 271 | |
| 272 AutoFillCreditCardSheetController* sheet = | |
| 273 [controller_ creditCardSheetController]; | |
| 274 ASSERT_TRUE(sheet != nil); | |
| 275 AutoFillCreditCardModel* cm = [sheet creditCardModel]; | |
| 276 EXPECT_TRUE([[cm nameOnCard] isEqualToString:@"DCH"]); | |
| 277 EXPECT_TRUE([[cm creditCardNumber] isEqualToString:@"1234567891011121"]); | |
| 278 EXPECT_TRUE([[cm expirationMonth] isEqualToString:@"01"]); | |
| 279 EXPECT_TRUE([[cm expirationYear] isEqualToString:@"2012"]); | |
| 280 | |
| 281 // Check that user-visible text is obfuscated. | |
| 282 NSTextField* numberField = [sheet creditCardNumberField]; | |
| 283 ASSERT_TRUE(numberField != nil); | |
| 284 EXPECT_TRUE([[numberField stringValue] isEqualToString:@"************1121"]); | |
| 285 | |
| 286 [sheet save:nil]; | |
| 287 [controller_ closeDialog]; | |
| 288 | |
| 289 ASSERT_TRUE(observer_.hit_); | |
| 290 ASSERT_TRUE(observer_.credit_cards_.size() == 1); | |
| 291 ASSERT_EQ(0, observer_.credit_cards_[0].Compare(*credit_cards()[0])); | |
| 292 } | |
| 293 | |
| 294 TEST_F(AutoFillDialogControllerTest, TwoProfiles) { | |
| 295 AutoFillProfile profile1; | |
| 296 profile1.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Joe")); | |
| 297 profiles().push_back(&profile1); | |
| 298 AutoFillProfile profile2; | |
| 299 profile2.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Bob")); | |
| 300 profiles().push_back(&profile2); | |
| 301 LoadDialog(); | |
| 302 [controller_ closeDialog]; | |
| 303 | |
| 304 // Should not hit our observer. | |
| 305 ASSERT_FALSE(observer_.hit_); | |
| 306 | |
| 307 // Sizes should match. And should be 2. | |
| 308 ASSERT_EQ([controller_ profiles].size(), profiles().size()); | |
| 309 ASSERT_EQ([controller_ profiles].size(), 2UL); | |
| 310 | |
| 311 // Contents should match. With the exception of the |unique_id|. | |
| 312 for (size_t i = 0, count = profiles().size(); i < count; i++) { | |
| 313 ASSERT_EQ(0, [controller_ profiles][i].Compare(*profiles()[i])); | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 TEST_F(AutoFillDialogControllerTest, TwoCreditCards) { | |
| 318 CreditCard credit_card1; | |
| 319 credit_card1.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe")); | |
| 320 credit_cards().push_back(&credit_card1); | |
| 321 CreditCard credit_card2; | |
| 322 credit_card2.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Bob")); | |
| 323 credit_cards().push_back(&credit_card2); | |
| 324 LoadDialog(); | |
| 325 [controller_ closeDialog]; | |
| 326 | |
| 327 // Should not hit our observer. | |
| 328 ASSERT_FALSE(observer_.hit_); | |
| 329 | |
| 330 // Sizes should match. And should be 2. | |
| 331 ASSERT_EQ([controller_ creditCards].size(), credit_cards().size()); | |
| 332 ASSERT_EQ([controller_ creditCards].size(), 2UL); | |
| 333 | |
| 334 // Contents should match. With the exception of the |unique_id|. | |
| 335 for (size_t i = 0, count = credit_cards().size(); i < count; i++) { | |
| 336 ASSERT_EQ(0, [controller_ creditCards][i].Compare(*credit_cards()[i])); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 TEST_F(AutoFillDialogControllerTest, AddNewProfile) { | |
| 341 AutoFillProfile profile; | |
| 342 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Joe")); | |
| 343 profiles().push_back(&profile); | |
| 344 LoadDialog(); | |
| 345 [controller_ addNewAddress:nil]; | |
| 346 AutoFillAddressSheetController* sheet = [controller_ addressSheetController]; | |
| 347 ASSERT_TRUE(sheet != nil); | |
| 348 AutoFillAddressModel* model = [sheet addressModel]; | |
| 349 ASSERT_TRUE(model != nil); | |
| 350 [model setFullName:@"Don"]; | |
| 351 [sheet save:nil]; | |
| 352 [controller_ closeDialog]; | |
| 353 | |
| 354 // Should hit our observer. | |
| 355 ASSERT_TRUE(observer_.hit_); | |
| 356 | |
| 357 // Sizes should be different. New size should be 2. | |
| 358 ASSERT_NE(observer_.profiles_.size(), profiles().size()); | |
| 359 ASSERT_EQ(observer_.profiles_.size(), 2UL); | |
| 360 | |
| 361 // New address should match. Don't compare labels. | |
| 362 AutoFillProfile new_profile; | |
| 363 new_profile.SetInfo(AutofillType(NAME_FULL), ASCIIToUTF16("Don")); | |
| 364 ASSERT_EQ(0, observer_.profiles_[1].Compare(new_profile)); | |
| 365 } | |
| 366 | |
| 367 TEST_F(AutoFillDialogControllerTest, AddNewCreditCard) { | |
| 368 CreditCard credit_card; | |
| 369 credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe")); | |
| 370 credit_cards().push_back(&credit_card); | |
| 371 LoadDialog(); | |
| 372 [controller_ addNewCreditCard:nil]; | |
| 373 AutoFillCreditCardSheetController* sheet = | |
| 374 [controller_ creditCardSheetController]; | |
| 375 ASSERT_TRUE(sheet != nil); | |
| 376 AutoFillCreditCardModel* model = [sheet creditCardModel]; | |
| 377 ASSERT_TRUE(model != nil); | |
| 378 [model setNameOnCard:@"Don"]; | |
| 379 [sheet save:nil]; | |
| 380 [controller_ closeDialog]; | |
| 381 | |
| 382 // Should hit our observer. | |
| 383 ASSERT_TRUE(observer_.hit_); | |
| 384 | |
| 385 // Sizes should be different. New size should be 2. | |
| 386 ASSERT_NE(observer_.credit_cards_.size(), credit_cards().size()); | |
| 387 ASSERT_EQ(observer_.credit_cards_.size(), 2UL); | |
| 388 | |
| 389 // New credit card should match. Don't compare labels. | |
| 390 CreditCard new_credit_card; | |
| 391 new_credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Don")); | |
| 392 ASSERT_EQ(0, observer_.credit_cards_[1].Compare(new_credit_card)); | |
| 393 } | |
| 394 | |
| 395 TEST_F(AutoFillDialogControllerTest, AddNewEmptyProfile) { | |
| 396 AutoFillProfile profile; | |
| 397 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Joe")); | |
| 398 profiles().push_back(&profile); | |
| 399 LoadDialog(); | |
| 400 [controller_ addNewAddress:nil]; | |
| 401 AutoFillAddressSheetController* sheet = [controller_ addressSheetController]; | |
| 402 ASSERT_TRUE(sheet != nil); | |
| 403 [sheet save:nil]; | |
| 404 [controller_ closeDialog]; | |
| 405 | |
| 406 // Should not hit our observer. | |
| 407 ASSERT_FALSE(observer_.hit_); | |
| 408 | |
| 409 // Empty profile should not be saved. | |
| 410 ASSERT_EQ(0UL, observer_.profiles_.size()); | |
| 411 } | |
| 412 | |
| 413 TEST_F(AutoFillDialogControllerTest, AddNewEmptyCreditCard) { | |
| 414 CreditCard credit_card; | |
| 415 credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe")); | |
| 416 credit_cards().push_back(&credit_card); | |
| 417 LoadDialog(); | |
| 418 [controller_ addNewCreditCard:nil]; | |
| 419 AutoFillCreditCardSheetController* sheet = | |
| 420 [controller_ creditCardSheetController]; | |
| 421 ASSERT_TRUE(sheet != nil); | |
| 422 [sheet save:nil]; | |
| 423 [controller_ closeDialog]; | |
| 424 | |
| 425 // Should hit our observer. | |
| 426 ASSERT_FALSE(observer_.hit_); | |
| 427 | |
| 428 // Empty credit card should not be saved. | |
| 429 ASSERT_EQ(0UL, observer_.credit_cards_.size()); | |
| 430 } | |
| 431 | |
| 432 TEST_F(AutoFillDialogControllerTest, DeleteProfile) { | |
| 433 AutoFillProfile profile; | |
| 434 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Joe")); | |
| 435 profiles().push_back(&profile); | |
| 436 LoadDialog(); | |
| 437 [controller_ selectAddressAtIndex:0]; | |
| 438 [controller_ deleteSelection:nil]; | |
| 439 [controller_ closeDialog]; | |
| 440 | |
| 441 // Should hit our observer. | |
| 442 ASSERT_TRUE(observer_.hit_); | |
| 443 | |
| 444 // Sizes should be different. New size should be 0. | |
| 445 ASSERT_NE(observer_.profiles_.size(), profiles().size()); | |
| 446 ASSERT_EQ(observer_.profiles_.size(), 0UL); | |
| 447 } | |
| 448 | |
| 449 TEST_F(AutoFillDialogControllerTest, DeleteCreditCard) { | |
| 450 CreditCard credit_card; | |
| 451 credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe")); | |
| 452 credit_cards().push_back(&credit_card); | |
| 453 LoadDialog(); | |
| 454 [controller_ selectCreditCardAtIndex:0]; | |
| 455 [controller_ deleteSelection:nil]; | |
| 456 [controller_ closeDialog]; | |
| 457 | |
| 458 // Should hit our observer. | |
| 459 ASSERT_TRUE(observer_.hit_); | |
| 460 | |
| 461 // Sizes should be different. New size should be 0. | |
| 462 ASSERT_NE(observer_.credit_cards_.size(), credit_cards().size()); | |
| 463 ASSERT_EQ(observer_.credit_cards_.size(), 0UL); | |
| 464 } | |
| 465 | |
| 466 TEST_F(AutoFillDialogControllerTest, TwoProfilesDeleteOne) { | |
| 467 AutoFillProfile profile; | |
| 468 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Joe")); | |
| 469 profiles().push_back(&profile); | |
| 470 AutoFillProfile profile2; | |
| 471 profile2.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Bob")); | |
| 472 profiles().push_back(&profile2); | |
| 473 LoadDialog(); | |
| 474 [controller_ selectAddressAtIndex:1]; | |
| 475 [controller_ deleteSelection:nil]; | |
| 476 [controller_ closeDialog]; | |
| 477 | |
| 478 // Should hit our observer. | |
| 479 ASSERT_TRUE(observer_.hit_); | |
| 480 | |
| 481 // Sizes should be different. New size should be 1. | |
| 482 ASSERT_NE(observer_.profiles_.size(), profiles().size()); | |
| 483 ASSERT_EQ(observer_.profiles_.size(), 1UL); | |
| 484 ASSERT_EQ(0, observer_.profiles_[0].Compare(profile)); | |
| 485 } | |
| 486 | |
| 487 TEST_F(AutoFillDialogControllerTest, TwoCreditCardsDeleteOne) { | |
| 488 CreditCard credit_card; | |
| 489 credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe")); | |
| 490 credit_cards().push_back(&credit_card); | |
| 491 CreditCard credit_card2; | |
| 492 credit_card2.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Bob")); | |
| 493 credit_cards().push_back(&credit_card2); | |
| 494 LoadDialog(); | |
| 495 [controller_ selectCreditCardAtIndex:1]; | |
| 496 [controller_ deleteSelection:nil]; | |
| 497 [controller_ closeDialog]; | |
| 498 | |
| 499 // Should hit our observer. | |
| 500 ASSERT_TRUE(observer_.hit_); | |
| 501 | |
| 502 // Sizes should be different. New size should be 1. | |
| 503 ASSERT_NE(observer_.credit_cards_.size(), credit_cards().size()); | |
| 504 ASSERT_EQ(observer_.credit_cards_.size(), 1UL); | |
| 505 | |
| 506 // First credit card should match. | |
| 507 ASSERT_EQ(0, observer_.credit_cards_[0].Compare(credit_card)); | |
| 508 } | |
| 509 | |
| 510 TEST_F(AutoFillDialogControllerTest, DeleteMultiple) { | |
| 511 AutoFillProfile profile; | |
| 512 profile.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Joe")); | |
| 513 profiles().push_back(&profile); | |
| 514 AutoFillProfile profile2; | |
| 515 profile2.SetInfo(AutofillType(NAME_FIRST), ASCIIToUTF16("Bob")); | |
| 516 profiles().push_back(&profile2); | |
| 517 | |
| 518 CreditCard credit_card; | |
| 519 credit_card.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Joe")); | |
| 520 credit_cards().push_back(&credit_card); | |
| 521 CreditCard credit_card2; | |
| 522 credit_card2.SetInfo(AutofillType(CREDIT_CARD_NAME), ASCIIToUTF16("Bob")); | |
| 523 credit_cards().push_back(&credit_card2); | |
| 524 | |
| 525 LoadDialog(); | |
| 526 [controller_ selectAddressAtIndex:1]; | |
| 527 [controller_ addSelectedCreditCardAtIndex:0]; | |
| 528 ASSERT_FALSE([controller_ editButtonEnabled]); | |
| 529 [controller_ deleteSelection:nil]; | |
| 530 [controller_ selectAddressAtIndex:0]; | |
| 531 ASSERT_TRUE([controller_ editButtonEnabled]); | |
| 532 [controller_ closeDialog]; | |
| 533 | |
| 534 // Should hit our observer. | |
| 535 ASSERT_TRUE(observer_.hit_); | |
| 536 | |
| 537 // Sizes should be different. New size should be 1. | |
| 538 ASSERT_NE(observer_.profiles_.size(), profiles().size()); | |
| 539 ASSERT_EQ(observer_.profiles_.size(), 1UL); | |
| 540 | |
| 541 // Sizes should be different. New size should be 1. | |
| 542 ASSERT_NE(observer_.credit_cards_.size(), credit_cards().size()); | |
| 543 ASSERT_EQ(observer_.credit_cards_.size(), 1UL); | |
| 544 | |
| 545 // Profiles should match. | |
| 546 ASSERT_EQ(0, observer_.profiles_[0].Compare(profile)); | |
| 547 | |
| 548 // Second credit card should match. | |
| 549 ASSERT_EQ(0, observer_.credit_cards_[0].Compare(credit_card2)); | |
| 550 } | |
| 551 | |
| 552 // Auxilliary profiles are enabled by default. | |
| 553 TEST_F(AutoFillDialogControllerTest, AuxiliaryProfilesTrue) { | |
| 554 LoadDialog(); | |
| 555 [controller_ closeDialog]; | |
| 556 | |
| 557 // Should not hit our observer. | |
| 558 ASSERT_FALSE(observer_.hit_); | |
| 559 | |
| 560 // Auxiliary profiles setting should be unchanged. | |
| 561 ASSERT_TRUE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 562 prefs::kAutoFillAuxiliaryProfilesEnabled)); | |
| 563 } | |
| 564 | |
| 565 TEST_F(AutoFillDialogControllerTest, AuxiliaryProfilesFalse) { | |
| 566 helper_.profile()->GetPrefs()->SetBoolean( | |
| 567 prefs::kAutoFillAuxiliaryProfilesEnabled, false); | |
| 568 LoadDialog(); | |
| 569 [controller_ closeDialog]; | |
| 570 | |
| 571 // Should not hit our observer. | |
| 572 ASSERT_FALSE(observer_.hit_); | |
| 573 | |
| 574 // Auxiliary profiles setting should be unchanged. | |
| 575 ASSERT_FALSE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 576 prefs::kAutoFillAuxiliaryProfilesEnabled)); | |
| 577 } | |
| 578 | |
| 579 TEST_F(AutoFillDialogControllerTest, AuxiliaryProfilesChanged) { | |
| 580 helper_.profile()->GetPrefs()->SetBoolean( | |
| 581 prefs::kAutoFillAuxiliaryProfilesEnabled, false); | |
| 582 LoadDialog(); | |
| 583 [controller_ setAuxiliaryEnabled:YES]; | |
| 584 [controller_ closeDialog]; | |
| 585 | |
| 586 // Should not hit our observer. | |
| 587 ASSERT_FALSE(observer_.hit_); | |
| 588 | |
| 589 // Auxiliary profiles setting should be unchanged. | |
| 590 ASSERT_TRUE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 591 prefs::kAutoFillAuxiliaryProfilesEnabled)); | |
| 592 } | |
| 593 | |
| 594 TEST_F(AutoFillDialogControllerTest, WaitForDataToLoad) { | |
| 595 AutoFillProfile profile; | |
| 596 profiles().push_back(&profile); | |
| 597 CreditCard credit_card; | |
| 598 credit_cards().push_back(&credit_card); | |
| 599 helper_.test_profile_->test_manager_->test_data_is_loaded_ = false; | |
| 600 LoadDialog(); | |
| 601 [controller_ closeDialog]; | |
| 602 | |
| 603 // Should not hit our observer. | |
| 604 ASSERT_FALSE(observer_.hit_); | |
| 605 | |
| 606 // Sizes should match. | |
| 607 ASSERT_EQ([controller_ profiles].size(), profiles().size()); | |
| 608 ASSERT_EQ([controller_ creditCards].size(), credit_cards().size()); | |
| 609 | |
| 610 // Contents should match. | |
| 611 size_t i = 0; | |
| 612 size_t count = profiles().size(); | |
| 613 for (i = 0; i < count; i++) { | |
| 614 // Do not compare labels. Label is a derived field. | |
| 615 ASSERT_EQ([controller_ profiles][i], *profiles()[i]); | |
| 616 } | |
| 617 count = credit_cards().size(); | |
| 618 for (i = 0; i < count; i++) { | |
| 619 ASSERT_EQ([controller_ creditCards][i], *credit_cards()[i]); | |
| 620 } | |
| 621 } | |
| 622 | |
| 623 // AutoFill is enabled by default. | |
| 624 TEST_F(AutoFillDialogControllerTest, AutoFillEnabledTrue) { | |
| 625 LoadDialog(); | |
| 626 [controller_ closeDialog]; | |
| 627 | |
| 628 // Should not hit our observer. | |
| 629 ASSERT_FALSE(observer_.hit_); | |
| 630 | |
| 631 // AutoFill enabled setting should be unchanged. | |
| 632 ASSERT_TRUE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 633 prefs::kAutoFillEnabled)); | |
| 634 } | |
| 635 | |
| 636 TEST_F(AutoFillDialogControllerTest, AutoFillEnabledFalse) { | |
| 637 helper_.profile()->GetPrefs()->SetBoolean(prefs::kAutoFillEnabled, false); | |
| 638 LoadDialog(); | |
| 639 [controller_ closeDialog]; | |
| 640 | |
| 641 // Should not hit our observer. | |
| 642 ASSERT_FALSE(observer_.hit_); | |
| 643 | |
| 644 // AutoFill enabled setting should be unchanged. | |
| 645 ASSERT_FALSE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 646 prefs::kAutoFillEnabled)); | |
| 647 } | |
| 648 | |
| 649 TEST_F(AutoFillDialogControllerTest, AutoFillEnabledChanged) { | |
| 650 helper_.profile()->GetPrefs()->SetBoolean( | |
| 651 prefs::kAutoFillAuxiliaryProfilesEnabled, false); | |
| 652 LoadDialog(); | |
| 653 [controller_ setAutoFillEnabled:YES]; | |
| 654 [controller_ closeDialog]; | |
| 655 | |
| 656 // Should not hit our observer. | |
| 657 ASSERT_FALSE(observer_.hit_); | |
| 658 | |
| 659 // Auxiliary profiles setting should be unchanged. | |
| 660 ASSERT_TRUE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 661 prefs::kAutoFillEnabled)); | |
| 662 } | |
| 663 | |
| 664 TEST_F(AutoFillDialogControllerTest, PolicyRefresh) { | |
| 665 LoadDialog(); | |
| 666 ASSERT_FALSE([controller_ autoFillManaged]); | |
| 667 | |
| 668 // Disable AutoFill through configuration policy. | |
| 669 helper_.profile()->GetTestingPrefService()->SetManagedPref( | |
| 670 prefs::kAutoFillEnabled, Value::CreateBooleanValue(false)); | |
| 671 ASSERT_TRUE([controller_ autoFillManaged]); | |
| 672 ASSERT_FALSE([controller_ autoFillEnabled]); | |
| 673 | |
| 674 // Enabling through policy should switch to enabled but not editable. | |
| 675 helper_.profile()->GetTestingPrefService()->SetManagedPref( | |
| 676 prefs::kAutoFillEnabled, Value::CreateBooleanValue(true)); | |
| 677 ASSERT_TRUE([controller_ autoFillManaged]); | |
| 678 ASSERT_TRUE([controller_ autoFillEnabled]); | |
| 679 | |
| 680 [controller_ closeDialog]; | |
| 681 } | |
| 682 | |
| 683 TEST_F(AutoFillDialogControllerTest, PolicyDisabledAndSave) { | |
| 684 // Disable AutoFill through configuration policy. | |
| 685 helper_.profile()->GetTestingPrefService()->SetManagedPref( | |
| 686 prefs::kAutoFillEnabled, Value::CreateBooleanValue(false)); | |
| 687 helper_.profile()->GetPrefs()->SetBoolean( | |
| 688 prefs::kAutoFillAuxiliaryProfilesEnabled, false); | |
| 689 LoadDialog(); | |
| 690 | |
| 691 // Save should be disabled. | |
| 692 ASSERT_TRUE([controller_ autoFillManagedAndDisabled]); | |
| 693 | |
| 694 [controller_ setAuxiliaryEnabled:YES]; | |
| 695 [controller_ closeDialog]; | |
| 696 | |
| 697 // Observer should not have been called. | |
| 698 ASSERT_FALSE(observer_.hit_); | |
| 699 | |
| 700 // Auxiliary profiles setting should be unchanged. | |
| 701 ASSERT_FALSE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 702 prefs::kAutoFillAuxiliaryProfilesEnabled)); | |
| 703 } | |
| 704 | |
| 705 TEST_F(AutoFillDialogControllerTest, PolicyEnabledAndSave) { | |
| 706 // Enable AutoFill through configuration policy. | |
| 707 helper_.profile()->GetTestingPrefService()->SetManagedPref( | |
| 708 prefs::kAutoFillEnabled, Value::CreateBooleanValue(true)); | |
| 709 helper_.profile()->GetPrefs()->SetBoolean( | |
| 710 prefs::kAutoFillAuxiliaryProfilesEnabled, false); | |
| 711 LoadDialog(); | |
| 712 | |
| 713 // Save should be enabled. | |
| 714 ASSERT_FALSE([controller_ autoFillManagedAndDisabled]); | |
| 715 | |
| 716 [controller_ setAuxiliaryEnabled:YES]; | |
| 717 [controller_ closeDialog]; | |
| 718 | |
| 719 // Observer should not have been notified. | |
| 720 ASSERT_FALSE(observer_.hit_); | |
| 721 | |
| 722 // Auxiliary profiles setting should have been saved. | |
| 723 ASSERT_TRUE(helper_.profile()->GetPrefs()->GetBoolean( | |
| 724 prefs::kAutoFillAuxiliaryProfilesEnabled)); | |
| 725 } | |
| 726 | |
| 727 } // namespace | |
| OLD | NEW |