mirror of
				https://github.com/MinimalBible/MinimalBible-Legacy
				synced 2025-11-03 18:00:34 -05:00 
			
		
		
		
	Compare commits
	
		
			5 Commits
		
	
	
		
			ugly-unit-
			...
			master-old
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 3d19e751d9 | |||
| 2eeb8829f2 | |||
| 597b8b29cc | |||
| 26e754a6d8 | |||
| db5adbd091 | 
@ -2,6 +2,11 @@ language: android
 | 
			
		||||
env:
 | 
			
		||||
  matrix:
 | 
			
		||||
    - ANDROID_SDKS=android-19,sysimg-19 ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a
 | 
			
		||||
 | 
			
		||||
android:
 | 
			
		||||
  components:
 | 
			
		||||
    - build-tools-20.0.0
 | 
			
		||||
 | 
			
		||||
before_install:
 | 
			
		||||
  - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
 | 
			
		||||
  - emulator -avd test -no-skin -no-audio -no-window &
 | 
			
		||||
 | 
			
		||||
@ -52,7 +52,12 @@ android {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Move the tests to tests/java, tests/res, etc...
 | 
			
		||||
        androidTest.setRoot('src/test')
 | 
			
		||||
        androidTest {
 | 
			
		||||
            manifest.srcFile 'src/test/AndroidManifest.xml'
 | 
			
		||||
            java.srcDirs = ['src/test/java']
 | 
			
		||||
            resources.srcDirs = ['src/test/res']
 | 
			
		||||
            assets.srcDirs = ['src/test/assets']
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Move the build types to build-types/<type>
 | 
			
		||||
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
 | 
			
		||||
 | 
			
		||||
@ -1,12 +0,0 @@
 | 
			
		||||
package org.bspeice.minimalbible;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Massive shout-out to <a href="https://github.com/vovkab">vovkab</a> for this idea.
 | 
			
		||||
 */
 | 
			
		||||
public interface Injectable {
 | 
			
		||||
 | 
			
		||||
    public Object[] getModules();
 | 
			
		||||
 | 
			
		||||
    public void inject(Object o);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -10,7 +10,7 @@ import java.io.File;
 | 
			
		||||
 | 
			
		||||
import dagger.ObjectGraph;
 | 
			
		||||
 | 
			
		||||
public class MinimalBible extends Application implements Injectable {
 | 
			
		||||
public class MinimalBible extends Application {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The graph used by Dagger to track dependencies
 | 
			
		||||
@ -71,14 +71,13 @@ public class MinimalBible extends Application implements Injectable {
 | 
			
		||||
 | 
			
		||||
    public ObjectGraph getObjGraph() {
 | 
			
		||||
        if (graph == null) {
 | 
			
		||||
            graph = ObjectGraph.create(getModules());
 | 
			
		||||
            graph = ObjectGraph.create(MinimalBibleModules.class);
 | 
			
		||||
        }
 | 
			
		||||
        return graph;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object[] getModules() {
 | 
			
		||||
        return MinimalBibleModules.list();
 | 
			
		||||
    public void plusObjGraph(Object... modules) {
 | 
			
		||||
        graph = graph.plus(modules);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -16,12 +16,4 @@ import dagger.Module;
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
public class MinimalBibleModules {
 | 
			
		||||
 | 
			
		||||
    private MinimalBibleModules() {}
 | 
			
		||||
 | 
			
		||||
    public static Object[] list() {
 | 
			
		||||
        return new Object[] {
 | 
			
		||||
                MinimalBibleModules.class
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								MinimalBible/src/test/AndroidManifest.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								MinimalBible/src/test/AndroidManifest.xml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,19 @@
 | 
			
		||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 | 
			
		||||
    package="org.bspeice.minimalbible">
 | 
			
		||||
 | 
			
		||||
    <uses-sdk
 | 
			
		||||
        android:minSdkVersion="8"
 | 
			
		||||
        android:targetSdkVersion="20" />
 | 
			
		||||
    <uses-permission android:name="android.permission.INTERNET"/>
 | 
			
		||||
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 | 
			
		||||
 | 
			
		||||
    <application
 | 
			
		||||
        android:allowBackup="true"
 | 
			
		||||
        android:icon="@drawable/ic_launcher"
 | 
			
		||||
        android:label="@string/app_name"
 | 
			
		||||
        android:theme="@style/AppTheme"
 | 
			
		||||
        android:name="org.bspeice.minimalbible.test.MinimalBibleTest" >
 | 
			
		||||
 | 
			
		||||
    </application>
 | 
			
		||||
 | 
			
		||||
</manifest>
 | 
			
		||||
@ -1,38 +1,75 @@
 | 
			
		||||
package org.bspeice.minimalbible.test;
 | 
			
		||||
 | 
			
		||||
import android.app.Application;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.os.Build;
 | 
			
		||||
 | 
			
		||||
import org.bspeice.minimalbible.MinimalBible;
 | 
			
		||||
 | 
			
		||||
import java.lang.reflect.InvocationTargetException;
 | 
			
		||||
import java.lang.reflect.Method;
 | 
			
		||||
 | 
			
		||||
import dagger.ObjectGraph;
 | 
			
		||||
 | 
			
		||||
public class MinimalBibleTest extends Application {
 | 
			
		||||
    public MinimalBibleTest(Context ctx) {
 | 
			
		||||
        attachBaseContext(ctx);
 | 
			
		||||
public class MinimalBibleTest extends MinimalBible {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The graph used by Dagger to track dependencies
 | 
			
		||||
     */
 | 
			
		||||
    private ObjectGraph graph;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A singleton reference to the Application currently being run.
 | 
			
		||||
     * Used mostly so we have a fixed point to get the App Context from
 | 
			
		||||
     */
 | 
			
		||||
	private static MinimalBibleTest instance;
 | 
			
		||||
 | 
			
		||||
    private String TAG = "MinimalBibleTest";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create the application, and persist the application Context
 | 
			
		||||
     */
 | 
			
		||||
	public MinimalBibleTest() {
 | 
			
		||||
		instance = this;		
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the Application Context. Please note, all attempts to get the App Context should come
 | 
			
		||||
     * through here, and please be sure that the Application won't satisfy what you need.
 | 
			
		||||
     * @return The Application Context
 | 
			
		||||
     */
 | 
			
		||||
	public static Context getAppContext() {
 | 
			
		||||
        return instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the Application, rather than just the Application Context. You likely should be using
 | 
			
		||||
     * this, rather than {@link #getAppContext()}
 | 
			
		||||
     * @return The MinimalBible {@link android.app.Application} object
 | 
			
		||||
     */
 | 
			
		||||
    public static MinimalBibleTest getApplication() {
 | 
			
		||||
        return instance;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Create the {@link android.app.Application}. Responsible for building and
 | 
			
		||||
     * holding on to the master ObjectGraph.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void attachBaseContext(Context base) {
 | 
			
		||||
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
 | 
			
		||||
            super.attachBaseContext(base);
 | 
			
		||||
        } else {
 | 
			
		||||
            try {
 | 
			
		||||
                Class<Application> applicationClass = Application.class;
 | 
			
		||||
                Method attach = applicationClass.getDeclaredMethod("attach", Context.class);
 | 
			
		||||
                attach.setAccessible(true);
 | 
			
		||||
                attach.invoke(this, base);
 | 
			
		||||
            } catch (NoSuchMethodException e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            } catch (IllegalAccessException e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            } catch (InvocationTargetException e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
    public void onCreate() {
 | 
			
		||||
        super.onCreate();
 | 
			
		||||
        //TODO: Is this necessary?
 | 
			
		||||
        inject(this);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Inject a Dagger object
 | 
			
		||||
     * @param o The object to be injected
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public void inject(Object o) {
 | 
			
		||||
        getObjGraph().inject(o);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public ObjectGraph getObjGraph() {
 | 
			
		||||
        if (graph == null) {
 | 
			
		||||
            graph = ObjectGraph.create(MinimalBibleModulesTest.class);
 | 
			
		||||
        }
 | 
			
		||||
        return graph;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -1,28 +0,0 @@
 | 
			
		||||
package org.bspeice.minimalbible.test;
 | 
			
		||||
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
 | 
			
		||||
import org.bspeice.minimalbible.Injectable;
 | 
			
		||||
 | 
			
		||||
import dagger.ObjectGraph;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by bspeice on 6/27/14.
 | 
			
		||||
 */
 | 
			
		||||
public abstract class MinimalBibleTestMockable extends MinimalBibleTest implements Injectable {
 | 
			
		||||
 | 
			
		||||
    private ObjectGraph mObjectGraph;
 | 
			
		||||
 | 
			
		||||
    public MinimalBibleTestMockable(Context context) {
 | 
			
		||||
        super(context);
 | 
			
		||||
        mObjectGraph = ObjectGraph.create(getModules());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public abstract Object[] getModules();
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void inject(Object o) {
 | 
			
		||||
        mObjectGraph.inject(o);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -1,88 +1,47 @@
 | 
			
		||||
package org.bspeice.minimalbible.test.activities.downloader;
 | 
			
		||||
 | 
			
		||||
import android.app.Application;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.content.ContextWrapper;
 | 
			
		||||
import android.content.Intent;
 | 
			
		||||
import android.os.Bundle;
 | 
			
		||||
import android.os.Handler;
 | 
			
		||||
import android.os.Looper;
 | 
			
		||||
import android.support.v4.app.Fragment;
 | 
			
		||||
import android.support.v4.app.FragmentManager;
 | 
			
		||||
import android.test.ActivityUnitTestCase;
 | 
			
		||||
import android.test.ActivityInstrumentationTestCase2;
 | 
			
		||||
 | 
			
		||||
import org.bspeice.minimalbible.MinimalBible;
 | 
			
		||||
import org.bspeice.minimalbible.activities.downloader.BookListFragment;
 | 
			
		||||
import org.bspeice.minimalbible.activities.downloader.DownloadActivity;
 | 
			
		||||
import org.bspeice.minimalbible.activities.downloader.DownloadPrefs;
 | 
			
		||||
import org.bspeice.minimalbible.test.MinimalBibleModulesTest;
 | 
			
		||||
import org.bspeice.minimalbible.test.MinimalBibleTest;
 | 
			
		||||
import org.bspeice.minimalbible.test.MinimalBibleTestMockable;
 | 
			
		||||
import org.crosswire.jsword.book.BookCategory;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.CountDownLatch;
 | 
			
		||||
 | 
			
		||||
import javax.inject.Inject;
 | 
			
		||||
 | 
			
		||||
import dagger.Module;
 | 
			
		||||
import dagger.Provides;
 | 
			
		||||
import de.devland.esperandro.Esperandro;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by bspeice on 6/23/14.
 | 
			
		||||
 */
 | 
			
		||||
public class BookListFragmentTest extends ActivityUnitTestCase<DownloadActivity> {
 | 
			
		||||
    private static Class activityUnderTest = DownloadActivity.class;
 | 
			
		||||
public class BookListFragmentTest extends ActivityInstrumentationTestCase2<DownloadActivity> {
 | 
			
		||||
 | 
			
		||||
    @Module(injects = TestDialogDisplayedIfFirstTimeFragment.class,
 | 
			
		||||
            addsTo = MinimalBibleModulesTest.class,
 | 
			
		||||
            overrides = true
 | 
			
		||||
            addsTo = MinimalBibleModulesTest.class
 | 
			
		||||
    )
 | 
			
		||||
    protected static class BookListFragmentTestModule{
 | 
			
		||||
        @Provides
 | 
			
		||||
        DownloadPrefs providePrefs() {
 | 
			
		||||
            return Esperandro.getPreferences(DownloadPrefs.class,
 | 
			
		||||
                    MinimalBible.getApplication());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    protected static class BookListFragmentTestModule{}
 | 
			
		||||
 | 
			
		||||
    public BookListFragmentTest() {
 | 
			
		||||
        super(activityUnderTest);
 | 
			
		||||
        super(DownloadActivity.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Inject DownloadPrefs downloadPrefs;
 | 
			
		||||
    FragmentManager mFragmentManager;
 | 
			
		||||
 | 
			
		||||
    private Application mApplication;
 | 
			
		||||
    private Context mContext;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setUp() throws Exception {
 | 
			
		||||
        super.setUp();
 | 
			
		||||
 | 
			
		||||
        // Set 'dexmaker.dexcache' system property, otherwise sometimes it is null and test will crash
 | 
			
		||||
        // System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
 | 
			
		||||
 | 
			
		||||
        mContext = new ContextWrapper(getInstrumentation().getTargetContext()) {
 | 
			
		||||
            @Override
 | 
			
		||||
            public Context getApplicationContext() {
 | 
			
		||||
                return mApplication;
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        mApplication = new MinimalBibleTestMockable(mContext) {
 | 
			
		||||
            @Override public Object[] getModules() {
 | 
			
		||||
                return new Object[]{new BookListFragmentTestModule()};
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        setApplication(mApplication);
 | 
			
		||||
        mFragmentManager = getActivity().getSupportFragmentManager();
 | 
			
		||||
        assertNotNull(mFragmentManager);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void tearDown() throws Exception {
 | 
			
		||||
        super.tearDown();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public <F extends Fragment> F startFragment(F fragment) {
 | 
			
		||||
        try {
 | 
			
		||||
@ -109,19 +68,15 @@ public class BookListFragmentTest extends ActivityUnitTestCase<DownloadActivity>
 | 
			
		||||
        return (F)(mFragmentManager.findFragmentById(android.R.id.content));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void testApplicationReplacementWorks() {
 | 
			
		||||
        setActivityContext(mContext);
 | 
			
		||||
        startActivity(new Intent(mContext, activityUnderTest), null, null);
 | 
			
		||||
 | 
			
		||||
        assertTrue(getActivity().getApplicationContext() instanceof MinimalBibleTestMockable);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected class TestDialogDisplayedIfFirstTimeFragment extends BookListFragment {
 | 
			
		||||
        /**
 | 
			
		||||
         * If the refresh dialog is blank after calling display, it must be showing the warning
 | 
			
		||||
         * @return Whether the warning dialog is showing
 | 
			
		||||
         */
 | 
			
		||||
        public boolean callDisplayModules() {
 | 
			
		||||
        public boolean callDisplayModules(DownloadPrefs prefs) {
 | 
			
		||||
            // Inject the new preferences...
 | 
			
		||||
            this.downloadPrefs = prefs;
 | 
			
		||||
            displayModules();
 | 
			
		||||
            return (refreshDialog == null);
 | 
			
		||||
        }
 | 
			
		||||
@ -134,30 +89,29 @@ public class BookListFragmentTest extends ActivityUnitTestCase<DownloadActivity>
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void testDialogDisplayedIfFirstTime() {
 | 
			
		||||
        setActivityContext(mContext);
 | 
			
		||||
        startActivity(new Intent(mContext, activityUnderTest), null, null);
 | 
			
		||||
 | 
			
		||||
        ((MinimalBible)getActivity().getApplication()).plusObjGraph(BookListFragmentTestModule.class);
 | 
			
		||||
        TestDialogDisplayedIfFirstTimeFragment f = new TestDialogDisplayedIfFirstTimeFragment();
 | 
			
		||||
        f.setArgs(BookCategory.BIBLE);
 | 
			
		||||
        startFragment(f);
 | 
			
		||||
 | 
			
		||||
        assertNotNull(f);
 | 
			
		||||
        DownloadPrefs prefs = Esperandro.getPreferences(DownloadPrefs.class, getActivity());
 | 
			
		||||
        prefs.hasShownDownloadDialog(false);
 | 
			
		||||
 | 
			
		||||
        downloadPrefs.hasShownDownloadDialog(false);
 | 
			
		||||
        assertTrue(f.callDisplayModules());
 | 
			
		||||
        assertNotNull(f);
 | 
			
		||||
        assertTrue(f.callDisplayModules(Esperandro.getPreferences(DownloadPrefs.class, getActivity())));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void testRefreshDisplayedAfterFirstTime() {
 | 
			
		||||
        setActivityContext(mContext);
 | 
			
		||||
        startActivity(new Intent(mContext, activityUnderTest), null, null);
 | 
			
		||||
        ((MinimalBible)getActivity().getApplication()).plusObjGraph(BookListFragmentTestModule.class);
 | 
			
		||||
 | 
			
		||||
        TestDialogDisplayedIfFirstTimeFragment f = new TestDialogDisplayedIfFirstTimeFragment();
 | 
			
		||||
        f.setArgs(BookCategory.BIBLE);
 | 
			
		||||
        startFragment(f);
 | 
			
		||||
 | 
			
		||||
        assertNotNull(f);
 | 
			
		||||
        DownloadPrefs prefs = Esperandro.getPreferences(DownloadPrefs.class, getActivity());
 | 
			
		||||
        prefs.hasShownDownloadDialog(true);
 | 
			
		||||
 | 
			
		||||
        downloadPrefs.hasShownDownloadDialog(true);
 | 
			
		||||
        assertFalse(f.callDisplayModules());
 | 
			
		||||
        assertNotNull(f);
 | 
			
		||||
        assertFalse(f.callDisplayModules(Esperandro.getPreferences(DownloadPrefs.class, getActivity())));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user