1 package com.xtremelabs.robolectric.shadows; 2 3 import android.appwidget.AppWidgetManager; 4 import android.appwidget.AppWidgetProvider; 5 import android.content.ComponentName; 6 import android.content.Context; 7 import android.content.ContextWrapper; 8 import android.view.View; 9 import android.widget.RemoteViews; 10 import android.widget.TextView; 11 import com.xtremelabs.robolectric.R; 12 import com.xtremelabs.robolectric.Robolectric; 13 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 14 import org.junit.Before; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 18 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 19 import static org.junit.Assert.*; 20 21 @RunWith(WithTestDefaultsRunner.class) 22 public class AppWidgetManagerTest { 23 private AppWidgetManager appWidgetManager; 24 private ShadowAppWidgetManager shadowAppWidgetManager; 25 26 @Before setUp()27 public void setUp() throws Exception { 28 appWidgetManager = AppWidgetManager.getInstance(Robolectric.application); 29 shadowAppWidgetManager = shadowOf(appWidgetManager); 30 } 31 32 @Test getInstance_shouldReturnSameInstance()33 public void getInstance_shouldReturnSameInstance() throws Exception { 34 assertNotNull(appWidgetManager); 35 assertSame(AppWidgetManager.getInstance(Robolectric.application), appWidgetManager); 36 assertSame(AppWidgetManager.getInstance(new ContextWrapper(Robolectric.application)), appWidgetManager); 37 } 38 39 @Test createWidget_shouldInflateViewAndAssignId()40 public void createWidget_shouldInflateViewAndAssignId() throws Exception { 41 int widgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); 42 View widgetView = shadowAppWidgetManager.getViewFor(widgetId); 43 44 assertEquals("Hola", ((TextView) widgetView.findViewById(R.id.subtitle)).getText()); 45 } 46 47 @Test getViewFor_shouldReturnSameViewEveryTimeForGivenWidgetId()48 public void getViewFor_shouldReturnSameViewEveryTimeForGivenWidgetId() throws Exception { 49 int widgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); 50 View widgetView = shadowAppWidgetManager.getViewFor(widgetId); 51 52 assertNotNull(widgetView); 53 assertSame(widgetView, shadowAppWidgetManager.getViewFor(widgetId)); 54 } 55 56 @Test createWidget_shouldAllowForMultipleInstancesOfWidgets()57 public void createWidget_shouldAllowForMultipleInstancesOfWidgets() throws Exception { 58 int widgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); 59 View widgetView = shadowAppWidgetManager.getViewFor(widgetId); 60 61 assertNotSame(widgetId, 62 shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main)); 63 assertNotSame(widgetView, 64 shadowAppWidgetManager.getViewFor(shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main))); 65 } 66 67 @Test shouldReplaceLayoutIfAndOnlyIfLayoutIdIsDifferent()68 public void shouldReplaceLayoutIfAndOnlyIfLayoutIdIsDifferent() throws Exception { 69 int widgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); 70 View originalWidgetView = shadowAppWidgetManager.getViewFor(widgetId); 71 assertContains("Main Layout", originalWidgetView); 72 73 appWidgetManager.updateAppWidget(widgetId, new RemoteViews("whatevs", R.layout.main)); 74 assertSame(originalWidgetView, shadowAppWidgetManager.getViewFor(widgetId)); 75 76 appWidgetManager.updateAppWidget(widgetId, new RemoteViews("whatevs", R.layout.media)); 77 assertNotSame(originalWidgetView, shadowAppWidgetManager.getViewFor(widgetId)); 78 79 View mediaWidgetView = shadowAppWidgetManager.getViewFor(widgetId); 80 assertContains("Media Layout", mediaWidgetView); 81 } 82 83 @Test getAppWidgetIds()84 public void getAppWidgetIds() { 85 int expectedWidgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); 86 87 int[] appWidgetIds = appWidgetManager.getAppWidgetIds( 88 new ComponentName( 89 SpanishTestAppWidgetProvider.class.getPackage().getName(), 90 SpanishTestAppWidgetProvider.class.getName())); 91 92 assertEquals(1, appWidgetIds.length); 93 assertEquals(expectedWidgetId, appWidgetIds[0]); 94 } 95 assertContains(String expectedText, View view)96 private void assertContains(String expectedText, View view) { 97 String actualText = shadowOf(view).innerText(); 98 assertTrue("Expected <" + actualText + "> to contain <" + expectedText + ">", actualText.contains(expectedText)); 99 } 100 101 public static class SpanishTestAppWidgetProvider extends AppWidgetProvider { onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)102 @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 103 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main); 104 remoteViews.setTextViewText(R.id.subtitle, "Hola"); 105 appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); 106 } 107 } 108 } 109