forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
56 changed files
with
934 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
89 changes: 89 additions & 0 deletions
89
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,89 @@ | ||
package com.facebook.react.common; | ||
|
||
import android.view.View; | ||
|
||
import com.facebook.react.common.annotations.VisibleForTesting; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Utility methods for managing testIDs on views and mapping them back to React Tags. | ||
*/ | ||
public class TestIdUtil { | ||
private static final ConcurrentHashMap<String, Integer> TEST_IDS = new ConcurrentHashMap<>(); | ||
private static final ConcurrentHashMap<Integer, Integer> ORIGINAL_REACT_TAGS = new ConcurrentHashMap<>(); | ||
private static volatile boolean hasTagMappings; | ||
|
||
/** | ||
* 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. Before the view's Id is overridden it is stored | ||
* in an internal association with the view's identity hash code for later retrieval | ||
* (see {@link #getOriginalReactTag(View)}). {@link View#addOn} | ||
* | ||
* @param view | ||
* @param testId | ||
* @param <T> | ||
*/ | ||
public static <T extends View> void setTestId(T view, String testId) { | ||
int mappedTestId; | ||
if (!TEST_IDS.containsKey(testId)) { | ||
mappedTestId = view.getResources().getIdentifier(testId, "id", view.getContext().getPackageName()); | ||
TEST_IDS.put(testId, mappedTestId); | ||
} else { | ||
mappedTestId = TEST_IDS.get(testId); | ||
} | ||
|
||
if (mappedTestId != 0 && view.getId() != mappedTestId) { | ||
ORIGINAL_REACT_TAGS.put(System.identityHashCode(view), view.getId()); | ||
hasTagMappings = true; | ||
view.setId(mappedTestId); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the tag originally generated by the JS when the view was created prior to | ||
* it being potentially overridden by {@link #setTestId(View, String)}. If no view has had it's Id | ||
* overridden by {@link #setTestId(View, String)} then this method simply returns view.getId(). | ||
* In non __DEV__ environments this should be as performant as calling view.getId(). | ||
* | ||
* @param view | ||
* @param <T> | ||
* @return | ||
*/ | ||
public static <T extends View> int getOriginalReactTag(T view) { | ||
if (!hasTagMappings) { | ||
return view.getId(); | ||
} | ||
Integer tag = ORIGINAL_REACT_TAGS.get(System.identityHashCode(view)); | ||
return tag != null ? tag.intValue() : view.getId(); | ||
} | ||
|
||
/** | ||
* Removes the internal mapping of this view's identity to it's original react tag set from JS | ||
* and restores it's Id prior to it having been overridden by setTestId. | ||
* | ||
* @param view | ||
* @param <T> | ||
*/ | ||
public static <T extends View> void removeMapping(T view) { | ||
if (!hasTagMappings) { | ||
return; | ||
} | ||
int identityHashCode = System.identityHashCode(view); | ||
Integer originalReactTag = ORIGINAL_REACT_TAGS.get(identityHashCode); | ||
if (originalReactTag != null) { | ||
view.setId(originalReactTag); | ||
ORIGINAL_REACT_TAGS.remove(identityHashCode); | ||
} | ||
return; | ||
} | ||
|
||
/** | ||
* Used by tests to clear the static member Maps. | ||
*/ | ||
@VisibleForTesting | ||
public static void resetTestState() { | ||
ORIGINAL_REACT_TAGS.clear(); | ||
TEST_IDS.clear(); | ||
} | ||
} |
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
Oops, something went wrong.