From 4dac8754d7496cc042b63f200823669d0cd0db93 Mon Sep 17 00:00:00 2001 From: Alex Odawa Date: Tue, 17 Sep 2024 20:18:22 +0200 Subject: [PATCH] added performAccessibilityActivateWithEpectedResult: to KIFUIViewTestActor --- KIF.xcodeproj/project.pbxproj | 8 + Sources/KIF/Classes/KIFUIViewTestActor.h | 5 + Sources/KIF/Classes/KIFUIViewTestActor.m | 19 +++ TestHost/AccessibilityViewController.m | 149 ++++++++++++++++++ TestHost/Base.lproj/MainStoryboard.storyboard | 63 +++++++- TestHost/TapViewController.m | 50 ------ ...cessibilityActivationTests_ViewTestActor.m | 36 +++++ Tests/CustomActionTests_ViewTestActor.m | 8 +- 8 files changed, 277 insertions(+), 61 deletions(-) create mode 100644 TestHost/AccessibilityViewController.m create mode 100644 Tests/AccessibilityActivationTests_ViewTestActor.m diff --git a/KIF.xcodeproj/project.pbxproj b/KIF.xcodeproj/project.pbxproj index 7089bf3c..8a6967e6 100644 --- a/KIF.xcodeproj/project.pbxproj +++ b/KIF.xcodeproj/project.pbxproj @@ -115,6 +115,8 @@ ACA242E42C3DA55400E6F1B6 /* CustomActionTests_ViewTestActor.m in Sources */ = {isa = PBXBuildFile; fileRef = ACA242E32C3DA55400E6F1B6 /* CustomActionTests_ViewTestActor.m */; }; ACA242E72C3DB47400E6F1B6 /* UIAccessibilityCustomAction+KIFAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = ACA242E52C3DB46A00E6F1B6 /* UIAccessibilityCustomAction+KIFAdditions.m */; }; ACA242E92C3DB4EA00E6F1B6 /* UIAccessibilityCustomAction+KIFAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = ACA242E82C3DB4B000E6F1B6 /* UIAccessibilityCustomAction+KIFAdditions.h */; settings = {ATTRIBUTES = (Private, ); }; }; + ACDB72682CA3146B00D9796E /* AccessibilityViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = ACDB72672CA3146B00D9796E /* AccessibilityViewController.m */; }; + ACDB726A2CA314A800D9796E /* AccessibilityActivationTests_ViewTestActor.m in Sources */ = {isa = PBXBuildFile; fileRef = ACDB72692CA314A800D9796E /* AccessibilityActivationTests_ViewTestActor.m */; }; AE62FCD01A1D20E5002B10DA /* WebViewTests.m in Sources */ = {isa = PBXBuildFile; fileRef = AE62FCCF1A1D20E5002B10DA /* WebViewTests.m */; }; AE62FCD61A1D2447002B10DA /* WebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AE62FCD51A1D2447002B10DA /* WebViewController.m */; }; AE62FCD81A1D2667002B10DA /* index.html in Resources */ = {isa = PBXBuildFile; fileRef = AE62FCD71A1D2667002B10DA /* index.html */; }; @@ -321,6 +323,8 @@ ACA242E32C3DA55400E6F1B6 /* CustomActionTests_ViewTestActor.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CustomActionTests_ViewTestActor.m; sourceTree = ""; }; ACA242E52C3DB46A00E6F1B6 /* UIAccessibilityCustomAction+KIFAdditions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIAccessibilityCustomAction+KIFAdditions.m"; sourceTree = ""; }; ACA242E82C3DB4B000E6F1B6 /* UIAccessibilityCustomAction+KIFAdditions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIAccessibilityCustomAction+KIFAdditions.h"; sourceTree = ""; }; + ACDB72672CA3146B00D9796E /* AccessibilityViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AccessibilityViewController.m; sourceTree = ""; }; + ACDB72692CA314A800D9796E /* AccessibilityActivationTests_ViewTestActor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AccessibilityActivationTests_ViewTestActor.m; sourceTree = ""; }; AE62FCCF1A1D20E5002B10DA /* WebViewTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebViewTests.m; sourceTree = ""; }; AE62FCD51A1D2447002B10DA /* WebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebViewController.m; sourceTree = ""; }; AE62FCD71A1D2667002B10DA /* index.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = index.html; sourceTree = ""; }; @@ -633,6 +637,7 @@ EB60ECC6177F8C83005A041A /* TestHost */ = { isa = PBXGroup; children = ( + ACDB72672CA3146B00D9796E /* AccessibilityViewController.m */, 3812FB601A1212A700335733 /* AnimationViewController.m */, 2CDEE1CA181DBED200DF6E63 /* PickerController.m */, CD19A99E1F46482200BF0325 /* CustomPickerController.m */, @@ -713,6 +718,7 @@ FA8DA74D1A77117A00E0C644 /* KIFUIViewTestActor Tests */ = { isa = PBXGroup; children = ( + ACDB72692CA314A800D9796E /* AccessibilityActivationTests_ViewTestActor.m */, FA4915641A7827D000A78E57 /* PickerTests_ViewTestActor.m */, B66B1BF5202BCF2000D0E4B2 /* AutocorrectTests_ViewTestActor.m */, CD4E91F11F479FF2005D530C /* CustomPickerTests_ViewTestActor.m */, @@ -987,6 +993,7 @@ FA8A3C5D1A772CD100206350 /* WaitForAbscenceTests_ViewTestActor.m in Sources */, FA8A3C5B1A77281900206350 /* WebViewTests_ViewTestActor.m in Sources */, EABD46B11857A0F300A5F081 /* SearchFieldTests.m in Sources */, + ACDB726A2CA314A800D9796E /* AccessibilityActivationTests_ViewTestActor.m in Sources */, EABD46B21857A0F300A5F081 /* CascadingFailureTests.m in Sources */, EABD46B31857A0F300A5F081 /* CompositionTests.m in Sources */, CD4E91F01F479A8C005D530C /* CustomPickerTests.m in Sources */, @@ -1057,6 +1064,7 @@ EB60ED02177F9032005A041A /* TapViewController.m in Sources */, 2CDEE1CB181DBED200DF6E63 /* PickerController.m in Sources */, EB60ED03177F9032005A041A /* TestSuiteViewController.m in Sources */, + ACDB72682CA3146B00D9796E /* AccessibilityViewController.m in Sources */, D9EA274318F05A6700D87E57 /* ScrollViewController.m in Sources */, 8EAA1EE229D3AF7A008F6029 /* OffscreenViewController.m in Sources */, 3812FB611A1212A700335733 /* AnimationViewController.m in Sources */, diff --git a/Sources/KIF/Classes/KIFUIViewTestActor.h b/Sources/KIF/Classes/KIFUIViewTestActor.h index 06d3b653..1803e5ff 100644 --- a/Sources/KIF/Classes/KIFUIViewTestActor.h +++ b/Sources/KIF/Classes/KIFUIViewTestActor.h @@ -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 diff --git a/Sources/KIF/Classes/KIFUIViewTestActor.m b/Sources/KIF/Classes/KIFUIViewTestActor.m index 5bf50547..8ae35815 100644 --- a/Sources/KIF/Classes/KIFUIViewTestActor.m +++ b/Sources/KIF/Classes/KIFUIViewTestActor.m @@ -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]; } @@ -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 diff --git a/TestHost/AccessibilityViewController.m b/TestHost/AccessibilityViewController.m new file mode 100644 index 00000000..068c125f --- /dev/null +++ b/TestHost/AccessibilityViewController.m @@ -0,0 +1,149 @@ +// +// AccessibilityViewController.m +// Test Host +// +// Created by Alex Odawa on 17/09/2024. +// + +#import + +@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 diff --git a/TestHost/Base.lproj/MainStoryboard.storyboard b/TestHost/Base.lproj/MainStoryboard.storyboard index 9f91fdb4..568e3418 100644 --- a/TestHost/Base.lproj/MainStoryboard.storyboard +++ b/TestHost/Base.lproj/MainStoryboard.storyboard @@ -3,7 +3,7 @@ - + @@ -268,12 +268,32 @@ + + + + + + + + + + + + + + - + @@ -291,7 +311,7 @@ - + @@ -311,7 +331,7 @@ - + @@ -329,7 +349,7 @@ - + @@ -2142,6 +2162,35 @@ Line Break + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2187,10 +2236,10 @@ Line Break - + - + diff --git a/TestHost/TapViewController.m b/TestHost/TapViewController.m index 1ed1c42a..db6b1a10 100644 --- a/TestHost/TapViewController.m +++ b/TestHost/TapViewController.m @@ -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 @@ -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 - diff --git a/Tests/AccessibilityActivationTests_ViewTestActor.m b/Tests/AccessibilityActivationTests_ViewTestActor.m new file mode 100644 index 00000000..131f3541 --- /dev/null +++ b/Tests/AccessibilityActivationTests_ViewTestActor.m @@ -0,0 +1,36 @@ +// +// AccessibilityActivateTests_ViewTestActor.m +// KIF Tests +// +// Created by Alex Odawa on 09/07/2024. +// + +#import + +@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 diff --git a/Tests/CustomActionTests_ViewTestActor.m b/Tests/CustomActionTests_ViewTestActor.m index acb9cc53..ddde51e7 100644 --- a/Tests/CustomActionTests_ViewTestActor.m +++ b/Tests/CustomActionTests_ViewTestActor.m @@ -15,7 +15,7 @@ @implementation CustomActionTests_ViewTestActor - (void)beforeEach { - [[viewTester usingLabel:@"Tapping"] tap]; + [[viewTester usingLabel:@"Accessibility"] tap]; } - (void)afterEach @@ -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