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