How to unit test Home Screen Widgets using Shadow App Widget Manager?

236 views
Skip to first unread message

Damian Mazurkiewicz

unread,
Jan 9, 2015, 8:09:00 AM1/9/15
to robol...@googlegroups.com
Hello,

I have a custom WidgetProvider class which creates widgets and I am currently trying to unit test behaviour of different methods (such as onUpdate) and layout changes.

I was able to write a test for default layout inflation but I cannot find a way to test onUpdate?

@Test
public void widget2x1DefaultLayoutTest() throws Exception {
int id = shadowAppWidgetManager.createWidget(Widget2x1.class, R.layout.view_widget2x1);

View view = shadowAppWidgetManager.getViewFor(id);
Drawable drawable = context.getResources().getDrawable(R.drawable.icon);

ImageView logo = (ImageView) view.findViewById(R.id.widget_logo);
ImageView icon = (ImageView) view.findViewById(R.id.icon);

TextView max = (TextView) view.findViewById(R.id.max);
TextView min = (TextView) view.findViewById(R.id.min);

assertThat(logo.getVisibility(), is(View.VISIBLE));

assertThat(icon.getDrawable(), is(drawable));
assertThat(icon.getVisibility(), is(View.VISIBLE));

assertThat(maxTemperature.getText().toString(), is("--"));
assertThat(maxTemperature.getVisibility(), is(View.VISIBLE));

assertThat(minTemperature.getText().toString(), is("--"));
assertThat(minTemperature.getVisibility(), is(View.VISIBLE));
}

Any help would be appreciated.

Thanks,
Damian

Erich Douglass

unread,
Jan 12, 2015, 1:11:24 PM1/12/15
to robol...@googlegroups.com
It's been awhile since I've tested a widget, but it looks like you do something like:

AppWidgetManager manager = AppWidgetManager.getInstance(Robolectric.application)
manager.updateAppWidget(id, views)

Which should call the update method on your widget.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages