More boundary refactoring and testing

Only testing branch logic is so nice...
This commit is contained in:
Bradlee Speice 2014-11-22 17:31:53 -05:00
parent 05d2d006e4
commit 8d65327853
2 changed files with 68 additions and 24 deletions

View File

@ -2,6 +2,7 @@ package org.bspeice.minimalbible.activity.downloader
import org.jetbrains.spek.api.Spek import org.jetbrains.spek.api.Spek
import kotlin.test.assertTrue import kotlin.test.assertTrue
import android.content.DialogInterface
/** /**
* Created by bspeice on 11/22/14. * Created by bspeice on 11/22/14.
@ -10,16 +11,13 @@ import kotlin.test.assertTrue
class BookListFragmentSpek : Spek() {{ class BookListFragmentSpek : Spek() {{
given("A BookListFragment with showDialog() mocked out") { given("A BookListFragment with showDialog() mocked out") {
class TestableFragment : BookListFragment() { val fragment = object : BookListFragment() {
var condition = false var condition = false
override fun showDialog() { override fun showDialog() {
condition = true condition = true
} }
} }
val fragment = TestableFragment()
on("attempting to display modules with the dialog not shown already") { on("attempting to display modules with the dialog not shown already") {
fragment.displayModules(false) fragment.displayModules(false)
@ -30,7 +28,7 @@ class BookListFragmentSpek : Spek() {{
} }
given("a BookListFragment with displayLanguageSpinner() mocked out") { given("a BookListFragment with displayLanguageSpinner() mocked out") {
class TestableFragment : BookListFragment() { val fragment = object : BookListFragment() {
var condition = false var condition = false
override fun displayLanguageSpinner() { override fun displayLanguageSpinner() {
@ -38,8 +36,6 @@ class BookListFragmentSpek : Spek() {{
} }
} }
val fragment = TestableFragment()
on("attempting to display modules with the dialog already shown") { on("attempting to display modules with the dialog already shown") {
fragment.displayModules(true) 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)
}
}
}
} }
} }

View File

@ -111,7 +111,7 @@ public class BookListFragment extends BaseFragment {
void showDialog() { void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
DownloadDialogListener dialogListener = new DownloadDialogListener(); DownloadDialogListener dialogListener = new DownloadDialogListener(this, downloadPrefs);
builder.setMessage( builder.setMessage(
"About to contact servers to download content. Continue?") "About to contact servers to download content. Continue?")
.setPositiveButton("Yes", dialogListener) .setPositiveButton("Yes", dialogListener)
@ -176,39 +176,53 @@ public class BookListFragment extends BaseFragment {
}); });
} }
private class DownloadDialogListener implements static class DownloadDialogListener implements
DialogInterface.OnClickListener { DialogInterface.OnClickListener {
BookListFragment fragment;
DownloadPrefs downloadPrefs;
DownloadDialogListener(BookListFragment fragment, DownloadPrefs downloadPrefs) {
this.fragment = fragment;
this.downloadPrefs = downloadPrefs;
}
@Override @Override
public void onClick(@NotNull DialogInterface dialog, int which) { public void onClick(@NotNull DialogInterface dialog, int which) {
downloadPrefs.hasShownDownloadDialog(true); downloadPrefs.hasShownDownloadDialog(true);
handleButton(which);
}
void handleButton(int which) {
switch (which) { switch (which) {
case DialogInterface.BUTTON_POSITIVE: case DialogInterface.BUTTON_POSITIVE:
buttonPositive();
break;
// case DialogInterface.BUTTON_NEGATIVE:
default:
buttonNegative();
break;
}
}
void buttonPositive() {
// Clicked ready to continue - allow downloading in the future // Clicked ready to continue - allow downloading in the future
downloadPrefs.hasEnabledDownload(true); downloadPrefs.hasEnabledDownload(true);
// And warn them that it has been enabled in the future. // And warn them that it has been enabled in the future.
showToast("Downloading now enabled. Disable in settings"); showToast("Downloading now enabled. Disable in settings");
displayModules(); fragment.displayModules();
break; }
// case DialogInterface.BUTTON_NEGATIVE: void buttonNegative() {
default:
// Clicked to not download - Permanently disable downloading // Clicked to not download - Permanently disable downloading
downloadPrefs.hasEnabledDownload(false); downloadPrefs.hasEnabledDownload(false);
showToast("Disabling downloading. Re-enable it in settings."); showToast("Disabling downloading. Re-enable it in settings.");
shutdown(); fragment.getActivity().finish();
break;
}
} }
void showToast(String text) { void showToast(String text) {
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show(); Toast.makeText(fragment.getActivity(), text, Toast.LENGTH_SHORT).show();
}
void shutdown() {
getActivity().finish();
} }
} }
} }