Skip to content

Commit 1524182

Browse files
codeman7material-automation
authored andcommitted
[ActionSheet] Allow clients to get the view associated with a given action.
PiperOrigin-RevId: 366489478
1 parent 53e1d5e commit 1524182

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed

components/ActionSheet/src/MDCActionSheetController.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ __attribute__((objc_subclassing_restricted)) @interface MDCActionSheetController
9393
*/
9494
- (void)addAction:(nonnull MDCActionSheetAction *)action;
9595

96+
/**
97+
Returns the view associated with a given @c action.
98+
99+
@param action The action used to create the view.
100+
*/
101+
- (nullable UIView *)viewForAction:(nonnull MDCActionSheetAction *)action;
102+
96103
/**
97104
The object that acts as the delegate of the @c MDCActionSheetController
98105
*/

components/ActionSheet/src/MDCActionSheetController.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
#import "private/MDCActionSheetItemTableViewCell.h"
1919
#import "private/MaterialActionSheetStrings.h"
2020
#import "private/MaterialActionSheetStrings_table.h"
21+
#import "MDCActionSheetAction.h"
2122
#import "MDCActionSheetControllerDelegate.h"
2223
#import "MaterialAvailability.h"
24+
#import "MaterialBottomSheet.h"
25+
#import "MaterialElevation.h"
2326
#import "MaterialShadowElevations.h"
2427
#import "MaterialTypography.h"
2528
#import "MaterialMath.h"
@@ -184,6 +187,16 @@ - (void)addAction:(MDCActionSheetAction *)action {
184187
[self updateTable];
185188
}
186189

190+
- (UIView *)viewForAction:(MDCActionSheetAction *)action {
191+
if (![self.actions containsObject:action]) {
192+
return nil;
193+
}
194+
[self.view layoutIfNeeded];
195+
NSUInteger rowIndex = [self.actions indexOfObject:action];
196+
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
197+
return [self.tableView cellForRowAtIndexPath:indexPath];
198+
}
199+
187200
- (NSArray<MDCActionSheetAction *> *)actions {
188201
return [_actions copy];
189202
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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

components/ActionSheet/tests/unit/MDCActionSheetControllerTests.m

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,58 @@ - (void)testTableViewContentInsetsWithHeaderDividerViewAndNoTitleOrMessage {
569569
XCTAssertGreaterThan(self.actionSheet.tableView.contentInset.top, originalTableContentInset);
570570
}
571571

572+
- (void)testViewForActionWhenNoActionsAdded {
573+
// Given
574+
self.actionSheet.view.bounds = CGRectMake(0, 0, 500, 500);
575+
MDCActionSheetAction *action = [MDCActionSheetAction actionWithTitle:@"Foo"
576+
image:nil
577+
handler:nil];
578+
579+
// When
580+
UIView *view = [self.actionSheet viewForAction:action];
581+
582+
// Then
583+
XCTAssertNil(view);
584+
}
585+
586+
- (void)testViewForActionWhenMultipleActionsAreAdded {
587+
// Given
588+
self.actionSheet.view.bounds = CGRectMake(0, 0, 500, 500);
589+
MDCActionSheetAction *actionOne = [MDCActionSheetAction actionWithTitle:@"Foo"
590+
image:nil
591+
handler:nil];
592+
MDCActionSheetAction *actionTwo = [MDCActionSheetAction actionWithTitle:@"Bar"
593+
image:nil
594+
handler:nil];
595+
[self.actionSheet addAction:actionOne];
596+
[self.actionSheet addAction:actionTwo];
597+
598+
// When
599+
UIView *view = [self.actionSheet viewForAction:actionTwo];
600+
601+
// Then
602+
XCTAssertNotNil(view);
603+
}
604+
605+
- (void)testViewForActionWhenActionHasNotBeenAddedThenAdded {
606+
// Given
607+
self.actionSheet.view.bounds = CGRectMake(0, 0, 500, 500);
608+
MDCActionSheetAction *action = [MDCActionSheetAction actionWithTitle:@"Foo"
609+
image:nil
610+
handler:nil];
611+
612+
// When
613+
UIView *view = [self.actionSheet viewForAction:action];
614+
615+
// Then
616+
XCTAssertNil(view);
617+
618+
// When
619+
[self.actionSheet addAction:action];
620+
view = [self.actionSheet viewForAction:action];
621+
622+
// Then
623+
XCTAssertNotNil(view);
624+
}
625+
572626
@end

0 commit comments

Comments
 (0)