mirror of
https://github.com/MinimalBible/MinimalBible-Legacy
synced 2024-12-04 13:59:16 -05:00
More ugly unit testing...
This commit is contained in:
parent
1c15767d10
commit
df3cf9652e
@ -52,12 +52,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Move the tests to tests/java, tests/res, etc...
|
// Move the tests to tests/java, tests/res, etc...
|
||||||
androidTest {
|
androidTest.setRoot('src/test')
|
||||||
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, ...
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package org.bspeice.minimalbible;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Massive shout-out to <a href="https://github.com/vovkab">vovkab</a> for this idea.
|
||||||
|
*/
|
||||||
|
public interface Injectable {
|
||||||
|
|
||||||
|
public Object[] getModules();
|
||||||
|
|
||||||
|
public void inject(Object o);
|
||||||
|
|
||||||
|
}
|
@ -10,7 +10,7 @@ import java.io.File;
|
|||||||
|
|
||||||
import dagger.ObjectGraph;
|
import dagger.ObjectGraph;
|
||||||
|
|
||||||
public class MinimalBible extends Application {
|
public class MinimalBible extends Application implements Injectable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The graph used by Dagger to track dependencies
|
* The graph used by Dagger to track dependencies
|
||||||
@ -71,13 +71,14 @@ public class MinimalBible extends Application {
|
|||||||
|
|
||||||
public ObjectGraph getObjGraph() {
|
public ObjectGraph getObjGraph() {
|
||||||
if (graph == null) {
|
if (graph == null) {
|
||||||
graph = ObjectGraph.create(MinimalBibleModules.class);
|
graph = ObjectGraph.create(getModules());
|
||||||
}
|
}
|
||||||
return graph;
|
return graph;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void plusObjGraph(Object... modules) {
|
@Override
|
||||||
graph = graph.plus(modules);
|
public Object[] getModules() {
|
||||||
|
return MinimalBibleModules.list();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,4 +16,12 @@ import dagger.Module;
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
public class MinimalBibleModules {
|
public class MinimalBibleModules {
|
||||||
|
|
||||||
|
private MinimalBibleModules() {}
|
||||||
|
|
||||||
|
public static Object[] list() {
|
||||||
|
return new Object[] {
|
||||||
|
MinimalBibleModules.class
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
<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>
|
|
@ -1,75 +1,38 @@
|
|||||||
package org.bspeice.minimalbible.test;
|
package org.bspeice.minimalbible.test;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
import org.bspeice.minimalbible.MinimalBible;
|
import org.bspeice.minimalbible.MinimalBible;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
import dagger.ObjectGraph;
|
import dagger.ObjectGraph;
|
||||||
|
|
||||||
public class MinimalBibleTest extends MinimalBible {
|
public class MinimalBibleTest extends Application {
|
||||||
|
public MinimalBibleTest(Context ctx) {
|
||||||
/**
|
attachBaseContext(ctx);
|
||||||
* The graph used by Dagger to track dependencies
|
|
||||||
*/
|
|
||||||
private ObjectGraph graph;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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 static MinimalBibleTest getApplication() {
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the {@link android.app.Application}. Responsible for building and
|
|
||||||
* holding on to the master ObjectGraph.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void attachBaseContext(Context base) {
|
||||||
super.onCreate();
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
|
||||||
//TODO: Is this necessary?
|
super.attachBaseContext(base);
|
||||||
inject(this);
|
} else {
|
||||||
}
|
try {
|
||||||
|
Class<Application> applicationClass = Application.class;
|
||||||
/**
|
Method attach = applicationClass.getDeclaredMethod("attach", Context.class);
|
||||||
* Inject a Dagger object
|
attach.setAccessible(true);
|
||||||
* @param o The object to be injected
|
attach.invoke(this, base);
|
||||||
*/
|
} catch (NoSuchMethodException e) {
|
||||||
@Override
|
e.printStackTrace();
|
||||||
public void inject(Object o) {
|
} catch (IllegalAccessException e) {
|
||||||
getObjGraph().inject(o);
|
e.printStackTrace();
|
||||||
}
|
} catch (InvocationTargetException e) {
|
||||||
|
e.printStackTrace();
|
||||||
public ObjectGraph getObjGraph() {
|
}
|
||||||
if (graph == null) {
|
|
||||||
graph = ObjectGraph.create(MinimalBibleModulesTest.class);
|
|
||||||
}
|
}
|
||||||
return graph;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,28 @@
|
|||||||
|
package org.bspeice.minimalbible.test;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import org.bspeice.minimalbible.Injectable;
|
||||||
|
|
||||||
|
import dagger.ObjectGraph;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by bspeice on 6/27/14.
|
||||||
|
*/
|
||||||
|
public abstract class MinimalBibleTestMockable extends MinimalBibleTest implements Injectable {
|
||||||
|
|
||||||
|
private ObjectGraph mObjectGraph;
|
||||||
|
|
||||||
|
public MinimalBibleTestMockable(Context context) {
|
||||||
|
super(context);
|
||||||
|
mObjectGraph = ObjectGraph.create(getModules());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract Object[] getModules();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void inject(Object o) {
|
||||||
|
mObjectGraph.inject(o);
|
||||||
|
}
|
||||||
|
}
|
@ -1,47 +1,88 @@
|
|||||||
package org.bspeice.minimalbible.test.activities.downloader;
|
package org.bspeice.minimalbible.test.activities.downloader;
|
||||||
|
|
||||||
|
import android.app.Application;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.ContextWrapper;
|
||||||
|
import android.content.Intent;
|
||||||
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.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.test.ActivityInstrumentationTestCase2;
|
import android.test.ActivityUnitTestCase;
|
||||||
|
|
||||||
import org.bspeice.minimalbible.MinimalBible;
|
import org.bspeice.minimalbible.MinimalBible;
|
||||||
import org.bspeice.minimalbible.test.MinimalBibleModulesTest;
|
|
||||||
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.bspeice.minimalbible.test.MinimalBibleModulesTest;
|
||||||
|
import org.bspeice.minimalbible.test.MinimalBibleTest;
|
||||||
|
import org.bspeice.minimalbible.test.MinimalBibleTestMockable;
|
||||||
import org.crosswire.jsword.book.BookCategory;
|
import org.crosswire.jsword.book.BookCategory;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
|
import dagger.Provides;
|
||||||
import de.devland.esperandro.Esperandro;
|
import de.devland.esperandro.Esperandro;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by bspeice on 6/23/14.
|
* Created by bspeice on 6/23/14.
|
||||||
*/
|
*/
|
||||||
public class BookListFragmentTest extends ActivityInstrumentationTestCase2<DownloadActivity> {
|
public class BookListFragmentTest extends ActivityUnitTestCase<DownloadActivity> {
|
||||||
|
private static Class activityUnderTest = DownloadActivity.class;
|
||||||
|
|
||||||
@Module(injects = TestDialogDisplayedIfFirstTimeFragment.class,
|
@Module(injects = TestDialogDisplayedIfFirstTimeFragment.class,
|
||||||
addsTo = MinimalBibleModulesTest.class
|
addsTo = MinimalBibleModulesTest.class,
|
||||||
|
overrides = true
|
||||||
)
|
)
|
||||||
protected static class BookListFragmentTestModule{}
|
protected static class BookListFragmentTestModule{
|
||||||
|
@Provides
|
||||||
public BookListFragmentTest() {
|
DownloadPrefs providePrefs() {
|
||||||
super(DownloadActivity.class);
|
return Esperandro.getPreferences(DownloadPrefs.class,
|
||||||
|
MinimalBible.getApplication());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BookListFragmentTest() {
|
||||||
|
super(activityUnderTest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject DownloadPrefs downloadPrefs;
|
||||||
FragmentManager mFragmentManager;
|
FragmentManager mFragmentManager;
|
||||||
|
|
||||||
|
private Application mApplication;
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
|
||||||
mFragmentManager = getActivity().getSupportFragmentManager();
|
// Set 'dexmaker.dexcache' system property, otherwise sometimes it is null and test will crash
|
||||||
assertNotNull(mFragmentManager);
|
// System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
|
||||||
|
|
||||||
|
mContext = new ContextWrapper(getInstrumentation().getTargetContext()) {
|
||||||
|
@Override
|
||||||
|
public Context getApplicationContext() {
|
||||||
|
return mApplication;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
mApplication = new MinimalBibleTestMockable(mContext) {
|
||||||
|
@Override public Object[] getModules() {
|
||||||
|
return new Object[]{new BookListFragmentTestModule()};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
setApplication(mApplication);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void tearDown() throws Exception {
|
||||||
|
super.tearDown();
|
||||||
|
}
|
||||||
|
|
||||||
public <F extends Fragment> F startFragment(F fragment) {
|
public <F extends Fragment> F startFragment(F fragment) {
|
||||||
try {
|
try {
|
||||||
@ -68,15 +109,19 @@ public class BookListFragmentTest extends ActivityInstrumentationTestCase2<Downl
|
|||||||
return (F)(mFragmentManager.findFragmentById(android.R.id.content));
|
return (F)(mFragmentManager.findFragmentById(android.R.id.content));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testApplicationReplacementWorks() {
|
||||||
|
setActivityContext(mContext);
|
||||||
|
startActivity(new Intent(mContext, activityUnderTest), null, null);
|
||||||
|
|
||||||
|
assertTrue(getActivity().getApplicationContext() instanceof MinimalBibleTestMockable);
|
||||||
|
}
|
||||||
|
|
||||||
protected class TestDialogDisplayedIfFirstTimeFragment extends BookListFragment {
|
protected class TestDialogDisplayedIfFirstTimeFragment extends BookListFragment {
|
||||||
/**
|
/**
|
||||||
* If the refresh dialog is blank after calling display, it must be showing the warning
|
* If the refresh dialog is blank after calling display, it must be showing the warning
|
||||||
* @return Whether the warning dialog is showing
|
* @return Whether the warning dialog is showing
|
||||||
*/
|
*/
|
||||||
public boolean callDisplayModules(DownloadPrefs prefs) {
|
public boolean callDisplayModules() {
|
||||||
// Inject the new preferences...
|
|
||||||
this.downloadPrefs = prefs;
|
|
||||||
displayModules();
|
displayModules();
|
||||||
return (refreshDialog == null);
|
return (refreshDialog == null);
|
||||||
}
|
}
|
||||||
@ -89,17 +134,30 @@ public class BookListFragmentTest extends ActivityInstrumentationTestCase2<Downl
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testDialogDisplayedIfFirstTime() {
|
public void testDialogDisplayedIfFirstTime() {
|
||||||
/*
|
setActivityContext(mContext);
|
||||||
SharedPreferences prefs = getActivity()
|
startActivity(new Intent(mContext, activityUnderTest), null, null);
|
||||||
.getSharedPreferences("DownloadPrefs", Context.MODE_PRIVATE);
|
|
||||||
prefs.edit().putBoolean("hasShownDownloadDialog", false);
|
|
||||||
*/
|
|
||||||
((MinimalBible)getActivity().getApplication()).plusObjGraph(BookListFragmentTestModule.class);
|
|
||||||
TestDialogDisplayedIfFirstTimeFragment f = new TestDialogDisplayedIfFirstTimeFragment();
|
TestDialogDisplayedIfFirstTimeFragment f = new TestDialogDisplayedIfFirstTimeFragment();
|
||||||
f.setArgs(BookCategory.BIBLE);
|
f.setArgs(BookCategory.BIBLE);
|
||||||
startFragment(f);
|
startFragment(f);
|
||||||
|
|
||||||
assertNotNull(f);
|
assertNotNull(f);
|
||||||
assertTrue(f.callDisplayModules(Esperandro.getPreferences(DownloadPrefs.class, getActivity())));
|
|
||||||
|
downloadPrefs.hasShownDownloadDialog(false);
|
||||||
|
assertTrue(f.callDisplayModules());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRefreshDisplayedAfterFirstTime() {
|
||||||
|
setActivityContext(mContext);
|
||||||
|
startActivity(new Intent(mContext, activityUnderTest), null, null);
|
||||||
|
|
||||||
|
TestDialogDisplayedIfFirstTimeFragment f = new TestDialogDisplayedIfFirstTimeFragment();
|
||||||
|
f.setArgs(BookCategory.BIBLE);
|
||||||
|
startFragment(f);
|
||||||
|
|
||||||
|
assertNotNull(f);
|
||||||
|
|
||||||
|
downloadPrefs.hasShownDownloadDialog(true);
|
||||||
|
assertFalse(f.callDisplayModules());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user