From 8d65327853ee5ee383a98e2ec41e7a7d6600edd9 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sat, 22 Nov 2014 17:31:53 -0500 Subject: [PATCH] More boundary refactoring and testing Only testing branch logic is so nice... --- .../downloader/BookListFragmentTest.kt | 44 ++++++++++++++--- .../activity/downloader/BookListFragment.java | 48 ++++++++++++------- 2 files changed, 68 insertions(+), 24 deletions(-) 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 index c42f3a0..b2aa8e4 100644 --- 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 @@ -2,6 +2,7 @@ package org.bspeice.minimalbible.activity.downloader import org.jetbrains.spek.api.Spek import kotlin.test.assertTrue +import android.content.DialogInterface /** * Created by bspeice on 11/22/14. @@ -10,16 +11,13 @@ import kotlin.test.assertTrue class BookListFragmentSpek : Spek() {{ given("A BookListFragment with showDialog() mocked out") { - class TestableFragment : BookListFragment() { + val fragment = object : 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) @@ -30,7 +28,7 @@ class BookListFragmentSpek : Spek() {{ } given("a BookListFragment with displayLanguageSpinner() mocked out") { - class TestableFragment : BookListFragment() { + val fragment = object : BookListFragment() { var condition = false override fun displayLanguageSpinner() { @@ -38,8 +36,6 @@ class BookListFragmentSpek : Spek() {{ } } - val fragment = TestableFragment() - on("attempting to display modules with the dialog already shown") { fragment.displayModules(true) @@ -48,5 +44,39 @@ class BookListFragmentSpek : Spek() {{ } } } + + given("a DownloadDialogListener with with buttonPositive() mocked out") { + val listener = object : BookListFragment.DownloadDialogListener(null, null) { + var condition = false + override fun buttonPositive() { + condition = true + } + } + + on("handling a positive button press") { + listener.handleButton(DialogInterface.BUTTON_POSITIVE) + + it("should call the proper handler") { + assertTrue(listener.condition) + } + } + } + + given("A DownloadDialogListener with buttonNegative() mocked out") { + val listener = object : BookListFragment.DownloadDialogListener(null, null) { + var condition = false + override fun buttonNegative() { + condition = true + } + } + + on("handling a negative button press") { + listener.handleButton(DialogInterface.BUTTON_NEGATIVE) + + it("should call the proper handler") { + assertTrue(listener.condition) + } + } + } } } \ 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 905ba58..21fc527 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 @@ -111,7 +111,7 @@ public class BookListFragment extends BaseFragment { void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - DownloadDialogListener dialogListener = new DownloadDialogListener(); + DownloadDialogListener dialogListener = new DownloadDialogListener(this, downloadPrefs); builder.setMessage( "About to contact servers to download content. Continue?") .setPositiveButton("Yes", dialogListener) @@ -176,39 +176,53 @@ public class BookListFragment extends BaseFragment { }); } - private class DownloadDialogListener implements + static class DownloadDialogListener implements DialogInterface.OnClickListener { + BookListFragment fragment; + DownloadPrefs downloadPrefs; + + DownloadDialogListener(BookListFragment fragment, DownloadPrefs downloadPrefs) { + this.fragment = fragment; + this.downloadPrefs = downloadPrefs; + } + @Override public void onClick(@NotNull DialogInterface dialog, int which) { downloadPrefs.hasShownDownloadDialog(true); + handleButton(which); + } + void handleButton(int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: - // Clicked ready to continue - allow downloading in the future - downloadPrefs.hasEnabledDownload(true); - - // And warn them that it has been enabled in the future. - showToast("Downloading now enabled. Disable in settings"); - displayModules(); + buttonPositive(); break; // case DialogInterface.BUTTON_NEGATIVE: default: - // Clicked to not download - Permanently disable downloading - downloadPrefs.hasEnabledDownload(false); - showToast("Disabling downloading. Re-enable it in settings."); - shutdown(); + buttonNegative(); break; } } - void showToast(String text) { - Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); + void buttonPositive() { + // Clicked ready to continue - allow downloading in the future + downloadPrefs.hasEnabledDownload(true); + + // And warn them that it has been enabled in the future. + showToast("Downloading now enabled. Disable in settings"); + fragment.displayModules(); } - void shutdown() { - getActivity().finish(); + void buttonNegative() { + // Clicked to not download - Permanently disable downloading + downloadPrefs.hasEnabledDownload(false); + showToast("Disabling downloading. Re-enable it in settings."); + fragment.getActivity().finish(); + } + + void showToast(String text) { + Toast.makeText(fragment.getActivity(), text, Toast.LENGTH_SHORT).show(); } } - } \ No newline at end of file