During my unit Tests I want to basically test if an Activity is started by a simple pressure on a button, that is in an HorizontalListView
. I've already succeed in starting new Activities during previous unit test but this one is not acting as wanted. I've got an assertion failure telling me that the activity that I want to start is still null.
Actually, when I launch my unit test, it seems to not perform the click. I have to perform it myself during the test by using my little finger for the test to pass.
To see if I was clicking on the right View I used Logs to see which view my test was clicking and which I actually want to click: and it seems to be the right view (the right Id, the right Position and the right View). So i think the problem isn't where I'm clicking.
The Activity i'm testing is called StudioActivity and the Activity i want to start is called AddBoasterActivity.
My Unit test method:
public void testSwitchToAddBoaster(){
assertTrue(mBoasterPreviewFragment.getWithAddButton());
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(AddBoasterActivity.class.getName(), null, false);
mStudioActivity.runOnUiThread(new Runnable() {
@Override
public void run(){
int lPosition = mBoasterPreviewFragment.getAdapter().getCount()-1;
View lView = mBoasterPreviewFragment.getBoaster().getChildAt(lPosition);
//HERE ARE LOGS TO SEE IF MY TEST IS PERFORMING
//THE CLICK ON THE RIGHT VIEW AT THE GOOD POSITION
Log.i("TEST POSITION:", ""+lPosition);
Log.i("TEST ID: ",""+lView.getId());
Log.i("TEST VIEW: ", ""+lView);
//AND IT'S THE GOOD VIEW!
mBoasterPreviewFragment.getBoaster().performItemClick(lView,lPosition,lView.getId());
}
});
AddBoasterActivity lAddBoasterActivity = (AddBoasterActivity) getInstrumentation().waitForMonitorWithTimeout(activityMonitor,5000);
assertNotNull(lAddBoasterActivity); //HERE IS THE FAILURE, THE ACTIVITY HASN'T BEEN STARTED
mWFBoasterPreviewFragment = new WeakReference<BoasterPreviewFragment>((BoasterPreviewFragment) lAddBoasterActivity.getSupportFragmentManager().findFragmentById(R.id.fragment_preview));
mBoasterPreviewFragment = mWFBoasterPreviewFragment.get();
assertNotNull(mBoasterPreviewFragment);
lAddBoasterActivity.finish();
}
The OnItemClickListener:
mBoaster.setAdapter(mAdapter);
if (withAddBoasterButton) {
mBoaster.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == mAdapter.getCount() - 1) {
//HERE IS THE CURRENT VIEW
Log.i("POSITION:", ""+position);
Log.i("ID: ",""+id);
Log.i("VIEW: ", ""+view);
Intent intent = new Intent(getActivity().getApplicationContext(), AddBoasterActivity.class);
intent.putExtra(AddBoasterActivity.EXTRA_BOASTER_SELECTED, mSelectedUsers);
intent.putExtra(AddBoasterActivity.EXTRA_IS_STREAM, isStream);
startActivityForResult(intent, AddBoasterActivity.REQUEST_CODE_ACTIVITY);
}
}});
}
Performing manually a click is quite embarrassing for tests which have to be automated. So if someone has any idea why it is acting like this.
Thanks for the help!