2016-06-08 24 views
5

La mia attività ospita due frammenti. In onCreate() sto determinando quale frammento verrà mostrato.Attività di test e frammento specifico con espresso

@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() prendersi cura di transazioni e frammento commettere ...

Questo è il mio test di Classe:

@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())); 
} 

}

Come faccio a sapere in prova Class quale Frammento dovrebbe essere mostrato?

risposta

8

un'istanza la regola di non avviare automaticamente l'attività:

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

Quindi lanciare la vostra attività manualmente e passare un intento che siete interessati in:

Intent intent = new Intent(); 
intent.setAction(MainIntentService.ACTION_TARGET_OPENER); 
mNotesActivityTestRule.launchActivity(intent); 
Problemi correlati