1

私のアクティビティには、登録された ContextMenu を持つ GridView があります。ここで、このアクティビティをテストしたいのでperformLongClick()、GridView の最初の子で言うと、コンテキスト メニューが開きます。このメニューの最初のエントリを押したいのですが、それは可能ですか?

public class TestClass extends extends ActivityInstrumentationTestCase2<MainActivity> {
    public void testMe() {
        final GridView gv = getActivity().findViewById(R.id.some_id);
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                gv.getChildAt(0).performLongClick();
            }
        });
        getInstrumenttation().waitForIdleSync();

        //Registered Context Menu opens

        //????some method to get contextMenu and press first item
        //getActivity().getContextMenu().performClick(0); <---
    }
}
4

1 に答える 1

3

OK、MainActivity で「onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo info)」をオーバーライドして問題を解決し、メニューをグローバル変数として設定しました。その後、テスト クラスで ContextMenu を取得し、クリックを実行できます。

public void testDelete() {
    performLongClick(gv.getChildAt(1));

    final ContextMenu contextMenu = getActivity().getContextMenu();
    assertTrue(contextMenu != null);

    getActivity().runOnUiThread(new Runnable() {
        public void run() {
            contextMenu.performIdentifierAction(R.id.menuItemId, 0);
        }
    });
    getInstrumentation().waitForIdleSync();
}
于 2012-11-19T17:16:29.617 に答える