Skip to content

Commit

Permalink
added performAccessibilityActivateWithEpectedResult: to KIFUIViewTest…
Browse files Browse the repository at this point in the history
…Actor
  • Loading branch information
RoyalPineapple committed Sep 24, 2024
1 parent c36bb1f commit c2341aa
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 54 deletions.
5 changes: 5 additions & 0 deletions Sources/KIF/Classes/KIFUIViewTestActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ extern NSString *const inputFieldTestString;
*/
- (void)activateCustomActionWithName:(NSString *)name expectedResult:(BOOL)expectedResult;

/*!
@abstract Activates a found element via `accessibilityActivate()`.
@param expectedResult The expected boolean return from activation of the element.
*/
- (void)performAccessibilityActivateWithExpectedResult:(BOOL)expectedResult;

#pragma mark Waiting & Finding

Expand Down
19 changes: 19 additions & 0 deletions Sources/KIF/Classes/KIFUIViewTestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ - (void)swipeFromEdge:(UIRectEdge)edge
[self.actor swipeFromEdge:edge];
}

#pragma mark - Accesibility Actions

- (void)activateCustomActionWithName:(NSString *)name;{
[self activateCustomActionWithName:name expectedResult:YES];
}
Expand All @@ -414,12 +416,29 @@ - (void)activateCustomActionWithName:(NSString *)name expectedResult:(BOOL)expec

[self runBlock:^KIFTestStepResult(NSError **error) {
if([[found.element KIF_customActionWithName:name] KIF_activate] == expectedResult) {
[self waitForAnimationsToFinish];
return KIFTestStepResultSuccess;
}
[self waitForAnimationsToFinish];
return KIFTestStepResultFailure;
}];
}
}

- (void)performAccessibilityActivateWithExpectedResult:(BOOL)expectedResult;
{
@autoreleasepool {
KIFUIObject *found = [self _predicateSearchWithRequiresMatch:YES mustBeTappable:NO];

[self runBlock:^KIFTestStepResult(NSError **error) {
if([found.element accessibilityActivate] == expectedResult) {
[self waitForAnimationsToFinish];
return KIFTestStepResultSuccess;
}
[self waitForAnimationsToFinish];
return KIFTestStepResultFailure;
}];
}
}

#pragma mark - Scroll/Table/CollectionView Actions
Expand Down
149 changes: 149 additions & 0 deletions TestHost/AccessibilityViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
//
// AccessibilityViewController.m
// Test Host
//
// Created by Alex Odawa on 17/09/2024.
//

#import <UIKit/UIKit.h>

@interface AccessibilityViewController_AccessibilityView : UIView
@property (nonatomic, assign) BOOL activationReturnValue;
@property (nonatomic, assign) int activationCount;

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UISwitch *activationSwitch;


@end


@implementation AccessibilityViewController_AccessibilityView

- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
self.isAccessibilityElement = YES;
self.accessibilityLabel = @"AccessibilityView";


self.activationReturnValue = YES;

self.label = [[UILabel alloc] initWithFrame: CGRectZero];
[self addSubview:self.label];

self.backgroundColor = [UIColor systemTealColor];
self.label.text = @"Returns YES";


self.activationSwitch = [[UISwitch alloc] initWithFrame: CGRectZero];
[self addSubview: self.activationSwitch];

[self.activationSwitch setOn:self.activationReturnValue];
[self.activationSwitch addTarget: self action: @selector(toggleReturnValue) forControlEvents: UIControlEventValueChanged];
self.activationSwitch.accessibilityLabel = @"AccessibilitySwitch";

return self;
}

- (void)toggleReturnValue {
self.activationReturnValue = !self.activationReturnValue;

if (self.activationReturnValue == YES) {
self.backgroundColor = [UIColor systemTealColor];
self.label.text = @"Returns YES";
} else {
self.backgroundColor = [UIColor systemTealColor];
self.label.text = @"Returns NO";
}
[self setNeedsLayout];
}

-(void)layoutSubviews {
[super layoutSubviews];
[self.label sizeToFit];
self.label.frame = CGRectMake((self.frame.size.width - self.label.frame.size.width) / 2,
(self.frame.size.height - self.label.frame.size.height) / 2,
self.label.frame.size.width,
self.label.frame.size.height);

[self.activationSwitch sizeToFit];
self.activationSwitch.frame = CGRectMake((self.frame.size.width - self.activationSwitch.frame.size.width) / 2,
CGRectGetMaxY(self.label.frame) + 10 ,
self.activationSwitch.frame.size.width,
self.activationSwitch.frame.size.width);
}

- (BOOL)accessibilityActivate {
self.activationCount += 1;
self.accessibilityValue = [NSString stringWithFormat:@"Activated: %i", self.activationCount];
return self.activationReturnValue;
}

@end

@interface AccessibilityViewController : UIViewController
@property (weak, nonatomic) IBOutlet AccessibilityViewController_AccessibilityView *accessibilityView;

@end

@implementation AccessibilityViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.accessibilityView.accessibilityCustomActions = [self customActions];
}


- (NSArray *)customActions
{
NSArray *actions = @[self.customActionWithoutArgument, self.customActionWithArgument, self.customActionThatFails];
if (@available(iOS 13.0, *)) {
return [actions arrayByAddingObject: self.customActionWithBlock];
}
return actions;
}

- (UIAccessibilityCustomAction *)customActionWithBlock
{
if (@available(iOS 13.0, *)) {
return [[UIAccessibilityCustomAction alloc] initWithName: @"Action With block handler"
actionHandler:^BOOL(UIAccessibilityCustomAction * _Nonnull customAction) {
return YES;
}];
} else {
return nil;
}
}

