Skip to content

Commit

Permalink
Addressing code review comments from @justinseanmartin
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley Cooper committed Feb 8, 2018
1 parent 069614f commit 5ebf267
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 142 deletions.
36 changes: 0 additions & 36 deletions Classes/KIFTestActor.h
Expand Up @@ -159,42 +159,6 @@ typedef void (^KIFTestCompletionBlock)(KIFTestStepResult result, NSError *error)
*/
+ (void)setStepDelay:(NSTimeInterval)newStepDelay;

/*!
@abstract Enable autocorrect in KIF tests
@discussion When @c NO, autocorrect is disabled for the duration of the app lifecycle. It is highly unlikely that tests should ever require autocorrect, but this will provide the opportunity to decide whether or not to enable it.
*/
+ (void)setEnableAutocorrect:(BOOL)enableAutocorrect;

/*!
@abstract Enable smart dashes in KIF tests
@discussion When @c NO, smart dashes are disabled for the duration of the app lifecycle. It is highly unlikely that tests should ever require smart dashes, but this will provide the opportunity to decide whether or not to enable them.
*/
+ (void)setEnableSmartDashes:(BOOL)enableSmartDashes;

/*!
@abstract Enable smart quotes in KIF tests
@discussion When @c NO, smart quotes are disabled for the duration of the app lifecycle. It is highly unlikely that tests should ever require smart quotes, but this will provide the opportunity to decide whether or not to enable them.
*/
+ (void)setEnableSmartQuotes:(BOOL)enableSmartQuotes;

/*!
@abstract Whether autocorrect is enabled in KIF tests.
@discussion To enable autocorrect, call +setAutocorrectDisabled: with @c YES.
*/
+ (BOOL)autocorrectEnabled;

/*!
@abstract Whether smart dashes are enabled in KIF tests.
@discussion To enable smart dashes, call +setSmartDashesDisabled: with @c YES.
*/
+ (BOOL)smartDashesEnabled;

/*!
@abstract Whether smart quotes are enabled in KIF tests.
@discussion To enable smart quotes, call +setSmartQuotesDisabled: with @c YES.
*/
+ (BOOL)smartQuotesEnabled;

/*!
@abstract Fails the test.
@discussion Mostly useful for test debugging or as a placeholder when building new tests.
Expand Down
36 changes: 1 addition & 35 deletions Classes/KIFTestActor.m
@@ -1,5 +1,5 @@
//
// KIFTester.m
// KIFTestActor.m
// KIF
//
// Created by Brian Nickel on 12/13/12.
Expand Down Expand Up @@ -130,10 +130,6 @@ - (void)runBlock:(KIFTestExecutionBlock)executionBlock
static NSTimeInterval KIFTestStepDefaultTimeout = 10.0;
static NSTimeInterval KIFTestStepDelay = 0.1;

static BOOL KIFAutocorrectEnabled = NO;
static BOOL KIFSmartDashesEnabled = NO;
static BOOL KIFSmartQuotesEnabled = NO;

+ (NSTimeInterval)defaultAnimationWaitingTimeout
{
return KIFTestStepDefaultAnimationWaitingTimeout;
Expand Down Expand Up @@ -174,36 +170,6 @@ + (void)setStepDelay:(NSTimeInterval)newStepDelay;
KIFTestStepDelay = newStepDelay;
}

+ (void)setEnableAutocorrect:(BOOL)enableAutocorrect;
{
KIFAutocorrectEnabled = enableAutocorrect;
}

+ (void)setEnableSmartDashes:(BOOL)enableSmartDashes;
{
KIFSmartDashesEnabled = enableSmartDashes;
}

+ (void)setEnableSmartQuotes:(BOOL)enableSmartQuotes;
{
KIFSmartQuotesEnabled = enableSmartQuotes;
}

+ (BOOL)autocorrectEnabled;
{
return KIFAutocorrectEnabled;
}

+ (BOOL)smartDashesEnabled;
{
return KIFSmartDashesEnabled;
}

+ (BOOL)smartQuotesEnabled;
{
return KIFSmartQuotesEnabled;
}

#pragma mark Generic tests

- (void)fail
Expand Down
6 changes: 0 additions & 6 deletions Classes/KIFTestCase.m
Expand Up @@ -12,7 +12,6 @@
#import "UIApplication-KIFAdditions.h"
#import "KIFTestActor.h"
#import "KIFAccessibilityEnabler.h"
#import "KIFTextInputTraitsOverrides.h"

#define SIG(class, selector) [class instanceMethodSignatureForSelector:selector]

Expand Down Expand Up @@ -66,11 +65,6 @@ + (NSArray *)testInvocations
+ (void)setUp
{
KIFEnableAccessibility();

KIFSetAutocorrect([KIFTestActor autocorrectEnabled]);
KIFSetSmartDashes([KIFTestActor smartDashesEnabled]);
KIFSetSmartQuotes([KIFTestActor smartQuotesEnabled]);

[self performSetupTearDownWithSelector:@selector(beforeAll)];
}

Expand Down
21 changes: 17 additions & 4 deletions Classes/KIFTextInputTraitsOverrides.h
Expand Up @@ -5,8 +5,21 @@
// Created by Harley Cooper on 1/31/18.
//

#import <Foundation/Foundation.h>
@interface KIFTextInputTraitsOverrides : NSObject

FOUNDATION_EXTERN void KIFSetAutocorrect(BOOL);
FOUNDATION_EXTERN void KIFSetSmartQuotes(BOOL);
FOUNDATION_EXTERN void KIFSetSmartDashes(BOOL);
/*!
@abstract If set to @c YES then KIF will observe default autocorrect behavior. If set to @c NO then autocorrect will always be disabled.
*/
@property(class) BOOL allowDefaultAutocorrectBehavior;

