Skip to content

Commit 2c94dec

Browse files
committed
Refactor for testability ✅
1 parent aea5366 commit 2c94dec

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

opensrp-core/src/main/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapter.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.view.ViewGroup;
88

99
import androidx.annotation.NonNull;
10+
import androidx.annotation.VisibleForTesting;
1011
import androidx.recyclerview.widget.RecyclerView;
1112

1213
import org.smartregister.commonregistry.CommonPersonObject;
@@ -18,11 +19,10 @@
1819
*/
1920
public class RecyclerViewPaginatedAdapter<V extends RecyclerView.ViewHolder> extends RecyclerViewCursorAdapter {
2021
private final RecyclerViewProvider<RecyclerView.ViewHolder> listItemProvider;
21-
private CommonRepository commonRepository;
22-
2322
public int totalcount = 0;
2423
public int currentlimit = 20;
2524
public int currentoffset = 0;
25+
private CommonRepository commonRepository;
2626

2727
public RecyclerViewPaginatedAdapter(Cursor cursor,
2828
RecyclerViewProvider<RecyclerView.ViewHolder>
@@ -47,9 +47,8 @@ public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType
4747
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor) {
4848
if (listItemProvider.isFooterViewHolder(viewHolder)) {
4949
// make sure counts are updated before updating the view
50-
(new Handler(getMainLooper())).post(() -> {
51-
listItemProvider.getFooterView(viewHolder, getCurrentPageCount(), getTotalPageCount(), hasNextPage(), hasPreviousPage());
52-
});
50+
updateFooterViewCounts(listItemProvider, viewHolder);
51+
5352
} else {
5453
CommonPersonObject personinlist = commonRepository.readAllcommonforCursorAdapter(cursor);
5554
CommonPersonObjectClient pClient = new CommonPersonObjectClient(personinlist.getCaseId(),
@@ -59,6 +58,11 @@ public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor)
5958
}
6059
}
6160

61+
@VisibleForTesting
62+
protected void updateFooterViewCounts(RecyclerViewProvider<RecyclerView.ViewHolder> listItemProvider, RecyclerView.ViewHolder viewHolder) {
63+
new Handler(getMainLooper()).post(() -> listItemProvider.getFooterView(viewHolder, getCurrentPageCount(), getTotalPageCount(), hasNextPage(), hasPreviousPage()));
64+
}
65+
6266
// Pagination
6367
private int getCurrentPageCount() {
6468
if (currentoffset != 0) {
@@ -97,28 +101,28 @@ public void previousPageOffset() {
97101
currentoffset = currentoffset - currentlimit;
98102
}
99103

100-
public void setTotalcount(int totalcount) {
101-
this.totalcount = totalcount;
102-
}
103-
104104
public int getTotalcount() {
105105
return totalcount;
106106
}
107107

108-
public void setCurrentoffset(int currentoffset) {
109-
this.currentoffset = currentoffset;
108+
public void setTotalcount(int totalcount) {
109+
this.totalcount = totalcount;
110110
}
111111

112112
public int getCurrentoffset() {
113113
return currentoffset;
114114
}
115115

116-
public void setCurrentlimit(int currentlimit) {
117-
this.currentlimit = currentlimit;
116+
public void setCurrentoffset(int currentoffset) {
117+
this.currentoffset = currentoffset;
118118
}
119119

120120
public int getCurrentlimit() {
121121
return currentlimit;
122122
}
123123

124+
public void setCurrentlimit(int currentlimit) {
125+
this.currentlimit = currentlimit;
126+
}
127+
124128
}

opensrp-core/src/test/java/org/smartregister/cursoradapter/RecyclerViewPaginatedAdapterTest.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package org.smartregister.cursoradapter;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.Mockito.verify;
7+
import static org.mockito.Mockito.when;
8+
39
import android.content.Context;
410
import android.database.Cursor;
5-
import androidx.recyclerview.widget.RecyclerView;
611
import android.widget.LinearLayout;
712

13+
import androidx.recyclerview.widget.RecyclerView;
14+
import androidx.test.core.app.ApplicationProvider;
15+
816
import org.junit.Before;
917
import org.junit.Rule;
1018
import org.junit.Test;
1119
import org.mockito.ArgumentCaptor;
1220
import org.mockito.Captor;
1321
import org.mockito.Mock;
22+
import org.mockito.Mockito;
1423
import org.mockito.junit.MockitoJUnit;
1524
import org.mockito.junit.MockitoRule;
1625
import org.powermock.reflect.Whitebox;
17-
import androidx.test.core.app.ApplicationProvider;
1826
import org.smartregister.BaseUnitTest;
1927
import org.smartregister.commonregistry.CommonPersonObject;
2028
import org.smartregister.commonregistry.CommonPersonObjectClient;
@@ -23,12 +31,6 @@
2331
import java.util.HashMap;
2432
import java.util.Map;
2533

26-
import static org.junit.Assert.assertEquals;
27-
import static org.junit.Assert.assertNotNull;
28-
import static org.mockito.ArgumentMatchers.any;
29-
import static org.mockito.Mockito.verify;
30-
import static org.mockito.Mockito.when;
31-
3234

3335
/**
3436
* Created by Richard Kareko on 6/16/20.
@@ -99,10 +101,11 @@ public void testOnCreateFooterHolder() {
99101

100102
@Test
101103
public void testOnBindViewFootHolder() {
102-
adapter.setTotalcount(20);
104+
RecyclerViewPaginatedAdapter adapterSpy = Mockito.spy(adapter);
105+
adapterSpy.setTotalcount(20);
103106
when(listItemProvider.isFooterViewHolder(mockViewHolder)).thenReturn(true);
104-
adapter.onBindViewHolder(mockViewHolder, mCursor);
105-
verify(listItemProvider).getFooterView(mockViewHolder, 1, 1, false, false);
107+
adapterSpy.onBindViewHolder(mockViewHolder, mCursor);
108+
verify(adapterSpy).updateFooterViewCounts(listItemProvider, mockViewHolder);
106109

107110
}
108111

@@ -117,7 +120,7 @@ public void testOnBindViewHolder() {
117120
String caseId = "case 1";
118121
String relationId = "identifier 123";
119122
String type = "bindtype";
120-
CommonPersonObject personInList = new CommonPersonObject(caseId, relationId, details,type);
123+
CommonPersonObject personInList = new CommonPersonObject(caseId, relationId, details, type);
121124
personInList.setColumnmaps(columnmaps);
122125
when(commonRepository.readAllcommonforCursorAdapter(mCursor)).thenReturn(personInList);
123126

opensrp-core/src/test/java/org/smartregister/view/activity/BaseProfileActivityTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public void testOnCreateShouldBootstrapActivity() {
7171

7272
Mockito.verify(actionBar).setDisplayHomeAsUpEnabled(true);
7373
Mockito.verify(profileActivity).setContentView(ArgumentMatchers.anyInt());
74-
;
7574
Mockito.verify(profileActivity).findViewById(R.id.btn_profile_registration_info);
7675
Mockito.verify(profileActivity).findViewById(R.id.collapsing_toolbar_appbarlayout);
7776
Mockito.verify(profileActivity).findViewById(R.id.collapsing_toolbar);

0 commit comments

Comments
 (0)