-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #573 from opensrp/tt-apr-20-21-property-mngr
add tests TT
- Loading branch information
Showing
4 changed files
with
128 additions
and
7 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 39 additions & 3 deletions
42
...-json-form-wizard/src/test/java/com/vijay/jsonwizard/adapter/DynamicLabelAdapterTest.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 |
---|---|---|
@@ -1,35 +1,71 @@ | ||
package com.vijay.jsonwizard.adapter; | ||
|
||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
import android.widget.LinearLayout; | ||
|
||
import com.vijay.jsonwizard.BaseTest; | ||
import com.vijay.jsonwizard.R; | ||
import com.vijay.jsonwizard.model.DynamicLabelInfo; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.util.ReflectionHelpers; | ||
|
||
import java.util.ArrayList; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.spy; | ||
|
||
public class DynamicLabelAdapterTest extends BaseTest { | ||
|
||
private DynamicLabelAdapter multiSelectListAdapter; | ||
private DynamicLabelAdapter dynamicLabelAdapter; | ||
|
||
@Before | ||
public void setUp() { | ||
multiSelectListAdapter = spy(new DynamicLabelAdapter(RuntimeEnvironment.application, new ArrayList<DynamicLabelInfo>())); | ||
dynamicLabelAdapter = spy(new DynamicLabelAdapter(RuntimeEnvironment.application, new ArrayList<DynamicLabelInfo>())); | ||
} | ||
|
||
@Test | ||
public void testOnCreateViewHolder() { | ||
LinearLayout linearLayout = new LinearLayout(RuntimeEnvironment.application); | ||
RecyclerView.ViewHolder itemView = multiSelectListAdapter.onCreateViewHolder(linearLayout, 0); | ||
RecyclerView.ViewHolder itemView = dynamicLabelAdapter.onCreateViewHolder(linearLayout, 0); | ||
assertNotNull(itemView); | ||
assertTrue(itemView instanceof DynamicLabelAdapter.RecyclerViewHolder); | ||
} | ||
|
||
@Test | ||
public void testOnBindViewHolderShouldShowNonBlankViews() { | ||
DynamicLabelInfo dynamicLabelInfo = new DynamicLabelInfo("sampleTitle", "sampleText", "avatar_woman.png"); | ||
ArrayList<DynamicLabelInfo> dynamicLabelInfos = new ArrayList<>(); | ||
dynamicLabelInfos.add(dynamicLabelInfo); | ||
ReflectionHelpers.setField(dynamicLabelAdapter, "dynamicLabelInfoList", dynamicLabelInfos); | ||
LinearLayout linearLayout = new LinearLayout(RuntimeEnvironment.application); | ||
RecyclerView.ViewHolder viewHolder = dynamicLabelAdapter.onCreateViewHolder(linearLayout, 0); | ||
|
||
dynamicLabelAdapter.onBindViewHolder(viewHolder, 0); | ||
|
||
assertEquals(View.VISIBLE, viewHolder.itemView.findViewById(R.id.descriptionText).getVisibility()); | ||
assertEquals(View.VISIBLE, viewHolder.itemView.findViewById(R.id.labelTitle).getVisibility()); | ||
assertEquals(View.VISIBLE, viewHolder.itemView.findViewById(R.id.imageViewLabel).getVisibility()); | ||
} | ||
|
||
@Test | ||
public void testOnBindViewHolderShouldHideBlankViews() { | ||
DynamicLabelInfo dynamicLabelInfo = new DynamicLabelInfo("", "", ""); | ||
ArrayList<DynamicLabelInfo> dynamicLabelInfos = new ArrayList<>(); | ||
dynamicLabelInfos.add(dynamicLabelInfo); | ||
ReflectionHelpers.setField(dynamicLabelAdapter, "dynamicLabelInfoList", dynamicLabelInfos); | ||
LinearLayout linearLayout = new LinearLayout(RuntimeEnvironment.application); | ||
RecyclerView.ViewHolder viewHolder = dynamicLabelAdapter.onCreateViewHolder(linearLayout, 0); | ||
|
||
dynamicLabelAdapter.onBindViewHolder(viewHolder, 0); | ||
|
||
assertEquals(View.GONE, viewHolder.itemView.findViewById(R.id.descriptionText).getVisibility()); | ||
assertEquals(View.GONE, viewHolder.itemView.findViewById(R.id.labelTitle).getVisibility()); | ||
assertEquals(View.GONE, viewHolder.itemView.findViewById(R.id.imageViewLabel).getVisibility()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/PropertyManagerTest.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,82 @@ | ||
package com.vijay.jsonwizard.utils; | ||
|
||
import android.Manifest; | ||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.telephony.TelephonyManager; | ||
|
||
import com.vijay.jsonwizard.BaseTest; | ||
import com.vijay.jsonwizard.activities.JsonFormActivity; | ||
import com.vijay.jsonwizard.interfaces.OnActivityRequestPermissionResultListener; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.MockitoAnnotations; | ||
import org.robolectric.Robolectric; | ||
import org.robolectric.util.ReflectionHelpers; | ||
|
||
import java.util.HashMap; | ||
|
||
import static com.vijay.jsonwizard.utils.PropertyManager.DEVICE_ID_PROPERTY; | ||
import static com.vijay.jsonwizard.utils.PropertyManager.PHONE_NUMBER_PROPERTY; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
|
||
public class PropertyManagerTest extends BaseTest { | ||
|
||
private JsonFormActivity jsonFormActivity; | ||
|
||
private PropertyManager propertyManager; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
jsonFormActivity = spy(Robolectric.buildActivity(JsonFormActivity.class, getJsonFormActivityIntent()).create().get()); | ||
propertyManager = ReflectionHelpers.getField(jsonFormActivity, "propertyManager"); | ||
} | ||
|
||
@Test | ||
public void testHandleOnRequestPermissionResultsShouldPopulatePropertiesIfPermissionIsGranted() { | ||
ReflectionHelpers.setField(propertyManager, "mContext", jsonFormActivity); | ||
doReturn(createMockTelephonyManager()).when(jsonFormActivity).getSystemService(Context.TELEPHONY_SERVICE); | ||
HashMap<Integer, OnActivityRequestPermissionResultListener> resultListenerHashMap = ReflectionHelpers.getField(jsonFormActivity, "onActivityRequestPermissionResultListeners"); | ||
resultListenerHashMap.get(PermissionUtils.PHONE_STATE_PERMISSION) | ||
.onRequestPermissionResult(PermissionUtils.PHONE_STATE_PERMISSION, new String[]{Manifest.permission.READ_PHONE_STATE}, new int[]{PackageManager.PERMISSION_GRANTED}); | ||
|
||
HashMap<String, String> mProperties = ReflectionHelpers.getField(propertyManager, "mProperties"); | ||
assertEquals(4, mProperties.size()); | ||
assertEquals("mda-2323", mProperties.get(DEVICE_ID_PROPERTY)); | ||
assertEquals("tel-123", mProperties.get(PHONE_NUMBER_PROPERTY)); | ||
} | ||
|
||
@Test | ||
public void testHandleOnRequestPermissionResultsShouldNotPopulatePropertiesIfPermissionIsDenied() { | ||
ReflectionHelpers.setField(propertyManager, "mContext", jsonFormActivity); | ||
HashMap<Integer, OnActivityRequestPermissionResultListener> resultListenerHashMap = ReflectionHelpers.getField(jsonFormActivity, "onActivityRequestPermissionResultListeners"); | ||
resultListenerHashMap.get(PermissionUtils.PHONE_STATE_PERMISSION) | ||
.onRequestPermissionResult(PermissionUtils.PHONE_STATE_PERMISSION, new String[]{Manifest.permission.READ_PHONE_STATE}, new int[]{PackageManager.PERMISSION_DENIED}); | ||
|
||
HashMap<String, String> mProperties = ReflectionHelpers.getField(propertyManager, "mProperties"); | ||
assertTrue(mProperties.isEmpty()); | ||
} | ||
|
||
private TelephonyManager createMockTelephonyManager() { | ||
TelephonyManager mockTelephonyManager = mock(TelephonyManager.class); | ||
doReturn(TelephonyManager.PHONE_TYPE_SIP).when(mockTelephonyManager).getPhoneType(); | ||
doReturn("mda-2323").when(mockTelephonyManager).getDeviceId(); | ||
doReturn("sub-2123").when(mockTelephonyManager).getSubscriberId(); | ||
doReturn("sim-123").when(mockTelephonyManager).getSimSerialNumber(); | ||
doReturn("tel-123").when(mockTelephonyManager).getLine1Number(); | ||
return mockTelephonyManager; | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
if (jsonFormActivity != null) | ||
jsonFormActivity.finish(); | ||
} | ||
} |