diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/MBTestCase.java b/app/src/androidTest/java/org/bspeice/minimalbible/MBTestCase.java new file mode 100644 index 0000000..45b5759 --- /dev/null +++ b/app/src/androidTest/java/org/bspeice/minimalbible/MBTestCase.java @@ -0,0 +1,15 @@ +package org.bspeice.minimalbible; + +import android.test.AndroidTestCase; + +/** + * A TestCase specifically for running Android JUnit tests. + * This is not intended to replace InstrumentationTest, etc., just + * replace the bare JUnit tests. + */ +public class MBTestCase extends AndroidTestCase { + + public void setUp() { + System.setProperty("dexmaker.dexcache", getContext().getCacheDir().getPath()); + } +} diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java index f036bfd..777af1a 100644 --- a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java +++ b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookDownloadManagerTest.java @@ -1,10 +1,12 @@ package org.bspeice.minimalbible.test.activity.downloader.manager; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.util.Log; -import junit.framework.TestCase; - import org.bspeice.minimalbible.Injector; +import org.bspeice.minimalbible.MBTestCase; +import org.bspeice.minimalbible.activity.downloader.DownloadPrefs; import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager; import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; @@ -15,6 +17,7 @@ import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.Installer; +import org.mockito.Mockito; import java.util.Collection; import java.util.concurrent.TimeUnit; @@ -31,8 +34,10 @@ import rx.functions.Action1; import rx.functions.Func1; import static com.jayway.awaitility.Awaitility.await; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; -public class BookDownloadManagerTest extends TestCase implements Injector { +public class BookDownloadManagerTest extends MBTestCase implements Injector { ObjectGraph mObjectGraph; @Inject BookDownloadManager bookDownloadManager; @@ -114,9 +119,19 @@ public class BookDownloadManagerTest extends TestCase implements Injector { @SuppressWarnings("unused") public static class BookDownloadManagerTestModules { Injector i; - + ConnectivityManager manager; + DownloadPrefs prefs; BookDownloadManagerTestModules(Injector i) { this.i = i; + + // Set reasonable defaults for the manager and preferences, can over-ride if need-be + manager = mock(ConnectivityManager.class); + NetworkInfo mockNetworkInfo = Mockito.mock(NetworkInfo.class); + + when(manager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); + + prefs = mock(DownloadPrefs.class); } @Provides @@ -137,10 +152,19 @@ public class BookDownloadManagerTest extends TestCase implements Injector { return new InstallManager().getInstallers().values(); } + void setConnectivityManager(ConnectivityManager manager) { + this.manager = manager; + } + + void setPrefs(DownloadPrefs prefs) { + this.prefs = prefs; + } + @Provides @Singleton RefreshManager refreshManager(Collection installers) { - return new RefreshManager(installers); + return new RefreshManager(installers, + prefs, manager); } } } \ No newline at end of file diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java index 323a87e..e693e57 100644 --- a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java +++ b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/InstalledManagerTest.java @@ -1,10 +1,12 @@ package org.bspeice.minimalbible.test.activity.downloader.manager; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import android.util.Log; -import junit.framework.TestCase; - import org.bspeice.minimalbible.Injector; +import org.bspeice.minimalbible.MBTestCase; +import org.bspeice.minimalbible.activity.downloader.DownloadPrefs; import org.bspeice.minimalbible.activity.downloader.manager.BookDownloadManager; import org.bspeice.minimalbible.activity.downloader.manager.InstalledManager; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; @@ -12,6 +14,7 @@ import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Books; import org.crosswire.jsword.book.install.InstallManager; import org.crosswire.jsword.book.install.Installer; +import org.mockito.Mockito; import java.util.Collection; import java.util.List; @@ -27,6 +30,9 @@ import rx.Observable; import rx.functions.Action1; import rx.functions.Func1; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** * Test the InstalledManager * Currently due to limitations with JSword (which I'm currently investigating) you can't delete @@ -34,7 +40,7 @@ import rx.functions.Func1; * in between it being deleted. Unfortunately, that means that this TestCase really can't guarantee * much, since I can't install a book at runtime to be removed. */ -public class InstalledManagerTest extends TestCase implements Injector { +public class InstalledManagerTest extends MBTestCase implements Injector { ObjectGraph mObjectGraph; @Inject InstalledManager iM; @@ -46,7 +52,8 @@ public class InstalledManagerTest extends TestCase implements Injector { mObjectGraph.inject(o); } - public void setUp() throws Exception { + @Override + public void setUp() { super.setUp(); mObjectGraph = ObjectGraph.create(new IMTestModules(this)); mObjectGraph.inject(this); @@ -99,9 +106,19 @@ public class InstalledManagerTest extends TestCase implements Injector { @SuppressWarnings("unused") static class IMTestModules { Injector i; - + ConnectivityManager manager; + DownloadPrefs prefs; public IMTestModules(Injector i) { this.i = i; + + // Set reasonable defaults for the manager and preferences, can over-ride if need-be + manager = mock(ConnectivityManager.class); + NetworkInfo mockNetworkInfo = Mockito.mock(NetworkInfo.class); + + when(manager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); + + prefs = mock(DownloadPrefs.class); } @Provides @@ -127,10 +144,19 @@ public class InstalledManagerTest extends TestCase implements Injector { return new InstallManager().getInstallers().values(); } + void setConnectivityManager(ConnectivityManager manager) { + this.manager = manager; + } + + void setPrefs(DownloadPrefs prefs) { + this.prefs = prefs; + } + @Provides @Singleton RefreshManager refreshManager(Collection installers) { - return new RefreshManager(installers); + return new RefreshManager(installers, + prefs, manager); } } } \ No newline at end of file diff --git a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/RefreshManagerTest.java b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/RefreshManagerTest.java index a1569a9..745af93 100644 --- a/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/RefreshManagerTest.java +++ b/app/src/androidTest/java/org/bspeice/minimalbible/test/activity/downloader/manager/RefreshManagerTest.java @@ -1,11 +1,15 @@ package org.bspeice.minimalbible.test.activity.downloader.manager; -import junit.framework.TestCase; +import android.net.ConnectivityManager; +import android.net.NetworkInfo; import org.bspeice.minimalbible.Injector; +import org.bspeice.minimalbible.MBTestCase; +import org.bspeice.minimalbible.activity.downloader.DownloadPrefs; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.install.Installer; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -28,7 +32,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -public class RefreshManagerTest extends TestCase implements Injector { +public class RefreshManagerTest extends MBTestCase implements Injector { /** * The object graph that should be given to classes under test. Each test is responsible @@ -137,9 +141,19 @@ public class RefreshManagerTest extends TestCase implements Injector { @SuppressWarnings("unused") class RMTModules { Collection installers; - + ConnectivityManager manager; + DownloadPrefs prefs; RMTModules(Collection installers) { this.installers = installers; + + // Set reasonable defaults for the manager and preferences, can over-ride if need-be + manager = mock(ConnectivityManager.class); + NetworkInfo mockNetworkInfo = Mockito.mock(NetworkInfo.class); + + when(manager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); + + prefs = mock(DownloadPrefs.class); } @Provides @@ -148,10 +162,19 @@ public class RefreshManagerTest extends TestCase implements Injector { return this.installers; } + void setConnectivityManager(ConnectivityManager manager) { + this.manager = manager; + } + + void setPrefs(DownloadPrefs prefs) { + this.prefs = prefs; + } + @Provides @Singleton RefreshManager refreshManager(Collection installers) { - return new RefreshManager(installers); + return new RefreshManager(installers, + prefs, manager); } } } \ No newline at end of file diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java index 46dafd6..017b7db 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java @@ -1,6 +1,7 @@ package org.bspeice.minimalbible.activity.downloader; import android.content.Context; +import android.net.ConnectivityManager; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.MinimalBibleModules; @@ -106,7 +107,9 @@ public class DownloadActivityModules { } @Provides @Singleton - RefreshManager provideRefreshManager(Collection installers) { - return new RefreshManager(installers); + RefreshManager provideRefreshManager(Collection installers, DownloadPrefs prefs, + @Named("DownloadActivityContext") Context context) { + return new RefreshManager(installers, prefs, + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt index 47e434f..e073cc0 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/RefreshManager.kt @@ -5,12 +5,17 @@ import java.util.concurrent.atomic.AtomicBoolean import rx.Observable import org.crosswire.jsword.book.Book import rx.schedulers.Schedulers +import java.util.Calendar +import org.bspeice.minimalbible.activity.downloader.DownloadPrefs +import android.net.ConnectivityManager /** * Created by bspeice on 10/22/14. */ -class RefreshManager(val installers: Collection) { +class RefreshManager(val installers: Collection, + val prefs: DownloadPrefs, + val connManager: ConnectivityManager?) { val refreshComplete = AtomicBoolean() val availableModules: Observable>> = Observable.from(installers) @@ -36,11 +41,27 @@ class RefreshManager(val installers: Collection) { availableModules.subscribe({}, {}, { refreshComplete set true }) } - fun doReload(): Boolean = true + val fifteenDaysAgo = Calendar.getInstance().getTime().getTime() - 1296000 + + fun doReload(enabledDownload: Boolean, lastUpdated: Long, onWifi: Boolean): Boolean = + if (!enabledDownload || !onWifi) + false + else if (lastUpdated < fifteenDaysAgo) + true + else + false + + fun doReload(): Boolean = doReload(prefs.hasEnabledDownload(), + prefs.downloadRefreshedOn(), + // TODO: Functional is awesome, but this might be a bit ridiculous + (if (connManager?.getActiveNetworkInfo() != null) + connManager!!.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI + else + false) + ) fun installerFromBook(b: Book): Observable = Observable.just( availableModules.filter { it.flatMap { it.value } contains b - } - .toBlocking().first().entrySet().first().getKey()) + }.toBlocking().first().entrySet().first().getKey()) } \ No newline at end of file