Skip to content

Commit

Permalink
fix: Add a test for the keyboard input and perform related fixes (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jul 20, 2024
1 parent 33d3ef9 commit af826e8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
check-latest: true
- run: npm install --no-package-lock
name: Install dev dependencies
- run: npm run lint
Expand All @@ -45,11 +46,15 @@ jobs:
analyze_wda:
strategy:
matrix:
os: [macos-11, macos-12]
xcode_version: [13, 14]
os: [macos-12, macos-13, macos-14]
xcode_version: [13, 14, 15]
exclude:
- os: macos-11
xcode_version: 14
- os: macos-13
xcode_version: 13
- os: macos-14
xcode_version: 13
- os: macos-12
xcode_version: 15
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
45 changes: 45 additions & 0 deletions WebDriverAgentMac/IntegrationTests/AMW3CActionsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ - (void)testErroneousGestures
},
],

// Keys are not balanced
@[@{
@"type": @"key",
@"id": @"keyboard",
@"actions": @[
@{@"type": @"keyDown", @"value": @"n"},
@{@"type": @"keyUp", @"value": @"n"},
@{@"type": @"keyUp", @"value": @"b"},
],
},
],

];

for (NSArray<NSDictionary<NSString *, id> *> *invalidGesture in invalidGestures) {
Expand All @@ -274,6 +286,7 @@ - (void)testErroneousGestures

- (void)testClick
{
[self switchToButtonsTab];
XCUIElement *checkbox = self.testedApplication.checkBoxes.firstMatch;
NSNumber *value = checkbox.value;

Expand All @@ -300,6 +313,7 @@ - (void)testClick

- (void)testRightClick
{
[self switchToButtonsTab];
XCUIElement *checkbox = self.testedApplication.checkBoxes.firstMatch;
NSNumber *value = checkbox.value;

Expand All @@ -324,5 +338,36 @@ - (void)testRightClick
XCTAssertTrue([checkbox.value boolValue] == [value boolValue]);
}

- (void)testKeys
{
[self switchToEditsTab];
XCUIElement *edit = self.testedApplication.textFields.firstMatch;
[edit click];

NSArray<NSDictionary<NSString *, id> *> *gesture =
@[@{
@"type": @"key",
@"id": @"keyboard",
@"actions": @[
@{@"type": @"keyDown", @"value": @"\uE008"},
@{@"type": @"pause", @"duration": @500},
@{@"type": @"keyDown", @"value": @"n"},
@{@"type": @"keyUp", @"value": @"n"},
@{@"type": @"keyDown", @"value": @"b"},
@{@"type": @"keyUp", @"value": @"b"},
@{@"type": @"keyDown", @"value": @"a"},
@{@"type": @"keyUp", @"value": @"a"},
@{@"type": @"pause", @"duration": @500},
@{@"type": @"keyUp", @"value": @"\uE008"},
],
},
];
NSError *error;
XCTAssertTrue([self.testedApplication fb_performW3CActions:gesture
elementCache:nil
error:&error]);
XCTAssertNil(error);
XCTAssertEqualObjects(edit.value, @"NBA");
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ @interface FBW3CKeyItem : FBBaseActionItem

@property (nullable, readonly, nonatomic) FBW3CKeyItem *previousItem;

- (NSUInteger)calculateModifierForChain:(NSArray *)allItems
- (NSUInteger)calculateModifiersForChain:(NSArray *)allItems
lastItemIndex:(NSUInteger)lastItemIndex;

@end
Expand Down Expand Up @@ -436,7 +436,7 @@ - (nullable instancetype)initWithActionItem:(NSDictionary<NSString *, id> *)acti
return self;
}

- (NSUInteger)calculateModifierForChain:(NSArray *)allItems
- (NSUInteger)calculateModifiersForChain:(NSArray *)allItems
lastItemIndex:(NSUInteger)lastItemIndex
{
NSUInteger result = 0;
Expand Down Expand Up @@ -501,7 +501,7 @@ - (BOOL)hasDownPairInItems:(NSArray *)allItems
BOOL isKeyDown = [item isKindOfClass:FBKeyDownItem.class];
BOOL isKeyUp = !isKeyDown && [item isKindOfClass:FBKeyUpItem.class];
if (!isKeyUp && !isKeyDown) {
break;
continue;
}

NSString *value = [item performSelector:@selector(value)];
Expand Down Expand Up @@ -536,36 +536,46 @@ + (NSUInteger)defaultTypingFrequency
}

NSNumber *modifier = AMToMetaModifier(self.value);
NSTimeInterval offsetSeconds = FBMillisToSeconds(self.offset);
XCPointerEventPath *result = nil == eventPath ? [[XCPointerEventPath alloc] initForTextInput] : eventPath;
if (nil != modifier) {
NSUInteger previousModifier = [self calculateModifierForChain:allItems lastItemIndex:currentItemIndex - 1];
[result setModifiers:previousModifier & ~[modifier unsignedIntValue]
mergeWithCurrentModifierFlags:NO
atOffset:offsetSeconds];
return @[result];
return @[];
}

NSTimeInterval offsetSeconds = FBMillisToSeconds(self.offset);
XCPointerEventPath *result = nil == eventPath ? [[XCPointerEventPath alloc] initForTextInput] : eventPath;

NSString *specialKey = AMToSpecialKey(self.value);
NSUInteger modifiers = [self calculateModifiersForChain:allItems lastItemIndex:currentItemIndex];
if (nil != specialKey) {
NSUInteger previousModifier = [self calculateModifierForChain:allItems lastItemIndex:currentItemIndex];
if ([specialKey isEqualToString:@""]) {
// NOOP
[result setModifiers:previousModifier
mergeWithCurrentModifierFlags:NO
atOffset:offsetSeconds];
} else {
if (specialKey.length > 0) {
[result typeKey:specialKey
modifiers:previousModifier
modifiers:modifiers
atOffset:offsetSeconds];
}
return @[result];
}

[result typeText:self.value
atOffset:offsetSeconds
typingSpeed:[self.class defaultTypingFrequency]
shouldRedact:NO];
NSUInteger len = [self.value length];
unichar buffer[len + 1];
[self.value getCharacters:buffer range:NSMakeRange(0, len)];
for (int i = 0; i < 1; i++) {
unichar charCode = buffer[i];
NSString *oneChar = [NSString stringWithFormat:@"%C", charCode];
// 0x7F is the end of the first half of the ASCII table, where (almoust) all
// chars have their own key representations
if (charCode <= 0x7F) {
[result typeKey:oneChar
modifiers:modifiers
atOffset:offsetSeconds];
} else {
// The typeText API does not respect modifiers
// while the typeKey API can only enter keys
[result typeText:oneChar
atOffset:offsetSeconds
typingSpeed:[self.class defaultTypingFrequency]
shouldRedact:NO];
}
}

return @[result];
}

Expand Down Expand Up @@ -608,7 +618,7 @@ - (BOOL)hasUpPairInItems:(NSArray *)allItems
BOOL isKeyDown = [item isKindOfClass:FBKeyDownItem.class];
BOOL isKeyUp = !isKeyDown && [item isKindOfClass:FBKeyUpItem.class];
if (!isKeyUp && !isKeyDown) {
break;
continue;
}

NSString *value = [item performSelector:@selector(value)];
Expand All @@ -635,20 +645,7 @@ - (BOOL)hasUpPairInItems:(NSArray *)allItems
return nil;
}

NSNumber *modifier = AMToMetaModifier(self.value);
if (nil == modifier) {
return @[];
}
NSUInteger previousModifier = [self calculateModifierForChain:allItems lastItemIndex:currentItemIndex - 1];
if ([modifier unsignedIntValue] == previousModifier) {
return @[];
}

XCPointerEventPath *result = nil == eventPath ? [[XCPointerEventPath alloc] initForTextInput] : eventPath;
[result setModifiers:previousModifier | [modifier unsignedIntValue]
mergeWithCurrentModifierFlags:NO
atOffset:FBMillisToSeconds(self.offset)];
return @[result];
return @[];
}

@end
Expand Down

0 comments on commit af826e8

Please sign in to comment.