Unit testing now working

Also demo test case for async testing.
This commit is contained in:
Bradlee Speice 2014-05-24 15:11:26 -04:00
parent bba77bb45a
commit 2c494edadc
5 changed files with 79 additions and 7 deletions

View File

@ -39,6 +39,10 @@ dependencies {
// Handled by appcompat
// compile 'com.google.android:support-v4:r7'
// And our unit testing needs some specific stuff (and specific stuff included again)
androidTestCompile 'junit:junit:4.11+'
androidTestProvided 'com.squareup.dagger:dagger-compiler:1.2.0'
}
android {
@ -71,6 +75,7 @@ android {
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'

View File

@ -60,4 +60,6 @@ public class MinimalBible extends Application {
public void inject(Object o) {
graph.inject(o);
}
public ObjectGraph getObjGraph() { return graph; }
}

View File

@ -1,6 +1,5 @@
package org.bspeice.minimalbible.activities.downloader;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
@ -55,14 +54,20 @@ public class BookItemHolder {
@OnClick(R.id.download_ibtn_download)
public void onDownloadItem(View v) {
Log.d("BookListAdapter", v.toString());
displayProgress(0); // Can assume 0 since the download is now starting
// TODO: Write a unit test to make sure that this is called - displayProgress() assumes it
downloadManager.getDownloadBus().register(this);
downloadManager.getDownloadBus()
.post(new DownloadProgressEvent(DownloadProgressEvent.PROGRESS_BEGINNING, b));
// TODO: Kick off a service to actually do the downloading, rather than simulate
downloadManager.getDownloadBus().post(new DownloadProgressEvent(47, b));
}
public void onEventMainThread(DownloadProgressEvent event) {
if (event.getB().equals(this.b)) {
displayProgress((int) event.toCircular());
}
}
/**
* Display the current progress of this download
* @param progress The progress out of 360 (degrees of a circle)
@ -70,7 +75,7 @@ public class BookItemHolder {
private void displayProgress(int progress) {
if (progress <= 0) {
if (progress == DownloadProgressEvent.PROGRESS_BEGINNING) {
// Download starting
RelativeLayout.LayoutParams acronymParams =
(RelativeLayout.LayoutParams)acronym.getLayoutParams();

View File

@ -3,12 +3,15 @@ package org.bspeice.minimalbible.activities.downloader.manager;
import org.crosswire.jsword.book.Book;
/**
* Created by bspeice on 5/19/14.
* Used for notifying that a book's download progress is ongoing
*/
public class DownloadProgressEvent {
private int progress;
private Book b;
public static final int PROGRESS_COMPLETE = 100;
public static final int PROGRESS_BEGINNING = 0;
public DownloadProgressEvent(int progress, Book b) {
this.progress = progress;
this.b = b;

View File

@ -1,15 +1,72 @@
package org.bspeice.minimalbible.test;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.MinimalBibleModules;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.downloader.BookItemHolder;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadManager;
import org.bspeice.minimalbible.activities.downloader.manager.DownloadProgressEvent;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.install.Installer;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import dagger.Module;
/**
* Tests for the Download activity
*/
public class DownloadActivityTest extends InstrumentationTestCase {
@Module(addsTo = MinimalBibleModules.class,
injects = DownloadActivityTest.class)
public static class DownloadActivityTestModule {}
@Inject
DownloadManager dm;
public void setUp() {
MinimalBible.getApplication().getObjGraph()
.plus(DownloadActivityTestModule.class).inject(this);
}
public void testBasicAssertion() {
assertEquals(true, true);
}
/**
* When we start a download, make sure a progress event of 0 is triggered.
*/
public void testInitialProgressEventOnDownload() throws InterruptedException {
final CountDownLatch signal = new CountDownLatch(1);
Installer i = (Installer) dm.getInstallers().values().toArray()[0];
Book testBook = i.getBooks().get(0);
View dummyView = LayoutInflater.from(MinimalBible.getApplication())
.inflate(R.layout.list_download_items, null);
BookItemHolder holder = new BookItemHolder(dummyView, testBook);
dm.getDownloadBus().register(new Object() {
public void onEvent(DownloadProgressEvent event) {
Log.d("testInitial", Integer.toString(event.getProgress()));
if (event.getProgress() == 0) {
signal.countDown();
}
}
});
holder.onDownloadItem(dummyView);
signal.await(10, TimeUnit.SECONDS);
if (signal.getCount() != 0) {
fail("Event did not trigger!");
}
}
}