I have a PoC Unit test. It needs refactoring.

Desperately.
ugly-unit-test
Bradlee Speice 2014-06-24 00:38:43 -04:00
parent 018fe29a75
commit 969adad9b0
5 changed files with 173 additions and 108 deletions

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

@ -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,42 @@
package org.bspeice.minimalbible.test;
import android.test.InstrumentationTestCase;
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;
/**
* Tests for the Download activity
*/
public class MinimalBibleTest extends InstrumentationTestCase {
@Module(addsTo = MinimalBibleModules.class,
injects = MinimalBibleTest.class)
public static class DownloadActivityTestModule {}
public void setUp() {
MinimalBible application = MinimalBible.getApplication();
ObjectGraph graph = application.getObjGraph();
ObjectGraph plusGraph = graph.plus(DownloadActivityTestModule.class);
plusGraph.inject(this);
}
/**
* If we've made it to the actual test, injection seems to be working correctly.
*/
public void testBasicInjection() {
assertEquals(true, true);
}
}

View File

@ -0,0 +1,123 @@
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.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<DownloadActivity> {
@Module(injects = TestDialogDisplayedIfFirstTimeFragment.class,
addsTo = ActivityDownloaderModule.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())));
}
}