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.
* This closes facebook#9777.
- Loading branch information
Showing
55 changed files
with
468 additions
and
135 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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
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<>(); | ||
|
||
/** | ||
* Looks for defined resource IDs in R.class by the name of testId. 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 internal hash code for later retrieval | ||
* (see {@link #getOriginalReactTag(View)}). | ||
* | ||
* @param view | ||
* @param testId | ||
* @param <T> | ||
*/ | ||
public static <T extends View> void setTestId(T view, String testId) { | ||
if (!TEST_IDS.containsKey(testId)) { | ||
TEST_IDS.put(testId, view.getResources().getIdentifier(testId, "id", view.getContext().getPackageName())); | ||
} | ||
int mappedTestId = TEST_IDS.get(testId); | ||
if (mappedTestId != 0) { | ||
ORIGINAL_REACT_TAGS.put(System.identityHashCode(view), view.getId()); | ||
view.setId(mappedTestId); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the tag originally generated by the JS when the view was created prior to | ||
* it being potentially overwritten by {@link #setTestId}. | ||
* | ||
* @param view | ||
* @param <T> | ||
* @return | ||
*/ | ||
public static <T extends View> int getOriginalReactTag(T view) { | ||
Integer idFromJs = ORIGINAL_REACT_TAGS.get(System.identityHashCode(view)); | ||
return idFromJs != null ? idFromJs.intValue() : view.getId(); | ||
} | ||
|
||
/** | ||
* Used internally to clear the state of test ids. | ||
*/ | ||
@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
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.