簡體   English   中英

Robolectric測試gridview onclick開始新的活動

[英]Robolectric testing gridview onclick starting new activity

我陷入了測試,檢查下一個Activity是否已啟動但是在點擊時使用gridview。 這意味着如果單擊一個adaper,它將啟動新的Activity(DetailActivity)。 我提供了gridview適配器,它按列表收集數據。

這是完整的代碼:

@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
}

錯誤是java.lang.NullpointerException。

任何想法都會得到滿足。 謝謝。

好的。 由於這種情況經歷了不愉快的時間后,我已成功地使測試工作。 感謝@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
}

以下適用於RoboElectric 3.1.2

loginButton.callOnClick();

Intent startedIntent = shadowOf(activity).getNextStartedActivity();
ShadowIntent shadowIntent = shadowOf(startedIntent);
assertEquals(NextActivity.class.getName(), shadowIntent.getIntentClass()); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM