Browse files

Add ShadowAppWidgetHost#createView

  • Loading branch information...
1 parent a08109f commit a01fe8bc701965b48a1329342553360383dc65ed Aaron VonderHaar committed Nov 20, 2012
View
18 src/main/java/com/xtremelabs/robolectric/shadows/ShadowAppWidgetHost.java
@@ -1,12 +1,21 @@
package com.xtremelabs.robolectric.shadows;
import android.appwidget.AppWidgetHost;
+import android.appwidget.AppWidgetHostView;
+import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
+import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
+import com.xtremelabs.robolectric.internal.RealObject;
+
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
@Implements(AppWidgetHost.class)
public class ShadowAppWidgetHost {
+ @RealObject
+ private AppWidgetHost realAppWidgetHost;
+
private Context context;
private int hostId;
@@ -22,4 +31,13 @@ public Context getContext() {
public int getHostId() {
return hostId;
}
+
+ @Implementation
+ public AppWidgetHostView createView(Context context, int appWidgetId,
+ AppWidgetProviderInfo appWidget) {
+ AppWidgetHostView hostView = new AppWidgetHostView(context);
+ hostView.setAppWidget(appWidgetId, appWidget);
+ shadowOf(hostView).setHost(realAppWidgetHost);
+ return hostView;
+ }
}
View
10 src/main/java/com/xtremelabs/robolectric/shadows/ShadowAppWidgetHostView.java
@@ -1,5 +1,6 @@
package com.xtremelabs.robolectric.shadows;
+import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetProviderInfo;
import com.xtremelabs.robolectric.internal.Implementation;
@@ -10,6 +11,7 @@
private int appWidgetId;
private AppWidgetProviderInfo appWidgetInfo;
+ private AppWidgetHost host;
@Implementation
public void setAppWidget(int appWidgetId, AppWidgetProviderInfo info) {
@@ -26,4 +28,12 @@ public int getAppWidgetId() {
public AppWidgetProviderInfo getAppWidgetInfo() {
return appWidgetInfo;
}
+
+ public AppWidgetHost getHost() {
+ return host;
+ }
+
+ public void setHost(AppWidgetHost host) {
+ this.host = host;
+ }
}
View
34 src/test/java/com/xtremelabs/robolectric/shadows/AppWidgetHostTest.java
@@ -1,6 +1,8 @@
package com.xtremelabs.robolectric.shadows;
import android.appwidget.AppWidgetHost;
+import android.appwidget.AppWidgetHostView;
+import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
@@ -11,6 +13,7 @@
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
@RunWith(WithTestDefaultsRunner.class)
@@ -35,4 +38,35 @@ public void shouldKnowItsContext() throws Exception {
public void shouldKnowItsHostId() throws Exception {
assertThat(shadowAppWidgetHost.getHostId(), is(404));
}
+
+ @Test
+ public void createView_shouldReturnAppWidgetHostView() throws Exception {
+ AppWidgetHostView hostView = appWidgetHost.createView(context, 0, null);
+ assertNotNull(hostView);
+ }
+
+ @Test
+ public void createView_shouldSetViewsContext() throws Exception {
+ AppWidgetHostView hostView = appWidgetHost.createView(context, 0, null);
+ assertThat(hostView.getContext(), sameInstance(context));
+ }
+
+ @Test
+ public void createView_shouldSetViewsAppWidgetId() throws Exception {
+ AppWidgetHostView hostView = appWidgetHost.createView(null, 765, null);
+ assertThat(hostView.getAppWidgetId(), is(765));
+ }
+
+ @Test
+ public void createView_shouldSetViewsAppWidgetInfo() throws Exception {
+ AppWidgetProviderInfo info = new AppWidgetProviderInfo();
+ AppWidgetHostView hostView = appWidgetHost.createView(null, 0, info);
+ assertThat(hostView.getAppWidgetInfo(), sameInstance(info));
+ }
+
+ @Test
+ public void createView_shouldSetHostViewsHost() throws Exception {
+ AppWidgetHostView hostView = appWidgetHost.createView(null, 0, null);
+ assertThat(shadowOf(hostView).getHost(), sameInstance(appWidgetHost));
+ }
}
View
19 src/test/java/com/xtremelabs/robolectric/shadows/AppWidgetHostViewTest.java
@@ -1,5 +1,6 @@
package com.xtremelabs.robolectric.shadows;
+import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetProviderInfo;
import com.xtremelabs.robolectric.Robolectric;
@@ -8,17 +9,19 @@
import org.junit.Test;
import org.junit.runner.RunWith;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.sameInstance;
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
@RunWith(WithTestDefaultsRunner.class)
public class AppWidgetHostViewTest {
private AppWidgetHostView appWidgetHostView;
+ private ShadowAppWidgetHostView shadowAppWidgetHostView;
@Before
public void setUp() throws Exception {
appWidgetHostView = new AppWidgetHostView(Robolectric.application);
+ shadowAppWidgetHostView = shadowOf(appWidgetHostView);
}
@Test
@@ -33,4 +36,16 @@ public void shouldKnowItsAppWidgetProviderInfo() throws Exception {
appWidgetHostView.setAppWidget(0, providerInfo);
assertThat(appWidgetHostView.getAppWidgetInfo(), sameInstance(providerInfo));
}
+
+ @Test
+ public void shouldHaveNullHost() throws Exception {
+ assertThat(shadowAppWidgetHostView.getHost(), nullValue());
+ }
+
+ @Test
+ public void shouldBeAbleToHaveHostSet() throws Exception {
+ AppWidgetHost host = new AppWidgetHost(null, 0);
+ shadowAppWidgetHostView.setHost(host);
+ assertThat(shadowAppWidgetHostView.getHost(), sameInstance(host));
+ }
}

0 comments on commit a01fe8b

Please sign in to comment.