From 71fb362ffea4d7c7abfc8a7615e7db216be50e4e Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 17 Feb 2015 22:44:04 -0500 Subject: [PATCH] Get Robolectric test support working! --- app-test/build.gradle | 7 ++++- .../activity/search/BasicSearchTest.java | 31 +++++++++++++++++++ .../activity/search/SearchModules.java | 2 +- .../activity/search/SearchProvider.kt | 12 ++++--- app/src/main/project.properties | 3 ++ 5 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 app-test/src/test/java/org/bspeice/minimalbible/activity/search/BasicSearchTest.java create mode 100644 app/src/main/project.properties diff --git a/app-test/build.gradle b/app-test/build.gradle index a92bcc0..d918a66 100644 --- a/app-test/build.gradle +++ b/app-test/build.gradle @@ -6,6 +6,7 @@ apply plugin: 'com.github.kt3k.coveralls' evaluationDependsOn(":app") buildscript { + ext.kotlin_version = '0.10.770' repositories { mavenCentral() } @@ -13,6 +14,7 @@ buildscript { dependencies { // Version lock for https://github.com/kt3k/coveralls-gradle-plugin/issues/27 classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1x' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } @@ -20,6 +22,7 @@ repositories { maven { url 'http://repository.jetbrains.com/repo' } + mavenCentral() } // TODO: Make sure we test against the Debug variant @@ -51,9 +54,11 @@ dependencies { testCompile firstVariant.javaCompile.classpath testCompile firstVariant.javaCompile.outputs.files testCompile files(androidModule.plugins.findPlugin("com.android.application").getBootClasspath()) + compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" } def buildExcludeTree(path, excludes) { + //noinspection GroovyAssignabilityCheck def tree = fileTree(path).exclude(excludes) tree } @@ -87,6 +92,6 @@ coveralls { } tasks.withType(Test) { - scanForTestClasses = false + scanForTestClasses = true includes = testIncludes } diff --git a/app-test/src/test/java/org/bspeice/minimalbible/activity/search/BasicSearchTest.java b/app-test/src/test/java/org/bspeice/minimalbible/activity/search/BasicSearchTest.java new file mode 100644 index 0000000..ad9a866 --- /dev/null +++ b/app-test/src/test/java/org/bspeice/minimalbible/activity/search/BasicSearchTest.java @@ -0,0 +1,31 @@ +package org.bspeice.minimalbible.activity.search; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.Robolectric; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.annotation.Config; + +import static org.junit.Assert.assertNotNull; + +/** + * Right now this is the only Robolectric test. Some clarifications on this should be made: + * There's an ongoing issue with the ActionBarDrawerToggle, meaning that the BasicSearch + * activity is the only one eligible for testing + * (https://github.com/robolectric/robolectric/issues/1424) + *

+ * Additionally, Robolectric only supports up to Jellybean, which is why the emulateSdk. + * Finally, in Gradle, tests run relative to app-test, whereas the IDE may try and run + * things in the project root. Be careful when changing the manifest location. + */ +@RunWith(RobolectricTestRunner.class) +@Config(emulateSdk = 18, manifest = "../app/src/main/AndroidManifest.xml") +public class BasicSearchTest { + + @Test + public void testBuildActivity() { + BasicSearch activity = Robolectric.buildActivity(BasicSearch.class) + .create().get(); + assertNotNull(activity); + } +} diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/search/SearchModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/search/SearchModules.java index 7b6b55b..e2ed856 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/search/SearchModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/search/SearchModules.java @@ -18,7 +18,7 @@ public class SearchModules { @Provides SearchProvider searchProvider(@Named("MainBook") Book book, IndexManager indexManager) { - return new SearchProvider(book, indexManager); + return new SearchProvider(indexManager, book); } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchProvider.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchProvider.kt index aba70da..8e7df9e 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchProvider.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchProvider.kt @@ -10,27 +10,31 @@ import org.crosswire.jsword.index.IndexManager * This is the entry point for handling the actual bible search. Likely will support * an "advanced" search in the future, but for now, basicTextSearch is what you get. */ -class SearchProvider(val b: Book, val indexManager: IndexManager) { +class SearchProvider(val indexManager: IndexManager, val b: Book?) { val defaultSearchType = SearchType.ANY_WORDS [suppress("UNUSED_PARAMETER")] public fun basicTextSearch(text: String): List { if (!isSearchAvailable()) { - Log.w("SearchProvider", "Search unavailable, index status of ${b.getInitials()}: ${b.getIndexStatus()}") + Log.w("SearchProvider", "Search unavailable, index status of ${b?.getInitials()}: ${b?.getIndexStatus()}") return listOf() } val searchText = defaultSearchType decorate text - val results = b find searchText + // We already checked for null in isSearchAvailable(), but Kotlin + // doesn't keep track of that (yet) + val results = b!!.find(searchText) return results map { it as Verse } } /** * Handler to check if the index is available - because it doesn't - * seem to register itself properly in the book metadata + * seem to register itself properly in the book metadata. + * This check MUST guarantee that the book is not null. */ public fun isSearchAvailable(): Boolean = + b != null && indexManager isIndexed b } diff --git a/app/src/main/project.properties b/app/src/main/project.properties new file mode 100644 index 0000000..b33be29 --- /dev/null +++ b/app/src/main/project.properties @@ -0,0 +1,3 @@ +# suppress inspection "UnusedProperty" for whole file +android.library.reference.1=../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.3 +android.library.reference.2=../../build/intermediates/exploded-aar/com.android.support/support-v4/21.0.3 \ No newline at end of file