Add a "testing mode" inject

Doesn't do anything currently, but will allow for Activities/etc. which can't be easily sub-classed, mocked, etc. during runs to modify behavior.
This commit is contained in:
Bradlee Speice 2014-07-07 23:28:28 -04:00
parent a15e78978b
commit 9548bb50ef
3 changed files with 51 additions and 3 deletions

View File

@ -2,8 +2,7 @@ package org.bspeice.minimalbible;
import android.app.Application;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import javax.inject.Named;
import javax.inject.Singleton;
import dagger.Module;
@ -24,4 +23,17 @@ public class MinimalBibleModules {
Application provideApplication() {
return app;
}
/**
* This field allows us to set application-wide whether we are in a test or not
* Allows components on down the line to know whether they should set some things up or not.
* Additionally, not a Singleton so we can enable/disable testing mode as needed. However,
* for production, it's always false.
* @return Whether we are in a test - false
*/
@Provides
@Named("Testing")
boolean isTest() {
return false;
}
}

View File

@ -2,14 +2,25 @@ package org.bspeice.minimalbible;
/**
* List modules to be used during testing
* Also the entry point for setting whether or not we are using testing mode
*/
public class Modules {
private static TestModules testModules = new TestModules();
private Modules() {}
public static void enableTestingMode() {
testModules.setTestMode(true);
}
public static void disableTestingMode() {
testModules.setTestMode(false);
}
public static Object[] list(MinimalBible app) {
return new Object[] {
new MinimalBibleModules(app),
new TestModules()
testModules
};
}
}

View File

@ -3,6 +3,8 @@ package org.bspeice.minimalbible;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import javax.inject.Named;
import dagger.Module;
import dagger.Provides;
@ -17,4 +19,27 @@ public class TestModules {
@Provides CharSequence provideString() {
return testActivityTitle;
}
/**
* Provide an application-wide hub to enable/disable a "testing" mode
* Each application is free to interpret what this means, but allows for programming
* different behavior to respond to different testing needs in code that can't be mocked
* *cough cough* `Activities`.
* @return
*/
@Provides
@Named("Testing")
boolean isTest() {
return isTest;
}
private boolean isTest;
public void setTestMode(boolean isTest) {
this.isTest = isTest;
}
public boolean getTestMode() {
return isTest;
}
}