From 2c94dec98954bfc02fe7bebba28ab2cef545d010 Mon Sep 17 00:00:00 2001 From: Martin Ndegwa Date: Tue, 20 Sep 2022 10:17:15 +0300 Subject: [PATCH] =?UTF-8?q?Refactor=20for=20testability=20=E2=9C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RecyclerViewPaginatedAdapter.java | 30 +++++++++++-------- .../RecyclerViewPaginatedAdapterTest.java | 27 +++++++++-------- .../activity/BaseProfileActivityTest.java | 1 - 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/opensrp-core/src/main/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapter.java b/opensrp-core/src/main/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapter.java index 42052d759..22c00744c 100644 --- a/opensrp-core/src/main/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapter.java +++ b/opensrp-core/src/main/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapter.java @@ -7,6 +7,7 @@ import android.view.ViewGroup; import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import androidx.recyclerview.widget.RecyclerView; import org.smartregister.commonregistry.CommonPersonObject; @@ -18,11 +19,10 @@ */ public class RecyclerViewPaginatedAdapter extends RecyclerViewCursorAdapter { private final RecyclerViewProvider listItemProvider; - private CommonRepository commonRepository; - public int totalcount = 0; public int currentlimit = 20; public int currentoffset = 0; + private CommonRepository commonRepository; public RecyclerViewPaginatedAdapter(Cursor cursor, RecyclerViewProvider @@ -47,9 +47,8 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor) { if (listItemProvider.isFooterViewHolder(viewHolder)) { // make sure counts are updated before updating the view - (new Handler(getMainLooper())).post(() -> { - listItemProvider.getFooterView(viewHolder, getCurrentPageCount(), getTotalPageCount(), hasNextPage(), hasPreviousPage()); - }); + updateFooterViewCounts(listItemProvider, viewHolder); + } else { CommonPersonObject personinlist = commonRepository.readAllcommonforCursorAdapter(cursor); CommonPersonObjectClient pClient = new CommonPersonObjectClient(personinlist.getCaseId(), @@ -59,6 +58,11 @@ public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor) } } + @VisibleForTesting + protected void updateFooterViewCounts(RecyclerViewProvider listItemProvider, RecyclerView.ViewHolder viewHolder) { + new Handler(getMainLooper()).post(() -> listItemProvider.getFooterView(viewHolder, getCurrentPageCount(), getTotalPageCount(), hasNextPage(), hasPreviousPage())); + } + // Pagination private int getCurrentPageCount() { if (currentoffset != 0) { @@ -97,28 +101,28 @@ public void previousPageOffset() { currentoffset = currentoffset - currentlimit; } - public void setTotalcount(int totalcount) { - this.totalcount = totalcount; - } - public int getTotalcount() { return totalcount; } - public void setCurrentoffset(int currentoffset) { - this.currentoffset = currentoffset; + public void setTotalcount(int totalcount) { + this.totalcount = totalcount; } public int getCurrentoffset() { return currentoffset; } - public void setCurrentlimit(int currentlimit) { - this.currentlimit = currentlimit; + public void setCurrentoffset(int currentoffset) { + this.currentoffset = currentoffset; } public int getCurrentlimit() { return currentlimit; } + public void setCurrentlimit(int currentlimit) { + this.currentlimit = currentlimit; + } + } diff --git a/opensrp-core/src/test/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapterTest.java b/opensrp-core/src/test/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapterTest.java index 11cdf06fe..91172bc67 100644 --- a/opensrp-core/src/test/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapterTest.java +++ b/opensrp-core/src/test/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapterTest.java @@ -1,20 +1,28 @@ package org.smartregister.cursoradapter; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import android.content.Context; import android.database.Cursor; -import androidx.recyclerview.widget.RecyclerView; import android.widget.LinearLayout; +import androidx.recyclerview.widget.RecyclerView; +import androidx.test.core.app.ApplicationProvider; + import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.powermock.reflect.Whitebox; -import androidx.test.core.app.ApplicationProvider; import org.smartregister.BaseUnitTest; import org.smartregister.commonregistry.CommonPersonObject; import org.smartregister.commonregistry.CommonPersonObjectClient; @@ -23,12 +31,6 @@ import java.util.HashMap; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - /** * Created by Richard Kareko on 6/16/20. @@ -99,10 +101,11 @@ public void testOnCreateFooterHolder() { @Test public void testOnBindViewFootHolder() { - adapter.setTotalcount(20); + RecyclerViewPaginatedAdapter adapterSpy = Mockito.spy(adapter); + adapterSpy.setTotalcount(20); when(listItemProvider.isFooterViewHolder(mockViewHolder)).thenReturn(true); - adapter.onBindViewHolder(mockViewHolder, mCursor); - verify(listItemProvider).getFooterView(mockViewHolder, 1, 1, false, false); + adapterSpy.onBindViewHolder(mockViewHolder, mCursor); + verify(adapterSpy).updateFooterViewCounts(listItemProvider, mockViewHolder); } @@ -117,7 +120,7 @@ public void testOnBindViewHolder() { String caseId = "case 1"; String relationId = "identifier 123"; String type = "bindtype"; - CommonPersonObject personInList = new CommonPersonObject(caseId, relationId, details,type); + CommonPersonObject personInList = new CommonPersonObject(caseId, relationId, details, type); personInList.setColumnmaps(columnmaps); when(commonRepository.readAllcommonforCursorAdapter(mCursor)).thenReturn(personInList); diff --git a/opensrp-core/src/test/java/org/smartregister/view/activity/BaseProfileActivityTest.java b/opensrp-core/src/test/java/org/smartregister/view/activity/BaseProfileActivityTest.java index 3ddd719ae..ec139f478 100644 --- a/opensrp-core/src/test/java/org/smartregister/view/activity/BaseProfileActivityTest.java +++ b/opensrp-core/src/test/java/org/smartregister/view/activity/BaseProfileActivityTest.java @@ -71,7 +71,6 @@ public void testOnCreateShouldBootstrapActivity() { Mockito.verify(actionBar).setDisplayHomeAsUpEnabled(true); Mockito.verify(profileActivity).setContentView(ArgumentMatchers.anyInt()); - ; Mockito.verify(profileActivity).findViewById(R.id.btn_profile_registration_info); Mockito.verify(profileActivity).findViewById(R.id.collapsing_toolbar_appbarlayout); Mockito.verify(profileActivity).findViewById(R.id.collapsing_toolbar);