OLD | NEW |
(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/payments/payment_request_selector_view_controller.h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
| 9 #import "ios/chrome/browser/payments/payment_request_selector_view_controller_da
ta_source.h" |
| 10 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/collection_view_controller_test.h
" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 18 @interface TestPaymentRequestSelectorMediator |
| 19 : NSObject<PaymentRequestSelectorViewControllerDataSource> |
| 20 |
| 21 @property(nonatomic, copy) NSArray<CollectionViewItem*>* items; |
| 22 |
| 23 @property(nonatomic, readwrite, assign) PaymentRequestSelectorState state; |
| 24 |
| 25 @end |
| 26 |
| 27 @implementation TestPaymentRequestSelectorMediator |
| 28 |
| 29 @synthesize state = _state; |
| 30 @synthesize selectedItemIndex = _selectedItemIndex; |
| 31 @synthesize items = _items; |
| 32 |
| 33 - (instancetype)init { |
| 34 self = [super init]; |
| 35 if (self) { |
| 36 _items = |
| 37 @[ [[PaymentsTextItem alloc] init], [[PaymentsTextItem alloc] init] ]; |
| 38 _selectedItemIndex = 0; |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 #pragma mark - PaymentRequestSelectorViewControllerDataSource |
| 44 |
| 45 - (CollectionViewItem*)headerItem { |
| 46 return [[CollectionViewItem alloc] init]; |
| 47 } |
| 48 |
| 49 - (NSArray<CollectionViewItem*>*)selectableItems { |
| 50 return _items; |
| 51 } |
| 52 |
| 53 - (CollectionViewItem*)selectableItemAtIndex:(NSUInteger)index { |
| 54 return [_items objectAtIndex:index]; |
| 55 } |
| 56 |
| 57 - (CollectionViewItem*)addButtonItem { |
| 58 return [[CollectionViewItem alloc] init]; |
| 59 } |
| 60 |
| 61 @end |
| 62 |
| 63 class PaymentRequestSelectorViewControllerTest |
| 64 : public CollectionViewControllerTest { |
| 65 protected: |
| 66 PaymentRequestSelectorViewControllerTest() {} |
| 67 |
| 68 CollectionViewController* InstantiateController() override { |
| 69 PaymentRequestSelectorViewController* viewController = |
| 70 [[PaymentRequestSelectorViewController alloc] init]; |
| 71 mediator_ = [[TestPaymentRequestSelectorMediator alloc] init]; |
| 72 [viewController setDataSource:mediator_]; |
| 73 return viewController; |
| 74 } |
| 75 |
| 76 PaymentRequestSelectorViewController* |
| 77 GetPaymentRequestSelectorViewController() { |
| 78 return base::mac::ObjCCastStrict<PaymentRequestSelectorViewController>( |
| 79 controller()); |
| 80 } |
| 81 |
| 82 TestPaymentRequestSelectorMediator* mediator_; |
| 83 }; |
| 84 |
| 85 // Tests that the correct number of items are displayed after loading the model |
| 86 // and that the correct item appears to be selected. |
| 87 TEST_F(PaymentRequestSelectorViewControllerTest, TestModel) { |
| 88 CreateController(); |
| 89 CheckController(); |
| 90 |
| 91 [GetPaymentRequestSelectorViewController() loadModel]; |
| 92 |
| 93 ASSERT_EQ(1, NumberOfSections()); |
| 94 // One header item, two selectable items, and one add button. |
| 95 ASSERT_EQ(4, NumberOfItemsInSection(0)); |
| 96 |
| 97 // The first item should be of type CollectionViewItem. |
| 98 id item = GetCollectionViewItem(0, 0); |
| 99 ASSERT_TRUE([item isMemberOfClass:[CollectionViewItem class]]); |
| 100 |
| 101 // The next two selectable items should be of type PaymentsTextItem. The first |
| 102 // one should appear to be selected. |
| 103 item = GetCollectionViewItem(0, 1); |
| 104 ASSERT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]); |
| 105 PaymentsTextItem* selectable_item = item; |
| 106 EXPECT_EQ(MDCCollectionViewCellAccessoryCheckmark, |
| 107 selectable_item.accessoryType); |
| 108 |
| 109 item = GetCollectionViewItem(0, 2); |
| 110 ASSERT_TRUE([item isMemberOfClass:[PaymentsTextItem class]]); |
| 111 selectable_item = item; |
| 112 EXPECT_EQ(MDCCollectionViewCellAccessoryNone, selectable_item.accessoryType); |
| 113 |
| 114 // The last item should be of type CollectionViewItem. |
| 115 item = GetCollectionViewItem(0, 3); |
| 116 EXPECT_TRUE([item isMemberOfClass:[CollectionViewItem class]]); |
| 117 } |
| 118 |
| 119 // Tests that the correct number of items are displayed after loading the model |
| 120 // in the pending state. |
| 121 TEST_F(PaymentRequestSelectorViewControllerTest, TestModelPendingState) { |
| 122 CreateController(); |
| 123 CheckController(); |
| 124 |
| 125 [mediator_ setState:PaymentRequestSelectorStatePending]; |
| 126 [GetPaymentRequestSelectorViewController() loadModel]; |
| 127 |
| 128 ASSERT_EQ(1, NumberOfSections()); |
| 129 // There should be only one item. |
| 130 ASSERT_EQ(1, NumberOfItemsInSection(0)); |
| 131 |
| 132 // The item should be of type StatusItem. |
| 133 id item = GetCollectionViewItem(0, 0); |
| 134 EXPECT_TRUE([item isMemberOfClass:[StatusItem class]]); |
| 135 } |
OLD | NEW |