forked from moredip/Frank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyboardCommand.m
61 lines (43 loc) · 2.22 KB
/
KeyboardCommand.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Created by pete on 5/20/12.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "KeyboardCommand.h"
#import "JSON.h"
#import "KIFTestStep.h"
#import "FranklyProtocolHelper.h"
#import "UIApplication-KIFAdditions.h"
// KIF internal helpers which are not exposed but which we will be using none-the-less.
@interface KIFTestStep (Internals)
+ (BOOL)_enterCharacter:(NSString *)characterString;
@end
@implementation KeyboardCommand {
}
- (BOOL)checkForKeyboard {
return nil != [[UIApplication sharedApplication] keyboardWindow];
}
- (NSString *)generateKeyboardNotPresentErrorResponse {
return [FranklyProtocolHelper generateErrorResponseWithReason:@"keyboard not present"
andDetails:@"The iOS keyboard is not currently present, so Frank can't use it so simulate typing. You might want to simulate a tap on the thing you want to type into first."];
}
- (NSString *)generateFailedToEnterCharacterErrorResponse:(NSString *)characterWhichFailed {
return [FranklyProtocolHelper generateErrorResponseWithReason:@"failed to type character"
andDetails:[NSString stringWithFormat:@"We were unable to type the character '%@'. This is probably because we couldn't find it on the keyboard. Please report this to the frank-discuss mailing list."]];
}
- (NSString *)handleCommandWithRequestBody:(NSString *)requestBody {
NSDictionary *requestCommand = [requestBody JSONValue];
NSString *textToType = [requestCommand objectForKey:@"text_to_type"];
if( ![self checkForKeyboard] ){
return [self generateKeyboardNotPresentErrorResponse];
}
// this loop was lifted from [KIFTestStep stepToEnterText:intoViewWithAccessibilityLabel:traits:expectedResult:]
for (NSUInteger characterIndex = 0; characterIndex < [textToType length]; characterIndex++) {
NSString *characterString = [textToType substringWithRange:NSMakeRange(characterIndex, 1)];
if (![KIFTestStep _enterCharacter:characterString]) {
return [self generateFailedToEnterCharacterErrorResponse:characterString];
}
}
return [FranklyProtocolHelper generateSuccessResponseWithoutResults];
}
@end