-
Notifications
You must be signed in to change notification settings - Fork 101
feat: support react native buttons label extraction in user steps #1109
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bba3569
feat(android): support repro-steps for button
abdelhamid-f-nasser ae914ab
feat(example): add repro-steps button samples
abdelhamid-f-nasser 59933de
feat: Support masking in example components
abdelhamid-f-nasser 2998e5f
chore(example): resolve review comments
abdelhamid-f-nasser c044bf4
chore(android): resolve review comments
abdelhamid-f-nasser 2ccb73a
fix(android): handle private view label extraction
abdelhamid-f-nasser 81f0913
fix(android): adjust multi label button string
abdelhamid-f-nasser f11d8e0
chore(android): format code docs
abdelhamid-f-nasser aa65b2a
refactor(example): improve four tier button
abdelhamid-f-nasser 1b07437
chore(android): Remove redundant NoLabels Extraction
abdelhamid-f-nasser 479d722
docs: update changelog.md
abdelhamid-f-nasser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
android/src/main/java/com/instabug/reactlibrary/utils/RNTouchedViewExtractor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.instabug.reactlibrary.utils; | ||
|
||
import android.text.TextUtils; | ||
import android.view.View; | ||
import android.view.ViewParent; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import com.facebook.react.views.text.ReactTextView; | ||
import com.facebook.react.views.view.ReactViewGroup; | ||
import com.instabug.library.core.InstabugCore; | ||
import com.instabug.library.visualusersteps.TouchedView; | ||
import com.instabug.library.visualusersteps.TouchedViewExtractor; | ||
import com.instabug.library.visualusersteps.VisualUserStepsHelper; | ||
|
||
public class RNTouchedViewExtractor implements TouchedViewExtractor { | ||
|
||
private final int depthTraversalLimit = 3; | ||
|
||
/** | ||
* Determines whether the native Android SDK should depend on native extraction | ||
* when a label is not found by the RNTouchedViewExtractor. | ||
* | ||
* <p> | ||
* - {@code RNTouchedViewExtractor} tries to find a label. | ||
* <br> | ||
* - If it returns a label, the view is labeled with the one returned. | ||
* <br> | ||
* - If it returns {@code null}: | ||
* <br> | ||
* - If {@code shouldDependOnNative} is {@code true}, the native Android SDK | ||
* will try to extract the label from the view. | ||
* <br> | ||
* - If it's {@code false}, the Android SDK will label it {@code null} as returned | ||
* from {@code RNTouchedViewExtractor} without trying to label it. | ||
* </p> | ||
* | ||
* @return {@code true} if the native Android SDK should depend on native extraction, | ||
* {@code false} otherwise. | ||
*/ | ||
@Override | ||
public boolean getShouldDependOnNative() { | ||
return true; | ||
} | ||
|
||
|
||
@Nullable | ||
@Override | ||
public TouchedView extract(@NonNull View view, @NonNull TouchedView touchedView) { | ||
ReactViewGroup reactViewGroup = findReactButtonViewGroup(view); | ||
// If no button is found return `null` to leave the extraction of the touched view to the native Android SDK. | ||
if (reactViewGroup == null) return null; | ||
abdelhamid-f-nasser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return getExtractionStrategy(reactViewGroup).extract(reactViewGroup, touchedView); | ||
} | ||
|
||
@Nullable | ||
private ReactViewGroup findReactButtonViewGroup(@NonNull View startView) { | ||
if (isReactButtonViewGroup(startView)) return (ReactViewGroup) startView; | ||
ViewParent currentParent = startView.getParent(); | ||
int depth = 1; | ||
do { | ||
if (currentParent == null || isReactButtonViewGroup(currentParent)) | ||
return (ReactViewGroup) currentParent; | ||
currentParent = currentParent.getParent(); | ||
depth++; | ||
} while (depth < depthTraversalLimit); | ||
return null; | ||
} | ||
|
||
private boolean isReactButtonViewGroup(@NonNull View view) { | ||
return (view instanceof ReactViewGroup) && view.isFocusable() && view.isClickable(); | ||
} | ||
|
||
private boolean isReactButtonViewGroup(@NonNull ViewParent viewParent) { | ||
if (!(viewParent instanceof ReactViewGroup)) return false; | ||
ReactViewGroup group = (ReactViewGroup) viewParent; | ||
return group.isFocusable() && group.isClickable(); | ||
} | ||
|
||
private ReactButtonExtractionStrategy getExtractionStrategy(ReactViewGroup reactButton) { | ||
boolean isPrivateView = VisualUserStepsHelper.isPrivateView(reactButton); | ||
if (isPrivateView) return new PrivateViewLabelExtractionStrategy(); | ||
|
||
int labelsCount = 0; | ||
int groupsCount = 0; | ||
for (int index = 0; index < reactButton.getChildCount(); index++) { | ||
View currentView = reactButton.getChildAt(index); | ||
if (currentView instanceof ReactTextView) { | ||
|
||
labelsCount++; | ||
continue; | ||
} | ||
if (currentView instanceof ReactViewGroup) { | ||
groupsCount++; | ||
} | ||
} | ||
if (labelsCount > 1 || groupsCount > 0) return new MultiLabelsExtractionStrategy(); | ||
if (labelsCount == 1) return new SingleLabelExtractionStrategy(); | ||
return new NoLabelsExtractionStrategy(); | ||
} | ||
|
||
interface ReactButtonExtractionStrategy { | ||
@Nullable | ||
TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView); | ||
} | ||
|
||
class MultiLabelsExtractionStrategy implements ReactButtonExtractionStrategy { | ||
private final String MULTI_LABEL_BUTTON_PRE_STRING = "a button that contains \"%s\""; | ||
|
||
@Override | ||
@Nullable | ||
public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { | ||
|
||
touchedView.setProminentLabel( | ||
InstabugCore.composeProminentLabelForViewGroup(reactButton, MULTI_LABEL_BUTTON_PRE_STRING) | ||
); | ||
return touchedView; | ||
} | ||
} | ||
|
||
class PrivateViewLabelExtractionStrategy implements ReactButtonExtractionStrategy { | ||
|
||
private final String PRIVATE_VIEW_LABEL_BUTTON_PRE_STRING = "a button"; | ||
|
||
@Override | ||
public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { | ||
touchedView.setProminentLabel(PRIVATE_VIEW_LABEL_BUTTON_PRE_STRING); | ||
return touchedView; | ||
} | ||
} | ||
|
||
class SingleLabelExtractionStrategy implements ReactButtonExtractionStrategy { | ||
|
||
@Override | ||
public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { | ||
ReactTextView targetLabel = null; | ||
for (int index = 0; index < reactButton.getChildCount(); index++) { | ||
View currentView = reactButton.getChildAt(index); | ||
if (!(currentView instanceof ReactTextView)) continue; | ||
targetLabel = (ReactTextView) currentView; | ||
break; | ||
} | ||
if (targetLabel == null) return touchedView; | ||
|
||
String labelText = getLabelText(targetLabel); | ||
touchedView.setProminentLabel(InstabugCore.composeProminentLabelFor(labelText, false)); | ||
return touchedView; | ||
} | ||
|
||
@Nullable | ||
private String getLabelText(ReactTextView textView) { | ||
String labelText = null; | ||
if (!TextUtils.isEmpty(textView.getText())) { | ||
labelText = textView.getText().toString(); | ||
} else if (!TextUtils.isEmpty(textView.getContentDescription())) { | ||
labelText = textView.getContentDescription().toString(); | ||
} | ||
return labelText; | ||
} | ||
} | ||
|
||
class NoLabelsExtractionStrategy implements ReactButtonExtractionStrategy { | ||
@Override | ||
public TouchedView extract(ReactViewGroup reactButton, TouchedView touchedView) { | ||
return touchedView; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.