forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding full support for testID with uiautomator.
* Calling view.setId() with the matching resource-id of an id found in R.class. Added TestIdUtil to facilitate this. * Updating the android sample project to include a testID example. Updating the e2e test to use it. * Changing the signature for virtually all Event Classes to require the View instead of the viewTag. This reduces the number of locations where TestIdUtil.getOriginalReactTag is called. * Minimizing the impact in non __DEV__ environments where testID should not be set by simply returning view.getId() in TestIdUtil.getOriginalReactTag. * This closes facebook#9777.
- Loading branch information
1 parent
82911a8
commit e31123e
Showing
32 changed files
with
383 additions
and
129 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
55 changes: 55 additions & 0 deletions
55
ReactAndroid/src/main/java/com/facebook/react/common/TestIdUtil.java
This file contains 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,55 @@ | ||
package com.facebook.react.common; | ||
|
||
import android.view.View; | ||
|
||
import com.facebook.react.common.annotations.VisibleForTesting; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class TestIdUtil { | ||
private static final ConcurrentHashMap<String, Integer> mTestIds = new ConcurrentHashMap<>(); | ||
// Integer values in R.class are typically large. To avoid colliding with R.class we | ||
// use smaller values for ids when no resource id exists. | ||
private static final int mStartingInternalId = 1; | ||
private static final AtomicInteger mInternalId = new AtomicInteger(mStartingInternalId); | ||
|
||
/** | ||
* Looks for defined resource IDs in R.class by the name of testId and if a matching resource ID is | ||
* found it is passed to the view's setId method. If the given testId cannot be found in R.class, | ||
* an increment value is assigned instead. | ||
*/ | ||
public static <T extends View> void setTestId(T view, String testId) { | ||
int mappedTestId; | ||
if (!mTestIds.containsKey(testId)) { | ||
mappedTestId = view.getResources().getIdentifier(testId, "id", view.getContext().getPackageName()); | ||
final boolean idNotFoundInResources = mappedTestId <= 0; | ||
if (idNotFoundInResources) { | ||
mappedTestId = mInternalId.getAndIncrement(); | ||
} | ||
mTestIds.put(testId, mappedTestId); | ||
} else { | ||
mappedTestId = mTestIds.get(testId); | ||
} | ||
|
||
if (mappedTestId != 0 && view.getId() != mappedTestId) { | ||
view.setId(mappedTestId); | ||
} | ||
} | ||
|
||
/** | ||
* Used for e2e tests that do not yet have testIDs stored in ids.xml. It is strongly | ||
* advised that you reference ids that have been generated in R.class to avoid collisions and | ||
* to properly support UIAutomatorViewer. | ||
*/ | ||
@VisibleForTesting | ||
public static int getTestId(String testId) { | ||
return mTestIds.containsKey(testId) ? mTestIds.get(testId) : View.NO_ID; | ||
} | ||
|
||
@VisibleForTesting | ||
public static void resetStateInTest() { | ||
mTestIds.clear(); | ||
mInternalId.set(mStartingInternalId); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
ReactAndroid/src/main/java/com/facebook/react/common/ViewMethodsUtil.java
This file contains 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,16 @@ | ||
package com.facebook.react.common; | ||
|
||
import android.view.View; | ||
|
||
public class ViewMethodsUtil { | ||
|
||
/** | ||
* Returns the react tag for the view. If no react tag has been set then {@link View#NO_ID} is | ||
* returned. | ||
*/ | ||
public static int reactTagFor(View view) { | ||
return view == null || view.getTag() == null ? | ||
View.NO_ID : | ||
(int) view.getTag(); | ||
} | ||
} |
This file contains 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 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 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.
e31123e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @jsdevel , are these changes merged in to master? I couldn't see these e.g. in https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java
setTestId method.
e31123e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PardeepK unfortunately they're not merged into master. The PR that I had opened was rejected by the react-native maintainers.
e31123e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jsdevel for answering!
Accessing a view with resource-id is very fundamental and basic thing, which should be there. In absence of this we loose flexibility of testing/accessing elements by id.
Could you please let me know the reason for rejection by react-native maintainers?
e31123e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See the PR link from my previous comment.