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

Side by Side Diff: ios/chrome/browser/ui/payments/full_card_requester_unittest.mm

Issue 2959133002: [Payment Request] unit tests for the FullCardRequester (Closed)
Patch Set: Created 3 years, 5 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #import "ios/chrome/browser/ui/payments/full_card_requester.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/test/ios/wait_util.h"
11 #include "components/autofill/core/browser/autofill_manager.h"
12 #include "components/autofill/core/browser/autofill_test_utils.h"
13 #include "components/autofill/core/browser/credit_card.h"
14 #include "components/autofill/ios/browser/autofill_driver_ios.h"
15 #import "ios/chrome/browser/autofill/autofill_agent.h"
16 #import "ios/chrome/browser/autofill/autofill_controller.h"
17 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
18 #include "ios/chrome/browser/infobars/infobar_manager_impl.h"
19 #include "ios/chrome/browser/ui/autofill/card_unmask_prompt_view_bridge.h"
20 #import "ios/chrome/browser/web/chrome_web_test.h"
21 #import "ios/chrome/test/scoped_key_window.h"
22 #import "ios/testing/ocmock_complex_type_helper.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/ocmock/OCMock/OCMock.h"
25 #include "third_party/ocmock/gtest_support.h"
26
27 #if !defined(__has_feature) || !__has_feature(objc_arc)
28 #error "This file requires ARC support."
29 #endif
30
31 @interface FullCardRequesterConsumerMock
32 : OCMockComplexTypeHelper<FullCardRequesterConsumer>
33 @end
34
35 @implementation FullCardRequesterConsumerMock
36
37 typedef void (^mock_full_card_request_did_succeed)(const autofill::CreditCard&,
38 const base::string16&);
39
40 - (void)fullCardRequestDidSucceedWithCard:(const autofill::CreditCard&)card
41 verificationCode:
42 (const base::string16&)verificationCode {
43 return static_cast<mock_full_card_request_did_succeed>(
44 [self blockForSelector:_cmd])(card, verificationCode);
45 }
46
47 @end
48
49 class PaymentRequestFullCardRequesterTest : public ChromeWebTest {
50 protected:
51 PaymentRequestFullCardRequesterTest()
52 : credit_card_(autofill::test::GetCreditCard()) {
53 TestChromeBrowserState::Builder test_cbs_builder;
54 chrome_browser_state_ = test_cbs_builder.Build();
55 }
56
57 void SetUp() override {
58 ChromeWebTest::SetUp();
59
60 // Set up what is needed to have an instance of autofill::AutofillManager.
61 AutofillAgent* autofill_agent =
62 [[AutofillAgent alloc] initWithBrowserState:chrome_browser_state_.get()
63 webState:web_state()];
64 InfoBarManagerImpl::CreateForWebState(web_state());
65 autofill_controller_ = [[AutofillController alloc]
66 initWithBrowserState:chrome_browser_state_.get()
67 webState:web_state()
68 autofillAgent:autofill_agent
69 passwordGenerationManager:nullptr
70 downloadEnabled:NO];
71 }
72
73 void TearDown() override {
74 [autofill_controller_ detachFromWebState];
75
76 ChromeWebTest::TearDown();
77 }
78
79 autofill::CreditCard credit_card_;
80 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
81 // Manages autofill for a single page.
82 AutofillController* autofill_controller_;
83 };
84
85 // Tests that the FullCardRequester presents and dismisses the card unmask
86 // prompt view controller, when the full card is requested and when the user
87 // enters the CVC/expiration information respectively.
88 TEST_F(PaymentRequestFullCardRequesterTest, PresentAndDismiss) {
89 UIViewController* base_view_controller = [[UIViewController alloc] init];
90 ScopedKeyWindow scoped_key_window_;
91 [scoped_key_window_.Get() setRootViewController:base_view_controller];
92
93 FullCardRequester full_card_requester(nil, base_view_controller,
94 chrome_browser_state_.get());
95
96 EXPECT_EQ(nil, base_view_controller.presentedViewController);
97
98 autofill::AutofillManager* autofill_manager =
99 autofill::AutofillDriverIOS::FromWebState(web_state())
100 ->autofill_manager();
101 full_card_requester.GetFullCard(&credit_card_, autofill_manager);
102
103 // Spin the run loop to trigger the animation.
104 base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
105 EXPECT_TRUE([base_view_controller.presentedViewController
106 isMemberOfClass:[CardUnmaskPromptViewController class]]);
107
108 full_card_requester.OnUnmaskVerificationResult(
109 autofill::AutofillClient::SUCCESS);
110
111 // Wait until the view controller is ordered to be dismissed and the animation
112 // completes.
113 WaitForCondition(^bool() {
114 return !base_view_controller.presentedViewController;
115 });
116 EXPECT_EQ(nil, base_view_controller.presentedViewController);
117 }
118
119 // Tests that calling the FullCardRequester's delegate method which signals that
120 // the full credit card details have been successfully received, causes the
121 // FullCardRequester's delegate method to get called.
122 TEST_F(PaymentRequestFullCardRequesterTest, FullCardRequestSucceeded) {
123 // Mock the consumer.
124 id consumer =
125 [OCMockObject mockForProtocol:@protocol(FullCardRequesterConsumer)];
126 id consumer_mock([[FullCardRequesterConsumerMock alloc]
127 initWithRepresentedObject:consumer]);
128 SEL selector = @selector(fullCardRequestDidSucceedWithCard:verificationCode:);
129 [consumer_mock onSelector:selector
130 callBlockExpectation:^(const autofill::CreditCard& card,
131 const base::string16& verificationCode) {
132 EXPECT_EQ(credit_card_, card);
133 EXPECT_EQ(base::ASCIIToUTF16("123"), verificationCode);
134 }];
135
136 FullCardRequester full_card_requester(consumer_mock, nil,
137 chrome_browser_state_.get());
138
139 full_card_requester.OnFullCardRequestSucceeded(credit_card_,
140 base::ASCIIToUTF16("123"));
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698