Merge branch 'ugly-unit-test'

Bradlee Speice 2014-06-27 21:36:25 -04:00
commit 26e754a6d8
9 changed files with 234 additions and 110 deletions

View File

@ -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/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...

View File

@ -76,6 +76,10 @@ public class MinimalBible extends Application {
return graph;
}
public void plusObjGraph(Object... modules) {
graph = graph.plus(modules);
}
/**
* Notify jSword that it needs to store files in the Android internal directory
* NOTE: Android will uninstall these files if you uninstall MinimalBible.

View File

@ -40,7 +40,7 @@ public class BookListFragment extends BaseFragment {
* The fragment argument representing the section number for this fragment.
* Not a candidate for Dart (yet) because I would have to write a Parcelable around it.
*/
private static final String ARG_BOOK_CATEGORY = "book_category";
protected static final String ARG_BOOK_CATEGORY = "book_category";
private final String TAG = "BookListFragment";
@ -48,9 +48,9 @@ public class BookListFragment extends BaseFragment {
ListView downloadsAvailable;
@Inject RefreshManager refreshManager;
@Inject DownloadPrefs downloadPrefs;
@Inject protected DownloadPrefs downloadPrefs;
private ProgressDialog refreshDialog;
protected ProgressDialog refreshDialog;
private LayoutInflater inflater;
/**
@ -93,7 +93,7 @@ public class BookListFragment extends BaseFragment {
* Trigger the functionality to display a list of modules. Prompts user if downloading
* from the internet is allowable.
*/
private void displayModules() {
protected void displayModules() {
boolean dialogDisplayed = downloadPrefs.hasShownDownloadDialog();
if (!dialogDisplayed) {

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

@ -1,104 +0,0 @@
package org.bspeice.minimalbible.test;
import android.test.InstrumentationTestCase;
import android.util.Log;
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.DLProgressEvent;
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 org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookException;
import org.crosswire.jsword.book.Books;
import org.crosswire.jsword.book.install.InstallException;
import org.crosswire.jsword.book.install.Installer;
import org.crosswire.jsword.passage.NoSuchKeyException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
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 rx.Observable;
import static com.jayway.awaitility.Awaitility.await;
/**
* Tests for the Download activity
*/
public class DownloadActivityTest extends InstrumentationTestCase {
@Module(addsTo = MinimalBibleModules.class,
injects = DownloadActivityTest.class)
public static class DownloadActivityTestModule {}
@Inject DownloadManager dm;
@Inject InstalledManager im;
@Inject RefreshManager rm;
@Inject BookDownloadManager bdm;
public void setUp() {
MinimalBible application = MinimalBible.getApplication();
ObjectGraph graph = application.getObjGraph();
ObjectGraph plusGraph = graph.plus(DownloadActivityTestModule.class);
plusGraph.inject(this);
}
public void testBasicAssertion() {
assertEquals(true, true);
}
/**
* Test that we can successfully download and remove a book
*/
/* Testing disabled for right now, I'm having lots of issues with the jSword API being
inconsistent on Android. These will be enabled again once I can take some time to figure that
out, or mock out the sections I need. For now in the phase of heavy development, I'll do without
public void testInstallAndRemoveBook() {
// Install a book
Installer i = (Installer) dm.getInstallers().values().toArray()[0];
final Book testBook = i.getBooks().get(0);
bdm.installBook(testBook);
await().atMost(30, TimeUnit.SECONDS)
.until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return Books.installed().getBooks().contains(testBook);
}
});
// Validate that we can actually do something with the book
// TODO: Validate that the book exists on the filesystem too
try {
assertNotNull(testBook.getRawText(testBook.getKey("Gen 1:1")));
} catch (BookException e) {
fail(e.getMessage());
} catch (NoSuchKeyException e) {
fail(e.getMessage());
}
// Remove the book and make sure it's gone
// TODO: Validate that the book is off the filesystem
im.removeBook(testBook);
await().atMost(10, TimeUnit.SECONDS)
.until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return !Books.installed().getBooks().contains(testBook);
}
});
}
*/
}

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

@ -0,0 +1,75 @@
package org.bspeice.minimalbible.test;
import android.content.Context;
import org.bspeice.minimalbible.MinimalBible;
import dagger.ObjectGraph;
public class MinimalBibleTest extends MinimalBible {
/**
* 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
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

@ -0,0 +1,105 @@
package org.bspeice.minimalbible.test.activities.downloader;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.test.ActivityInstrumentationTestCase2;
import org.bspeice.minimalbible.MinimalBible;
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 dagger.Module;
import de.devland.esperandro.Esperandro;
/**
* Created by bspeice on 6/23/14.
*/
public class BookListFragmentTest extends ActivityInstrumentationTestCase2<DownloadActivity> {
@Module(injects = TestDialogDisplayedIfFirstTimeFragment.class,
addsTo = MinimalBibleModulesTest.class
)
protected static class BookListFragmentTestModule{}
public BookListFragmentTest() {
super(DownloadActivity.class);
}
FragmentManager mFragmentManager;
public void setUp() throws Exception {
super.setUp();
mFragmentManager = getActivity().getSupportFragmentManager();
assertNotNull(mFragmentManager);
}
public <F extends Fragment> F startFragment(F fragment) {
try {
mFragmentManager.beginTransaction()
.replace(android.R.id.content, fragment)
.commit();
} catch (Exception e) {
e.printStackTrace();
}
final CountDownLatch signal = new CountDownLatch(1);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mFragmentManager.executePendingTransactions();
signal.countDown();
}
});
try {
signal.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
return (F)(mFragmentManager.findFragmentById(android.R.id.content));
}
protected class TestDialogDisplayedIfFirstTimeFragment extends BookListFragment {
/**
* If the refresh dialog is blank after calling display, it must be showing the warning
* @return Whether the warning dialog is showing
*/
public boolean callDisplayModules(DownloadPrefs prefs) {
// Inject the new preferences...
this.downloadPrefs = prefs;
displayModules();
return (refreshDialog == null);
}
public void setArgs(BookCategory c) {
Bundle args = new Bundle();
args.putString(ARG_BOOK_CATEGORY, c.toString());
this.setArguments(args);
}
}
public void testDialogDisplayedIfFirstTime() {
/*
SharedPreferences prefs = getActivity()
.getSharedPreferences("DownloadPrefs", Context.MODE_PRIVATE);
prefs.edit().putBoolean("hasShownDownloadDialog", false);
*/
((MinimalBible)getActivity().getApplication()).plusObjGraph(BookListFragmentTestModule.class);
TestDialogDisplayedIfFirstTimeFragment f = new TestDialogDisplayedIfFirstTimeFragment();
f.setArgs(BookCategory.BIBLE);
startFragment(f);
assertNotNull(f);
assertTrue(f.callDisplayModules(Esperandro.getPreferences(DownloadPrefs.class, getActivity())));
}
}