|
| 1 | +// Copyright 2021-present the Material Components for iOS authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#import "MaterialSnapshot.h" |
| 16 | + |
| 17 | +#import "MaterialActionSheet.h" |
| 18 | + |
| 19 | +/** |
| 20 | + Snapshot test for the @c viewForAction: API. |
| 21 | + */ |
| 22 | +@interface MDCActionSheetControllerViewForActionSnapshotTests : MDCSnapshotTestCase |
| 23 | +/** The @c MDCActionSheetController being tested. */ |
| 24 | +@property(nonatomic, strong, nullable) MDCActionSheetController *actionSheet; |
| 25 | + |
| 26 | +/** An Action Sheet action. */ |
| 27 | +@property(nonatomic, strong) MDCActionSheetAction *action1; |
| 28 | + |
| 29 | +/** An Action Sheet action. */ |
| 30 | +@property(nonatomic, strong) MDCActionSheetAction *action2; |
| 31 | + |
| 32 | +@end |
| 33 | + |
| 34 | +@implementation MDCActionSheetControllerViewForActionSnapshotTests |
| 35 | + |
| 36 | +- (void)setUp { |
| 37 | + [super setUp]; |
| 38 | + |
| 39 | + // Uncomment below to recreate all the goldens (or add the following line to the specific |
| 40 | + // test you wish to recreate the golden for). |
| 41 | + // self.recordMode = YES; |
| 42 | + |
| 43 | + self.actionSheet = [[MDCActionSheetController alloc] init]; |
| 44 | + self.action1 = [MDCActionSheetAction |
| 45 | + actionWithTitle:@"Foo" |
| 46 | + image:[UIImage mdc_testImageOfSize:CGSizeMake(24, 24) |
| 47 | + withStyle:MDCSnapshotTestImageStyleCheckerboard] |
| 48 | + handler:nil]; |
| 49 | + self.action2 = [MDCActionSheetAction |
| 50 | + actionWithTitle:@"Bar" |
| 51 | + image:[UIImage mdc_testImageOfSize:CGSizeMake(24, 24) |
| 52 | + withStyle:MDCSnapshotTestImageStyleEllipses] |
| 53 | + handler:nil]; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)tearDown { |
| 57 | + if (self.actionSheet.presentingViewController) { |
| 58 | + XCTestExpectation *expectation = |
| 59 | + [[XCTestExpectation alloc] initWithDescription:@"Action sheet is dismissed"]; |
| 60 | + [self.actionSheet dismissViewControllerAnimated:NO |
| 61 | + completion:^{ |
| 62 | + [expectation fulfill]; |
| 63 | + }]; |
| 64 | + [self waitForExpectations:@[ expectation ] timeout:5]; |
| 65 | + } |
| 66 | + self.action1 = nil; |
| 67 | + self.action2 = nil; |
| 68 | + self.actionSheet = nil; |
| 69 | + |
| 70 | + [super tearDown]; |
| 71 | +} |
| 72 | + |
| 73 | +- (void)testOneActionAddedToActionSheet { |
| 74 | + // Given |
| 75 | + [self.actionSheet addAction:self.action1]; |
| 76 | + |
| 77 | + // When |
| 78 | + UIView *view = [self.actionSheet viewForAction:self.action1]; |
| 79 | + |
| 80 | + // Then |
| 81 | + [self generateSnapshotAndVerifyForView:view]; |
| 82 | +} |
| 83 | + |
| 84 | +- (void)testMultipleActionsAddedToActionSheetViewForFirstIndex { |
| 85 | + // Given |
| 86 | + [self.actionSheet addAction:self.action1]; |
| 87 | + [self.actionSheet addAction:self.action2]; |
| 88 | + |
| 89 | + // When |
| 90 | + UIView *view = [self.actionSheet viewForAction:self.action1]; |
| 91 | + |
| 92 | + // Then |
| 93 | + [self generateSnapshotAndVerifyForView:view]; |
| 94 | +} |
| 95 | + |
| 96 | +- (void)testMultipleActionsAddedToActionSheetViewForLastIndex { |
| 97 | + // Given |
| 98 | + [self.actionSheet addAction:self.action1]; |
| 99 | + [self.actionSheet addAction:self.action2]; |
| 100 | + |
| 101 | + // When |
| 102 | + UIView *view = [self.actionSheet viewForAction:self.action2]; |
| 103 | + |
| 104 | + // Then |
| 105 | + [self generateSnapshotAndVerifyForView:view]; |
| 106 | +} |
| 107 | + |
| 108 | +#pragma mark - Helpers |
| 109 | + |
| 110 | +- (void)generateSnapshotAndVerifyForView:(UIView *)view { |
| 111 | + UIView *snapshotView = [view mdc_addToBackgroundView]; |
| 112 | + [self snapshotVerifyView:snapshotView]; |
| 113 | +} |
| 114 | + |
| 115 | +@end |
0 commit comments