Skip to content

Commit

Permalink
Refactor for testability ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
ndegwamartin committed Sep 20, 2022
1 parent aea5366 commit 2c94dec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,11 +19,10 @@
*/
public class RecyclerViewPaginatedAdapter<V extends RecyclerView.ViewHolder> extends RecyclerViewCursorAdapter {
private final RecyclerViewProvider<RecyclerView.ViewHolder> listItemProvider;
private CommonRepository commonRepository;

public int totalcount = 0;
public int currentlimit = 20;
public int currentoffset = 0;
private CommonRepository commonRepository;

public RecyclerViewPaginatedAdapter(Cursor cursor,
RecyclerViewProvider<RecyclerView.ViewHolder>
Expand All @@ -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(),
Expand All @@ -59,6 +58,11 @@ public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, Cursor cursor)
}
}

@VisibleForTesting
protected void updateFooterViewCounts(RecyclerViewProvider<RecyclerView.ViewHolder> listItemProvider, RecyclerView.ViewHolder viewHolder) {
new Handler(getMainLooper()).post(() -> listItemProvider.getFooterView(viewHolder, getCurrentPageCount(), getTotalPageCount(), hasNextPage(), hasPreviousPage()));
}

// Pagination
private int getCurrentPageCount() {
if (currentoffset != 0) {
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);

}

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2c94dec

Please sign in to comment.