@Deprecated public void putWidgetInfo(int appWidgetId, AppWidgetProviderInfo expectedWidgetInfo) { addBoundWidget(appWidgetId, expectedWidgetInfo); }
public void addBoundWidget(int appWidgetId, AppWidgetProviderInfo providerInfo) { addInstalledProvider(providerInfo); bindAppWidgetId(appWidgetId, providerInfo.provider); widgetInfos.get(appWidgetId).info = providerInfo; }
/** * Creates a widget by inflating its layout. * * @param appWidgetProviderClass the app widget provider class * @param widgetLayoutId id of the layout to inflate * @return the ID of the new widget */ public int createWidget(Class<? extends AppWidgetProvider> appWidgetProviderClass, int widgetLayoutId) { return createWidgets(appWidgetProviderClass, widgetLayoutId, 1)[0]; }
@Test public void createWidget_shouldAllowForMultipleInstancesOfWidgets() throws Exception { int widgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); View widgetView = shadowAppWidgetManager.getViewFor(widgetId); assertNotSame(widgetId, shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main)); assertNotSame(widgetView, shadowAppWidgetManager.getViewFor(shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main))); }
/** * Triggers a reapplication of the most recent set of actions against the widget, which is what happens when the * phone is rotated. Does not attempt to simulate a change in screen geometry. * * @param appWidgetId the ID of the widget to be affected */ public void reconstructWidgetViewAsIfPhoneWasRotated(int appWidgetId) { WidgetInfo widgetInfo = widgetInfos.get(appWidgetId); widgetInfo.view = createWidgetView(widgetInfo.layoutId); widgetInfo.lastRemoteViews.reapply(context, widgetInfo.view); }
@Implementation protected boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider) { if (validWidgetProviderComponentName) { bindAppWidgetId(appWidgetId, provider); return allowedToBindWidgets; } else { throw new IllegalArgumentException("not an appwidget provider"); } }
@Implementation protected void updateAppWidget(int[] appWidgetIds, RemoteViews views) { for (int appWidgetId : appWidgetIds) { updateAppWidget(appWidgetId, views); } }
@Test public void getInstalledProviders_returnsWidgetList() throws Exception { AppWidgetProviderInfo info1 = new AppWidgetProviderInfo(); info1.label = "abc"; AppWidgetProviderInfo info2 = new AppWidgetProviderInfo(); info2.label = "def"; shadowAppWidgetManager.addInstalledProvider(info1); shadowAppWidgetManager.addInstalledProvider(info2); List<AppWidgetProviderInfo> installedProviders = appWidgetManager.getInstalledProviders(); assertEquals(2, installedProviders.size()); assertEquals(info1, installedProviders.get(0)); assertEquals(info2, installedProviders.get(1)); }
@Test public void getAppWidgetIds() { int expectedWidgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); int[] appWidgetIds = appWidgetManager.getAppWidgetIds( new ComponentName( SpanishTestAppWidgetProvider.class.getPackage().getName(), SpanishTestAppWidgetProvider.class.getName())); assertEquals(1, appWidgetIds.length); assertEquals(expectedWidgetId, appWidgetIds[0]); }
@Test public void testRefreshQuery() { RuntimeEnvironment.application.getSharedPreferences("WidgetConfiguration_" + appWidgetId, MODE_PRIVATE) .edit() .putString(RuntimeEnvironment.application.getString(R.string.pref_widget_theme), RuntimeEnvironment.application.getString(R.string.pref_widget_theme_value_light)) .putString(RuntimeEnvironment.application.getString(R.string.pref_widget_query), "Google") .apply(); widgetProvider.onReceive(RuntimeEnvironment.application, new Intent(BuildConfig.APPLICATION_ID + ".ACTION_REFRESH_WIDGET") .putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)); View view = shadowOf(widgetManager).getViewFor(appWidgetId); assertThat((TextView) view.findViewById(R.id.title)) .containsText("Google"); assertThat((TextView) view.findViewById(R.id.subtitle)) .doesNotContainText(R.string.loading_text); } }
/** * Simulates updating an {@code AppWidget} with a new set of views * * @param appWidgetId id of widget * @param views views to update */ @Implementation protected void updateAppWidget(int appWidgetId, RemoteViews views) { WidgetInfo widgetInfo = widgetInfos.get(appWidgetId); int layoutId = views.getLayoutId(); if (widgetInfo.layoutId != layoutId || alwaysRecreateViewsDuringUpdate) { widgetInfo.view = createWidgetView(layoutId); widgetInfo.layoutId = layoutId; } widgetInfo.lastRemoteViews = views; views.reapply(context, widgetInfo.view); }
@Implementation public boolean bindAppWidgetIdIfAllowed(int appWidgetId, ComponentName provider) { if (validWidgetProviderComponentName) { bindAppWidgetId(appWidgetId, provider); return allowedToBindWidgets; } else { throw new IllegalArgumentException("not an appwidget provider"); } }
@Implementation protected void updateAppWidget(int[] appWidgetIds, RemoteViews views) { for (int appWidgetId : appWidgetIds) { updateAppWidget(appWidgetId, views); } }
@Test public void bindAppWidgetId_shouldRecordAppWidgetInfo() throws Exception { ComponentName provider = new ComponentName("abc", "123"); AppWidgetProviderInfo providerInfo = new AppWidgetProviderInfo(); providerInfo.provider = provider; shadowAppWidgetManager.addInstalledProvider(providerInfo); appWidgetManager.bindAppWidgetIdIfAllowed(90210, provider); assertSame(providerInfo, appWidgetManager.getAppWidgetInfo(90210)); }
@Test public void createWidget_shouldInflateViewAndAssignId() throws Exception { int widgetId = shadowAppWidgetManager.createWidget(SpanishTestAppWidgetProvider.class, R.layout.main); View widgetView = shadowAppWidgetManager.getViewFor(widgetId); assertEquals("Hola", ((TextView) widgetView.findViewById(R.id.subtitle)).getText()); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @Before public void setUp() { widgetProvider = new WidgetProvider(); alarmManager = (AlarmManager) RuntimeEnvironment.application.getSystemService(Context.ALARM_SERVICE); jobScheduler = (JobScheduler) RuntimeEnvironment.application.getSystemService(Context.JOB_SCHEDULER_SERVICE); widgetManager = AppWidgetManager.getInstance(RuntimeEnvironment.application); appWidgetId = shadowOf(widgetManager).createWidget(WidgetProvider.class, R.layout.appwidget); }
@Test public void testUpdateBest() { RuntimeEnvironment.application.getSharedPreferences("WidgetConfiguration_" + appWidgetId, MODE_PRIVATE) .edit() .putString(RuntimeEnvironment.application.getString(R.string.pref_widget_theme), RuntimeEnvironment.application.getString(R.string.pref_widget_theme_value_dark)) .putString(RuntimeEnvironment.application.getString(R.string.pref_widget_section), RuntimeEnvironment.application.getString(R.string.pref_widget_section_value_best)) .apply(); widgetProvider.onReceive(RuntimeEnvironment.application, new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE) .putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{appWidgetId})); View view = shadowOf(widgetManager).getViewFor(appWidgetId); assertThat((TextView) view.findViewById(R.id.title)) .containsText(R.string.title_activity_best); assertThat((TextView) view.findViewById(R.id.subtitle)) .doesNotContainText(R.string.loading_text); }
public void addBoundWidget(int appWidgetId, AppWidgetProviderInfo providerInfo) { addInstalledProvider(providerInfo); bindAppWidgetId(appWidgetId, providerInfo.provider); widgetInfos.get(appWidgetId).info = providerInfo; }
/** * Creates a bunch of widgets by inflating the same layout multiple times. * * @param appWidgetProviderClass the app widget provider class * @param widgetLayoutId id of the layout to inflate * @param howManyToCreate number of new widgets to create * @return the IDs of the new widgets */ public int[] createWidgets(Class<? extends AppWidgetProvider> appWidgetProviderClass, int widgetLayoutId, int howManyToCreate) { AppWidgetProvider appWidgetProvider = ReflectionHelpers.callConstructor(appWidgetProviderClass); int[] newWidgetIds = new int[howManyToCreate]; for (int i = 0; i < howManyToCreate; i++) { View widgetView = createWidgetView(widgetLayoutId); int myWidgetId = nextWidgetId++; widgetInfos.put(myWidgetId, new WidgetInfo(widgetView, widgetLayoutId, appWidgetProvider)); newWidgetIds[i] = myWidgetId; } appWidgetProvider.onUpdate(context, realAppWidgetManager, newWidgetIds); return newWidgetIds; }
@Test public void getAppWidgetInfo_shouldReturnSpecifiedAppWidgetInfo() throws Exception { AppWidgetProviderInfo expectedWidgetInfo = new AppWidgetProviderInfo(); shadowAppWidgetManager.addBoundWidget(26, expectedWidgetInfo); assertEquals(expectedWidgetInfo, appWidgetManager.getAppWidgetInfo(26)); assertEquals(null, appWidgetManager.getAppWidgetInfo(27)); }