有什么方法可以获取 Android 中 Junit 测试的 Actionbar 项目的参考吗?

发布于 2024-12-26 00:20:35 字数 61 浏览 0 评论 0原文

如何为 android 中的操作栏项目编写 junit 测试用例?有什么方法可以获取其执行点击事件的参考吗?

How do I write junit test cases for actionbar items in android ? Any way of getting its reference for performing click events on it ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾城花音 2025-01-02 00:20:35

您可以像这样模拟单击 ActionBar 项目:

public void testButton(){
    final View view = activity.findViewById(com.example.R.id.button1);
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            view.requestFocus();
            view.callOnClick();
        }
    });
}

You can simulate clicking an ActionBar item like this:

public void testButton(){
    final View view = activity.findViewById(com.example.R.id.button1);
    activity.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            view.requestFocus();
            view.callOnClick();
        }
    });
}
是伱的 2025-01-02 00:20:35

在下面的示例中,我能够检索操作栏(本机或 ActionBarSherlock)的导航选项卡按钮。然后我用 TouchUtils.clickView() 单击它们:

try {

// Trying to get the ActionBar view '@id/android:action_bar_container' dynamically
int resId =
a.getResources().getIdentifier("action_bar_container", "id", "android");
View actionBarContainer = a.findViewById(resId);

// The class 'com.android.internal.widget.ActionBarContainer' must be in
// the classpath of this test project to be able to call
// the method 'getTabContainer' at runtime
Method getTabContainer =
com.android.internal.widget.ActionBarContainer.class.getMethod("getTabContainer",
(Class<?>[]) null);

HorizontalScrollView tabContainer =
(HorizontalScrollView) getTabContainer.invoke(actionBarContainer, (Object[]) null);
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);

} catch (Exception e) {

// Trying with SherlockActionBar
com.actionbarsherlock.internal.widget.ActionBarContainer actionBarContainer =
(com.actionbarsherlock...) a.findViewById(R.id.abs__action_bar_container);

HorizontalScrollView tabContainer =
(HorizontalScrollView) actionBarContainer.getTabContainer();
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);

}
}

In this following example, i'm able to retrieve the navigation tab button of the action bar (native or ActionBarSherlock). Then i click on them with TouchUtils.clickView():

try {

// Trying to get the ActionBar view '@id/android:action_bar_container' dynamically
int resId =
a.getResources().getIdentifier("action_bar_container", "id", "android");
View actionBarContainer = a.findViewById(resId);

// The class 'com.android.internal.widget.ActionBarContainer' must be in
// the classpath of this test project to be able to call
// the method 'getTabContainer' at runtime
Method getTabContainer =
com.android.internal.widget.ActionBarContainer.class.getMethod("getTabContainer",
(Class<?>[]) null);

HorizontalScrollView tabContainer =
(HorizontalScrollView) getTabContainer.invoke(actionBarContainer, (Object[]) null);
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);

} catch (Exception e) {

// Trying with SherlockActionBar
com.actionbarsherlock.internal.widget.ActionBarContainer actionBarContainer =
(com.actionbarsherlock...) a.findViewById(R.id.abs__action_bar_container);

HorizontalScrollView tabContainer =
(HorizontalScrollView) actionBarContainer.getTabContainer();
return ((ViewGroup) tabContainer.getChildAt(0)).getChildAt(tabIndex);

}
}
定格我的天空 2025-01-02 00:20:35

使用robotium.jar库

import com.jayway.android.robotium.solo.Solo;

private Solo solo;
this.solo = new Solo(getInstrumentation(),getActivity());
//R.id.menu_action_signup Menu Iten id.
this.solo.clickOnView(this.solo.getView(R.id.menu_action_signup));

use robotium.jar library

import com.jayway.android.robotium.solo.Solo;

private Solo solo;
this.solo = new Solo(getInstrumentation(),getActivity());
//R.id.menu_action_signup Menu Iten id.
this.solo.clickOnView(this.solo.getView(R.id.menu_action_signup));
稚气少女 2025-01-02 00:20:35

我通过创建自己的自定义 MenuItem 类并手动为 Activity 调用 onOptionsItemSelected(MenuItem item) 来解决。
对于 Junit 测试还有其他正确的方法吗?

I resolved by creating my own custom MenuItem class and calling onOptionsItemSelected(MenuItem item) for the Activity manually.
Any other proper way for doing this for Junit testing ?

山有枢 2025-01-02 00:20:35

我正在使用 ActionBarSherlock 提供的 ActionBar 并遇到了这个问题。我发现执行此操作的最佳方法是:

  1. 在您的测试活动中保留在 onCreateOptionsMenu 中传递的 Menu 对象的实例,并使其可供您的测试用例访问。
  2. 在您的测试用例中,您需要引用您的 Instrumentation 和菜单,以便您可以通过 id 选择 MenuItem:

    private void clickOnMenuItem(int menuItemId, 仪器仪表, 菜单 menuInstance) {
    最终整数 itemId = menuItemId;
    最终菜单菜单= menuInstance;
    Instruments.runOnMainSync(new Runnable() {
        @覆盖
        公共无效运行(){
            菜单.performIdentifierAction(itemId, 0);
        }
    });
    

    }

I'm using an ActionBar provided by ActionBarSherlock and ran into this issue. The best way I've found to do this is:

  1. In your test activity retain an instance of the Menu object passed in onCreateOptionsMenu and make this accessible to your test case.
  2. In your test case you will need a reference to your Instrumentation and the Menu allowing you to select your MenuItem by id:

    private void clickOnMenuItem(int menuItemId, Instrumentation instruments, Menu menuInstance) {
    final Integer itemId = menuItemId;
    final Menu menu = menuInstance;
    instruments.runOnMainSync(new Runnable() {
        @Override
        public void run() {
            menu.performIdentifierAction(itemId, 0);
        }
    });
    

    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文