简体   繁体   中英

(Robotium) Action bar up/home button click

I use Robotium as the library for Junit test for my Android application. I wrote some tests which works well. But when I try to write a test for native ActionBar 's Up/Home button click , it failed.

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

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 I am using an Android 4.2.1 device.

I use this function:

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.

In our application Home button eventually calls NavUtils.navigateUpFromSameTask . When using

solo.clickOnActionBarHomeButton();

to click Home button, it doesn't work. However, I have noticed that solo.clickOnScreen clicks on Home button reliably. So we use this method instead of solo.clickOnActionBarHomeButton :

protected void clickOnHome() {
    solo.clickOnScreen(50, 50); //usually hits Home button
}

You can click the Up/Home button (that is the first ImageButton element in the activity) by using the following code:

solo.clickOnImageButton(0);
instrumentation.waitForIdleSync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM