blob: e719300f589e1598a85ce9e11e453a70ed1ffa35 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/payments/payment_items_display_coordinator.h"
#include "base/mac/foundation_util.h"
#include "base/test/ios/wait_util.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#import "ios/chrome/browser/payments/payment_items_display_view_controller.h"
#include "ios/chrome/browser/payments/payment_request.h"
#include "ios/chrome/browser/payments/payment_request_test_util.h"
#include "ios/web/public/payments/payment_request.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
#include "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
typedef PlatformTest PaymentItemsDisplayCoordinatorTest;
// Tests that invoking start and stop on the coordinator presents and dismisses
// the payment items display view controller, respectively.
TEST(PaymentItemsDisplayCoordinatorTest, StartAndStop) {
std::unique_ptr<PaymentRequest> payment_request =
payment_request_test_util::CreateTestPaymentRequest();
UIViewController* base_view_controller = [[UIViewController alloc] init];
UINavigationController* navigation_controller =
[[UINavigationController alloc]
initWithRootViewController:base_view_controller];
PaymentItemsDisplayCoordinator* coordinator =
[[PaymentItemsDisplayCoordinator alloc]
initWithBaseViewController:base_view_controller];
[coordinator setPaymentRequest:payment_request.get()];
EXPECT_EQ(1u, navigation_controller.viewControllers.count);
[coordinator start];
// Short delay to allow animation to complete.
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_EQ(2u, navigation_controller.viewControllers.count);
[coordinator stop];
// Short delay to allow animation to complete.
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_EQ(1u, navigation_controller.viewControllers.count);
}
// Tests that calling the view controller delegate method which notifies the
// coordinator that the user has confirmed the payment request invokes the
// the corresponding coordinator delegate method.
TEST(PaymentItemsDisplayCoordinatorTest, DidConfirm) {
std::unique_ptr<PaymentRequest> payment_request =
payment_request_test_util::CreateTestPaymentRequest();
UIViewController* base_view_controller = [[UIViewController alloc] init];
UINavigationController* navigation_controller =
[[UINavigationController alloc]
initWithRootViewController:base_view_controller];
PaymentItemsDisplayCoordinator* coordinator =
[[PaymentItemsDisplayCoordinator alloc]
initWithBaseViewController:base_view_controller];
[coordinator setPaymentRequest:payment_request.get()];
// Mock the coordinator delegate.
id delegate = [OCMockObject
mockForProtocol:@protocol(PaymentItemsDisplayCoordinatorDelegate)];
[[delegate expect] paymentItemsDisplayCoordinatorDidConfirm:coordinator];
[coordinator setDelegate:delegate];
EXPECT_EQ(1u, navigation_controller.viewControllers.count);
[coordinator start];
// Short delay to allow animation to complete.
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_EQ(2u, navigation_controller.viewControllers.count);
// Call the controller delegate method.
PaymentItemsDisplayViewController* view_controller =
base::mac::ObjCCastStrict<PaymentItemsDisplayViewController>(
navigation_controller.visibleViewController);
[coordinator paymentItemsDisplayViewControllerDidConfirm:view_controller];
EXPECT_OCMOCK_VERIFY(delegate);
}
// Tests that calling the view controller delegate method which notifies the
// coordinator that the user has chosen to return to the previous screen invokes
// the corresponding coordinator delegate method.
TEST(PaymentItemsDisplayCoordinatorTest, DidReturn) {
std::unique_ptr<PaymentRequest> payment_request =
payment_request_test_util::CreateTestPaymentRequest();
UIViewController* base_view_controller = [[UIViewController alloc] init];
UINavigationController* navigation_controller =
[[UINavigationController alloc]
initWithRootViewController:base_view_controller];
PaymentItemsDisplayCoordinator* coordinator =
[[PaymentItemsDisplayCoordinator alloc]
initWithBaseViewController:base_view_controller];
[coordinator setPaymentRequest:payment_request.get()];
// Mock the coordinator delegate.
id delegate = [OCMockObject
mockForProtocol:@protocol(PaymentItemsDisplayCoordinatorDelegate)];
[[delegate expect] paymentItemsDisplayCoordinatorDidReturn:coordinator];
[coordinator setDelegate:delegate];
EXPECT_EQ(1u, navigation_controller.viewControllers.count);
[coordinator start];
// Short delay to allow animation to complete.
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_EQ(2u, navigation_controller.viewControllers.count);
// Call the controller delegate method.
PaymentItemsDisplayViewController* view_controller =
base::mac::ObjCCastStrict<PaymentItemsDisplayViewController>(
navigation_controller.visibleViewController);
[coordinator paymentItemsDisplayViewControllerDidReturn:view_controller];
EXPECT_OCMOCK_VERIFY(delegate);
}