Skip to content

Commit

Permalink
Merge pull request #573 from opensrp/tt-apr-20-21-property-mngr
Browse files Browse the repository at this point in the history
add tests TT
  • Loading branch information
bennsimon authored Apr 26, 2021
2 parents ea6fc66 + 6d3a2d1 commit bb846c0
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.io.IOException;
import java.util.ArrayList;

import timber.log.Timber;

public class DynamicLabelAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
private final ArrayList<DynamicLabelInfo> dynamicLabelInfoList;
Expand All @@ -37,29 +39,30 @@ public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
final RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
String dynamicLabelTitle = dynamicLabelInfoList.get(position).getDynamicLabelTitle();
DynamicLabelInfo dynamicLabelInfo = dynamicLabelInfoList.get(position);
String dynamicLabelTitle = dynamicLabelInfo.getDynamicLabelTitle();
if (StringUtils.isNotBlank(dynamicLabelTitle)) {
recyclerViewHolder.tileTextView.setText(dynamicLabelTitle);
recyclerViewHolder.tileTextView.setVisibility(View.VISIBLE);
} else {
recyclerViewHolder.tileTextView.setVisibility(View.GONE);
}

String dynamicLabelText = dynamicLabelInfoList.get(position).getDynamicLabelText();
String dynamicLabelText = dynamicLabelInfo.getDynamicLabelText();
if (StringUtils.isNotBlank(dynamicLabelText)) {
recyclerViewHolder.descriptionTextView.setText(dynamicLabelText);
recyclerViewHolder.descriptionTextView.setVisibility(View.VISIBLE);
} else {
recyclerViewHolder.descriptionTextView.setVisibility(View.GONE);
}

String dynamicLabelImageSrc = dynamicLabelInfoList.get(position).getDynamicLabelImageSrc();
String dynamicLabelImageSrc = dynamicLabelInfo.getDynamicLabelImageSrc();
if (StringUtils.isNotBlank(dynamicLabelImageSrc)) {
try {
recyclerViewHolder.imageViewLabel.setImageDrawable(FormUtils.readImageFromAsset(context, dynamicLabelImageSrc));
recyclerViewHolder.imageViewLabel.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
Timber.e(e);
recyclerViewHolder.imageViewLabel.setVisibility(View.GONE);
}
} else {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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());
}
}
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();
}
}

0 comments on commit bb846c0

Please sign in to comment.