-
Notifications
You must be signed in to change notification settings - Fork 9
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 #160 from deepueg/unit-test-nav-util
Unit tests for NavUtil and Route
- Loading branch information
Showing
3 changed files
with
131 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
51 changes: 51 additions & 0 deletions
51
android/lib/src/test/java/com/ern/api/impl/navigation/NavUtilTest.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,51 @@ | ||
package com.ern.api.impl.navigation; | ||
|
||
import android.os.Bundle; | ||
|
||
import com.ernnavigationApi.ern.model.NavigationBar; | ||
|
||
import org.json.JSONObject; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class NavUtilTest { | ||
|
||
@Test | ||
public void testGetPathReturnsNullForInValidBundle() { | ||
Bundle b = new Bundle(); | ||
assertThat(NavUtils.getPath(b)).isNull(); | ||
b.putString("someKey", "someValue"); | ||
assertThat(NavUtils.getPath(b)).isNull(); | ||
b.putString("path", "validPath"); | ||
assertThat(NavUtils.getPath(b)).isEqualTo("validPath"); | ||
} | ||
|
||
@Test | ||
public void testGetPayload() { | ||
Bundle b = new Bundle(); | ||
assertThat(NavUtils.getPayload(b)).isNull(); | ||
b.putString("jsonPayload", "badJson"); | ||
assertThat(NavUtils.getPayload(b)).isNull(); | ||
b.putString("jsonPayload", "{}"); | ||
assertThat(NavUtils.getPayload(b)).isInstanceOf(JSONObject.class); | ||
} | ||
|
||
@Test | ||
public void testGetNavBar() { | ||
Bundle b = new Bundle(); | ||
Bundle navBar = new NavigationBar.Builder("NavBar").build().toBundle(); | ||
assertThat(NavUtils.getNavBar(b)).isNull(); | ||
b.putString("navigationBar", "badJson"); | ||
assertThat(NavUtils.getNavBar(b)).isNull(); | ||
b.putString("navigationBar", "{}"); | ||
assertThat(NavUtils.getNavBar(b)).isNull(); | ||
b.putBundle("navigationBar", navBar); | ||
assertThat(NavUtils.getNavBar(b)).isInstanceOf(NavigationBar.class); | ||
navBar.remove("title"); //Remove required parameter | ||
assertThat(NavUtils.getNavBar(b)).isNull(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
android/lib/src/test/java/com/ern/api/impl/navigation/RouteTest.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,72 @@ | ||
package com.ern.api.impl.navigation; | ||
|
||
import android.os.Bundle; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
public class RouteTest { | ||
|
||
@Test | ||
public void testRoute() { | ||
Route route = new Route.Builder(new Bundle()).build(); | ||
assertThat(route).isNotNull(); | ||
assertThat(route.getArguments().size()).isEqualTo(0); | ||
assertThat(route.getActionId()).isEqualTo(0); | ||
assertThat(route.isCompleted()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testRoutingNotifierSuccess() { | ||
RoutingNotifier notifier = new RoutingNotifier() { | ||
@Override | ||
public void routingComplete(@NonNull RoutingResult result) { | ||
assertThat(result.isComplete).isTrue(); | ||
assertThat(result.message).isNull(); | ||
} | ||
}; | ||
Route route = new Route.Builder(new Bundle()).routingNotifier(notifier).build(); | ||
route.setResult(true, null); | ||
} | ||
|
||
@Test | ||
public void testRoutingNotifierFailure() { | ||
final String FAILURE_MESSAGE = "some message"; | ||
RoutingNotifier notifier = new RoutingNotifier() { | ||
@Override | ||
public void routingComplete(@NonNull RoutingResult result) { | ||
assertThat(result.isComplete).isFalse(); | ||
assertThat(result.message).isEqualTo(FAILURE_MESSAGE); | ||
} | ||
}; | ||
Route route = new Route.Builder(new Bundle()).routingNotifier(notifier).build(); | ||
route.setResult(false, FAILURE_MESSAGE); | ||
} | ||
|
||
@Test | ||
public void testRoutingNotifierOnNullNotifier() { | ||
Route route = new Route.Builder(new Bundle()).routingNotifier(null).build(); | ||
route.setResult(true, null); | ||
assertThat(route.isCompleted()); | ||
} | ||
|
||
@Test | ||
public void testMultipleSetResult() { | ||
Route route = new Route.Builder(new Bundle()).routingNotifier(null).build(); | ||
route.setResult(true, null); | ||
assertThat(route.isCompleted()); | ||
boolean isCaught = false; | ||
try { | ||
route.setResult(true, null); | ||
} catch (IllegalStateException e) { | ||
isCaught = true; | ||
} | ||
assertThat(isCaught).isTrue(); | ||
} | ||
} |