/*!
@abstract If set to @c YES then KIF will observe default smart dashes behavior. If set to @c NO then smart dashes will always be disabled.
*/
@property(class) BOOL allowDefaultSmartDashesBehavior;

/*!
@abstract If set to @c YES then KIF will observe default smart quotes behavior. If set to @c NO then smart quotes will always be disabled.
*/
@property(class) BOOL allowDefaultSmartQuotesBehavior;

@end
156 changes: 109 additions & 47 deletions Classes/KIFTextInputTraitsOverrides.m
Expand Up @@ -8,68 +8,130 @@
#import <objc/runtime.h>
#import "KIFTextInputTraitsOverrides.h"

static IMP autocorrectOriginalImp;
static IMP smartDashesOriginalImp;
static IMP smartQuotesOriginalImp;
@interface KIFTextInputTraitsOverrides()

void KIFSetAutocorrect(BOOL setAutocorrectOn) {
if(!autocorrectOriginalImp) {
autocorrectOriginalImp = class_getMethodImplementation([UITextField class], @selector(autocorrectionType));
}
/*!
@abstract Swizzles the @c autocorrectionType property of @c UITextField
@discussion Sets the property to have default behavior when @c allowDefaultAutocorrectBehavior is set to @c YES, and always return @c UITextAutocorrectionTypeNo when it's set to no.
*/
+ (void)KIFSwizzleTextFieldAutocorrect;

/*!
@abstract Swizzles the @c smartDashesType property of @c UITextField
@discussion Sets the property to have default behavior when @c allowDefaultSmartDashesBehavior is set to @c YES, and always return @c UITextSmartDashesTypeNo when it's set to no.
*/
+ (void)KIFSwizzleTextFieldSmartDashes;

/*!
@abstract Swizzles the @c smartQuotesType property of @c UITextField
@discussion Sets the property to have default behavior when @c allowDefaultSmartQuotesBehavior is set to @c YES, and always return @c UITextSmartQuotesTypeNo when it's set to no.
*/
+ (void)KIFSwizzleTextFieldSmartQuotes;

@end

@implementation KIFTextInputTraitsOverrides

static BOOL KIFAutocorrectEnabled = NO;
static BOOL KIFSmartDashesEnabled = NO;
static BOOL KIFSmartQuotesEnabled = NO;

+ (void)load
{
[self KIFSwizzleTextFieldAutocorrect];
[self KIFSwizzleTextFieldSmartDashes];
[self KIFSwizzleTextFieldSmartQuotes];
}

+ (BOOL)allowDefaultAutocorrectBehavior
{
return KIFAutocorrectEnabled;
}

+ (void)setAllowDefaultAutocorrectBehavior:(BOOL)allowDefaultBehavior
{
KIFAutocorrectEnabled = allowDefaultBehavior;
}

+ (BOOL)allowDefaultSmartDashesBehavior
{
return KIFSmartDashesEnabled;
}

+ (void)setAllowDefaultSmartDashesBehavior:(BOOL)allowDefaultBehavior
{
KIFSmartDashesEnabled = allowDefaultBehavior;
}

IMP autocorrectImp;
if(setAutocorrectOn) {
autocorrectImp = autocorrectOriginalImp;
} else if(!setAutocorrectOn) {
autocorrectImp = imp_implementationWithBlock(^(UITextField *_self) {
return UITextAutocorrectionTypeNo;
+ (BOOL)allowDefaultSmartQuotesBehavior
{
return KIFSmartQuotesEnabled;
}

+ (void)setAllowDefaultSmartQuotesBehavior:(BOOL)allowDefaultBehavior
{
KIFSmartQuotesEnabled = allowDefaultBehavior;
}

+ (void)KIFSwizzleTextFieldAutocorrect
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
struct objc_method_description autocorrectionTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(autocorrectionType), NO, YES);
IMP autocorrectOriginalImp = class_getMethodImplementation([UITextField class], @selector(autocorrectionType));
IMP autocorrectImp = imp_implementationWithBlock(^(UITextField *_self) {
if(self.allowDefaultAutocorrectBehavior) {
return (NSInteger) autocorrectOriginalImp(_self, @selector(autocorrectionType));
} else {
return UITextAutocorrectionTypeNo;
}
});
}
struct objc_method_description autocorrectionTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(autocorrectionType), NO, YES);
class_replaceMethod([UITextField class], @selector(autocorrectionType), autocorrectImp, autocorrectionTypeMethodDescription.types);
class_replaceMethod([UITextField class], @selector(autocorrectionType), autocorrectImp, autocorrectionTypeMethodDescription.types);
});
}

