Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widen firstResponder searches to all windows #1143

Merged

Conversation

zradke
Copy link
Contributor

@zradke zradke commented Mar 19, 2020

Previously searches for UIWindow.firstResponder were limited to the
UIApplication.keyWindow. In practice, it is very possible for users to
add additional valid windows that are not the key window and have first
responders. To accommodate those cases, this change broadens the search
for a first responder of an application to an array of first responders
found in UIApplication.windowsWithKeyWindow.

@zradke zradke force-pushed the zradke/first-responder-search branch from d4c61b2 to c0cebe1 Compare March 20, 2020 21:04
Copy link
Contributor

@justinseanmartin justinseanmartin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of minor questions, but overall looks good. Will wait on replies before approving/merging.

for (UIView *fallback in fallbackViews) {
if ([fallback isKindOfClass:[UITextField class]] || [fallback isKindOfClass:[UITextView class]] || [fallback isKindOfClass:[UISearchBar class]]) {
NSLog(@"KIF: Unable to find keyboard key for %@. Inserting manually.", characterString);
[(UITextField *)fallback setText:[[(UITextField *)fallback text] stringByAppendingString:characterString]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should print a warning if there is more than one match, to make the surface the somewhat arbitrary decision being made here.

[self clearTextFromElement:(UIAccessibilityElement *)firstResponder inView:firstResponder];
for (UIResponder *firstResponder in [[UIApplication sharedApplication] firstResponders]) {
if ([firstResponder isKindOfClass:[UIView class]]) {
[self clearTextFromElement:(UIAccessibilityElement *)firstResponder inView:(UIView *)firstResponder];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should only do this from one element and not multiple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that I should revert this portion so it continues to only check UIApplication.keyWindow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what I mean is to bail from the loop after clearing the text from some first responder. Probably would want to do the same as above and output a warning if there are multiple matching fields and we're only clearing the contents of one of them.

for (UIResponder *firstResponder in [[UIApplication sharedApplication] firstResponders]) {
foundResponder = firstResponder;

if ([foundResponder isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:eyeroll: at this special case being here, but not for the other method w/ traits matching below... (no action for you, just noticing this while reviewing)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I noticed a lot of inconsistencies around accessibilityLabel checking. Might warrant a deeper follow up to unify that logic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, KIFUITestActor is the old way of doing things and should eventually be deprecated. It probably isn't worth the time, as everything is already unified in KIFUIViewTestActor (new way).

}
KIFTestWaitCondition([[firstResponder accessibilityLabel] isEqualToString:label], error, @"Expected accessibility label for first responder to be '%@', got '%@'", label, [firstResponder accessibilityLabel]);

KIFTestWaitCondition(didMatch, error, @"Expected accessibility label for first responder to be '%@', got '%@'", label, [foundResponder accessibilityLabel]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the assertions should use dump the whole array of elements (or maybe at least the count of elements checked)? (same applies below)

KIFTestWaitCondition([[firstResponderIdentification accessibilityIdentifier] isEqualToString:accessibilityIdentifier],
BOOL didMatch = NO;
NSString *foundIdentifier = nil;
for (UIResponder __strong *firstResponder in [[UIApplication sharedApplication] firstResponders]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why. is the __strong needed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It lets me reassign to firstResponder in the for loop without having to declare a temporary local variable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably still opt to use the local variable to make the logic more straightforward, but don't feel strongly on this.

}
}

KIFTestWaitCondition(didMatch,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: indentation seems off

Copy link
Contributor Author

@zradke zradke Mar 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm looks like there's a mix of spaces & tabs in this file, so it looks fine with my config but wrong for github's. I'll fix all in file to convert them all to spaces. I'll do it as a separate commit but it will still dirty up the pull request so I'd advise ignoring whitespace afterward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking that up!

@zradke zradke force-pushed the zradke/first-responder-search branch from c0cebe1 to f6f5e1d Compare March 23, 2020 19:36

for (id fallback in fallbackViews) {
if ([fallback isKindOfClass:[UITextField class]] || [fallback isKindOfClass:[UITextView class]] || [fallback isKindOfClass:[UISearchBar class]]) {
if (fallbackViews.count > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of gathering the set of first responders that match the class type check and then output the log message only if there is more than one match. Just concerned that this might be too noisy if it is an expected common scenario.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

@justinseanmartin
Copy link
Contributor

LGTM. Take a look at my one latest comment and LMK what you think. I'll merge this as-is if you don't think it is worth addressing.

zradke added 2 commits March 24, 2020 12:36
Previously searches for `UIWindow.firstResponder` were limited to the
`UIApplication.keyWindow`. In practice, it is very possible for users to
add additional valid windows that are not the key window and have first
responders. To accommodate those cases, this change broadens the search
for a first responder of an application to an array of first responders
found in `UIApplication.windowsWithKeyWindow`.
@zradke zradke force-pushed the zradke/first-responder-search branch from f6f5e1d to ded9474 Compare March 24, 2020 16:38
@justinseanmartin justinseanmartin merged commit bc9266a into kif-framework:master Mar 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants