blob: 3c69fe4e94eb30a6cc54671633fe90868c0d9e09 [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/shipping_address_selection_view_controller.h"
#include "base/mac/foundation_util.h"
#include "base/memory/ptr_util.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/payments/cells/autofill_profile_item.h"
#import "ios/chrome/browser/payments/cells/payments_text_item.h"
#include "ios/chrome/browser/payments/payment_request.h"
#include "ios/chrome/browser/payments/payment_request_test_util.h"
#import "ios/chrome/browser/ui/autofill/cells/status_item.h"
#import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h"
#include "ios/web/public/payments/payment_request.h"
#include "testing/gtest/include/gtest/gtest.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
class PaymentRequestShippingAddressSelectionViewControllerTest
: public CollectionViewControllerTest {
protected:
PaymentRequestShippingAddressSelectionViewControllerTest()
: autofill_profile1_(autofill::test::GetFullProfile()),
autofill_profile2_(autofill::test::GetFullProfile2()) {
// Add testing profiles to autofill::TestPersonalDataManager.
personal_data_manager_.AddTestingProfile(&autofill_profile1_);
personal_data_manager_.AddTestingProfile(&autofill_profile2_);
}
CollectionViewController* InstantiateController() override {
payment_request_ = base::MakeUnique<PaymentRequest>(
payment_request_test_util::CreateTestWebPaymentRequest(),
&personal_data_manager_);
return [[ShippingAddressSelectionViewController alloc]
initWithPaymentRequest:payment_request_.get()];
}
ShippingAddressSelectionViewController*
GetShippingAddressSelectionViewController() {
return base::mac::ObjCCastStrict<ShippingAddressSelectionViewController>(
controller());
}
autofill::AutofillProfile autofill_profile1_;
autofill::AutofillProfile autofill_profile2_;
autofill::TestPersonalDataManager personal_data_manager_;
std::unique_ptr<PaymentRequest> payment_request_;
};
// Tests that the correct number of items are displayed after loading the model
// and that the correct item appears to be selected.
TEST_F(PaymentRequestShippingAddressSelectionViewControllerTest, TestModel) {
CreateController();
CheckController();
CheckTitleWithId(IDS_PAYMENTS_SHIPPING_ADDRESS_LABEL);
[GetShippingAddressSelectionViewController() loadModel];
ASSERT_EQ(1, NumberOfSections());
// One item for each of shipping addresses plus 2 for the message and the add
// button.
ASSERT_EQ(4U, static_cast<unsigned int>(NumberOfItemsInSection(0)));
// The first item should be of type TextAndMessageItem.
id item = GetCollectionViewItem(0, 0);
EXPECT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]);
// The next two items should be of type AutofillProfileItem. The first one
// should appear to be selected.
item = GetCollectionViewItem(0, 1);
ASSERT_TRUE([item isMemberOfClass:[AutofillProfileItem class]]);
AutofillProfileItem* shipping_address_item = item;
EXPECT_EQ(MDCCollectionViewCellAccessoryCheckmark,
shipping_address_item.accessoryType);
item = GetCollectionViewItem(0, 2);
EXPECT_TRUE([item isMemberOfClass:[AutofillProfileItem class]]);
shipping_address_item = item;
EXPECT_EQ(MDCCollectionViewCellAccessoryNone,
shipping_address_item.accessoryType);
// The last item should also be of type TextAndMessageItem.
item = GetCollectionViewItem(0, 3);
EXPECT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]);
// Test the error state.
[GetShippingAddressSelectionViewController() setErrorMessage:@"Lorem ipsum"];
[GetShippingAddressSelectionViewController() loadModel];
ASSERT_EQ(1, NumberOfSections());
// There should stil be 4 items in total.
ASSERT_EQ(4U, static_cast<unsigned int>(NumberOfItemsInSection(0)));
// Test the pending state.
[GetShippingAddressSelectionViewController() setPending:YES];
[GetShippingAddressSelectionViewController() loadModel];
ASSERT_EQ(1, NumberOfSections());
// There should be only one item.
ASSERT_EQ(1U, static_cast<unsigned int>(NumberOfItemsInSection(0)));
// The item should be of type StatusItem.
item = GetCollectionViewItem(0, 0);
EXPECT_TRUE([item isMemberOfClass:[StatusItem class]]);
}