Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Activity and specific fragment with espresso

My Activity is hosting two Fragments. In onCreate() I am determine which fragment will be shown.

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    handleIntent(getIntent());
}


private void handleIntent(Intent intent) {
    LogUtils.d(TAG, "handleIntent action=" + intent.getAction());
    if (MainIntentService.ACTION_TARGET_OPENER.equals(intent.getAction())) {
        loadOpener();
    } else if (MainIntentService.ACTION_TARGET_LOGIN.equals(intent.getAction())) {
        loadLogin();
    } else {
        //noop
    }
}

private void loadOpener() {
    OpenerFragment openerFragment = OpenerFragment.newInstance();
    loadFragment(R.id.frame_fragment_container, openerFragment, true);
}

loadFragment() take care of transaction and commiting fragment...

This is my test Class:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginScreenTest {


@Rule
public ActivityTestRule<LoginActivity> mNotesActivityTestRule =
        new ActivityTestRule<>(LoginActivity.class);


@Test
public void clickAddNoteButton_opensAddNoteUi() throws Exception {
    onView(withId(R.id.button_login_submit)).perform(click());
    onView(withId(R.id.text_login)).check(matches(isDisplayed()));
}

}

How can I tell in test Class which Fragment should be shown?

like image 680
5er Avatar asked Jun 08 '16 08:06

5er


1 Answers

Instantiate your rule to not launch activity automatically:

    @Rule
    public ActivityTestRule<LoginActivity> mNotesActivityTestRule =
        new ActivityTestRule<>(LoginActivity.class, false, false);

Then launch your activity manually and pass in an intent that you are interested in:

Intent intent = new Intent();
intent.setAction(MainIntentService.ACTION_TARGET_OPENER);
mNotesActivityTestRule.launchActivity(intent);
like image 190
Be_Negative Avatar answered Nov 05 '22 22:11

Be_Negative