From 1c15767d104bb24cdb99c14fccc599ecf5da59f7 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 27 Jun 2014 21:35:49 -0400 Subject: [PATCH] Prettify the unit testing --- MinimalBible/build.gradle | 9 +- .../{ => src/main}/AndroidManifest.xml | 0 MinimalBible/src/test/AndroidManifest.xml | 19 +++++ .../test/MinimalBibleModulesTest.java | 20 +++++ .../minimalbible/test/MinimalBibleTest.java | 83 +++++++++++++------ .../downloader/BookListFragmentTest.java | 22 +---- 6 files changed, 106 insertions(+), 47 deletions(-) rename MinimalBible/{ => src/main}/AndroidManifest.xml (100%) create mode 100644 MinimalBible/src/test/AndroidManifest.xml create mode 100644 MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleModulesTest.java diff --git a/MinimalBible/build.gradle b/MinimalBible/build.gradle index 9d1429a..04250e6 100644 --- a/MinimalBible/build.gradle +++ b/MinimalBible/build.gradle @@ -42,7 +42,7 @@ android { buildToolsVersion '20' sourceSets { main { - manifest.srcFile 'AndroidManifest.xml' + manifest.srcFile 'src/main/AndroidManifest.xml' java.srcDirs = ['src/main/java'] resources.srcDirs = ['src/main/res'] aidl.srcDirs = ['src'] @@ -52,7 +52,12 @@ android { } // Move the tests to tests/java, tests/res, etc... - androidTest.setRoot('src/test') + androidTest { + manifest.srcFile 'src/test/AndroidManifest.xml' + java.srcDirs = ['src/test/java'] + resources.srcDirs = ['src/test/res'] + assets.srcDirs = ['src/test/assets'] + } // Move the build types to build-types/ // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... diff --git a/MinimalBible/AndroidManifest.xml b/MinimalBible/src/main/AndroidManifest.xml similarity index 100% rename from MinimalBible/AndroidManifest.xml rename to MinimalBible/src/main/AndroidManifest.xml diff --git a/MinimalBible/src/test/AndroidManifest.xml b/MinimalBible/src/test/AndroidManifest.xml new file mode 100644 index 0000000..cce9973 --- /dev/null +++ b/MinimalBible/src/test/AndroidManifest.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + diff --git a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleModulesTest.java b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleModulesTest.java new file mode 100644 index 0000000..c356ded --- /dev/null +++ b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleModulesTest.java @@ -0,0 +1,20 @@ +package org.bspeice.minimalbible.test; + +import org.bspeice.minimalbible.MinimalBible; +import org.bspeice.minimalbible.MinimalBibleModules; + +import dagger.Module; + +/** + * Master module for MinimalBible + */ +@Module( + injects = { + MinimalBible.class + }, + includes = { + MinimalBibleModules.class + } +) +public class MinimalBibleModulesTest { +} diff --git a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleTest.java b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleTest.java index d99c253..0f54763 100644 --- a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleTest.java +++ b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/MinimalBibleTest.java @@ -1,42 +1,75 @@ package org.bspeice.minimalbible.test; -import android.test.InstrumentationTestCase; +import android.content.Context; import org.bspeice.minimalbible.MinimalBible; -import org.bspeice.minimalbible.MinimalBibleModules; -import org.bspeice.minimalbible.activities.downloader.manager.BookDownloadManager; -import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager; -import org.bspeice.minimalbible.activities.downloader.manager.InstalledManager; -import org.bspeice.minimalbible.activities.downloader.manager.RefreshManager; -import javax.inject.Inject; - -import dagger.Module; import dagger.ObjectGraph; -import static com.jayway.awaitility.Awaitility.await; +public class MinimalBibleTest extends MinimalBible { -/** - * Tests for the Download activity - */ -public class MinimalBibleTest extends InstrumentationTestCase { + /** + * The graph used by Dagger to track dependencies + */ + private ObjectGraph graph; - @Module(addsTo = MinimalBibleModules.class, - injects = MinimalBibleTest.class) - public static class DownloadActivityTestModule {} + /** + * A singleton reference to the Application currently being run. + * Used mostly so we have a fixed point to get the App Context from + */ + private static MinimalBibleTest instance; - public void setUp() { - MinimalBible application = MinimalBible.getApplication(); - ObjectGraph graph = application.getObjGraph(); - ObjectGraph plusGraph = graph.plus(DownloadActivityTestModule.class); - plusGraph.inject(this); + private String TAG = "MinimalBibleTest"; + + /** + * Create the application, and persist the application Context + */ + public MinimalBibleTest() { + instance = this; + } + + /** + * Get the Application Context. Please note, all attempts to get the App Context should come + * through here, and please be sure that the Application won't satisfy what you need. + * @return The Application Context + */ + public static Context getAppContext() { + return instance; } /** - * If we've made it to the actual test, injection seems to be working correctly. + * Get the Application, rather than just the Application Context. You likely should be using + * this, rather than {@link #getAppContext()} + * @return The MinimalBible {@link android.app.Application} object */ - public void testBasicInjection() { - assertEquals(true, true); + public static MinimalBibleTest getApplication() { + return instance; } + /** + * Create the {@link android.app.Application}. Responsible for building and + * holding on to the master ObjectGraph. + */ + @Override + public void onCreate() { + super.onCreate(); + //TODO: Is this necessary? + inject(this); + } + + /** + * Inject a Dagger object + * @param o The object to be injected + */ + @Override + public void inject(Object o) { + getObjGraph().inject(o); + } + + public ObjectGraph getObjGraph() { + if (graph == null) { + graph = ObjectGraph.create(MinimalBibleModulesTest.class); + } + return graph; + } } diff --git a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/activities/downloader/BookListFragmentTest.java b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/activities/downloader/BookListFragmentTest.java index d9e7813..5a69512 100644 --- a/MinimalBible/src/test/java/org/bspeice/minimalbible/test/activities/downloader/BookListFragmentTest.java +++ b/MinimalBible/src/test/java/org/bspeice/minimalbible/test/activities/downloader/BookListFragmentTest.java @@ -1,49 +1,31 @@ package org.bspeice.minimalbible.test.activities.downloader; -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.test.ActivityInstrumentationTestCase2; -import android.test.ActivityUnitTestCase; -import android.test.InstrumentationTestCase; -import android.view.ContextThemeWrapper; import org.bspeice.minimalbible.MinimalBible; -import org.bspeice.minimalbible.MinimalBibleModules; -import org.bspeice.minimalbible.R; -import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule; +import org.bspeice.minimalbible.test.MinimalBibleModulesTest; import org.bspeice.minimalbible.activities.downloader.BookListFragment; import org.bspeice.minimalbible.activities.downloader.DownloadActivity; import org.bspeice.minimalbible.activities.downloader.DownloadPrefs; import org.crosswire.jsword.book.BookCategory; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; - -import javax.inject.Inject; import dagger.Module; -import dagger.ObjectGraph; -import dagger.Provides; import de.devland.esperandro.Esperandro; -import static com.jayway.awaitility.Awaitility.await; - /** * Created by bspeice on 6/23/14. */ public class BookListFragmentTest extends ActivityInstrumentationTestCase2 { @Module(injects = TestDialogDisplayedIfFirstTimeFragment.class, - addsTo = ActivityDownloaderModule.class + addsTo = MinimalBibleModulesTest.class ) protected static class BookListFragmentTestModule{}