Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: ios/chrome/browser/ui/settings/autofill_collection_view_controller_unittest.mm

Issue 2592173002: Fix ios_chrome_unittests to crash on DCHECK failure. (Closed)
Patch Set: Address comments Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/chrome/browser/ui/settings/autofill_collection_view_controller.h" 5 #import "ios/chrome/browser/ui/settings/autofill_collection_view_controller.h"
6 6
7 #include "base/guid.h" 7 #include "base/guid.h"
8 #include "base/mac/foundation_util.h" 8 #include "base/mac/foundation_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #import "base/test/ios/wait_util.h" 10 #import "base/test/ios/wait_util.h"
11 #include "components/autofill/core/browser/autofill_profile.h" 11 #include "components/autofill/core/browser/autofill_profile.h"
12 #include "components/autofill/core/browser/credit_card.h" 12 #include "components/autofill/core/browser/credit_card.h"
13 #include "components/autofill/core/browser/personal_data_manager.h" 13 #include "components/autofill/core/browser/personal_data_manager.h"
14 #include "ios/chrome/browser/autofill/personal_data_manager_factory.h" 14 #include "ios/chrome/browser/autofill/personal_data_manager_factory.h"
15 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.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 " 16 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h "
17 #include "ios/chrome/browser/ui/settings/personal_data_manager_data_changed_obse rver.h"
17 #include "ios/web/public/test/test_web_thread_bundle.h" 18 #include "ios/web/public/test/test_web_thread_bundle.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 @interface SettingsRootCollectionViewController (ExposedForTesting) 21 @interface SettingsRootCollectionViewController (ExposedForTesting)
21 - (void)editButtonPressed; 22 - (void)editButtonPressed;
22 @end 23 @end
23 24
24 namespace { 25 namespace {
25 26
26 class AutofillCollectionViewControllerTest 27 class AutofillCollectionViewControllerTest
27 : public CollectionViewControllerTest { 28 : public CollectionViewControllerTest {
28 protected: 29 protected:
29 void SetUp() override { 30 AutofillCollectionViewControllerTest() {
30 CollectionViewControllerTest::SetUp();
31 TestChromeBrowserState::Builder test_cbs_builder; 31 TestChromeBrowserState::Builder test_cbs_builder;
32 chrome_browser_state_ = test_cbs_builder.Build(); 32 chrome_browser_state_ = test_cbs_builder.Build();
33 // Profile import requires a PersonalDataManager which itself needs the 33 // Profile import requires a PersonalDataManager which itself needs the
34 // WebDataService; this is not initialized on a TestChromeBrowserState by 34 // WebDataService; this is not initialized on a TestChromeBrowserState by
35 // default. 35 // default.
36 chrome_browser_state_->CreateWebDataService(); 36 chrome_browser_state_->CreateWebDataService();
37 } 37 }
38 38
39 CollectionViewController* NewController() override NS_RETURNS_RETAINED { 39 CollectionViewController* NewController() override NS_RETURNS_RETAINED {
40 return [[AutofillCollectionViewController alloc] 40 return [[AutofillCollectionViewController alloc]
41 initWithBrowserState:chrome_browser_state_.get()]; 41 initWithBrowserState:chrome_browser_state_.get()];
42 } 42 }
43 43
44 void AddProfile(const std::string& origin, 44 void AddProfile(const std::string& origin,
45 const std::string& name, 45 const std::string& name,
46 const std::string& address) { 46 const std::string& address) {
47 autofill::PersonalDataManager* personalDataManager = 47 autofill::PersonalDataManager* personal_data_manager =
48 autofill::PersonalDataManagerFactory::GetForBrowserState( 48 autofill::PersonalDataManagerFactory::GetForBrowserState(
49 chrome_browser_state_.get()); 49 chrome_browser_state_.get());
50 autofill::AutofillProfile autofillProfile(base::GenerateGUID(), origin); 50 PersonalDataManagerDataChangedObserver observer(personal_data_manager);
51 autofillProfile.SetRawInfo(autofill::NAME_FULL, base::ASCIIToUTF16(name)); 51
52 autofillProfile.SetRawInfo(autofill::ADDRESS_HOME_LINE1, 52 autofill::AutofillProfile autofill_profile(base::GenerateGUID(), origin);
53 base::ASCIIToUTF16(address)); 53 autofill_profile.SetRawInfo(autofill::NAME_FULL, base::ASCIIToUTF16(name));
54 personalDataManager->SaveImportedProfile(autofillProfile); 54 autofill_profile.SetRawInfo(autofill::ADDRESS_HOME_LINE1,
55 base::ASCIIToUTF16(address));
56 personal_data_manager->SaveImportedProfile(autofill_profile);
57 observer.Wait(); // Wait for completion of the asynchronous operation.
55 } 58 }
56 59
57 web::TestWebThreadBundle thread_bundle_; 60 web::TestWebThreadBundle thread_bundle_;
58 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; 61 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
59 }; 62 };
60 63
61 // Default test case of no addresses or credit cards. 64 // Default test case of no addresses or credit cards.
62 TEST_F(AutofillCollectionViewControllerTest, TestInitialization) { 65 TEST_F(AutofillCollectionViewControllerTest, TestInitialization) {
63 CreateController(); 66 CreateController();
64 CheckController(); 67 CheckController();
(...skipping 11 matching lines...) Expand all
76 // Expect two sections (header and addresses section). 79 // Expect two sections (header and addresses section).
77 EXPECT_EQ(2, NumberOfSections()); 80 EXPECT_EQ(2, NumberOfSections());
78 // Expect header section to contain two rows. 81 // Expect header section to contain two rows.
79 EXPECT_EQ(2, NumberOfItemsInSection(0)); 82 EXPECT_EQ(2, NumberOfItemsInSection(0));
80 // Expect address section to contain 1 row (the address itself). 83 // Expect address section to contain 1 row (the address itself).
81 EXPECT_EQ(1, NumberOfItemsInSection(1)); 84 EXPECT_EQ(1, NumberOfItemsInSection(1));
82 } 85 }
83 86
84 // Adding a single credit card results in a credit card section. 87 // Adding a single credit card results in a credit card section.
85 TEST_F(AutofillCollectionViewControllerTest, TestOneCreditCard) { 88 TEST_F(AutofillCollectionViewControllerTest, TestOneCreditCard) {
86 autofill::PersonalDataManager* personalDataManager = 89 autofill::PersonalDataManager* personal_data_manager =
87 autofill::PersonalDataManagerFactory::GetForBrowserState( 90 autofill::PersonalDataManagerFactory::GetForBrowserState(
88 chrome_browser_state_.get()); 91 chrome_browser_state_.get());
89 autofill::CreditCard creditCard(base::GenerateGUID(), 92 PersonalDataManagerDataChangedObserver observer(personal_data_manager);
90 "https://www.example.com/"); 93
91 creditCard.SetRawInfo(autofill::CREDIT_CARD_NAME_FULL, 94 autofill::CreditCard credit_card(base::GenerateGUID(),
92 base::ASCIIToUTF16("Alan Smithee")); 95 "https://www.example.com/");
93 creditCard.SetRawInfo(autofill::CREDIT_CARD_NUMBER, 96 credit_card.SetRawInfo(autofill::CREDIT_CARD_NAME_FULL,
94 base::ASCIIToUTF16("378282246310005")); 97 base::ASCIIToUTF16("Alan Smithee"));
95 personalDataManager->SaveImportedCreditCard(creditCard); 98 credit_card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
99 base::ASCIIToUTF16("378282246310005"));
100 personal_data_manager->SaveImportedCreditCard(credit_card);
101 observer.Wait(); // Wait for completion of the asynchronous operation.
102
96 CreateController(); 103 CreateController();
97 // Expect two sections (header and credit card section). 104 // Expect two sections (header and credit card section).
98 EXPECT_EQ(2, NumberOfSections()); 105 EXPECT_EQ(2, NumberOfSections());
99 // Expect header section to contain two rows. 106 // Expect header section to contain two rows.
100 EXPECT_EQ(2, NumberOfItemsInSection(0)); 107 EXPECT_EQ(2, NumberOfItemsInSection(0));
101 // Expect credit card section to contain 1 row (the credit card itself). 108 // Expect credit card section to contain 1 row (the credit card itself).
102 EXPECT_EQ(1, NumberOfItemsInSection(1)); 109 EXPECT_EQ(1, NumberOfItemsInSection(1));
103 } 110 }
104 111
105 // Deleting the only profile results in item deletion and section deletion. 112 // Deleting the only profile results in item deletion and section deletion.
106 TEST_F(AutofillCollectionViewControllerTest, TestOneProfileItemDeleted) { 113 TEST_F(AutofillCollectionViewControllerTest, TestOneProfileItemDeleted) {
107 AddProfile("https://www.example.com/", "John Doe", "1 Main Street"); 114 AddProfile("https://www.example.com/", "John Doe", "1 Main Street");
108 CreateController(); 115 CreateController();
109 // Expect two sections (header and addresses section). 116 // Expect two sections (header and addresses section).
110 EXPECT_EQ(2, NumberOfSections()); 117 EXPECT_EQ(2, NumberOfSections());
111 // Expect header section to contain two rows. 118 // Expect header section to contain two rows.
112 EXPECT_EQ(2, NumberOfItemsInSection(0)); 119 EXPECT_EQ(2, NumberOfItemsInSection(0));
113 // Expect address section to contain 1 row (the address itself). 120 // Expect address section to contain 1 row (the address itself).
114 EXPECT_EQ(1, NumberOfItemsInSection(1)); 121 EXPECT_EQ(1, NumberOfItemsInSection(1));
115 122
116 AutofillCollectionViewController* view_controller = 123 AutofillCollectionViewController* view_controller =
117 base::mac::ObjCCastStrict<AutofillCollectionViewController>(controller()); 124 base::mac::ObjCCastStrict<AutofillCollectionViewController>(controller());
118 // Put the collectionView in 'edit' mode. 125 // Put the collectionView in 'edit' mode.
119 [view_controller editButtonPressed]; 126 [view_controller editButtonPressed];
127
120 // This is a bit of a shortcut, since actually clicking on the 'delete' 128 // This is a bit of a shortcut, since actually clicking on the 'delete'
121 // button would be tough. 129 // button would be tough.
122 void (^delete_item_with_wait)(int, int) = ^(int i, int j) { 130 void (^delete_item_with_wait)(int, int) = ^(int i, int j) {
123 __block BOOL completion_called = NO; 131 __block BOOL completion_called = NO;
124 this->DeleteItem(i, j, ^{ 132 this->DeleteItem(i, j, ^{
125 completion_called = YES; 133 completion_called = YES;
126 }); 134 });
127 base::test::ios::WaitUntilCondition(^bool() { 135 base::test::ios::WaitUntilCondition(^bool() {
128 return completion_called; 136 return completion_called;
129 }); 137 });
130 }; 138 };
131 139
140 autofill::PersonalDataManager* personal_data_manager =
141 autofill::PersonalDataManagerFactory::GetForBrowserState(
142 chrome_browser_state_.get());
143 PersonalDataManagerDataChangedObserver observer(personal_data_manager);
144
145 // This call cause a modification of the PersonalDataManager, so wait until
146 // the asynchronous task complete in addition to waiting for the UI update.
132 delete_item_with_wait(1, 0); 147 delete_item_with_wait(1, 0);
148 observer.Wait(); // Wait for completion of the asynchronous operation.
149
133 // Exit 'edit' mode. 150 // Exit 'edit' mode.
134 [view_controller editButtonPressed]; 151 [view_controller editButtonPressed];
135 152
136 // Verify the resulting UI. 153 // Verify the resulting UI.
137 EXPECT_EQ(1, NumberOfSections()); 154 EXPECT_EQ(1, NumberOfSections());
138 EXPECT_EQ(2, NumberOfItemsInSection(0)); 155 EXPECT_EQ(2, NumberOfItemsInSection(0));
139 } 156 }
140 157
141 } // namespace 158 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698