void KIFSetSmartDashes(BOOL setSmartDashesOn) {
+ (void)KIFSwizzleTextFieldSmartDashes
{
// This #ifdef is necessary for versions of Xcode before Xcode 9.
#ifdef __IPHONE_11_0
if (@available(iOS 11.0, *)) {
if(!smartDashesOriginalImp) {
smartDashesOriginalImp = class_getMethodImplementation([UITextField class], @selector(smartDashesType));
}

IMP smartDashesImp;
if(setSmartDashesOn) {
smartDashesImp = smartDashesOriginalImp;
} else if(!setSmartDashesOn) {
smartDashesImp = imp_implementationWithBlock(^(UITextField *_self) {
return UITextSmartDashesTypeNo;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
struct objc_method_description smartDashesTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(smartDashesType), NO, YES);
IMP smartDashesOriginalImp = class_getMethodImplementation([UITextField class], @selector(smartDashesType));
IMP smartDashesImp = imp_implementationWithBlock(^(UITextField *_self) {
if(self.allowDefaultSmartDashesBehavior) {
return (NSInteger) smartDashesOriginalImp(_self, @selector(smartDashesType));
} else {
return UITextSmartDashesTypeNo;
}
});
}
struct objc_method_description smartDashesTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(smartDashesType), NO, YES);
class_replaceMethod([UITextField class], @selector(smartDashesType), smartDashesImp, smartDashesTypeMethodDescription.types);
class_replaceMethod([UITextField class], @selector(smartDashesType), smartDashesImp, smartDashesTypeMethodDescription.types);
});
}
#endif
}

