Skip to content

Commit

Permalink
[ActionSheet] Add accessibility identifier (#4944)
Browse files Browse the repository at this point in the history
Add accessibility identifier to `MDCActionSheetAction`.
  • Loading branch information
codeman7 committed Aug 30, 2018
1 parent 0d1f514 commit 6cd0f77
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
7 changes: 6 additions & 1 deletion components/ActionSheet/src/MDCActionSheetController.h
Expand Up @@ -162,7 +162,7 @@ typedef void (^MDCActionSheetHandler)(MDCActionSheetAction *_Nonnull action);
An instance of MDCActionSheetAction is passed to MDCActionSheetController to
add an action to the action sheet.
*/
@interface MDCActionSheetAction : NSObject <NSCopying>
@interface MDCActionSheetAction : NSObject <NSCopying, UIAccessibilityIdentification>


/**
Expand Down Expand Up @@ -198,4 +198,9 @@ typedef void (^MDCActionSheetHandler)(MDCActionSheetAction *_Nonnull action);
*/
@property (nonatomic, nullable, readonly) UIImage *image;

/**
The @c accessibilityIdentifier for the view associated with this action.
*/
@property(nonatomic, nullable, copy) NSString *accessibilityIdentifier;

@end
5 changes: 4 additions & 1 deletion components/ActionSheet/src/MDCActionSheetController.m
Expand Up @@ -55,6 +55,7 @@ - (id)copyWithZone:(__unused NSZone *)zone {
MDCActionSheetAction *action = [[self class] actionWithTitle:self.title
image:self.image
handler:self.completionHandler];
action.accessibilityIdentifier = self.accessibilityIdentifier;
return action;
}

Expand Down Expand Up @@ -256,10 +257,12 @@ - (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MDCActionSheetItemTableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:ReuseIdentifier forIndexPath:indexPath];
cell.action = _actions[indexPath.row];
MDCActionSheetAction *action = _actions[indexPath.row];
cell.action = action;
cell.mdc_adjustsFontForContentSizeCategory = self.mdc_adjustsFontForContentSizeCategory;
cell.backgroundColor = self.backgroundColor;
cell.actionFont = self.actionFont;
cell.accessibilityIdentifier = action.accessibilityIdentifier;
return cell;
}

Expand Down
38 changes: 32 additions & 6 deletions components/ActionSheet/tests/unit/ActionSheetTest.swift
Expand Up @@ -18,10 +18,14 @@ import XCTest
import MaterialComponentsAlpha.MaterialActionSheet

class ActionSheetTest: XCTestCase {
func testNumberOfActions() {
// Given
let actionSheet = MDCActionSheetController(title: nil)

var actionSheet: MDCActionSheetController!

override func setUp() {
actionSheet = MDCActionSheetController()
}

func testNumberOfActions() {
// Then
XCTAssertEqual(actionSheet.actions.count, 0)

Expand All @@ -35,9 +39,6 @@ class ActionSheetTest: XCTestCase {
}

func testTitleChange() {
// Given
let actionSheet = MDCActionSheetController(title: nil)

// Then
XCTAssertEqual(actionSheet.title, nil)

Expand All @@ -47,4 +48,29 @@ class ActionSheetTest: XCTestCase {
// Then
XCTAssertEqual(actionSheet.title, "New title")
}

func testAccessibilityIdentifiers() {
// Given
let rowCount = 1
let section = 0
let testIdentifier = "Test"

// When
let action = MDCActionSheetAction(title: "Title", image: nil, handler: nil)
action.accessibilityIdentifier = testIdentifier
actionSheet.addAction(action)

// Then
XCTAssertEqual(actionSheet.view.subviews.count, 2)
let tableView = actionSheet.view.subviews.flatMap{ $0 as? UITableView }.first
if let table = tableView {
XCTAssertEqual(table.numberOfRows(inSection: section), rowCount)
if let cell = table.cellForRow(at: IndexPath(row: rowCount - 1, section: section)) {
XCTAssertEqual(cell.accessibilityIdentifier, testIdentifier)
} else {
XCTFail("Cell wasn't loaded")
}
}

}
}

0 comments on commit 6cd0f77

Please sign in to comment.