OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/payment_request_coordinator.h" | 5 #import "ios/chrome/browser/payments/payment_request_coordinator.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.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/credit_card.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_test_util.h" | 13 #include "ios/chrome/browser/payments/payment_request_test_util.h" |
14 #import "ios/chrome/browser/payments/payment_request_view_controller.h" | 14 #import "ios/chrome/browser/payments/payment_request_view_controller.h" |
15 #import "ios/chrome/test/scoped_key_window.h" | 15 #import "ios/chrome/test/scoped_key_window.h" |
16 #import "ios/testing/ocmock_complex_type_helper.h" | 16 #import "ios/testing/ocmock_complex_type_helper.h" |
17 #include "ios/web/public/payments/payment_request.h" | 17 #include "ios/web/public/payments/payment_request.h" |
18 #import "ios/web/public/test/test_web_thread_bundle.h" | 18 #import "ios/web/public/test/test_web_thread_bundle.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
21 #include "third_party/ocmock/OCMock/OCMock.h" | 21 #include "third_party/ocmock/OCMock/OCMock.h" |
22 #include "third_party/ocmock/gtest_support.h" | 22 #include "third_party/ocmock/gtest_support.h" |
23 | 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
24 typedef PlatformTest PaymentRequestCoordinatorTest; | 28 typedef PlatformTest PaymentRequestCoordinatorTest; |
25 | 29 |
26 @interface PaymentRequestCoordinatorDelegateMock< | 30 @interface PaymentRequestCoordinatorDelegateMock< |
27 PaymentRequestCoordinatorDelegate>:OCMockComplexTypeHelper | 31 PaymentRequestCoordinatorDelegate>:OCMockComplexTypeHelper |
28 @end | 32 @end |
29 | 33 |
30 @implementation PaymentRequestCoordinatorDelegateMock | 34 @implementation PaymentRequestCoordinatorDelegateMock |
31 | 35 |
32 typedef void (^mock_coordinator_cancel)(PaymentRequestCoordinator*); | 36 typedef void (^mock_coordinator_cancel)(PaymentRequestCoordinator*); |
33 typedef void (^mock_coordinator_confirm)(PaymentRequestCoordinator*, | 37 typedef void (^mock_coordinator_confirm)(PaymentRequestCoordinator*, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 69 |
66 @end | 70 @end |
67 | 71 |
68 // Tests that invoking start and stop on the coordinator presents and | 72 // Tests that invoking start and stop on the coordinator presents and |
69 // dismisses | 73 // dismisses |
70 // the PaymentRequestViewController, respectively. | 74 // the PaymentRequestViewController, respectively. |
71 TEST(PaymentRequestCoordinatorTest, StartAndStop) { | 75 TEST(PaymentRequestCoordinatorTest, StartAndStop) { |
72 std::unique_ptr<PaymentRequest> payment_request = | 76 std::unique_ptr<PaymentRequest> payment_request = |
73 payment_request_test_util::CreateTestPaymentRequest(); | 77 payment_request_test_util::CreateTestPaymentRequest(); |
74 | 78 |
75 UIViewController* base_view_controller = | 79 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
76 [[[UIViewController alloc] init] autorelease]; | |
77 ScopedKeyWindow scoped_key_window_; | 80 ScopedKeyWindow scoped_key_window_; |
78 [scoped_key_window_.Get() setRootViewController:base_view_controller]; | 81 [scoped_key_window_.Get() setRootViewController:base_view_controller]; |
79 | 82 |
80 PaymentRequestCoordinator* coordinator = [[[PaymentRequestCoordinator alloc] | 83 PaymentRequestCoordinator* coordinator = [[PaymentRequestCoordinator alloc] |
81 initWithBaseViewController:base_view_controller] autorelease]; | 84 initWithBaseViewController:base_view_controller]; |
82 [coordinator setPaymentRequest:payment_request.get()]; | 85 [coordinator setPaymentRequest:payment_request.get()]; |
83 | 86 |
84 [coordinator start]; | 87 [coordinator start]; |
85 // Short delay to allow animation to complete. | 88 // Short delay to allow animation to complete. |
86 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1)); | 89 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1)); |
87 id persented_view_controller = | 90 id persented_view_controller = |
88 [coordinator baseViewController].presentedViewController; | 91 [coordinator baseViewController].presentedViewController; |
89 EXPECT_TRUE([persented_view_controller | 92 EXPECT_TRUE([persented_view_controller |
90 isMemberOfClass:[UINavigationController class]]); | 93 isMemberOfClass:[UINavigationController class]]); |
91 UINavigationController* navigation_controller = | 94 UINavigationController* navigation_controller = |
(...skipping 11 matching lines...) Expand all Loading... |
103 EXPECT_EQ(nil, [coordinator baseViewController].presentedViewController); | 106 EXPECT_EQ(nil, [coordinator baseViewController].presentedViewController); |
104 } | 107 } |
105 | 108 |
106 // Tests that calling the card unmasking delegate method which notifies the | 109 // Tests that calling the card unmasking delegate method which notifies the |
107 // coordinator about successful unmasking of a credit card invokes the | 110 // coordinator about successful unmasking of a credit card invokes the |
108 // appropriate coordinator delegate method with the expected information. | 111 // appropriate coordinator delegate method with the expected information. |
109 TEST(PaymentRequestCoordinatorTest, FullCardRequestDidSucceed) { | 112 TEST(PaymentRequestCoordinatorTest, FullCardRequestDidSucceed) { |
110 std::unique_ptr<PaymentRequest> payment_request = | 113 std::unique_ptr<PaymentRequest> payment_request = |
111 payment_request_test_util::CreateTestPaymentRequest(); | 114 payment_request_test_util::CreateTestPaymentRequest(); |
112 | 115 |
113 UIViewController* base_view_controller = | 116 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
114 [[[UIViewController alloc] init] autorelease]; | |
115 ScopedKeyWindow scoped_key_window_; | 117 ScopedKeyWindow scoped_key_window_; |
116 [scoped_key_window_.Get() setRootViewController:base_view_controller]; | 118 [scoped_key_window_.Get() setRootViewController:base_view_controller]; |
117 | 119 |
118 PaymentRequestCoordinator* coordinator = [[[PaymentRequestCoordinator alloc] | 120 PaymentRequestCoordinator* coordinator = [[PaymentRequestCoordinator alloc] |
119 initWithBaseViewController:base_view_controller] autorelease]; | 121 initWithBaseViewController:base_view_controller]; |
120 [coordinator setPaymentRequest:payment_request.get()]; | 122 [coordinator setPaymentRequest:payment_request.get()]; |
121 | 123 |
122 id delegate = [OCMockObject | 124 id delegate = [OCMockObject |
123 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; | 125 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; |
124 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] | 126 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] |
125 initWithRepresentedObject:delegate]); | 127 initWithRepresentedObject:delegate]); |
126 SEL selector = | 128 SEL selector = |
127 @selector(paymentRequestCoordinator:didConfirmWithPaymentResponse:); | 129 @selector(paymentRequestCoordinator:didConfirmWithPaymentResponse:); |
128 [delegate_mock onSelector:selector | 130 [delegate_mock onSelector:selector |
129 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator, | 131 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator, |
(...skipping 19 matching lines...) Expand all Loading... |
149 CVC:base::ASCIIToUTF16("123")]; | 151 CVC:base::ASCIIToUTF16("123")]; |
150 } | 152 } |
151 | 153 |
152 // Tests that calling the ShippingAddressSelectionCoordinator delegate method | 154 // Tests that calling the ShippingAddressSelectionCoordinator delegate method |
153 // which notifies the coordinator about selection of a shipping address invokes | 155 // which notifies the coordinator about selection of a shipping address invokes |
154 // the corresponding coordinator delegate method with the expected information. | 156 // the corresponding coordinator delegate method with the expected information. |
155 TEST(PaymentRequestCoordinatorTest, DidSelectShippingAddress) { | 157 TEST(PaymentRequestCoordinatorTest, DidSelectShippingAddress) { |
156 std::unique_ptr<PaymentRequest> payment_request = | 158 std::unique_ptr<PaymentRequest> payment_request = |
157 payment_request_test_util::CreateTestPaymentRequest(); | 159 payment_request_test_util::CreateTestPaymentRequest(); |
158 | 160 |
159 UIViewController* base_view_controller = | 161 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
160 [[[UIViewController alloc] init] autorelease]; | |
161 ScopedKeyWindow scoped_key_window_; | 162 ScopedKeyWindow scoped_key_window_; |
162 [scoped_key_window_.Get() setRootViewController:base_view_controller]; | 163 [scoped_key_window_.Get() setRootViewController:base_view_controller]; |
163 | 164 |
164 PaymentRequestCoordinator* coordinator = [[[PaymentRequestCoordinator alloc] | 165 PaymentRequestCoordinator* coordinator = [[PaymentRequestCoordinator alloc] |
165 initWithBaseViewController:base_view_controller] autorelease]; | 166 initWithBaseViewController:base_view_controller]; |
166 [coordinator setPaymentRequest:payment_request.get()]; | 167 [coordinator setPaymentRequest:payment_request.get()]; |
167 | 168 |
168 // Mock the coordinator delegate. | 169 // Mock the coordinator delegate. |
169 id delegate = [OCMockObject | 170 id delegate = [OCMockObject |
170 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; | 171 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; |
171 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] | 172 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] |
172 initWithRepresentedObject:delegate]); | 173 initWithRepresentedObject:delegate]); |
173 SEL selector = @selector(paymentRequestCoordinator:didSelectShippingAddress:); | 174 SEL selector = @selector(paymentRequestCoordinator:didSelectShippingAddress:); |
174 [delegate_mock onSelector:selector | 175 [delegate_mock onSelector:selector |
175 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator, | 176 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator, |
(...skipping 23 matching lines...) Expand all Loading... |
199 didSelectShippingAddress:profile.get()]; | 200 didSelectShippingAddress:profile.get()]; |
200 } | 201 } |
201 | 202 |
202 // Tests that calling the ShippingOptionSelectionCoordinator delegate method | 203 // Tests that calling the ShippingOptionSelectionCoordinator delegate method |
203 // which notifies the coordinator about selection of a shipping option invokes | 204 // which notifies the coordinator about selection of a shipping option invokes |
204 // the corresponding coordinator delegate method with the expected information. | 205 // the corresponding coordinator delegate method with the expected information. |
205 TEST(PaymentRequestCoordinatorTest, DidSelectShippingOption) { | 206 TEST(PaymentRequestCoordinatorTest, DidSelectShippingOption) { |
206 std::unique_ptr<PaymentRequest> payment_request = | 207 std::unique_ptr<PaymentRequest> payment_request = |
207 payment_request_test_util::CreateTestPaymentRequest(); | 208 payment_request_test_util::CreateTestPaymentRequest(); |
208 | 209 |
209 UIViewController* base_view_controller = | 210 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
210 [[[UIViewController alloc] init] autorelease]; | |
211 ScopedKeyWindow scoped_key_window_; | 211 ScopedKeyWindow scoped_key_window_; |
212 [scoped_key_window_.Get() setRootViewController:base_view_controller]; | 212 [scoped_key_window_.Get() setRootViewController:base_view_controller]; |
213 | 213 |
214 PaymentRequestCoordinator* coordinator = [[[PaymentRequestCoordinator alloc] | 214 PaymentRequestCoordinator* coordinator = [[PaymentRequestCoordinator alloc] |
215 initWithBaseViewController:base_view_controller] autorelease]; | 215 initWithBaseViewController:base_view_controller]; |
216 [coordinator setPaymentRequest:payment_request.get()]; | 216 [coordinator setPaymentRequest:payment_request.get()]; |
217 | 217 |
218 // Mock the coordinator delegate. | 218 // Mock the coordinator delegate. |
219 id delegate = [OCMockObject | 219 id delegate = [OCMockObject |
220 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; | 220 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; |
221 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] | 221 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] |
222 initWithRepresentedObject:delegate]); | 222 initWithRepresentedObject:delegate]); |
223 SEL selector = @selector(paymentRequestCoordinator:didSelectShippingOption:); | 223 SEL selector = @selector(paymentRequestCoordinator:didSelectShippingOption:); |
224 [delegate_mock onSelector:selector | 224 [delegate_mock onSelector:selector |
225 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator, | 225 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator, |
(...skipping 16 matching lines...) Expand all Loading... |
242 didSelectShippingOption:&shipping_option]; | 242 didSelectShippingOption:&shipping_option]; |
243 } | 243 } |
244 | 244 |
245 // Tests that calling the view controller delegate method which notifies the | 245 // Tests that calling the view controller delegate method which notifies the |
246 // coordinator about cancellation of the PaymentRequest invokes the | 246 // coordinator about cancellation of the PaymentRequest invokes the |
247 // corresponding coordinator delegate method. | 247 // corresponding coordinator delegate method. |
248 TEST(PaymentRequestCoordinatorTest, DidCancel) { | 248 TEST(PaymentRequestCoordinatorTest, DidCancel) { |
249 std::unique_ptr<PaymentRequest> payment_request = | 249 std::unique_ptr<PaymentRequest> payment_request = |
250 payment_request_test_util::CreateTestPaymentRequest(); | 250 payment_request_test_util::CreateTestPaymentRequest(); |
251 | 251 |
252 UIViewController* base_view_controller = | 252 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
253 [[[UIViewController alloc] init] autorelease]; | |
254 ScopedKeyWindow scoped_key_window_; | 253 ScopedKeyWindow scoped_key_window_; |
255 [scoped_key_window_.Get() setRootViewController:base_view_controller]; | 254 [scoped_key_window_.Get() setRootViewController:base_view_controller]; |
256 | 255 |
257 PaymentRequestCoordinator* coordinator = [[[PaymentRequestCoordinator alloc] | 256 PaymentRequestCoordinator* coordinator = [[PaymentRequestCoordinator alloc] |
258 initWithBaseViewController:base_view_controller] autorelease]; | 257 initWithBaseViewController:base_view_controller]; |
259 [coordinator setPaymentRequest:payment_request.get()]; | 258 [coordinator setPaymentRequest:payment_request.get()]; |
260 | 259 |
261 // Mock the coordinator delegate. | 260 // Mock the coordinator delegate. |
262 id delegate = [OCMockObject | 261 id delegate = [OCMockObject |
263 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; | 262 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; |
264 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] | 263 id delegate_mock([[PaymentRequestCoordinatorDelegateMock alloc] |
265 initWithRepresentedObject:delegate]); | 264 initWithRepresentedObject:delegate]); |
266 SEL selector = @selector(paymentRequestCoordinatorDidCancel:); | 265 SEL selector = @selector(paymentRequestCoordinatorDidCancel:); |
267 [delegate_mock onSelector:selector | 266 [delegate_mock onSelector:selector |
268 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator) { | 267 callBlockExpectation:^(PaymentRequestCoordinator* callerCoordinator) { |
269 EXPECT_EQ(coordinator, callerCoordinator); | 268 EXPECT_EQ(coordinator, callerCoordinator); |
270 }]; | 269 }]; |
271 [coordinator setDelegate:delegate_mock]; | 270 [coordinator setDelegate:delegate_mock]; |
272 | 271 |
273 [coordinator start]; | 272 [coordinator start]; |
274 // Short delay to allow animation to complete. | 273 // Short delay to allow animation to complete. |
275 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1)); | 274 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1)); |
276 | 275 |
277 // Call the controller delegate method. | 276 // Call the controller delegate method. |
278 [coordinator paymentRequestViewControllerDidCancel:nil]; | 277 [coordinator paymentRequestViewControllerDidCancel:nil]; |
279 } | 278 } |
OLD | NEW |