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
8 changed files
with
201 additions
and
13 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
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
141 changes: 141 additions & 0 deletions
141
ReactAndroid/src/test/java/com/facebook/react/uimanager/BaseViewManagerTest.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,141 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.uimanager; | ||
|
||
import android.content.Context; | ||
import android.content.res.Resources; | ||
import android.view.View; | ||
|
||
import com.facebook.csslayout.Spacing; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.MethodSorters; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.core.classloader.annotations.PowerMockIgnore; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.robolectric.RobolectricTestRunner; | ||
import org.robolectric.annotation.Config; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.AdditionalMatchers.not; | ||
import static org.mockito.Matchers.anyFloat; | ||
import static org.mockito.Matchers.anyInt; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
@Config(manifest= Config.NONE) | ||
@RunWith(RobolectricTestRunner.class) | ||
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) | ||
public class BaseViewManagerTest { | ||
|
||
@Mock | ||
Resources resources; | ||
@Mock | ||
Context context; | ||
@Mock | ||
private View view; | ||
private BaseViewManager<View, LayoutShadowNode> sut; | ||
|
||
private final String testID = "some-test-id"; | ||
private final int mappedTestID = 23457897; | ||
private final int originalJsTag = 5; | ||
private final String myPackage = "com.myApp"; | ||
|
||
|
||
@Before | ||
public void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
when(view.getContext()).thenReturn(context); | ||
when(view.getResources()).thenReturn(resources); | ||
when(view.getId()).thenReturn(originalJsTag); | ||
when(resources.getIdentifier(eq(testID), eq("id"), eq(myPackage))).thenReturn(mappedTestID); | ||
when(resources.getIdentifier(eq(testID), eq("id"), not(eq(myPackage)))).thenReturn(0); | ||
sut = new ViewManagerStub(); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
BaseViewManager.resetTestState(); | ||
} | ||
|
||
@Test | ||
public void testSetTestId_should_always_call_setTag() { | ||
String expectedTestID1 = "asdfasdf1"; | ||
String expectedTestID2 = "asdfasdf2"; | ||
when(context.getPackageName()).thenReturn("com.foo"); | ||
sut.setTestId(view, expectedTestID1); | ||
sut.setTestId(view, expectedTestID2); | ||
verify(view).setTag(expectedTestID1); | ||
verify(view).setTag(expectedTestID2); | ||
} | ||
|
||
@Test | ||
public void testSetTestId_should_not_set_the_mapped_testID_on_the_view_when_a_resource_id_is_not_found() { | ||
when(context.getPackageName()).thenReturn("com.foo"); | ||
sut.setTestId(view, testID); | ||
verify(view, never()).setId(anyInt()); | ||
} | ||
|
||
@Test | ||
public void testSetTestId_should_set_the_mapped_testID_on_the_view_when_a_resource_id_is_found() { | ||
when(context.getPackageName()).thenReturn(myPackage); | ||
sut.setTestId(view, testID); | ||
verify(view).setId(mappedTestID); | ||
} | ||
|
||
@Test | ||
public void getOriginalReactTag_should_return_the_original_tag_set_by_js_if_no_testID_has_been_set() { | ||
assertEquals("The original JS tag was not returned", BaseViewManager.getOriginalReactTag(view), originalJsTag); | ||
} | ||
|
||
@Test | ||
public void getOriginalReactTag_should_return_the_original_tag_set_by_js_if_testID_has_been_set() { | ||
when(context.getPackageName()).thenReturn(myPackage); | ||
sut.setTestId(view, testID); | ||
verify(view).setId(mappedTestID); | ||
assertEquals("The original JS tag was not returned", BaseViewManager.getOriginalReactTag(view), originalJsTag); | ||
} | ||
|
||
private static class ViewManagerStub extends BaseViewManager<View, LayoutShadowNode> { | ||
@Override | ||
public String getName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public LayoutShadowNode createShadowNodeInstance() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Class<? extends LayoutShadowNode> getShadowNodeClass() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected View createViewInstance(ThemedReactContext reactContext) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void updateExtraData(View root, Object extraData) { | ||
|
||
} | ||
} | ||
} |