OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/payments/shipping_address_selection_coordinator.h" | 5 #import "ios/chrome/browser/payments/shipping_address_selection_coordinator.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/test/ios/wait_util.h" | 9 #include "base/test/ios/wait_util.h" |
10 #include "components/autofill/core/browser/autofill_profile.h" | 10 #include "components/autofill/core/browser/autofill_profile.h" |
11 #include "components/autofill/core/browser/credit_card.h" | 11 #include "components/autofill/core/browser/autofill_test_utils.h" |
12 #include "components/autofill/core/browser/test_personal_data_manager.h" | 12 #include "components/autofill/core/browser/test_personal_data_manager.h" |
13 #include "ios/chrome/browser/payments/payment_request.h" | 13 #include "ios/chrome/browser/payments/payment_request.h" |
| 14 #import "ios/chrome/browser/payments/payment_request_selector_view_controller.h" |
14 #include "ios/chrome/browser/payments/payment_request_test_util.h" | 15 #include "ios/chrome/browser/payments/payment_request_test_util.h" |
15 #import "ios/chrome/browser/payments/shipping_address_selection_view_controller.
h" | |
16 #include "ios/web/public/payments/payment_request.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
19 #include "third_party/ocmock/OCMock/OCMock.h" | 18 #include "third_party/ocmock/OCMock/OCMock.h" |
20 #include "third_party/ocmock/gtest_support.h" | 19 #include "third_party/ocmock/gtest_support.h" |
21 | 20 |
22 #if !defined(__has_feature) || !__has_feature(objc_arc) | 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
23 #error "This file requires ARC support." | 22 #error "This file requires ARC support." |
24 #endif | 23 #endif |
25 | 24 |
26 class PaymentRequestShippingAddressSelectionCoordinatorTest | 25 class PaymentRequestShippingAddressSelectionCoordinatorTest |
27 : public PlatformTest { | 26 : public PlatformTest { |
28 protected: | 27 protected: |
29 PaymentRequestShippingAddressSelectionCoordinatorTest() { | 28 PaymentRequestShippingAddressSelectionCoordinatorTest() |
| 29 : autofill_profile1_(autofill::test::GetFullProfile()), |
| 30 autofill_profile2_(autofill::test::GetFullProfile2()) { |
| 31 // Add testing profiles to autofill::TestPersonalDataManager. |
| 32 personal_data_manager_.AddTestingProfile(&autofill_profile1_); |
| 33 personal_data_manager_.AddTestingProfile(&autofill_profile2_); |
30 payment_request_ = base::MakeUnique<PaymentRequest>( | 34 payment_request_ = base::MakeUnique<PaymentRequest>( |
31 payment_request_test_util::CreateTestWebPaymentRequest(), | 35 payment_request_test_util::CreateTestWebPaymentRequest(), |
32 &personal_data_manager_); | 36 &personal_data_manager_); |
33 } | 37 } |
34 | 38 |
| 39 autofill::AutofillProfile autofill_profile1_; |
| 40 autofill::AutofillProfile autofill_profile2_; |
35 autofill::TestPersonalDataManager personal_data_manager_; | 41 autofill::TestPersonalDataManager personal_data_manager_; |
36 std::unique_ptr<PaymentRequest> payment_request_; | 42 std::unique_ptr<PaymentRequest> payment_request_; |
37 }; | 43 }; |
38 | 44 |
39 // Tests that invoking start and stop on the coordinator presents and dismisses | 45 // Tests that invoking start and stop on the coordinator presents and dismisses |
40 // the ShippingAddressSelectionViewController, respectively. | 46 // the PaymentRequestSelectorViewController, respectively. |
41 TEST_F(PaymentRequestShippingAddressSelectionCoordinatorTest, StartAndStop) { | 47 TEST_F(PaymentRequestShippingAddressSelectionCoordinatorTest, StartAndStop) { |
42 UIViewController* base_view_controller = [[UIViewController alloc] init]; | 48 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
43 UINavigationController* navigation_controller = | 49 UINavigationController* navigation_controller = |
44 [[UINavigationController alloc] | 50 [[UINavigationController alloc] |
45 initWithRootViewController:base_view_controller]; | 51 initWithRootViewController:base_view_controller]; |
46 | 52 |
47 ShippingAddressSelectionCoordinator* coordinator = | 53 ShippingAddressSelectionCoordinator* coordinator = |
48 [[ShippingAddressSelectionCoordinator alloc] | 54 [[ShippingAddressSelectionCoordinator alloc] |
49 initWithBaseViewController:base_view_controller]; | 55 initWithBaseViewController:base_view_controller]; |
50 [coordinator setPaymentRequest:payment_request_.get()]; | 56 [coordinator setPaymentRequest:payment_request_.get()]; |
51 | 57 |
52 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 58 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
53 | 59 |
54 [coordinator start]; | 60 [coordinator start]; |
55 // Short delay to allow animation to complete. | 61 // Short delay to allow animation to complete. |
56 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 62 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
57 EXPECT_EQ(2u, navigation_controller.viewControllers.count); | 63 EXPECT_EQ(2u, navigation_controller.viewControllers.count); |
58 | 64 |
59 UIViewController* view_controller = | 65 UIViewController* view_controller = |
60 navigation_controller.visibleViewController; | 66 navigation_controller.visibleViewController; |
61 EXPECT_TRUE([view_controller | 67 EXPECT_TRUE([view_controller |
62 isMemberOfClass:[ShippingAddressSelectionViewController class]]); | 68 isMemberOfClass:[PaymentRequestSelectorViewController class]]); |
63 | 69 |
64 [coordinator stop]; | 70 [coordinator stop]; |
65 // Short delay to allow animation to complete. | 71 // Short delay to allow animation to complete. |
66 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 72 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
67 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 73 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
68 } | 74 } |
69 | 75 |
70 // Tests that calling the view controller delegate method which notifies the | 76 // Tests that calling the view controller delegate method which notifies the |
71 // delegate about selection of a shipping address invokes the corresponding | 77 // delegate about selection of a shipping address invokes the corresponding |
72 // coordinator delegate method. | 78 // coordinator delegate method. |
73 TEST_F(PaymentRequestShippingAddressSelectionCoordinatorTest, | 79 TEST_F(PaymentRequestShippingAddressSelectionCoordinatorTest, |
74 SelectedShippingAddress) { | 80 SelectedShippingAddress) { |
75 UIViewController* base_view_controller = [[UIViewController alloc] init]; | 81 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
76 UINavigationController* navigation_controller = | 82 UINavigationController* navigation_controller = |
77 [[UINavigationController alloc] | 83 [[UINavigationController alloc] |
78 initWithRootViewController:base_view_controller]; | 84 initWithRootViewController:base_view_controller]; |
79 | 85 |
80 ShippingAddressSelectionCoordinator* coordinator = | 86 ShippingAddressSelectionCoordinator* coordinator = |
81 [[ShippingAddressSelectionCoordinator alloc] | 87 [[ShippingAddressSelectionCoordinator alloc] |
82 initWithBaseViewController:base_view_controller]; | 88 initWithBaseViewController:base_view_controller]; |
83 [coordinator setPaymentRequest:payment_request_.get()]; | 89 [coordinator setPaymentRequest:payment_request_.get()]; |
84 | 90 |
85 // Mock the coordinator delegate. | 91 // Mock the coordinator delegate. |
86 id delegate = [OCMockObject | 92 id delegate = [OCMockObject |
87 mockForProtocol:@protocol(ShippingAddressSelectionCoordinatorDelegate)]; | 93 mockForProtocol:@protocol(ShippingAddressSelectionCoordinatorDelegate)]; |
88 autofill::AutofillProfile profile; | 94 autofill::AutofillProfile* profile = payment_request_->shipping_profiles()[1]; |
89 [[delegate expect] shippingAddressSelectionCoordinator:coordinator | 95 [[delegate expect] shippingAddressSelectionCoordinator:coordinator |
90 didSelectShippingAddress:&profile]; | 96 didSelectShippingAddress:profile]; |
91 [coordinator setDelegate:delegate]; | 97 [coordinator setDelegate:delegate]; |
92 | 98 |
93 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 99 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
94 | 100 |
95 [coordinator start]; | 101 [coordinator start]; |
96 // Short delay to allow animation to complete. | 102 // Short delay to allow animation to complete. |
97 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 103 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
98 EXPECT_EQ(2u, navigation_controller.viewControllers.count); | 104 EXPECT_EQ(2u, navigation_controller.viewControllers.count); |
99 | 105 |
100 // Call the controller delegate method. | 106 // Call the controller delegate method. |
101 ShippingAddressSelectionViewController* view_controller = | 107 PaymentRequestSelectorViewController* view_controller = |
102 base::mac::ObjCCastStrict<ShippingAddressSelectionViewController>( | 108 base::mac::ObjCCastStrict<PaymentRequestSelectorViewController>( |
103 navigation_controller.visibleViewController); | 109 navigation_controller.visibleViewController); |
104 [coordinator shippingAddressSelectionViewController:view_controller | 110 [coordinator paymentRequestSelectorViewController:view_controller |
105 didSelectShippingAddress:&profile]; | 111 didSelectItemAtIndex:1]; |
106 | 112 |
107 // Wait for the coordinator delegate to be notified. | 113 // Wait for the coordinator delegate to be notified. |
108 base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSecondsD(0.5)); | 114 base::test::ios::SpinRunLoopWithMinDelay(base::TimeDelta::FromSecondsD(0.5)); |
109 | 115 |
110 EXPECT_OCMOCK_VERIFY(delegate); | 116 EXPECT_OCMOCK_VERIFY(delegate); |
111 } | 117 } |
112 | 118 |
113 // Tests that calling the view controller delegate method which notifies the | 119 // Tests that calling the view controller delegate method which notifies the |
114 // delegate that the user has chosen to return without making a selection | 120 // delegate that the user has chosen to return without making a selection |
115 // invokes the corresponding coordinator delegate method. | 121 // invokes the corresponding coordinator delegate method. |
(...skipping 15 matching lines...) Expand all Loading... |
131 [coordinator setDelegate:delegate]; | 137 [coordinator setDelegate:delegate]; |
132 | 138 |
133 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 139 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
134 | 140 |
135 [coordinator start]; | 141 [coordinator start]; |
136 // Short delay to allow animation to complete. | 142 // Short delay to allow animation to complete. |
137 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 143 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
138 EXPECT_EQ(2u, navigation_controller.viewControllers.count); | 144 EXPECT_EQ(2u, navigation_controller.viewControllers.count); |
139 | 145 |
140 // Call the controller delegate method. | 146 // Call the controller delegate method. |
141 ShippingAddressSelectionViewController* view_controller = | 147 PaymentRequestSelectorViewController* view_controller = |
142 base::mac::ObjCCastStrict<ShippingAddressSelectionViewController>( | 148 base::mac::ObjCCastStrict<PaymentRequestSelectorViewController>( |
143 navigation_controller.visibleViewController); | 149 navigation_controller.visibleViewController); |
144 [coordinator shippingAddressSelectionViewControllerDidReturn:view_controller]; | 150 [coordinator paymentRequestSelectorViewControllerDidFinish:view_controller]; |
145 | 151 |
146 EXPECT_OCMOCK_VERIFY(delegate); | 152 EXPECT_OCMOCK_VERIFY(delegate); |
147 } | 153 } |
OLD | NEW |