mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-04 23:28:19 -05:00
More boundary refactoring and testing
Only testing branch logic is so nice...
This commit is contained in:
parent
05d2d006e4
commit
8d65327853
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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:
|
||||||
// Clicked ready to continue - allow downloading in the future
|
buttonPositive();
|
||||||
downloadPrefs.hasEnabledDownload(true);
|
|
||||||
|
|
||||||
// And warn them that it has been enabled in the future.
|
|
||||||
showToast("Downloading now enabled. Disable in settings");
|
|
||||||
displayModules();
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// case DialogInterface.BUTTON_NEGATIVE:
|
// case DialogInterface.BUTTON_NEGATIVE:
|
||||||
default:
|
default:
|
||||||
// Clicked to not download - Permanently disable downloading
|
buttonNegative();
|
||||||
downloadPrefs.hasEnabledDownload(false);
|
|
||||||
showToast("Disabling downloading. Re-enable it in settings.");
|
|
||||||
shutdown();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void showToast(String text) {
|
void buttonPositive() {
|
||||||
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
|
// 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() {
|
void buttonNegative() {
|
||||||
getActivity().finish();
|
// 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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user