Skip to content

Commit

Permalink
Adding ability to enable/disable autocorrect/smart quotes/smart dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Harley Cooper committed Feb 8, 2018
1 parent 168c9e9 commit 37c3f44
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 3 deletions.
36 changes: 36 additions & 0 deletions Classes/KIFTestActor.h
Expand Up @@ -159,6 +159,42 @@ 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
35 changes: 35 additions & 0 deletions Classes/KIFTestActor.m
Expand Up @@ -14,6 +14,7 @@
#import "KIFTestActor_Private.h"

#import "KIFAccessibilityEnabler.h"
#import "KIFTextInputTraitsOverrides.h"
#import "NSError-KIFAdditions.h"
#import "NSException-KIFAdditions.h"
#import "UIApplication-KIFAdditions.h"
Expand Down Expand Up @@ -129,6 +130,10 @@ - (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 @@ -169,6 +174,36 @@ + (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: 6 additions & 0 deletions Classes/KIFTestCase.m
Expand Up @@ -12,6 +12,7 @@
#import "UIApplication-KIFAdditions.h"
#import "KIFTestActor.h"
#import "KIFAccessibilityEnabler.h"
#import "KIFTextInputTraitsOverrides.h"

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

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

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

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

Expand Down
12 changes: 12 additions & 0 deletions Classes/KIFTextInputTraitsOverrides.h
@@ -0,0 +1,12 @@
//
// KIFTextInputTraitsOverrides.h
// KIF
//
// Created by Harley Cooper on 1/31/18.
//

#import <Foundation/Foundation.h>

FOUNDATION_EXTERN void KIFSetAutocorrect(BOOL);
FOUNDATION_EXTERN void KIFSetSmartQuotes(BOOL);
FOUNDATION_EXTERN void KIFSetSmartDashes(BOOL);
75 changes: 75 additions & 0 deletions Classes/KIFTextInputTraitsOverrides.m
@@ -0,0 +1,75 @@
//
// KIFTextInputTraitsOverrides.m
// KIF
//
// Created by Harley Cooper on 1/31/18.
//

#import <objc/runtime.h>
#import "KIFTextInputTraitsOverrides.h"

static IMP autocorrectOriginalImp;
static IMP smartDashesOriginalImp;
static IMP smartQuotesOriginalImp;

void KIFSetAutocorrect(BOOL setAutocorrectOn) {
if(!autocorrectOriginalImp) {
autocorrectOriginalImp = class_getMethodImplementation([UITextField class], @selector(autocorrectionType));
}

IMP autocorrectImp;
if(setAutocorrectOn) {
autocorrectImp = autocorrectOriginalImp;
} else if(!setAutocorrectOn) {
autocorrectImp = imp_implementationWithBlock(^(UITextField *_self) {
return UITextAutocorrectionTypeNo;
});
}
struct objc_method_description autocorrectionTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(autocorrectionType), NO, YES);
class_replaceMethod([UITextField class], @selector(autocorrectionType), autocorrectImp, autocorrectionTypeMethodDescription.types);
}

void KIFSetSmartDashes(BOOL setSmartDashesOn) {
// 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;
});
}
struct objc_method_description smartDashesTypeMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(smartDashesType), NO, YES);
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.
#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;
});
}
struct objc_method_description smartQuotesMethodDescription = protocol_getMethodDescription(@protocol(UITextInputTraits), @selector(smartQuotesType), NO, YES);
class_replaceMethod([UITextField class], @selector(smartQuotesType), smartQuotesImp, smartQuotesMethodDescription.types);
}
#endif

}
57 changes: 57 additions & 0 deletions KIF Tests/AutocorrectTests.m
@@ -0,0 +1,57 @@
//
// AutocorrectTests.m
// KIF Tests
//
// Created by Harley Cooper on 2/7/18.
//

#import <KIF/KIF.h>

@interface AutocorrectTests : KIFTestCase
@end

@implementation AutocorrectTests

+ (void)setUp
{
[KIFTestActor setEnableAutocorrect:YES];
[KIFTestActor setEnableSmartQuotes:YES];
[KIFTestActor setEnableSmartDashes:YES];
[super setUp];
}

+ (void)tearDown
{
[KIFTestActor setEnableAutocorrect:NO];
[KIFTestActor setEnableSmartQuotes:NO];
[KIFTestActor setEnableSmartDashes:NO];
[super tearDown];
}

- (void)beforeEach
{
[tester tapViewWithAccessibilityLabel:@"Tapping"];
}

- (void)afterEach
{
[tester tapViewWithAccessibilityLabel:@"Test Suite" traits:UIAccessibilityTraitButton];
}

- (void)testClearingAndEnteringTypoIntoViewWithAccessibilityLabel
{
[[tester validateEnteredText:NO] clearTextFromAndThenEnterText:@" teh " intoViewWithAccessibilityLabel:@"Greeting"];
[[viewTester usingValue:@" teh "] waitForAbsenceOfView];
}