- (UIAccessibilityCustomAction *)customActionWithoutArgument
{
return [[UIAccessibilityCustomAction alloc] initWithName:@"Action without argument" target:self selector:@selector(customActionHandlerWithoutArgument)];
}

- (UIAccessibilityCustomAction *)customActionWithArgument
{
return [[UIAccessibilityCustomAction alloc] initWithName:@"Action with argument" target:self selector:@selector(customActionHandlerWithArgument:)];
}

- (UIAccessibilityCustomAction *)customActionThatFails
{
return [[UIAccessibilityCustomAction alloc] initWithName:@"Action that fails" target:self selector:@selector(customActionThatFails)];
}

- (BOOL)customActionHandlerWithoutArgument
{
return YES;
}

- (BOOL)customActionHandlerWithArgument:(UIAccessibilityCustomAction *)action
{
return YES;
}

- (BOOL)customActionHandlerThatFails
{
return NO;
}

@end
50 changes: 0 additions & 50 deletions TestHost/TapViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ - (void)viewDidLoad
self.lineBreakLabel.accessibilityLabel = @"A\nB\nC\n\n";
self.stepper.isAccessibilityElement = YES;
self.stepper.accessibilityLabel = @"theStepper";
self.stepper.accessibilityCustomActions = self.customActions;
}

- (void)memoryWarningNotification:(NSNotification *)notification
Expand Down Expand Up @@ -96,56 +95,7 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
}


- (NSArray *)customActions
{
NSArray *actions = @[self.customActionWithoutArgument, self.customActionWithArgument, self.customActionThatFails];
if (@available(iOS 13.0, *)) {
return [actions arrayByAddingObject: self.customActionWithBlock];
}
return actions;
}

- (UIAccessibilityCustomAction *)customActionWithBlock
{
if (@available(iOS 13.0, *)) {
return [[UIAccessibilityCustomAction alloc] initWithName: @"Action With block handler"
actionHandler:^BOOL(UIAccessibilityCustomAction * _Nonnull customAction) {
return YES;
}];
} else {
return nil;
}
}

- (UIAccessibilityCustomAction *)customActionWithoutArgument
{
return [[UIAccessibilityCustomAction alloc] initWithName:@"Action without argument" target:self selector:@selector(customActionHandlerWithoutArgument)];
}

- (UIAccessibilityCustomAction *)customActionWithArgument
{
return [[UIAccessibilityCustomAction alloc] initWithName:@"Action with argument" target:self selector:@selector(customActionHandlerWithArgument:)];
}

- (UIAccessibilityCustomAction *)customActionThatFails
{
return [[UIAccessibilityCustomAction alloc] initWithName:@"Action that fails" target:self selector:@selector(customActionThatFails)];
}

- (BOOL)customActionHandlerWithoutArgument
{
return YES;
}

- (BOOL)customActionHandlerWithArgument:(UIAccessibilityCustomAction *)action
{
return YES;
}

- (BOOL)customActionHandlerThatFails
{
return NO;
}

#pragma mark - <UIImagePickerControllerDelegate>

Expand Down
36 changes: 36 additions & 0 deletions Tests/AccessibilityActivationTests_ViewTestActor.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// AccessibilityActivateTests_ViewTestActor.m
// KIF Tests
//
// Created by Alex Odawa on 09/07/2024.
//

#import <KIF/KIF.h>

@interface AccessibilityActivateTests_ViewTestActor : KIFTestCase
@end


@implementation AccessibilityActivateTests_ViewTestActor

- (void)beforeEach
{
[[viewTester usingLabel:@"Accessibility"] tap];
}

- (void)afterEach
{
[[[viewTester usingLabel:@"Test Suite"] usingTraits:UIAccessibilityTraitButton] tap];
}

- (void)testAccessibilityActivate
{
[[viewTester usingLabel:@"AccessibilityView"] performAccessibilityActivateWithExpectedResult: YES];
[[viewTester usingValue:@"Activated: 1"] waitForView];

[[viewTester usingLabel:@"AccessibilitySwitch"] setSwitchOn:false];
[[viewTester usingLabel:@"AccessibilityView"] performAccessibilityActivateWithExpectedResult: NO];
[[viewTester usingValue:@"Activated: 2"] waitForView];
}

@end
8 changes: 4 additions & 4 deletions Tests/CustomActionTests_ViewTestActor.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ @implementation CustomActionTests_ViewTestActor

- (void)beforeEach
{
[[viewTester usingLabel:@"Tapping"] tap];
[[viewTester usingLabel:@"Accessibility"] tap];
}

- (void)afterEach
Expand All @@ -26,14 +26,14 @@ - (void)afterEach
- (void)testCustomActions
{
if (@available(iOS 13.0, *)) {
[[viewTester usingLabel:@"theStepper"] activateCustomActionWithName:@"Action With block handler"];
[[viewTester usingLabel:@"AccessibilityView"] activateCustomActionWithName:@"Action With block handler"];
}

for (NSString *name in @[@"Action without argument", @"Action with argument"]) {
[[viewTester usingLabel:@"theStepper"] activateCustomActionWithName:name];
[[viewTester usingLabel:@"AccessibilityView"] activateCustomActionWithName:name];
}

[[viewTester usingLabel:@"theStepper"] activateCustomActionWithName:@"Action that fails" expectedResult:NO];
[[viewTester usingLabel:@"AccessibilityView"] activateCustomActionWithName:@"Action that fails" expectedResult:NO];
}

@end

0 comments on commit c2341aa

Please sign in to comment.