Browse files

Tried to get Robolectric and RoboGuice working together. Sadly they a…

…re not yet.
  • Loading branch information...
1 parent abcf50d commit c7b7c4caf3eec8afbde9212ebb92d4e0c625709e @avagadia avagadia committed Mar 17, 2011
View
34 Test/tests/gov/nasa/arc/geocam/memo/test/ActivityTest.java
@@ -2,20 +2,50 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
+
+import java.util.List;
+
import gov.nasa.arc.geocam.memo.R;
import gov.nasa.arc.geocam.memo.activity.GeoCamMemo;
+import gov.nasa.arc.geocam.memo.service.DjangoMemoImplementation;
+import gov.nasa.arc.geocam.memo.service.DjangoMemoInterface;
+import gov.nasa.arc.geocam.memo.test.injected.FakeDjangoMemoImplementation;
import gov.nasa.arc.geocam.memo.test.injected.InjectedTestRunner;
import roboguice.activity.RoboActivity;
+import roboguice.config.AbstractAndroidModule;
+
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import android.widget.ListView;
+
+import com.google.inject.Inject;
import com.xtremelabs.robolectric.RobolectricTestRunner;
@RunWith(InjectedTestRunner.class)
-public class ActivityTest {
+public class ActivityTest{
+ @Inject GeoCamMemo activity;
+ @Inject FakeDjangoMemoImplementation fakeDjangoMemo;
+
@Test
public void shouldHaveHappySmiles() throws Exception {
String hello = new GeoCamMemo().getResources().getString(R.string.hello);
- assertThat(hello, equalTo("Hello World, GeoCamMemo!"));
+ assertThat(hello, equalTo("Hello World, GeoCamMemo!"));
}
+
+ @Test
+ public void shouldDisplayMemos() throws Exception {
+ //arrange
+ activity.onCreate(null);
+ //FakeDjangoMemoImplementation fakeDjangoMemo = new FakeDjangoMemoImplementation();
+ List<String>fakeMemoList = fakeDjangoMemo.getMemos();
+ activity.setContentView(R.layout.main);
+
+ //act
+ ListView geoCamListView = (ListView)new GeoCamMemo().findViewById(R.id.MemoListView);
+
+ //assert
+ assertThat(geoCamListView.getChildCount(), equalTo(fakeMemoList.size()));
+ }
}
View
22 Test/tests/gov/nasa/arc/geocam/memo/test/injected/FakeDjangoMemoImplementation.java
@@ -0,0 +1,22 @@
+package gov.nasa.arc.geocam.memo.test.injected;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.inject.Singleton;
+
+import gov.nasa.arc.geocam.memo.service.DjangoMemoInterface;
+
+@Singleton
+public class FakeDjangoMemoImplementation implements DjangoMemoInterface{
+
+ @Override
+ public List<String> getMemos() {
+ // TODO Auto-generated method stub
+ List<String>testStringList = new ArrayList<String>();
+ testStringList.add("string1");
+ testStringList.add("string2");
+ testStringList.add("string3");
+ return testStringList;
+ }
+}
View
16 Test/tests/gov/nasa/arc/geocam/memo/test/injected/InjectedTestRunner.java
@@ -4,6 +4,10 @@
import org.junit.runners.model.InitializationError;
+import roboguice.inject.ContextScope;
+
+import android.app.Application;
+
import com.google.inject.Injector;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
@@ -15,10 +19,18 @@ public InjectedTestRunner(Class<?> testClass) throws InitializationError {
// TODO Auto-generated constructor stub
}
+ @Override protected Application createApplication() {
+ GeoCamMemoRoboApplication application = (GeoCamMemoRoboApplication)super.createApplication();
+ application.setModule(new TestInjectedModule());
+ return application;
+ }
+
@Override public void prepareTest(Object test){
- GeoCamMemoRoboApplication sampleApplication =
+ GeoCamMemoRoboApplication application =
(GeoCamMemoRoboApplication)Robolectric.application;
- Injector injector = sampleApplication.getInjector();
+ Injector injector = application.getInjector();
+ ContextScope scope = injector.getInstance(ContextScope.class);
+ scope.enter(application);
injector.injectMembers(test);
}
View
20 Test/tests/gov/nasa/arc/geocam/memo/test/injected/TestInjectedModule.java
@@ -0,0 +1,20 @@
+package gov.nasa.arc.geocam.memo.test.injected;
+
+import org.w3c.dom.css.Counter;
+
+import com.google.inject.Scopes;
+
+import gov.nasa.arc.geocam.memo.service.DjangoMemoInterface;
+import roboguice.config.AbstractAndroidModule;
+
+public class TestInjectedModule extends AbstractAndroidModule{
+
+ @Override
+ protected void configure() {
+ // TODO Auto-generated method stub
+ bind(Counter.class).in(Scopes.SINGLETON);
+ bind(DjangoMemoInterface.class).to(FakeDjangoMemoImplementation.class);
+ }
+
+
+}
View
5 android/res/layout/main.xml
@@ -9,4 +9,9 @@
android:layout_height="wrap_content"
android:text="@string/hello"
/>
+<ListView
+ android:id="@+id/MemoListView"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ />
</LinearLayout>
View
8 android/src/gov/nasa/arc/geocam/memo/GeoCamMemoRoboApplication.java
@@ -5,7 +5,13 @@
import java.util.List;
public class GeoCamMemoRoboApplication extends RoboApplication{
- protected void addApplicationModules(List<Module> modules) {
+ private Module module = new GeoCamMemoModule();
+
+ protected void addApplicationModules(List<Module> modules) {
modules.add(new GeoCamMemoModule());
}
+
+ public void setModule(Module module) {
+ this.module = module;
+ }
}
View
13 android/src/gov/nasa/arc/geocam/memo/activity/GeoCamMemo.java
@@ -1,5 +1,7 @@
package gov.nasa.arc.geocam.memo.activity;
+import java.util.List;
+
import com.google.inject.Inject;
import gov.nasa.arc.geocam.memo.R;
@@ -8,17 +10,22 @@
import roboguice.activity.RoboActivity;
import android.app.Activity;
import android.os.Bundle;
+import android.widget.ArrayAdapter;
+import android.widget.ListAdapter;
+import android.widget.ListView;
public class GeoCamMemo extends RoboActivity {
- @Inject protected DjangoMemoInterface djangoMemo;
+ @Inject DjangoMemoInterface djangoMemo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
-
- djangoMemo.getMemos();
+
+ List<String>memoList = djangoMemo.getMemos();
+ ListView memoListView = (ListView)findViewById(R.id.MemoListView);
+ memoListView.setAdapter(new ArrayAdapter(this, R.id.MemoListView, memoList.toArray()));
}
}
View
6 android/src/gov/nasa/arc/geocam/memo/service/DjangoMemoImplementation.java
@@ -1,13 +1,17 @@
package gov.nasa.arc.geocam.memo.service;
+import java.util.ArrayList;
import java.util.List;
public class DjangoMemoImplementation implements DjangoMemoInterface{
@Override
public List<String> getMemos() {
// TODO Auto-generated method stub
- return null;
+ List<String>testStringList = new ArrayList<String>();
+ testStringList.add("string1");
+ testStringList.add("string2");
+ return testStringList;
}
}

0 comments on commit c7b7c4c

Please sign in to comment.