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_method_selection_coordinator.h" | 5 #import "ios/chrome/browser/payments/payment_method_selection_coordinator.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/test/ios/wait_util.h" | 8 #include "base/test/ios/wait_util.h" |
9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
10 #include "components/autofill/core/browser/credit_card.h" | 10 #include "components/autofill/core/browser/credit_card.h" |
11 #include "components/autofill/core/browser/test_personal_data_manager.h" | 11 #include "components/autofill/core/browser/test_personal_data_manager.h" |
12 #import "ios/chrome/browser/payments/payment_method_selection_view_controller.h" | 12 #import "ios/chrome/browser/payments/payment_method_selection_view_controller.h" |
13 #include "ios/chrome/browser/payments/payment_request.h" | 13 #include "ios/chrome/browser/payments/payment_request.h" |
14 #include "ios/chrome/browser/payments/payment_request_test_util.h" | 14 #include "ios/chrome/browser/payments/payment_request_test_util.h" |
15 #include "ios/web/public/payments/payment_request.h" | 15 #include "ios/web/public/payments/payment_request.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
18 #include "third_party/ocmock/OCMock/OCMock.h" | 18 #include "third_party/ocmock/OCMock/OCMock.h" |
19 #include "third_party/ocmock/gtest_support.h" | 19 #include "third_party/ocmock/gtest_support.h" |
20 | 20 |
| 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 22 #error "This file requires ARC support." |
| 23 #endif |
| 24 |
21 typedef PlatformTest PaymentMethodSelectionCoordinatorTest; | 25 typedef PlatformTest PaymentMethodSelectionCoordinatorTest; |
22 | 26 |
23 // Tests that invoking start and stop on the coordinator presents and dismisses | 27 // Tests that invoking start and stop on the coordinator presents and dismisses |
24 // the PaymentMethodSelectionViewController, respectively. | 28 // the PaymentMethodSelectionViewController, respectively. |
25 TEST(PaymentMethodSelectionCoordinatorTest, StartAndStop) { | 29 TEST(PaymentMethodSelectionCoordinatorTest, StartAndStop) { |
26 std::unique_ptr<PaymentRequest> payment_request = | 30 std::unique_ptr<PaymentRequest> payment_request = |
27 payment_request_test_util::CreateTestPaymentRequest(); | 31 payment_request_test_util::CreateTestPaymentRequest(); |
28 | 32 |
29 UIViewController* base_view_controller = | 33 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
30 [[[UIViewController alloc] init] autorelease]; | |
31 UINavigationController* navigation_controller = | 34 UINavigationController* navigation_controller = |
32 [[[UINavigationController alloc] | 35 [[UINavigationController alloc] |
33 initWithRootViewController:base_view_controller] autorelease]; | 36 initWithRootViewController:base_view_controller]; |
34 | 37 |
35 PaymentMethodSelectionCoordinator* coordinator = | 38 PaymentMethodSelectionCoordinator* coordinator = |
36 [[[PaymentMethodSelectionCoordinator alloc] | 39 [[PaymentMethodSelectionCoordinator alloc] |
37 initWithBaseViewController:base_view_controller] autorelease]; | 40 initWithBaseViewController:base_view_controller]; |
38 [coordinator setPaymentRequest:payment_request.get()]; | 41 [coordinator setPaymentRequest:payment_request.get()]; |
39 | 42 |
40 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 43 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
41 | 44 |
42 [coordinator start]; | 45 [coordinator start]; |
43 // Short delay to allow animation to complete. | 46 // Short delay to allow animation to complete. |
44 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 47 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
45 EXPECT_EQ(2u, navigation_controller.viewControllers.count); | 48 EXPECT_EQ(2u, navigation_controller.viewControllers.count); |
46 | 49 |
47 UIViewController* view_controller = | 50 UIViewController* view_controller = |
48 navigation_controller.visibleViewController; | 51 navigation_controller.visibleViewController; |
49 EXPECT_TRUE([view_controller | 52 EXPECT_TRUE([view_controller |
50 isMemberOfClass:[PaymentMethodSelectionViewController class]]); | 53 isMemberOfClass:[PaymentMethodSelectionViewController class]]); |
51 | 54 |
52 [coordinator stop]; | 55 [coordinator stop]; |
53 // Short delay to allow animation to complete. | 56 // Short delay to allow animation to complete. |
54 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 57 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
55 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 58 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
56 } | 59 } |
57 | 60 |
58 // Tests that calling the view controller delegate method which notifies the | 61 // Tests that calling the view controller delegate method which notifies the |
59 // coordinator about selection of a payment method invokes the corresponding | 62 // coordinator about selection of a payment method invokes the corresponding |
60 // coordinator delegate method. | 63 // coordinator delegate method. |
61 TEST(PaymentMethodSelectionCoordinatorTest, DidSelectPaymentMethod) { | 64 TEST(PaymentMethodSelectionCoordinatorTest, DidSelectPaymentMethod) { |
62 std::unique_ptr<PaymentRequest> payment_request = | 65 std::unique_ptr<PaymentRequest> payment_request = |
63 payment_request_test_util::CreateTestPaymentRequest(); | 66 payment_request_test_util::CreateTestPaymentRequest(); |
64 | 67 |
65 UIViewController* base_view_controller = | 68 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
66 [[[UIViewController alloc] init] autorelease]; | |
67 UINavigationController* navigation_controller = | 69 UINavigationController* navigation_controller = |
68 [[[UINavigationController alloc] | 70 [[UINavigationController alloc] |
69 initWithRootViewController:base_view_controller] autorelease]; | 71 initWithRootViewController:base_view_controller]; |
70 | 72 |
71 PaymentMethodSelectionCoordinator* coordinator = | 73 PaymentMethodSelectionCoordinator* coordinator = |
72 [[[PaymentMethodSelectionCoordinator alloc] | 74 [[PaymentMethodSelectionCoordinator alloc] |
73 initWithBaseViewController:base_view_controller] autorelease]; | 75 initWithBaseViewController:base_view_controller]; |
74 [coordinator setPaymentRequest:payment_request.get()]; | 76 [coordinator setPaymentRequest:payment_request.get()]; |
75 | 77 |
76 // Mock the coordinator delegate. | 78 // Mock the coordinator delegate. |
77 id delegate = [OCMockObject | 79 id delegate = [OCMockObject |
78 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; | 80 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; |
79 std::unique_ptr<autofill::CreditCard> credit_card(new autofill::CreditCard()); | 81 std::unique_ptr<autofill::CreditCard> credit_card(new autofill::CreditCard()); |
80 [[delegate expect] paymentMethodSelectionCoordinator:coordinator | 82 [[delegate expect] paymentMethodSelectionCoordinator:coordinator |
81 didSelectPaymentMethod:credit_card.get()]; | 83 didSelectPaymentMethod:credit_card.get()]; |
82 [coordinator setDelegate:delegate]; | 84 [coordinator setDelegate:delegate]; |
83 | 85 |
(...skipping 17 matching lines...) Expand all Loading... |
101 EXPECT_OCMOCK_VERIFY(delegate); | 103 EXPECT_OCMOCK_VERIFY(delegate); |
102 } | 104 } |
103 | 105 |
104 // Tests that calling the view controller delegate method which notifies the | 106 // Tests that calling the view controller delegate method which notifies the |
105 // coordinator that the user has chosen to return without making a selection | 107 // coordinator that the user has chosen to return without making a selection |
106 // invokes the corresponding coordinator delegate method. | 108 // invokes the corresponding coordinator delegate method. |
107 TEST(PaymentMethodSelectionCoordinatorTest, DidReturn) { | 109 TEST(PaymentMethodSelectionCoordinatorTest, DidReturn) { |
108 std::unique_ptr<PaymentRequest> payment_request = | 110 std::unique_ptr<PaymentRequest> payment_request = |
109 payment_request_test_util::CreateTestPaymentRequest(); | 111 payment_request_test_util::CreateTestPaymentRequest(); |
110 | 112 |
111 UIViewController* base_view_controller = | 113 UIViewController* base_view_controller = [[UIViewController alloc] init]; |
112 [[[UIViewController alloc] init] autorelease]; | |
113 UINavigationController* navigation_controller = | 114 UINavigationController* navigation_controller = |
114 [[[UINavigationController alloc] | 115 [[UINavigationController alloc] |
115 initWithRootViewController:base_view_controller] autorelease]; | 116 initWithRootViewController:base_view_controller]; |
116 | 117 |
117 PaymentMethodSelectionCoordinator* coordinator = | 118 PaymentMethodSelectionCoordinator* coordinator = |
118 [[[PaymentMethodSelectionCoordinator alloc] | 119 [[PaymentMethodSelectionCoordinator alloc] |
119 initWithBaseViewController:base_view_controller] autorelease]; | 120 initWithBaseViewController:base_view_controller]; |
120 [coordinator setPaymentRequest:payment_request.get()]; | 121 [coordinator setPaymentRequest:payment_request.get()]; |
121 | 122 |
122 // Mock the coordinator delegate. | 123 // Mock the coordinator delegate. |
123 id delegate = [OCMockObject | 124 id delegate = [OCMockObject |
124 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; | 125 mockForProtocol:@protocol(PaymentMethodSelectionCoordinatorDelegate)]; |
125 [[delegate expect] paymentMethodSelectionCoordinatorDidReturn:coordinator]; | 126 [[delegate expect] paymentMethodSelectionCoordinatorDidReturn:coordinator]; |
126 [coordinator setDelegate:delegate]; | 127 [coordinator setDelegate:delegate]; |
127 | 128 |
128 EXPECT_EQ(1u, navigation_controller.viewControllers.count); | 129 EXPECT_EQ(1u, navigation_controller.viewControllers.count); |
129 | 130 |
130 [coordinator start]; | 131 [coordinator start]; |
131 // Short delay to allow animation to complete. | 132 // Short delay to allow animation to complete. |
132 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); | 133 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0)); |
133 EXPECT_EQ(2u, navigation_controller.viewControllers.count); | 134 EXPECT_EQ(2u, navigation_controller.viewControllers.count); |
134 | 135 |
135 // Call the controller delegate method. | 136 // Call the controller delegate method. |
136 PaymentMethodSelectionViewController* view_controller = | 137 PaymentMethodSelectionViewController* view_controller = |
137 base::mac::ObjCCastStrict<PaymentMethodSelectionViewController>( | 138 base::mac::ObjCCastStrict<PaymentMethodSelectionViewController>( |
138 navigation_controller.visibleViewController); | 139 navigation_controller.visibleViewController); |
139 [coordinator paymentMethodSelectionViewControllerDidReturn:view_controller]; | 140 [coordinator paymentMethodSelectionViewControllerDidReturn:view_controller]; |
140 | 141 |
141 EXPECT_OCMOCK_VERIFY(delegate); | 142 EXPECT_OCMOCK_VERIFY(delegate); |
142 } | 143 } |
OLD | NEW |