I have 2 activities, the first one is the startup one which in it's create method causes the second one to be launched, always. My Robolectric tests pass fine
Activity
public class LoginActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginview);
Intent intent = new Intent(this,MainActivity.class);
startActivity( intent );
}
I know my activity works fine cause it launches in my device and on the emulator
My Robolectric tests
public void testLoginFirstTime() throws Exception
{
LoginActivity activity = new LoginActivity();
activity.onCreate(null);
assertThat(activity, new StartedMatcher(MainActivity.class));
}
My Robotium test
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
public void testLoginFirstTime() throws Exception
{
solo.assertCurrentActivity("Expected MainActivity to launch", MainActivity.class);
}
What is wrong with my robotium assertion? it always thinks the current activity is login one even though as I watch the emulator/device I can see that Robotium does actually launch the MainActivity but it doesn't appear to know that the new activity has been launched. Edit: Meant to say if I add a button to my login view and launch the new activty via a button click then Robotium performs the click and detects the new activity has been launched ok.
Edit: Looks like its a Robotium limitation http://groups.google.com/group/robotium-developers/browse_thread/thread/79a70038c16e35e6 However it still leaves me with the issue of how to test my app with robotium the same way as a user will use it, ie, not cheating and starting in a different activity :(
You need to use the constructor solo = new Solo(Instrumentation instrumentation) and then after you have created the Solo object you call getActivity() in order to start the first Activity. Then it will work.