PoC using reflection

Test will need to be something more meaningful later.
This commit is contained in:
Bradlee Speice 2014-07-06 23:05:28 -04:00
parent 60185c934a
commit 5c71e8f014
8 changed files with 37 additions and 15 deletions

View File

@ -1,10 +1,12 @@
package org.bspeice.minimalbible;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.test.ActivityUnitTestCase;
import android.util.Log;
import org.bspeice.minimalbible.activity.download.DownloadActivity;
import java.lang.reflect.Field;
/**
* Created by Bradlee Speice on 7/5/2014.
*/
@ -12,7 +14,8 @@ import android.util.Log;
on using an activity that sets up any underlying fragments (includes navigation drawers).
The ActivityUnitTestCase doesn't set up enough of the Activity lifecycle.
*/
public class DownloadActivityTest extends ActivityInstrumentationTestCase2<DownloadActivity> {
public class DownloadActivityTest extends
ActivityInstrumentationTestCase2<DownloadActivity> {
public DownloadActivityTest() {
super(DownloadActivity.class);
@ -22,7 +25,17 @@ public class DownloadActivityTest extends ActivityInstrumentationTestCase2<Downl
DownloadActivity a = getActivity();
assertNotNull(a);
Log.w("DownloadActivityTest", a.actionTitle);
assertEquals(a.actionTitle, a.actionTitle, "Test");
Class c = a.getClass();
try {
// getField() is public-only
Field fTitle = c.getDeclaredField("testInject");
fTitle.setAccessible(true);
CharSequence title = (CharSequence)fTitle.get(a);
assertEquals(TestModules.testActivityTitle, title);
} catch (NoSuchFieldException e) {
fail(e.getMessage());
} catch (IllegalAccessException e) {
fail(e.getMessage());
}
}
}

View File

@ -9,7 +9,7 @@
android:theme="@style/AppTheme"
android:name=".MinimalBible" >
<activity
android:name=".DownloadActivity"
android:name=".activity.download.DownloadActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -2,6 +2,8 @@ package org.bspeice.minimalbible;
import android.app.Application;
import org.bspeice.minimalbible.activity.download.DownloadActivity;
import javax.inject.Singleton;
import dagger.Module;
@ -24,7 +26,7 @@ public class MinimalBibleModules {
return app;
}
@Provides String provideString() {
@Provides CharSequence provideString() {
return "Main";
}
}

View File

@ -1,4 +1,4 @@
package org.bspeice.minimalbible;
package org.bspeice.minimalbible.activity.download;
import android.app.Activity;
import android.os.Bundle;
@ -14,6 +14,10 @@ import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.NavigationDrawerFragment;
import org.bspeice.minimalbible.R;
import javax.inject.Inject;
@ -28,9 +32,10 @@ public class DownloadActivity extends ActionBarActivity
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
protected CharSequence mTitle;
@Inject String actionTitle;
//TODO: This will need to be refactored out later, for now it's a proof of concept.
@Inject CharSequence testInject;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -44,7 +49,6 @@ public class DownloadActivity extends ActionBarActivity
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
Log.w("DownloadActivity", "Title: " + mTitle.toString());
// Set up the drawer.
mNavigationDrawerFragment.setUp(

View File

@ -1,5 +1,7 @@
package org.bspeice.minimalbible;
import org.bspeice.minimalbible.activity.download.DownloadActivity;
import dagger.Module;
import dagger.Provides;
@ -10,7 +12,8 @@ import dagger.Provides;
overrides = true)
public class TestModules {
@Provides String provideString() {
return "Test";
public static CharSequence testActivityTitle = "Test";
@Provides CharSequence provideString() {
return testActivityTitle;
}
}

Binary file not shown.

View File

@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Sun Jul 06 17:56:45 EDT 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip

0
gradlew vendored Normal file → Executable file
View File