-
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 pull request #1212 from OpenSRP/tt--layer-unit-tests
Unit test Layer Package
- Loading branch information
Showing
8 changed files
with
367 additions
and
5 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
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
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
97 changes: 97 additions & 0 deletions
97
opensrp-reveal/src/test/java/org/smartregister/reveal/layer/DigitalGlobeLayerTest.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,97 @@ | ||
package org.smartregister.reveal.layer; | ||
|
||
import com.mapbox.mapboxsdk.style.layers.Layer; | ||
import com.mapbox.mapboxsdk.style.layers.RasterLayer; | ||
import com.mapbox.mapboxsdk.style.sources.RasterSource; | ||
import com.mapbox.mapboxsdk.style.sources.Source; | ||
import com.mapbox.mapboxsdk.utils.ThreadUtils; | ||
|
||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.reveal.BaseUnitTest; | ||
import org.smartregister.reveal.shadow.RasterLayerShadow; | ||
import org.smartregister.reveal.shadow.RasterSourceShadow; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Created by samuelgithengi on 1/12/21. | ||
*/ | ||
@Config(shadows = {RasterSourceShadow.class, RasterLayerShadow.class}) | ||
public class DigitalGlobeLayerTest extends BaseUnitTest { | ||
|
||
private DigitalGlobeLayer layer; | ||
|
||
|
||
@Before | ||
public void setUp() { | ||
ThreadUtils.init(RuntimeEnvironment.application); | ||
layer = new DigitalGlobeLayer(); | ||
|
||
} | ||
|
||
@Test | ||
public void testConstructorShouldInitializesLayers() { | ||
LinkedHashSet<Layer> layers = ReflectionHelpers.getField(layer, "layers"); | ||
List<Source> sources = ReflectionHelpers.getField(layer, "sources"); | ||
assertEquals(1, layers.size()); | ||
assertEquals(1, sources.size()); | ||
RasterLayer rasterLayer = (RasterLayer) layers.iterator().next(); | ||
assertEquals(DigitalGlobeLayer.satelliteLayerId, rasterLayer.getId()); | ||
assertEquals(DigitalGlobeLayer.satelliteSourceId, rasterLayer.getSourceId()); | ||
|
||
RasterSource rasterSource = (RasterSource) sources.get(0); | ||
assertEquals(DigitalGlobeLayer.satelliteSourceId, rasterSource.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetDisplayNameShouldReturnCorrectName() { | ||
assertEquals("Digital Globe", layer.getDisplayName()); | ||
} | ||
|
||
|
||
@Test | ||
public void testGetSourceIdsReturnCorrectIds() { | ||
assertArrayEquals(new String[]{DigitalGlobeLayer.satelliteSourceId}, layer.getSourceIds()); | ||
} | ||
|
||
|
||
@Test | ||
public void testGetLayersReturnCorrectLayers() { | ||
LinkedHashSet<Layer> layers = layer.getLayers(); | ||
assertEquals(1, layers.size()); | ||
Layer layer = layers.iterator().next(); | ||
MatcherAssert.assertThat(layers.iterator().next(), Matchers.instanceOf(RasterLayer.class)); | ||
assertEquals(DigitalGlobeLayer.satelliteLayerId, layer.getId()); | ||
assertEquals(DigitalGlobeLayer.satelliteSourceId, ((RasterLayer) layer).getSourceId()); | ||
} | ||
|
||
@Test | ||
public void testGetSourcesReturnCorrectSources() { | ||
List<Source> sources = layer.getSources(); | ||
assertEquals(1, sources.size()); | ||
Source source = sources.iterator().next(); | ||
MatcherAssert.assertThat(sources.iterator().next(), Matchers.instanceOf(Source.class)); | ||
assertEquals(DigitalGlobeLayer.satelliteSourceId, source.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetIdShouldReturnCorrectId() { | ||
assertEquals("dg-satellite-base-layer", layer.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetLayerIdsShouldReturnCorrectId() { | ||
assertArrayEquals(new String[]{DigitalGlobeLayer.satelliteLayerId}, layer.getLayerIds()); | ||
} | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
opensrp-reveal/src/test/java/org/smartregister/reveal/layer/MapboxLayerTest.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,99 @@ | ||
package org.smartregister.reveal.layer; | ||
|
||
import com.mapbox.mapboxsdk.style.layers.Layer; | ||
import com.mapbox.mapboxsdk.style.layers.RasterLayer; | ||
import com.mapbox.mapboxsdk.style.sources.RasterSource; | ||
import com.mapbox.mapboxsdk.style.sources.Source; | ||
import com.mapbox.mapboxsdk.utils.ThreadUtils; | ||
|
||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.util.ReflectionHelpers; | ||
import org.smartregister.reveal.BaseUnitTest; | ||
import org.smartregister.reveal.shadow.RasterLayerShadow; | ||
import org.smartregister.reveal.shadow.RasterSourceShadow; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Created by samuelgithengi on 1/12/21. | ||
*/ | ||
@Config(shadows = {RasterSourceShadow.class, RasterLayerShadow.class}) | ||
public class MapboxLayerTest extends BaseUnitTest { | ||
|
||
private MapBoxLayer layer; | ||
|
||
|
||
@Before | ||
public void setUp() { | ||
ThreadUtils.init(RuntimeEnvironment.application); | ||
layer = new MapBoxLayer(); | ||
|
||
} | ||
|
||
@Test | ||
public void testConstructorShouldInitializesLayers() { | ||
LinkedHashSet<Layer> layers = ReflectionHelpers.getField(layer, "layers"); | ||
List<Source> sources = ReflectionHelpers.getField(layer, "sources"); | ||
assertEquals(1, layers.size()); | ||
assertEquals(1, sources.size()); | ||
RasterLayer rasterLayer = (RasterLayer) layers.iterator().next(); | ||
assertEquals(MapBoxLayer.satelliteLayerId, rasterLayer.getId()); | ||
assertEquals(MapBoxLayer.satelliteSourceId, rasterLayer.getSourceId()); | ||
|
||
RasterSource rasterSource = (RasterSource) sources.get(0); | ||
assertEquals(MapBoxLayer.satelliteSourceId, rasterSource.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetDisplayNameShouldReturnCorrectName() { | ||
assertEquals("Mapbox Satellite", layer.getDisplayName()); | ||
} | ||
|
||
|
||
@Test | ||
public void testGetSourceIdsReturnCorrectIds() { | ||
assertArrayEquals(new String[]{MapBoxLayer.satelliteSourceId}, layer.getSourceIds()); | ||
} | ||
|
||
|
||
@Test | ||
public void testGetLayersReturnCorrectLayers() { | ||
LinkedHashSet<Layer> layers = layer.getLayers(); | ||
assertEquals(1, layers.size()); | ||
Layer layer = layers.iterator().next(); | ||
MatcherAssert.assertThat(layers.iterator().next(), Matchers.instanceOf(RasterLayer.class)); | ||
assertEquals(MapBoxLayer.satelliteLayerId, layer.getId()); | ||
assertEquals(MapBoxLayer.satelliteSourceId, ((RasterLayer) layer).getSourceId()); | ||
} | ||
|
||
@Test | ||
public void testGetSourcesReturnCorrectSources() { | ||
List<Source> sources = layer.getSources(); | ||
assertEquals(1, sources.size()); | ||
Source source = sources.iterator().next(); | ||
MatcherAssert.assertThat(sources.iterator().next(), Matchers.instanceOf(Source.class)); | ||
assertEquals(MapBoxLayer.satelliteSourceId, source.getId()); | ||
RasterSource rasterSource = (RasterSource) source; | ||
assertEquals(MapBoxLayer.satelliteSourceId, rasterSource.getUri()); | ||
} | ||
|
||
@Test | ||
public void testGetIdShouldReturnCorrectId() { | ||
assertEquals("mapbox-satellite-base-layer", layer.getId()); | ||
} | ||
|
||
@Test | ||
public void testGetLayerIdsShouldReturnCorrectId() { | ||
assertArrayEquals(new String[]{MapBoxLayer.satelliteLayerId}, layer.getLayerIds()); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
opensrp-reveal/src/test/java/org/smartregister/reveal/shadow/RasterLayerShadow.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,29 @@ | ||
package org.smartregister.reveal.shadow; | ||
|
||
import com.mapbox.mapboxsdk.style.layers.RasterLayer; | ||
|
||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
|
||
/** | ||
* Created by samuelgithengi on 1/12/21. | ||
*/ | ||
|
||
@Implements(RasterLayer.class) | ||
public class RasterLayerShadow extends LayerShadow { | ||
|
||
private String sourceId; | ||
|
||
@Implementation | ||
public void __constructor__(String layerId, String sourceId) { | ||
// Do nothing | ||
setLayerId(layerId); | ||
this.sourceId = sourceId; | ||
} | ||
|
||
@Implementation | ||
public String getSourceId() { | ||
return sourceId; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
opensrp-reveal/src/test/java/org/smartregister/reveal/shadow/RasterSourceShadow.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,41 @@ | ||
package org.smartregister.reveal.shadow; | ||
|
||
import com.mapbox.mapboxsdk.style.sources.RasterSource; | ||
import com.mapbox.mapboxsdk.style.sources.TileSet; | ||
|
||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
|
||
/** | ||
* Created by samuelgithengi on 1/12/21. | ||
*/ | ||
@Implements(RasterSource.class) | ||
public class RasterSourceShadow extends SourceShadow { | ||
|
||
|
||
private String id; | ||
private String uri; | ||
|
||
@Implementation | ||
public void __constructor__(String id, TileSet tileSet, int tileSize) { | ||
this.id = id; | ||
} | ||
|
||
@Implementation | ||
public void __constructor__(String id, String uri, int tileSize) { | ||
this.id = id; | ||
this.uri = uri; | ||
} | ||
|
||
@Implementation | ||
protected String nativeGetId() { | ||
return id; | ||
} | ||
|
||
@Implementation | ||
protected String nativeGetUrl() { | ||
return uri; | ||
} | ||
|
||
|
||
} |
96 changes: 96 additions & 0 deletions
96
...p-reveal/src/test/java/org/smartregister/reveal/widget/RevealToasterNotesFactoryTest.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,96 @@ | ||
package org.smartregister.reveal.widget; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import com.vijay.jsonwizard.fragments.JsonFormFragment; | ||
import com.vijay.jsonwizard.interfaces.CommonListener; | ||
import com.vijay.jsonwizard.interfaces.JsonApi; | ||
import com.vijay.jsonwizard.utils.ValidationStatus; | ||
import com.vijay.jsonwizard.validators.edittext.RequiredValidator; | ||
|
||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.Matchers; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Captor; | ||
import org.mockito.Mock; | ||
import org.robolectric.RuntimeEnvironment; | ||
import org.smartregister.reveal.BaseUnitTest; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* Created by samuelgithengi on 1/12/21. | ||
*/ | ||
public class RevealToasterNotesFactoryTest extends BaseUnitTest { | ||
|
||
private RevealToasterNotesFactory factory; | ||
|
||
private Context context; | ||
|
||
@Mock | ||
private JsonFormFragment formFragment; | ||
@Mock | ||
private CommonListener listener; | ||
@Mock | ||
private JSONObject jsonObject; | ||
@Mock | ||
private JsonApi jsonApi; | ||
@Captor | ||
private ArgumentCaptor<TextView> textViewArgumentCaptor; | ||
|
||
|
||
@Before | ||
public void setUp() throws JSONException { | ||
factory = new RevealToasterNotesFactory(); | ||
jsonObject = new JSONObject("{\"key\":\"displayRecommendedNumNetsToDistribute\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"\",\"openmrs_entity_id\":\"\",\"type\":\"toaster_notes\",\"text\":\"2 nets should be distributed\",\"text_color\":\"#000000\",\"toaster_type\":\"info\",\"v_required\":{\"value\":true,\"err\":\"Toast notes Field required\"}}"); | ||
context = RuntimeEnvironment.application; | ||
when(formFragment.getJsonApi()).thenReturn(jsonApi); | ||
} | ||
|
||
@Test | ||
public void testGetViewsFromJsonShouldReturnView() throws Exception { | ||
List<View> views = factory.getViewsFromJson("step1", context, formFragment, jsonObject, listener); | ||
assertEquals(1, views.size()); | ||
verify(jsonApi).addFormDataView(textViewArgumentCaptor.capture()); | ||
TextView textView = textViewArgumentCaptor.getValue(); | ||
assertEquals(com.vijay.jsonwizard.R.id.toaster_notes_text, textView.getId()); | ||
assertEquals("step1:displayRecommendedNumNetsToDistribute", textView.getTag(com.vijay.jsonwizard.R.id.address)); | ||
assertEquals("displayRecommendedNumNetsToDistribute", textView.getTag(com.vijay.jsonwizard.R.id.key)); | ||
MatcherAssert.assertThat(textView.getTag(com.vijay.jsonwizard.R.id.v_required), Matchers.instanceOf(RequiredValidator.class)); | ||
} | ||
|
||
@Test | ||
public void testValidateShouldReturnFailedValidation() throws Exception { | ||
factory.getViewsFromJson("step1", context, formFragment, jsonObject, listener); | ||
verify(jsonApi).addFormDataView(textViewArgumentCaptor.capture()); | ||
TextView textView = textViewArgumentCaptor.getValue(); | ||
ValidationStatus validateStatus = RevealToasterNotesFactory.validate(formFragment, textView); | ||
assertFalse(validateStatus.isValid()); | ||
assertEquals("Toast notes Field required", validateStatus.getErrorMessage()); | ||
} | ||
|
||
@Test | ||
public void testValidateShouldReturnValidValidationWhenViewIsNotShown() throws Exception { | ||
factory.getViewsFromJson("step1", context, formFragment, jsonObject, listener); | ||
verify(jsonApi).addFormDataView(textViewArgumentCaptor.capture()); | ||
TextView textView = textViewArgumentCaptor.getValue(); | ||
View view = (View) textView.getParent().getParent(); | ||
view.setVisibility(View.GONE); | ||
ValidationStatus validateStatus = RevealToasterNotesFactory.validate(formFragment, textView); | ||
assertTrue(validateStatus.isValid()); | ||
assertNull(validateStatus.getErrorMessage()); | ||
} | ||
} |