OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #import "ios/chrome/browser/ui/settings/autofill_collection_view_controller.h" |
| 6 |
| 7 #include "base/guid.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #import "base/test/ios/wait_util.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 #include "ios/chrome/browser/autofill/personal_data_manager_factory.h" |
| 15 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 17 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 @interface SettingsRootCollectionViewController (ExposedForTesting) |
| 21 - (void)editButtonPressed; |
| 22 @end |
| 23 |
| 24 namespace { |
| 25 |
| 26 class AutofillCollectionViewControllerTest |
| 27 : public CollectionViewControllerTest { |
| 28 protected: |
| 29 void SetUp() override { |
| 30 CollectionViewControllerTest::SetUp(); |
| 31 TestChromeBrowserState::Builder test_cbs_builder; |
| 32 chrome_browser_state_ = test_cbs_builder.Build(); |
| 33 // Profile import requires a PersonalDataManager which itself needs the |
| 34 // WebDataService; this is not initialized on a TestChromeBrowserState by |
| 35 // default. |
| 36 chrome_browser_state_->CreateWebDataService(); |
| 37 } |
| 38 |
| 39 CollectionViewController* NewController() override NS_RETURNS_RETAINED { |
| 40 return [[AutofillCollectionViewController alloc] |
| 41 initWithBrowserState:chrome_browser_state_.get()]; |
| 42 } |
| 43 |
| 44 void AddProfile(const std::string& origin, |
| 45 const std::string& name, |
| 46 const std::string& address) { |
| 47 autofill::PersonalDataManager* personalDataManager = |
| 48 autofill::PersonalDataManagerFactory::GetForBrowserState( |
| 49 chrome_browser_state_.get()); |
| 50 autofill::AutofillProfile autofillProfile(base::GenerateGUID(), origin); |
| 51 autofillProfile.SetRawInfo(autofill::NAME_FULL, base::ASCIIToUTF16(name)); |
| 52 autofillProfile.SetRawInfo(autofill::ADDRESS_HOME_LINE1, |
| 53 base::ASCIIToUTF16(address)); |
| 54 personalDataManager->SaveImportedProfile(autofillProfile); |
| 55 } |
| 56 |
| 57 web::TestWebThreadBundle thread_bundle_; |
| 58 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; |
| 59 }; |
| 60 |
| 61 // Default test case of no addresses or credit cards. |
| 62 TEST_F(AutofillCollectionViewControllerTest, TestInitialization) { |
| 63 CreateController(); |
| 64 CheckController(); |
| 65 |
| 66 // Expect one header section. |
| 67 EXPECT_EQ(1, NumberOfSections()); |
| 68 // Expect header section to contain two rows. |
| 69 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 70 } |
| 71 |
| 72 // Adding a single address results in an address section. |
| 73 TEST_F(AutofillCollectionViewControllerTest, TestOneProfile) { |
| 74 AddProfile("https://www.example.com/", "John Doe", "1 Main Street"); |
| 75 CreateController(); |
| 76 // Expect two sections (header and addresses section). |
| 77 EXPECT_EQ(2, NumberOfSections()); |
| 78 // Expect header section to contain two rows. |
| 79 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 80 // Expect address section to contain 1 row (the address itself). |
| 81 EXPECT_EQ(1, NumberOfItemsInSection(1)); |
| 82 } |
| 83 |
| 84 // Adding a single credit card results in a credit card section. |
| 85 TEST_F(AutofillCollectionViewControllerTest, TestOneCreditCard) { |
| 86 autofill::PersonalDataManager* personalDataManager = |
| 87 autofill::PersonalDataManagerFactory::GetForBrowserState( |
| 88 chrome_browser_state_.get()); |
| 89 autofill::CreditCard creditCard(base::GenerateGUID(), |
| 90 "https://www.example.com/"); |
| 91 creditCard.SetRawInfo(autofill::CREDIT_CARD_NAME_FULL, |
| 92 base::ASCIIToUTF16("Alan Smithee")); |
| 93 creditCard.SetRawInfo(autofill::CREDIT_CARD_NUMBER, |
| 94 base::ASCIIToUTF16("378282246310005")); |
| 95 personalDataManager->SaveImportedCreditCard(creditCard); |
| 96 CreateController(); |
| 97 // Expect two sections (header and credit card section). |
| 98 EXPECT_EQ(2, NumberOfSections()); |
| 99 // Expect header section to contain two rows. |
| 100 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 101 // Expect credit card section to contain 1 row (the credit card itself). |
| 102 EXPECT_EQ(1, NumberOfItemsInSection(1)); |
| 103 } |
| 104 |
| 105 // Deleting the only profile results in item deletion and section deletion. |
| 106 TEST_F(AutofillCollectionViewControllerTest, TestOneProfileItemDeleted) { |
| 107 AddProfile("https://www.example.com/", "John Doe", "1 Main Street"); |
| 108 CreateController(); |
| 109 // Expect two sections (header and addresses section). |
| 110 EXPECT_EQ(2, NumberOfSections()); |
| 111 // Expect header section to contain two rows. |
| 112 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 113 // Expect address section to contain 1 row (the address itself). |
| 114 EXPECT_EQ(1, NumberOfItemsInSection(1)); |
| 115 |
| 116 AutofillCollectionViewController* view_controller = |
| 117 base::mac::ObjCCastStrict<AutofillCollectionViewController>(controller()); |
| 118 // Put the collectionView in 'edit' mode. |
| 119 [view_controller editButtonPressed]; |
| 120 // This is a bit of a shortcut, since actually clicking on the 'delete' |
| 121 // button would be tough. |
| 122 void (^delete_item_with_wait)(int, int) = ^(int i, int j) { |
| 123 __block BOOL completion_called = NO; |
| 124 this->DeleteItem(i, j, ^{ |
| 125 completion_called = YES; |
| 126 }); |
| 127 base::test::ios::WaitUntilCondition(^bool() { |
| 128 return completion_called; |
| 129 }); |
| 130 }; |
| 131 |
| 132 delete_item_with_wait(1, 0); |
| 133 // Exit 'edit' mode. |
| 134 [view_controller editButtonPressed]; |
| 135 |
| 136 // Verify the resulting UI. |
| 137 EXPECT_EQ(1, NumberOfSections()); |
| 138 EXPECT_EQ(2, NumberOfItemsInSection(0)); |
| 139 } |
| 140 |
| 141 } // namespace |
OLD | NEW |