From 1f1a2822e8387821042b2ae0da34c0579e08e91a Mon Sep 17 00:00:00 2001 From: Paul Hawke Date: Sat, 26 Aug 2017 21:05:41 -0500 Subject: [PATCH] Moved 2 other tests over to use robolectric. --- .../java/fr/free/nrw/commons/MediaTest.java | 23 -------- .../nrw/commons/NearbyControllerTest.java | 57 ------------------- .../java/fr/free/nrw/commons/MediaTest.java | 25 ++++++++ .../nrw/commons/NearbyControllerTest.java | 44 ++++++++++++++ .../fr/free/nrw/commons/PageTitleTest.java | 20 +++---- 5 files changed, 79 insertions(+), 90 deletions(-) delete mode 100644 app/src/androidTest/java/fr/free/nrw/commons/MediaTest.java delete mode 100644 app/src/androidTest/java/fr/free/nrw/commons/NearbyControllerTest.java create mode 100644 app/src/test/java/fr/free/nrw/commons/MediaTest.java create mode 100644 app/src/test/java/fr/free/nrw/commons/NearbyControllerTest.java diff --git a/app/src/androidTest/java/fr/free/nrw/commons/MediaTest.java b/app/src/androidTest/java/fr/free/nrw/commons/MediaTest.java deleted file mode 100644 index ecb465293..000000000 --- a/app/src/androidTest/java/fr/free/nrw/commons/MediaTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package fr.free.nrw.commons; - -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.hamcrest.CoreMatchers.is; - -// TODO: use Robolectric and make it runnable without a connected device -@RunWith(AndroidJUnit4.class) -public class MediaTest { - @Test public void displayTitleShouldStripExtension() { - Media m = new Media("File:Example.jpg"); - Assert.assertThat(m.getDisplayTitle(), is("Example")); - } - - @Test public void displayTitleShouldUseSpaceForUnderscore() { - Media m = new Media("File:Example 1_2.jpg"); - Assert.assertThat(m.getDisplayTitle(), is("Example 1 2")); - } -} diff --git a/app/src/androidTest/java/fr/free/nrw/commons/NearbyControllerTest.java b/app/src/androidTest/java/fr/free/nrw/commons/NearbyControllerTest.java deleted file mode 100644 index aa32d2637..000000000 --- a/app/src/androidTest/java/fr/free/nrw/commons/NearbyControllerTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package fr.free.nrw.commons; - -import android.content.Context; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; - -import java.util.ArrayList; -import java.util.List; - -import fr.free.nrw.commons.location.LatLng; -import fr.free.nrw.commons.nearby.NearbyBaseMarker; -import fr.free.nrw.commons.nearby.NearbyController; -import fr.free.nrw.commons.nearby.Place; - -import static org.hamcrest.CoreMatchers.is; - -@RunWith(AndroidJUnit4.class) -public class NearbyControllerTest { - private Context instrumentationContext; - - @Before - public void setup() { - instrumentationContext = InstrumentationRegistry.getTargetContext(); - } - - @Test public void testNullAttractions() { - LatLng location = new LatLng(0, 0, 0); - - List options = - NearbyController.loadAttractionsFromLocationToBaseMarkerOptions( - location, - null, - instrumentationContext - ); - - Assert.assertThat(options.size(), is(0)); - } - - @Test public void testEmptyList() { - LatLng location = new LatLng(0, 0, 0); - List emptyList = new ArrayList<>(); - - List options = - NearbyController.loadAttractionsFromLocationToBaseMarkerOptions( - location, - emptyList, - instrumentationContext - ); - - Assert.assertThat(options.size(), is(0)); - } -} diff --git a/app/src/test/java/fr/free/nrw/commons/MediaTest.java b/app/src/test/java/fr/free/nrw/commons/MediaTest.java new file mode 100644 index 000000000..21cfc2c34 --- /dev/null +++ b/app/src/test/java/fr/free/nrw/commons/MediaTest.java @@ -0,0 +1,25 @@ +package fr.free.nrw.commons; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(RobolectricTestRunner.class) +@Config(constants = BuildConfig.class, sdk = 21) +public class MediaTest { + @Test + public void displayTitleShouldStripExtension() { + Media m = new Media("File:Example.jpg"); + assertThat(m.getDisplayTitle(), is("Example")); + } + + @Test + public void displayTitleShouldUseSpaceForUnderscore() { + Media m = new Media("File:Example 1_2.jpg"); + assertThat(m.getDisplayTitle(), is("Example 1 2")); + } +} diff --git a/app/src/test/java/fr/free/nrw/commons/NearbyControllerTest.java b/app/src/test/java/fr/free/nrw/commons/NearbyControllerTest.java new file mode 100644 index 000000000..6ae2063da --- /dev/null +++ b/app/src/test/java/fr/free/nrw/commons/NearbyControllerTest.java @@ -0,0 +1,44 @@ +package fr.free.nrw.commons; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + +import java.util.ArrayList; +import java.util.List; + +import fr.free.nrw.commons.location.LatLng; +import fr.free.nrw.commons.nearby.NearbyBaseMarker; +import fr.free.nrw.commons.nearby.Place; + +import static fr.free.nrw.commons.nearby.NearbyController.loadAttractionsFromLocationToBaseMarkerOptions; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(RobolectricTestRunner.class) +@Config(constants = BuildConfig.class, sdk = 21) +public class NearbyControllerTest { + + @Test + public void testNullAttractions() { + LatLng location = new LatLng(0, 0, 0); + + List options = loadAttractionsFromLocationToBaseMarkerOptions( + location, null, RuntimeEnvironment.application); + + assertThat(options.size(), is(0)); + } + + @Test + public void testEmptyList() { + LatLng location = new LatLng(0, 0, 0); + List emptyList = new ArrayList<>(); + + List options = loadAttractionsFromLocationToBaseMarkerOptions( + location, emptyList, RuntimeEnvironment.application); + + assertThat(options.size(), is(0)); + } +} diff --git a/app/src/test/java/fr/free/nrw/commons/PageTitleTest.java b/app/src/test/java/fr/free/nrw/commons/PageTitleTest.java index cd10c2828..455b3fbd0 100644 --- a/app/src/test/java/fr/free/nrw/commons/PageTitleTest.java +++ b/app/src/test/java/fr/free/nrw/commons/PageTitleTest.java @@ -1,6 +1,5 @@ package fr.free.nrw.commons; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; @@ -9,62 +8,63 @@ import org.robolectric.annotation.Config; import java.net.URLEncoder; import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; @RunWith(RobolectricTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) public class PageTitleTest { @Test public void displayTextShouldNotBeUnderscored() { - Assert.assertThat(new PageTitle("Ex_1 ").getDisplayText(), + assertThat(new PageTitle("Ex_1 ").getDisplayText(), is("Ex 1")); } @Test public void moreThanTwoColons() { - Assert.assertThat(new PageTitle("File:sample:a.jpg").getPrefixedText(), + assertThat(new PageTitle("File:sample:a.jpg").getPrefixedText(), is("File:Sample:a.jpg")); } @Test public void getTextShouldReturnWithoutNamespace() { - Assert.assertThat(new PageTitle("File:sample.jpg").getText(), + assertThat(new PageTitle("File:sample.jpg").getText(), is("Sample.jpg")); } @Test public void capitalizeNameAfterNamespace() { - Assert.assertThat(new PageTitle("File:sample.jpg").getPrefixedText(), + assertThat(new PageTitle("File:sample.jpg").getPrefixedText(), is("File:Sample.jpg")); } @Test public void prefixedTextShouldBeUnderscored() { - Assert.assertThat(new PageTitle("Ex 1 ").getPrefixedText(), + assertThat(new PageTitle("Ex 1 ").getPrefixedText(), is("Ex_1")); } @Test public void getMobileUriForTest() { - Assert.assertThat(new PageTitle("Test").getMobileUri().toString(), + assertThat(new PageTitle("Test").getMobileUri().toString(), is(BuildConfig.MOBILE_HOME_URL + "Test")); } @Test public void spaceBecomesUnderscoreInUri() { - Assert.assertThat(new PageTitle("File:Ex 1.jpg").getCanonicalUri().toString(), + assertThat(new PageTitle("File:Ex 1.jpg").getCanonicalUri().toString(), is(BuildConfig.HOME_URL + "File:Ex_1.jpg")); } @Test public void leaveSubpageNamesUncapitalizedInUri() { - Assert.assertThat(new PageTitle("User:Ex/subpage").getCanonicalUri().toString(), + assertThat(new PageTitle("User:Ex/subpage").getCanonicalUri().toString(), is(BuildConfig.HOME_URL + "User:Ex/subpage")); } @Test public void unicodeUri() throws Throwable { - Assert.assertThat(new PageTitle("User:例").getCanonicalUri().toString(), + assertThat(new PageTitle("User:例").getCanonicalUri().toString(), is(BuildConfig.HOME_URL + "User:" + URLEncoder.encode("例", "utf-8"))); } }