Prettify the unit testing

ugly-unit-test
Bradlee Speice 2014-06-27 21:35:49 -04:00
parent b19b740c43
commit 1c15767d10
6 changed files with 106 additions and 47 deletions

View File

@ -42,7 +42,7 @@ android {
buildToolsVersion '20' buildToolsVersion '20'
sourceSets { sourceSets {
main { main {
manifest.srcFile 'AndroidManifest.xml' manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java'] java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/res'] resources.srcDirs = ['src/main/res']
aidl.srcDirs = ['src'] aidl.srcDirs = ['src']
@ -52,7 +52,12 @@ android {
} }
// Move the tests to tests/java, tests/res, etc... // 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/<type> // Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...

View File

@ -0,0 +1,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.bspeice.minimalbible">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="20" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="org.bspeice.minimalbible.test.MinimalBibleTest" >
</application>
</manifest>

View File

@ -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 {
}

View File

@ -1,42 +1,75 @@
package org.bspeice.minimalbible.test; package org.bspeice.minimalbible.test;
import android.test.InstrumentationTestCase; import android.content.Context;
import org.bspeice.minimalbible.MinimalBible; 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 dagger.ObjectGraph;
import static com.jayway.awaitility.Awaitility.await; public class MinimalBibleTest extends MinimalBible {
/** /**
* Tests for the Download activity * The graph used by Dagger to track dependencies
*/ */
public class MinimalBibleTest extends InstrumentationTestCase { private ObjectGraph graph;
@Module(addsTo = MinimalBibleModules.class, /**
injects = MinimalBibleTest.class) * A singleton reference to the Application currently being run.
public static class DownloadActivityTestModule {} * Used mostly so we have a fixed point to get the App Context from
*/
private static MinimalBibleTest instance;
public void setUp() { private String TAG = "MinimalBibleTest";
MinimalBible application = MinimalBible.getApplication();
ObjectGraph graph = application.getObjGraph(); /**
ObjectGraph plusGraph = graph.plus(DownloadActivityTestModule.class); * Create the application, and persist the application Context
plusGraph.inject(this); */
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() { public static MinimalBibleTest getApplication() {
assertEquals(true, true); 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;
}
} }

View File

@ -1,49 +1,31 @@
package org.bspeice.minimalbible.test.activities.downloader; 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.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.test.ActivityInstrumentationTestCase2; 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.MinimalBible;
import org.bspeice.minimalbible.MinimalBibleModules; import org.bspeice.minimalbible.test.MinimalBibleModulesTest;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule;
import org.bspeice.minimalbible.activities.downloader.BookListFragment; import org.bspeice.minimalbible.activities.downloader.BookListFragment;
import org.bspeice.minimalbible.activities.downloader.DownloadActivity; import org.bspeice.minimalbible.activities.downloader.DownloadActivity;
import org.bspeice.minimalbible.activities.downloader.DownloadPrefs; import org.bspeice.minimalbible.activities.downloader.DownloadPrefs;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
import java.util.concurrent.CountDownLatch; 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.Module;
import dagger.ObjectGraph;
import dagger.Provides;
import de.devland.esperandro.Esperandro; import de.devland.esperandro.Esperandro;
import static com.jayway.awaitility.Awaitility.await;
/** /**
* Created by bspeice on 6/23/14. * Created by bspeice on 6/23/14.
*/ */
public class BookListFragmentTest extends ActivityInstrumentationTestCase2<DownloadActivity> { public class BookListFragmentTest extends ActivityInstrumentationTestCase2<DownloadActivity> {
@Module(injects = TestDialogDisplayedIfFirstTimeFragment.class, @Module(injects = TestDialogDisplayedIfFirstTimeFragment.class,
addsTo = ActivityDownloaderModule.class addsTo = MinimalBibleModulesTest.class
) )
protected static class BookListFragmentTestModule{} protected static class BookListFragmentTestModule{}