-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into dynamic-tasking
- Loading branch information
Showing
3 changed files
with
210 additions
and
0 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
78 changes: 78 additions & 0 deletions
78
.../src/test/java/org/smartregister/reveal/presenter/EventRegisterFragmentPresenterTest.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,78 @@ | ||
package org.smartregister.reveal.presenter; | ||
|
||
import org.apache.commons.lang3.ArrayUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.powermock.reflect.Whitebox; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.configurableviews.helper.ConfigurableViewsHelper; | ||
import org.smartregister.configurableviews.model.View; | ||
import org.smartregister.configurableviews.model.ViewConfiguration; | ||
import org.smartregister.reveal.BaseUnitTest; | ||
import org.smartregister.reveal.contract.EventRegisterContract; | ||
import org.smartregister.reveal.interactor.EventRegisterFragmentInteractor; | ||
import org.smartregister.reveal.util.Constants.EventsRegister; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Created by samuelgithengi on 12/1/20. | ||
*/ | ||
public class EventRegisterFragmentPresenterTest extends BaseUnitTest { | ||
@Mock | ||
private EventRegisterContract.View view; | ||
|
||
@Mock | ||
private ConfigurableViewsHelper viewsHelper; | ||
|
||
@Mock | ||
private EventRegisterFragmentInteractor interactor; | ||
|
||
private EventRegisterFragmentPresenter presenter; | ||
|
||
private Set<View> visibleColumns; | ||
|
||
@Before | ||
public void setUp() { | ||
presenter = new EventRegisterFragmentPresenter(view, EventsRegister.VIEW_IDENTIFIER); | ||
ReflectionHelpers.setField(presenter, "viewsHelper", viewsHelper); | ||
ReflectionHelpers.setField(presenter, "interactor", interactor); | ||
View view = new View(); | ||
view.setIdentifier(UUID.randomUUID().toString()); | ||
visibleColumns = Collections.singleton(view); | ||
} | ||
|
||
@Test | ||
public void testProcessViewConfigurationsShouldRegisterActiveColumns() { | ||
ViewConfiguration viewConfiguration = new ViewConfiguration(); | ||
viewConfiguration.setIdentifier(UUID.randomUUID().toString()); | ||
when(viewsHelper.getViewConfiguration(EventsRegister.VIEW_IDENTIFIER)).thenReturn(viewConfiguration); | ||
presenter.processViewConfigurations(); | ||
verify(viewsHelper).getViewConfiguration(eq(EventsRegister.VIEW_IDENTIFIER)); | ||
verify(viewsHelper).getRegisterActiveColumns(eq(EventsRegister.VIEW_IDENTIFIER)); | ||
verifyNoMoreInteractions(viewsHelper); | ||
} | ||
|
||
@Test | ||
public void testInitializeQueries() { | ||
String mainCondition = "task.group_id = ? AND task.plan_id = ? AND task.status NOT IN (?,?)"; | ||
Whitebox.setInternalState(presenter, "visibleColumns", visibleColumns); | ||
presenter.initializeQueries(mainCondition); | ||
verify(view).initializeAdapter(eq(visibleColumns)); | ||
String[] columns = ArrayUtils.addFirst(presenter.mainColumns(EventsRegister.TABLE_NAME), "ec_events.id as _id"); | ||
verify(view).initializeQueryParams(EventsRegister.TABLE_NAME, "SELECT COUNT(*) FROM ec_events WHERE " + mainCondition + " ", String.format("Select %s FROM %s WHERE %s ", StringUtils.join(columns, " , "), EventsRegister.TABLE_NAME, mainCondition)); | ||
verify(view).countExecute(); | ||
verify(view).filterandSortInInitializeQueries(); | ||
|
||
|
||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...l/src/test/java/org/smartregister/reveal/presenter/ValidateUserLocationPresenterTest.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,131 @@ | ||
package org.smartregister.reveal.presenter; | ||
|
||
import android.location.Location; | ||
|
||
import com.mapbox.mapboxsdk.geometry.LatLng; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.robolectric.util.ReflectionHelpers.ClassParameter; | ||
import org.smartregister.reveal.BaseUnitTest; | ||
import org.smartregister.reveal.R; | ||
import org.smartregister.reveal.contract.UserLocationContract; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.timeout; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Created by samuelgithengi on 12/1/20. | ||
*/ | ||
public class ValidateUserLocationPresenterTest extends BaseUnitTest { | ||
|
||
@Mock | ||
private UserLocationContract.UserLocationView locationView; | ||
|
||
@Mock | ||
private UserLocationContract.UserLocationCallback callback; | ||
|
||
private ValidateUserLocationPresenter presenter; | ||
|
||
private Location location; | ||
|
||
@Before | ||
public void setUp() { | ||
presenter = new ValidateUserLocationPresenter(locationView, callback); | ||
location = new Location("test"); | ||
location.setLatitude(12.1212); | ||
location.setLongitude(67.2232); | ||
} | ||
|
||
@Test | ||
public void testRequestUserLocationShouldInvokeGetUserLocation() { | ||
presenter.requestUserLocation(); | ||
verify(locationView).requestUserLocation(); | ||
} | ||
|
||
@Test | ||
public void testOnGetUserLocationShouldRequestSupervisorPassword() { | ||
|
||
when(callback.getTargetCoordinates()).thenReturn(new LatLng(11.23232, 34.223222)); | ||
|
||
presenter.onGetUserLocation(location); | ||
verify(callback).requestUserPassword(); | ||
verify(callback).getTargetCoordinates(); | ||
verify(locationView).hideProgressDialog(); | ||
} | ||
|
||
@Test | ||
public void testOnGetUserLocationShouldInvokeLocationValidate() { | ||
|
||
when(callback.getTargetCoordinates()).thenReturn(new LatLng(location.getLatitude(), location.getLongitude())); | ||
|
||
presenter.onGetUserLocation(location); | ||
verify(locationView).hideProgressDialog(); | ||
verify(callback).getTargetCoordinates(); | ||
verify(callback).onLocationValidated(); | ||
} | ||
|
||
@Test | ||
public void testOnGetUserLocationFailedShouldRequestForPasword() { | ||
presenter.onGetUserLocationFailed(); | ||
verify(locationView).hideProgressDialog(); | ||
verify(callback).requestUserPassword(); | ||
} | ||
|
||
|
||
@Test | ||
public void testWaitForUserLocationShouldInvokeOnGetUserLocation() { | ||
presenter = spy(presenter); | ||
when(callback.getTargetCoordinates()).thenReturn(new LatLng(location.getLatitude(), location.getLongitude())); | ||
|
||
when(locationView.getUserCurrentLocation()).thenReturn(location); | ||
presenter.waitForUserLocation(); | ||
|
||
verify(presenter, timeout(ASYNC_TIMEOUT)).onGetUserLocation(location); | ||
verify(locationView, timeout(ASYNC_TIMEOUT)).hideProgressDialog(); | ||
verify(callback, timeout(ASYNC_TIMEOUT)).getTargetCoordinates(); | ||
verify(callback, timeout(ASYNC_TIMEOUT)).onLocationValidated(); | ||
} | ||
|
||
|
||
@Test | ||
public void testWaitForUserLocationInvokeOnGetUserLocationFailed() { | ||
presenter = spy(presenter); | ||
when(locationView.getUserCurrentLocation()).thenAnswer(invocation -> { | ||
Thread.sleep(3000); | ||
return null; | ||
}); | ||
ReflectionHelpers.callInstanceMethod(presenter, "waitForUserLocation", ClassParameter.from(long.class, 61000)); | ||
|
||
verify(presenter, timeout(ASYNC_TIMEOUT)).onGetUserLocationFailed(); | ||
verify(locationView, timeout(ASYNC_TIMEOUT)).hideProgressDialog(); | ||
verify(callback, timeout(ASYNC_TIMEOUT)).requestUserPassword(); | ||
} | ||
|
||
|
||
@Test | ||
public void testWaitForUserLocationTryEvery2SecondsUntilTimeout() { | ||
AtomicInteger count = new AtomicInteger(); | ||
when(locationView.getUserCurrentLocation()).thenAnswer(invocation -> { | ||
Thread.sleep(1000); | ||
count.getAndIncrement(); | ||
return count.get() == 3 ? location : null; | ||
}); | ||
when(callback.getTargetCoordinates()).thenReturn(new LatLng(location.getLatitude(), location.getLongitude())); | ||
|
||
|
||
presenter.waitForUserLocation(); | ||
int ASYNC_TIMEOUT = 10 * 1000; | ||
verify(locationView, timeout(ASYNC_TIMEOUT).times(3)).getUserCurrentLocation(); | ||
verify(locationView, timeout(ASYNC_TIMEOUT).times(2)).showProgressDialog(R.string.narrowing_location_title, R.string.narrowing_location_message); | ||
verify(locationView, timeout(ASYNC_TIMEOUT)).hideProgressDialog(); | ||
verify(callback, timeout(ASYNC_TIMEOUT)).onLocationValidated(); | ||
|
||
} | ||
} |