From 9548bb50ef93fe754dc0fc646be8028db7144909 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 7 Jul 2014 23:28:28 -0400 Subject: [PATCH] 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. --- .../minimalbible/MinimalBibleModules.java | 16 ++++++++++-- .../org/bspeice/minimalbible/Modules.java | 13 +++++++++- .../org/bspeice/minimalbible/TestModules.java | 25 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java b/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java index 2a711e8..e57456d 100644 --- a/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/MinimalBibleModules.java @@ -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; + } } diff --git a/app/src/testConfig/java/org/bspeice/minimalbible/Modules.java b/app/src/testConfig/java/org/bspeice/minimalbible/Modules.java index 669e2eb..28462ad 100644 --- a/app/src/testConfig/java/org/bspeice/minimalbible/Modules.java +++ b/app/src/testConfig/java/org/bspeice/minimalbible/Modules.java @@ -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 }; } } diff --git a/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java b/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java index c2a0e18..fc4943e 100644 --- a/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java +++ b/app/src/testConfig/java/org/bspeice/minimalbible/TestModules.java @@ -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; + } }