(Robotium)操作栏上/ home键点击栏上、操作、Robotium、home

2023-09-06 03:40:24 作者:野性傀儡

我用 Robotium 作为库JUnit测试我的Andr​​oid应用程序。我写了一些测试,效果很好。但是,当我尝试写一个测试本土动作条的的/ home键点击,它失败了。

我的测试code是很简单的:

My test code is very simple:

Solo solo = new Solo(getInstrumentation(), getActivity());
...
solo.clickOnActionBarHomeButton(); // I expected it will click the Up/Home button of ActionBar

我希望上面的code会点击本土动作条的向上/ Home按键,但它失败了,什么也没有发生的行动吧。为什么?

I expected the above code will click the Up/Home button of native ActionBar, but it is failed, nothing was happening on action bar. Why??

PS 我使用的是Android 4.2.1的设备。

P.S. I am using an Android 4.2.1 device.

推荐答案

我用这个功能:

public void clickOnActionBarHomeButton(Solo solo) {
    View homeView = solo.getView(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.id.home : R.id.home);
    solo.clickOnView(homeView);
}

也许这不是一个完美的决定,但它的工作原理。

Maybe it's not a perfect decision, but it works.