- (void)testClearingAndEnteringQuotesIntoViewWithAccessibilityLabel
{
[tester clearTextFromAndThenEnterText:@"'\"'," intoViewWithAccessibilityLabel:@"Greeting" traits:UIAccessibilityTraitNone expectedResult:@"’”’,"];
}

- (void)testClearingAndEnteringDashesIntoViewWithAccessibilityLabel
{
[tester clearTextFromAndThenEnterText:@"--a" intoViewWithAccessibilityLabel:@"Greeting" traits:UIAccessibilityTraitNone expectedResult:@"—a"];
}

@end
60 changes: 60 additions & 0 deletions KIF Tests/AutocorrectTests_ViewTestActor.m
@@ -0,0 +1,60 @@
//
// AutocorrectTests_ViewTestActor.m
// KIF Tests
//
// Created by Harley Cooper on 2/7/18.
//

#import <KIF/KIF.h>

@interface AutocorrectTests_ViewTestActor : KIFTestCase
@end


@implementation AutocorrectTests_ViewTestActor

+ (void)setUp
{
[KIFTestActor setEnableAutocorrect:YES];
[KIFTestActor setEnableSmartQuotes:YES];
[KIFTestActor setEnableSmartDashes:YES];
[super setUp];
}

+ (void)tearDown
{
[KIFTestActor setEnableAutocorrect:NO];
[KIFTestActor setEnableSmartQuotes:NO];
[KIFTestActor setEnableSmartDashes:NO];
[super tearDown];
}

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

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

- (void)testClearingAndEnteringTypoIntoViewWithAccessibilityLabel
{
[[[viewTester validateEnteredText:NO] usingLabel:@"Greeting"] clearAndEnterText:@" teh "];
[[viewTester usingValue:@" teh "] waitForAbsenceOfView];
}

- (void)testClearingAndEnteringQuotesIntoViewWithAccessibilityLabel
{
[[viewTester usingLabel:@"Greeting"] clearAndEnterText:@"'\"'," expectedResult:@"’”’,"];
}

- (void)testClearingAndEnteringDashesIntoViewWithAccessibilityLabel
{
[[viewTester usingLabel:@"Greeting"] clearAndEnterText:@"--a" expectedResult:@"—a"];
}


@end

15 changes: 15 additions & 0 deletions KIF Tests/TypingTests.m
Expand Up @@ -68,6 +68,21 @@ - (void)testClearingAndEnteringTextIntoViewWithAccessibilityLabel
[tester clearTextFromAndThenEnterText:@"Yo" intoViewWithAccessibilityLabel:@"Greeting"];
}

- (void)testClearingAndEnteringQuotesIntoViewWithAccessibilityLabel
{
[tester clearTextFromAndThenEnterText:@"'\"'," intoViewWithAccessibilityLabel:@"Greeting"];
}

- (void)testClearingAndEnteringDashesIntoViewWithAccessibilityLabel
{
[tester clearTextFromAndThenEnterText:@"--a" intoViewWithAccessibilityLabel:@"Greeting"];
}

- (void)testClearingAndEnteringTypoIntoViewWithAccessibilityLabel
{
[tester clearTextFromAndThenEnterText:@" teh " intoViewWithAccessibilityLabel:@"Greeting"];
}

- (void)testEnteringReturnCharacterIntoViewWithAccessibilityLabel
{
[tester enterText:@"Hello\n" intoViewWithAccessibilityLabel:@"Other Text"];
Expand Down
15 changes: 15 additions & 0 deletions KIF Tests/TypingTests_ViewTestActor.m
Expand Up @@ -64,6 +64,21 @@ - (void)testEnteringTextIntoViewWithAccessibilityLabelExpectingResults
[[[viewTester usingLabel:@"Greeting"] usingValue:@"Hello, world"] waitForView];
}

- (void)testClearingAndEnteringQuotesIntoViewWithAccessibilityLabel
{
[[viewTester usingLabel:@"Greeting"] clearAndEnterText:@"'\"'," expectedResult:@"'\"',"];
}

- (void)testClearingAndEnteringDashesIntoViewWithAccessibilityLabel
{
[[viewTester usingLabel:@"Greeting"] clearAndEnterText:@"--a" expectedResult:@"--a"];
}

- (void)testClearingAndEnteringTypoIntoViewWithAccessibilityLabel
{
[[viewTester usingLabel:@"Greeting"] clearAndEnterText:@" teh " expectedResult:@" teh "];
}

- (void)testClearingAndEnteringTextIntoViewWithAccessibilityLabel
{
[[viewTester usingLabel:@"Greeting"] clearAndEnterText:@"Yo"];
Expand Down

0 comments on commit 37c3f44

Please sign in to comment.