Roboelectric test gridview onclick starts new activity
I am stuck creating a test that checks if the next activity is fired, but using a gridview on click. This means that if you click one click, it will launch a new Activity (DetailActivity). I am providing a gridview adapter that collects data from a list.
Here's the complete code:
@Test
public void shouldDisplayDetailActivityWhenAdapterClicked() throws Exception{
List<ImageNode> nodes = new ArrayList<ImageNode>();
ImageNode node = new ImageNode();
node.setId(36597698);
node.setContributorId("halfpoint");
node.setFileName("halfpoint150200457");
node.setFolder("halfpoint1502");
node.setDescription("halfpoint1502");
node.setMediaType("halfpoint1502");
node.setUrlThumb(URLHelper.buildThumbUrl(
node.getId(),
node.getContributorId(),
node.getFolder(),
node.getFileName(),
node.getDescription()));
node.setUrlFullSize(URLHelper.buildFullSizeUrl(node.getUrlThumb()));
nodes.add(node);
DetailLikeBoxAdapter mAdapter = new DetailLikeBoxAdapter(activity, nodes);
GridView gridView = (GridView) activity.findViewById(R.id.likebox_gridview);
View itemView = mAdapter.getView(0, null, gridView);
gridView.performItemClick(itemView, 0, mAdapter.getItemId(0));
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_KEY, node.getId());
startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_POS, 0);
startedIntent.putExtra(CommonConstants.DETAIL_IMAGE_URLFULLSIZES,node.getUrlFullSize() );
startedIntent.putExtra(CommonConstants.IS_BUILD_CATEGORY, false);// get intent of next activity on stack
ShadowIntent shadowIntent = shadowOf(startedIntent); // create shadow intent which starts next activity
assertEquals(DetailActivity.class.getName(), shadowIntent.getComponent().getClassName()); // compare shadow intent w/ desired next activity
}
Error: java.lang.NullpointerException.
any ideas will be suggested. Thank.
+3
source to share
2 answers
Good. After unsuccessful due to this case, I passed the test successfully. Follow @Eugen Martynov.
@Test
public void shouldDisplayDetailActivityWhenAdapterClicked() throws Exception{
List<ImageNode> nodes = new ArrayList<ImageNode>();
ImageNode node = new ImageNode();
node.setId(36597698);
node.setContributorId("halfpoint");
node.setFileName("halfpoint150200457");
node.setFolder("halfpoint1502");
node.setDescription("halfpoint1502");
node.setMediaType("halfpoint1502");
node.setUrlThumb(URLHelper.buildThumbUrl(
node.getId(),
node.getContributorId(),
node.getFolder(),
node.getFileName(),
node.getDescription()));
node.setUrlFullSize(URLHelper.buildFullSizeUrl(node.getUrlThumb()));
nodes.add(node);
DetailLikeBoxAdapter mAdapter = new DetailLikeBoxAdapter(activity, nodes);
GridView gridView = (GridView) activity.findViewById(R.id.likebox_gridview);
gridView.setAdapter(mAdapter);
View itemView = mAdapter.getView(0, null, gridView);
gridView.performItemClick(itemView, 0, mAdapter.getItemId(0));
Intent startedIntent = shadowOf(activity).getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent); // create shadow intent which starts next activity
System.out.println(DetailActivity.class.getName()+" "+shadowIntent.getComponent().getClassName() );
assertEquals(DetailActivity.class.getName(), shadowIntent.getComponent().getClassName()); // compare shadow intent w/ desired next activity
}
0
source to share