Chromium Code Reviews| Index: ios/chrome/browser/payments/payment_request_selector_view_controller_unittest.mm |
| diff --git a/ios/chrome/browser/payments/payment_request_selector_view_controller_unittest.mm b/ios/chrome/browser/payments/payment_request_selector_view_controller_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f989abb1f9527553ea967a02151613e6c63c9533 |
| --- /dev/null |
| +++ b/ios/chrome/browser/payments/payment_request_selector_view_controller_unittest.mm |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2017 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_request_selector_view_controller.h" |
| + |
| +#include "base/mac/foundation_util.h" |
| +#import "base/mac/scoped_nsobject.h" |
| +#import "ios/chrome/browser/payments/cells/payments_text_item.h" |
| +#import "ios/chrome/browser/payments/payment_request_selector_view_controller_data_source.h" |
| +#import "ios/chrome/browser/ui/autofill/cells/status_item.h" |
| +#import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h" |
| +#import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +@interface TestPaymentRequestSelectorMediator |
| + : NSObject<PaymentRequestSelectorViewControllerDataSource> |
| + |
| +@property(nonatomic, strong) NSArray<CollectionViewItem*>* items; |
|
lpromero
2017/04/11 17:30:19
Convention is to use s/strong/copy as the type has
Moe
2017/04/14 06:05:49
Done.
|
| +@property(nonatomic, assign) NSUInteger selectedItemIndex; |
| + |
| +@end |
| + |
| +@implementation TestPaymentRequestSelectorMediator |
| + |
| +@synthesize state = _state; |
| +@synthesize selectedItemIndex = _selectedItemIndex; |
| +@synthesize items = _items; |
| + |
| +- (instancetype)init { |
| + self = [super init]; |
| + if (self) { |
| + _items = @[ |
| + [[CollectionViewTextItem alloc] initWithType:kItemTypeSelectableItem], |
| + [[CollectionViewTextItem alloc] initWithType:kItemTypeSelectableItem] |
| + ]; |
| + _selectedItemIndex = 0; |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark - PaymentRequestSelectorViewControllerDataSource |
| + |
| +- (CollectionViewItem*)headerItem { |
| + return [[CollectionViewItem alloc] initWithType:kItemTypeHeaderItem]; |
| +} |
| + |
| +- (NSArray<CollectionViewItem*>*)selectableItems { |
| + return _items; |
| +} |
| + |
| +- (CollectionViewItem*)selectableItemAtIndex:(NSUInteger)index { |
| + return [_items objectAtIndex:index]; |
| +} |
| + |
| +- (NSString*)addButtonTitle { |
| + return @"Add item"; |
| +} |
| + |
| +@end |
| + |
| +class PaymentRequestSelectorViewControllerTest |
| + : public CollectionViewControllerTest { |
| + protected: |
| + PaymentRequestSelectorViewControllerTest() {} |
| + |
| + CollectionViewController* NewController() override NS_RETURNS_RETAINED { |
| + PaymentRequestSelectorViewController* viewController = |
| + [[PaymentRequestSelectorViewController alloc] init]; |
| + mediator_.reset([[TestPaymentRequestSelectorMediator alloc] init]); |
| + [viewController setDataSource:mediator_.get()]; |
| + return viewController; |
| + } |
| + |
| + PaymentRequestSelectorViewController* |
| + GetPaymentRequestSelectorViewController() { |
| + return base::mac::ObjCCastStrict<PaymentRequestSelectorViewController>( |
| + controller()); |
| + } |
| + |
| + base::scoped_nsobject<TestPaymentRequestSelectorMediator> mediator_; |
| +}; |
| + |
| +// Tests that the correct number of items are displayed after loading the model |
| +// and that the correct item appears to be selected. |
| +TEST_F(PaymentRequestSelectorViewControllerTest, TestModel) { |
| + CreateController(); |
| + CheckController(); |
| + |
| + [GetPaymentRequestSelectorViewController() loadModel]; |
| + |
| + ASSERT_EQ(1, NumberOfSections()); |
| + // One header item, two selectable items, and one add button. |
| + ASSERT_EQ(4, NumberOfItemsInSection(0)); |
| + |
| + // The first item should be of type CollectionViewItem. |
| + id item = GetCollectionViewItem(0, 0); |
| + ASSERT_TRUE([item isMemberOfClass:[CollectionViewItem class]]); |
| + |
| + // The next two selectable items should be of type CollectionViewTextItem. The |
| + // first one should appear to be selected. |
| + item = GetCollectionViewItem(0, 1); |
| + ASSERT_TRUE([item isMemberOfClass:[CollectionViewTextItem class]]); |
| + CollectionViewTextItem* selectable_item = item; |
| + EXPECT_EQ(MDCCollectionViewCellAccessoryCheckmark, |
| + selectable_item.accessoryType); |
| + |
| + item = GetCollectionViewItem(0, 2); |
| + ASSERT_TRUE([item isMemberOfClass:[CollectionViewTextItem class]]); |
| + selectable_item = item; |
| + EXPECT_EQ(MDCCollectionViewCellAccessoryNone, selectable_item.accessoryType); |
| + |
| + // The last item should be of type PaymentsTextItem. |
| + item = GetCollectionViewItem(0, 3); |
| + EXPECT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]); |
| +} |
| + |
| +// Tests that the correct number of items are displayed after loading the model |
| +// in the pending state. |
| +TEST_F(PaymentRequestSelectorViewControllerTest, TestModelPendingState) { |
| + CreateController(); |
| + CheckController(); |
| + |
| + [mediator_ setState:PaymentRequestSelectorViewControllerStatePending]; |
| + [GetPaymentRequestSelectorViewController() loadModel]; |
| + |
| + ASSERT_EQ(1, NumberOfSections()); |
| + // There should be only one item. |
| + ASSERT_EQ(1, NumberOfItemsInSection(0)); |
| + |
| + // The item should be of type StatusItem. |
| + id item = GetCollectionViewItem(0, 0); |
| + EXPECT_TRUE([item isMemberOfClass:[StatusItem class]]); |
| +} |