OLD | NEW |
| (Empty) |
1 // Copyright 2016 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/payments/shipping_address_selection_view_controller.
h" | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "components/autofill/core/browser/autofill_profile.h" | |
10 #include "components/autofill/core/browser/autofill_test_utils.h" | |
11 #include "components/autofill/core/browser/test_personal_data_manager.h" | |
12 #include "components/strings/grit/components_strings.h" | |
13 #import "ios/chrome/browser/payments/cells/autofill_profile_item.h" | |
14 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | |
15 #include "ios/chrome/browser/payments/payment_request.h" | |
16 #include "ios/chrome/browser/payments/payment_request_test_util.h" | |
17 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" | |
18 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" | |
19 #include "ios/web/public/payments/payment_request.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
23 #error "This file requires ARC support." | |
24 #endif | |
25 | |
26 class PaymentRequestShippingAddressSelectionViewControllerTest | |
27 : public CollectionViewControllerTest { | |
28 protected: | |
29 PaymentRequestShippingAddressSelectionViewControllerTest() | |
30 : autofill_profile1_(autofill::test::GetFullProfile()), | |
31 autofill_profile2_(autofill::test::GetFullProfile2()) { | |
32 // Add testing profiles to autofill::TestPersonalDataManager. | |
33 personal_data_manager_.AddTestingProfile(&autofill_profile1_); | |
34 personal_data_manager_.AddTestingProfile(&autofill_profile2_); | |
35 } | |
36 | |
37 CollectionViewController* InstantiateController() override { | |
38 payment_request_ = base::MakeUnique<PaymentRequest>( | |
39 payment_request_test_util::CreateTestWebPaymentRequest(), | |
40 &personal_data_manager_); | |
41 | |
42 return [[ShippingAddressSelectionViewController alloc] | |
43 initWithPaymentRequest:payment_request_.get()]; | |
44 } | |
45 | |
46 ShippingAddressSelectionViewController* | |
47 GetShippingAddressSelectionViewController() { | |
48 return base::mac::ObjCCastStrict<ShippingAddressSelectionViewController>( | |
49 controller()); | |
50 } | |
51 | |
52 autofill::AutofillProfile autofill_profile1_; | |
53 autofill::AutofillProfile autofill_profile2_; | |
54 autofill::TestPersonalDataManager personal_data_manager_; | |
55 std::unique_ptr<PaymentRequest> payment_request_; | |
56 }; | |
57 | |
58 // Tests that the correct number of items are displayed after loading the model | |
59 // and that the correct item appears to be selected. | |
60 TEST_F(PaymentRequestShippingAddressSelectionViewControllerTest, TestModel) { | |
61 CreateController(); | |
62 CheckController(); | |
63 CheckTitleWithId(IDS_PAYMENTS_SHIPPING_ADDRESS_LABEL); | |
64 | |
65 [GetShippingAddressSelectionViewController() loadModel]; | |
66 | |
67 ASSERT_EQ(1, NumberOfSections()); | |
68 // One item for each of shipping addresses plus 2 for the message and the add | |
69 // button. | |
70 ASSERT_EQ(4U, static_cast<unsigned int>(NumberOfItemsInSection(0))); | |
71 | |
72 // The first item should be of type TextAndMessageItem. | |
73 id item = GetCollectionViewItem(0, 0); | |
74 EXPECT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]); | |
75 | |
76 // The next two items should be of type AutofillProfileItem. The first one | |
77 // should appear to be selected. | |
78 item = GetCollectionViewItem(0, 1); | |
79 ASSERT_TRUE([item isMemberOfClass:[AutofillProfileItem class]]); | |
80 AutofillProfileItem* shipping_address_item = item; | |
81 EXPECT_EQ(MDCCollectionViewCellAccessoryCheckmark, | |
82 shipping_address_item.accessoryType); | |
83 | |
84 item = GetCollectionViewItem(0, 2); | |
85 EXPECT_TRUE([item isMemberOfClass:[AutofillProfileItem class]]); | |
86 shipping_address_item = item; | |
87 EXPECT_EQ(MDCCollectionViewCellAccessoryNone, | |
88 shipping_address_item.accessoryType); | |
89 | |
90 // The last item should also be of type TextAndMessageItem. | |
91 item = GetCollectionViewItem(0, 3); | |
92 EXPECT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]); | |
93 | |
94 // Test the error state. | |
95 [GetShippingAddressSelectionViewController() setErrorMessage:@"Lorem ipsum"]; | |
96 [GetShippingAddressSelectionViewController() loadModel]; | |
97 ASSERT_EQ(1, NumberOfSections()); | |
98 // There should stil be 4 items in total. | |
99 ASSERT_EQ(4U, static_cast<unsigned int>(NumberOfItemsInSection(0))); | |
100 | |
101 // Test the pending state. | |
102 [GetShippingAddressSelectionViewController() setPending:YES]; | |
103 [GetShippingAddressSelectionViewController() loadModel]; | |
104 | |
105 ASSERT_EQ(1, NumberOfSections()); | |
106 // There should be only one item. | |
107 ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(0))); | |
108 | |
109 // The item should be of type StatusItem. | |
110 item = GetCollectionViewItem(0, 0); | |
111 EXPECT_TRUE([item isMemberOfClass:[StatusItem class]]); | |
112 } | |
OLD | NEW |