From f60be3313114c06309803be2f17f029155ee0c3d Mon Sep 17 00:00:00 2001 From: lightboys22 Date: Thu, 6 Dec 2018 09:52:35 -0800 Subject: [PATCH] [ActionSheet] Allow changing action item accessibility labels. (#5803) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit i.e.: We need to show the title as "Resume". But the voice over message should be "Résumé" instead of "Rezoom". --- .../ActionSheet/src/MDCActionSheetController.m | 1 + .../private/MDCActionSheetItemTableViewCell.m | 2 ++ .../tests/unit/MDCActionSheetTableCellTest.m | 16 ++++++++++++++++ .../tests/unit/MDCActionSheetTestHelper.h | 3 +++ .../tests/unit/MDCActionSheetTestHelper.m | 13 +++++++++++++ 5 files changed, 35 insertions(+) diff --git a/components/ActionSheet/src/MDCActionSheetController.m b/components/ActionSheet/src/MDCActionSheetController.m index 140b77d5c98..f0a7fba94c1 100644 --- a/components/ActionSheet/src/MDCActionSheetController.m +++ b/components/ActionSheet/src/MDCActionSheetController.m @@ -56,6 +56,7 @@ - (id)copyWithZone:(__unused NSZone *)zone { image:self.image handler:self.completionHandler]; action.accessibilityIdentifier = self.accessibilityIdentifier; + action.accessibilityLabel = self.accessibilityLabel; return action; } diff --git a/components/ActionSheet/src/private/MDCActionSheetItemTableViewCell.m b/components/ActionSheet/src/private/MDCActionSheetItemTableViewCell.m index 8f8cab6e147..dd584fcfb10 100644 --- a/components/ActionSheet/src/private/MDCActionSheetItemTableViewCell.m +++ b/components/ActionSheet/src/private/MDCActionSheetItemTableViewCell.m @@ -143,6 +143,7 @@ - (void)commonMDCActionSheetItemViewInit { - (void)layoutSubviews { [super layoutSubviews]; + self.actionLabel.accessibilityLabel = _itemAction.accessibilityLabel; self.actionLabel.text = _itemAction.title; CGFloat leadingConstant; if (_itemAction.image) { @@ -159,6 +160,7 @@ - (void)layoutSubviews { - (void)setAction:(MDCActionSheetAction *)action { _itemAction = [action copy]; + self.actionLabel.accessibilityLabel = _itemAction.accessibilityLabel; self.actionLabel.text = _itemAction.title; self.actionImageView.image = _itemAction.image; [self setNeedsLayout]; diff --git a/components/ActionSheet/tests/unit/MDCActionSheetTableCellTest.m b/components/ActionSheet/tests/unit/MDCActionSheetTableCellTest.m index 1ff422e922d..f4c6cdf8047 100644 --- a/components/ActionSheet/tests/unit/MDCActionSheetTableCellTest.m +++ b/components/ActionSheet/tests/unit/MDCActionSheetTableCellTest.m @@ -152,4 +152,20 @@ - (void)testSetActionFont { } } +- (void)testSetActionItemAccessibilityLabel { + // Given + MDCActionSheetAction *action = [MDCActionSheetAction actionWithTitle:@"Resume" + image:nil + handler:nil]; + action.accessibilityLabel = @"Résumé"; + + // When + [self.actionSheet addAction:action]; + MDCActionSheetItemTableViewCell *cell = + [MDCActionSheetTestHelper getCellFromActionSheet:self.actionSheet atIndex:0]; + + // Then + XCTAssertEqual(cell.actionLabel.accessibilityLabel, action.accessibilityLabel); +} + @end diff --git a/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.h b/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.h index 8e3f15a5977..9c69ab96e6c 100644 --- a/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.h +++ b/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.h @@ -35,4 +35,7 @@ + (NSArray *)getCellsFromActionSheet: (MDCActionSheetController *)actionSheet; ++ (MDCActionSheetItemTableViewCell *)getCellFromActionSheet:(MDCActionSheetController *)actionSheet + atIndex:(NSUInteger)index; + @end diff --git a/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.m b/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.m index 153c6b79398..d73f7d964bb 100644 --- a/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.m +++ b/components/ActionSheet/tests/unit/MDCActionSheetTestHelper.m @@ -64,4 +64,17 @@ + (void)addNumberOfActions:(NSUInteger)actionsCount return cellsArray; } ++ (MDCActionSheetItemTableViewCell *)getCellFromActionSheet:(MDCActionSheetController *)actionSheet + atIndex:(NSUInteger)index { + if (index < actionSheet.actions.count) { + NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0]; + UITableView *table = actionSheet.tableView; + UITableViewCell *cell = [table.dataSource tableView:table cellForRowAtIndexPath:indexPath]; + if ([cell isKindOfClass:[MDCActionSheetItemTableViewCell class]]) { + return (MDCActionSheetItemTableViewCell *)cell; + } + } + return nil; +} + @end