mirror of
				https://github.com/MinimalBible/MinimalBible
				synced 2025-11-04 02:20:26 -05:00 
			
		
		
		
	Some more cleanup and additions
Android is seriously not test-friendly
This commit is contained in:
		@ -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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<Installer> installers) {
 | 
			
		||||
            return new RefreshManager(installers);
 | 
			
		||||
            return new RefreshManager(installers,
 | 
			
		||||
                    prefs, manager);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<Installer> installers) {
 | 
			
		||||
            return new RefreshManager(installers);
 | 
			
		||||
            return new RefreshManager(installers,
 | 
			
		||||
                    prefs, manager);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<Installer> installers;
 | 
			
		||||
 | 
			
		||||
        ConnectivityManager manager;
 | 
			
		||||
        DownloadPrefs prefs;
 | 
			
		||||
        RMTModules(Collection<Installer> 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<Installer> installers) {
 | 
			
		||||
            return new RefreshManager(installers);
 | 
			
		||||
            return new RefreshManager(installers,
 | 
			
		||||
                    prefs, manager);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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<Installer> installers) {
 | 
			
		||||
        return new RefreshManager(installers);
 | 
			
		||||
    RefreshManager provideRefreshManager(Collection<Installer> installers, DownloadPrefs prefs,
 | 
			
		||||
                                         @Named("DownloadActivityContext") Context context) {
 | 
			
		||||
        return new RefreshManager(installers, prefs,
 | 
			
		||||
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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<Installer>) {
 | 
			
		||||
class RefreshManager(val installers: Collection<Installer>,
 | 
			
		||||
                     val prefs: DownloadPrefs,
 | 
			
		||||
                     val connManager: ConnectivityManager?) {
 | 
			
		||||
    val refreshComplete = AtomicBoolean()
 | 
			
		||||
    val availableModules: Observable<Map<Installer, List<Book>>> =
 | 
			
		||||
            Observable.from(installers)
 | 
			
		||||
@ -36,11 +41,27 @@ class RefreshManager(val installers: Collection<Installer>) {
 | 
			
		||||
        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<Installer> = Observable.just(
 | 
			
		||||
            availableModules.filter {
 | 
			
		||||
                it.flatMap { it.value } contains b
 | 
			
		||||
            }
 | 
			
		||||
                    .toBlocking().first().entrySet().first().getKey())
 | 
			
		||||
            }.toBlocking().first().entrySet().first().getKey())
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user