void KIFSetSmartQuotes(BOOL setSmartQuotesOn) {
// This #ifdef is necessary for versions of Xcode before Xcode 9.
+ (void)KIFSwizzleTextFieldSmartQuotes
{
// This #ifdef is necessary for versions of Xcode before Xcode 9.
#ifdef __IPHONE_11_0
if (@available(iOS 11.0, *)) {
if(!smartQuotesOriginalImp) {
smartQuotesOriginalImp = class_getMethodImplementation([UITextField class], @selector(smartQuotesType));
}

IMP smartQuotesImp;
if(setSmartQuotesOn) {
smartQuotesImp = smartQuotesOriginalImp;
} else if(!setSmartQuotesOn) {
smartQuotesImp = imp_implementationWithBlock(^(UITextField *_self) {
return UITextSmartQuotesTypeNo;
if (@available(iOS 11.0, *)) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
struct objc_method_description smartQuotesTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(smartQuotesType), NO, YES);
IMP smartQuotesOriginalImp = class_getMethodImplementation([UITextField class], @selector(smartQuotesType));
IMP smartQuotesImp = imp_implementationWithBlock(^(UITextField *_self) {
if(self.allowDefaultSmartQuotesBehavior) {
return (NSInteger) smartQuotesOriginalImp(_self, @selector(smartQuotesType));
} else {
return UITextSmartQuotesTypeNo;
}
});
class_replaceMethod([UITextField class], @selector(smartQuotesType), smartQuotesImp, smartQuotesTypeMethodDescription.types);
});
}
struct objc_method_description smartQuotesMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(smartQuotesType), NO, YES);
class_replaceMethod([UITextField class], @selector(smartQuotesType), smartQuotesImp, smartQuotesMethodDescription.types);
}
#endif

}

@end
8 changes: 7 additions & 1 deletion Classes/KIFUITestActor.h
Expand Up @@ -120,7 +120,6 @@ typedef NS_ENUM(NSUInteger, KIFPullToRefreshTiming) {
*/
- (UIView *)waitForViewWithAccessibilityLabel:(NSString *)label traits:(UIAccessibilityTraits)traits;


/*!
@abstract Waits until a view or accessibility element is present.
@discussion The view or accessibility element with the given label is found in the view hierarchy. If the element isn't found, then the step will attempt to wait until it is. Note that the view does not necessarily have to be visible on the screen, and may be behind another view or offscreen. Views with their hidden property set to YES are ignored.
Expand All @@ -133,6 +132,13 @@ typedef NS_ENUM(NSUInteger, KIFPullToRefreshTiming) {
*/
- (UIView *)waitForViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits;

/*!
@abstract Waits until a view or accessibility element is no longer present.
@discussion The view or accessibility element with the given value is found in the view hierarchy. If the element is found, then the step will attempt to wait until it isn't. Note that the view does not necessarily have to be visible on the screen, and may be behind another view or offscreen. Views with their hidden property set to YES are considered absent.
@param value The accessibility value of the element to wait for.
*/
- (void)waitForAbsenceOfViewWithValue:(NSString *)value;

/*!
@abstract Waits until a view or accessibility element is no longer present.
@discussion The view or accessibility element with the given label is found in the view hierarchy. If the element is found, then the step will attempt to wait until it isn't. Note that the view does not necessarily have to be visible on the screen, and may be behind another view or offscreen. Views with their hidden property set to YES are considered absent.
Expand Down
5 changes: 5 additions & 0 deletions Classes/KIFUITestActor.m
Expand Up @@ -153,6 +153,11 @@ - (void)waitForAbsenceOfViewWithAccessibilityLabel:(NSString *)label traits:(UIA
[self waitForAbsenceOfViewWithAccessibilityLabel:label value:nil traits:traits];
}

- (void)waitForAbsenceOfViewWithValue:(NSString *)value
{
[self waitForAbsenceOfViewWithAccessibilityLabel:nil value:value traits:UIAccessibilityTraitNone];
}

- (void)waitForAbsenceOfViewWithAccessibilityLabel:(NSString *)label value:(NSString *)value traits:(UIAccessibilityTraits)traits
{
[self runBlock:^KIFTestStepResult(NSError **error) {
Expand Down

0 comments on commit 5ebf267

Please sign in to comment.