From 05d2d006e457718723fbe065a32b4ee443b9cbdf Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 22 Nov 2014 17:07:58 -0500 Subject: [PATCH] Boundary refactor, and add some tests --- .../downloader/BookListFragmentTest.kt | 52 ++++++++++++++ .../downloader/BookListFragmentTest.java | 70 ------------------- .../activity/downloader/BookListFragment.java | 30 ++++---- 3 files changed, 70 insertions(+), 82 deletions(-) create mode 100644 app-test/src/test/kotlin/org/bspeice/minimalbible/activity/downloader/BookListFragmentTest.kt delete mode 100644 app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/BookListFragmentTest.java diff --git a/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/downloader/BookListFragmentTest.kt b/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/downloader/BookListFragmentTest.kt new file mode 100644 index 0000000..c42f3a0 --- /dev/null +++ b/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/downloader/BookListFragmentTest.kt @@ -0,0 +1,52 @@ +package org.bspeice.minimalbible.activity.downloader + +import org.jetbrains.spek.api.Spek +import kotlin.test.assertTrue + +/** + * Created by bspeice on 11/22/14. + */ + +class BookListFragmentSpek : Spek() {{ + + given("A BookListFragment with showDialog() mocked out") { + class TestableFragment : BookListFragment() { + var condition = false + + override fun showDialog() { + condition = true + } + } + + val fragment = TestableFragment() + + on("attempting to display modules with the dialog not shown already") { + fragment.displayModules(false) + + it("should show the download dialog") { + assertTrue(fragment.condition) + } + } + } + + given("a BookListFragment with displayLanguageSpinner() mocked out") { + class TestableFragment : BookListFragment() { + var condition = false + + override fun displayLanguageSpinner() { + condition = true + } + } + + val fragment = TestableFragment() + + on("attempting to display modules with the dialog already shown") { + fragment.displayModules(true) + + it("should show the available languages spinner") { + assertTrue(fragment.condition) + } + } + } +} +} \ No newline at end of file diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/BookListFragmentTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/BookListFragmentTest.java deleted file mode 100644 index 0a74541..0000000 --- a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/BookListFragmentTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.bspeice.minimalbible.test.activity.downloader; - -import org.bspeice.minimalbible.MBTestCase; -import org.bspeice.minimalbible.activity.downloader.BookListFragment; -import org.crosswire.common.util.Language; -import org.crosswire.jsword.book.Book; -import org.crosswire.jsword.book.BookCategory; -import org.mockito.Mockito; - -import rx.Observable; -import rx.functions.Action1; - -import static org.mockito.Mockito.when; - -public class BookListFragmentTest extends MBTestCase { - - public void testBooksByLanguage() throws Exception { - BookCategory bibleCategory = BookCategory.BIBLE; - BookCategory dictionaryCategory = BookCategory.DICTIONARY; - Language russianLanguage = new Language("ru"); - Language englishLanguage = new Language("en"); - - final Book russianBible = Mockito.mock(Book.class); - when(russianBible.getBookCategory()).thenReturn(bibleCategory); - when(russianBible.getLanguage()).thenReturn(russianLanguage); - - final Book englishBible = Mockito.mock(Book.class); - when(englishBible.getBookCategory()).thenReturn(bibleCategory); - when(englishBible.getLanguage()).thenReturn(englishLanguage); - - final Book englishDictionary = Mockito.mock(Book.class); - when(englishDictionary.getBookCategory()).thenReturn(dictionaryCategory); - when(englishDictionary.getLanguage()).thenReturn(englishLanguage); - - Observable mockBooks = Observable.just(russianBible, englishBible, - englishDictionary); - - // Since we're not testing lifecycle here, don't worry about newInstance() - TestableBookListFragment fragment = new TestableBookListFragment(); - - fragment.booksByLanguage(mockBooks, englishLanguage, bibleCategory) - .subscribe(new Action1() { - @Override - public void call(Book book) { - assertEquals(englishBible, book); - } - }); - fragment.booksByLanguage(mockBooks, russianLanguage, bibleCategory) - .subscribe(new Action1() { - @Override - public void call(Book book) { - assertEquals(russianBible, book); - } - }); - fragment.booksByLanguage(mockBooks, englishLanguage, dictionaryCategory) - .subscribe(new Action1() { - @Override - public void call(Book book) { - assertEquals(englishDictionary, book); - } - }); - } - - public static class TestableBookListFragment extends BookListFragment { - @Override - public Observable booksByLanguage(Observable books, Language language, BookCategory category) { - return super.booksByLanguage(books, language, category); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java index 925ae5d..905ba58 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java @@ -93,26 +93,32 @@ public class BookListFragment extends BaseFragment { .getString(ARG_BOOK_CATEGORY)); } + void displayModules() { + displayModules(downloadPrefs.hasShownDownloadDialog()); + } + /** * Trigger the functionality to display a list of modules. Prompts user if downloading * from the internet is allowable. */ - void displayModules() { - boolean dialogDisplayed = downloadPrefs.hasShownDownloadDialog(); - - if (!dialogDisplayed) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - DownloadDialogListener dialogListener = new DownloadDialogListener(); - builder.setMessage( - "About to contact servers to download content. Continue?") - .setPositiveButton("Yes", dialogListener) - .setNegativeButton("No", dialogListener) - .setCancelable(false).show(); - } else { + void displayModules(boolean dialogDisplayed) { + if (!dialogDisplayed) { + showDialog(); + } else { displayLanguageSpinner(); } } + void showDialog() { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + DownloadDialogListener dialogListener = new DownloadDialogListener(); + builder.setMessage( + "About to contact servers to download content. Continue?") + .setPositiveButton("Yes", dialogListener) + .setNegativeButton("No", dialogListener) + .setCancelable(false).show(); + } + void displayLanguageSpinner() { ArrayAdapter adapter = new ArrayAdapter(this.getActivity(), android.R.layout.simple_spinner_item,