???????????
????@MediumTest
????public void testClickMeButton_layout() {
????//???Activity???????
????View decorView = mSecondTestUIActivity.getWindow().getDecorView();
????//?????????????????
????ViewAsserts.assertOnScreen(decorView?? mClickMeButton);
????ViewGroup.LayoutParams layoutParams = mClickMeButton.getLayoutParams();
????assertNotNull(layoutParams);
????//???????????WRAP_CONTENT
????assertEquals(layoutParams.width?? WindowManager.LayoutParams.WRAP_CONTENT);
????//???????????WRAP_CONTENT
????assertEquals(layoutParams.height?? WindowManager.LayoutParams.WRAP_CONTENT);
????}
??????????Ч??
????@MediumTest
????public void testClickMeButton_click() {
????TouchUtils.clickView(this?? mClickMeButton);
????}
???????????
????@SmallTest
????????ò????????С????????????
????@MediumTest
????????ò?????????е???????????
????@LargeTest
????????ò?????????????????????
???????????£??????????????????????????????????????@SmallTest??????????е?????100?????????????????@MediumTest??@LargeTest???????????????????????????????????????
???????????????
????Android ???????

????????????ü????ActivityUnitTestCase?????ActivityUnitTestCase??Activity????Android????????????????Activity?????????????????startActivity()???????????????Intent?????????????Activity??
public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
private Intent mLaunchIntent;
public LaunchActivityTest() {
super(LaunchActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mLaunchIntent = new Intent(getInstrumentation().getTargetContext()??
LaunchActivity.class);
}
@MediumTest
public void testPreconditions() {
startActivity(mLaunchIntent?? null?? null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
assertNotNull("mLaunchActivity is null"?? getActivity());
assertNotNull("mLaunchNextButton is null"?? launchNextButton);
}
??????????Activity
????Button??????????????LaunchActivity????????
????????????Intent????????Ч???????
?????????????????Intent??Button?????????????????getStartedActivityIntent()???????????????????????????????????Intent????????????????????????????????????Activity???????????????????棬????????????????Activity?????Intent??????????
????@MediumTest
????public void testNextActivityWasLaunchedWithIntent() {
????startActivity(mLaunchIntent?? null?? null);
????final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
????launchNextButton.performClick();
????final Intent launchIntent = getStartedActivityIntent();
????assertNotNull("Intent was null"?? launchIntent);
????assertTrue(isFinishCalled());
????final String payload = launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY);
????assertEquals("Payload is empty"?? LaunchActivity.STRING_PAYLOAD
?????? payload);
????}
???????????
??????Activity???????????????????????ActivityInstrumentationTestCase2???????????ActivityUnitTestCase?????ActivityInstrumentationTestCase2?е?????????Android?????????????????????????UI?С?
???????????????????????????ο????????е?SenderActivityTest.java??
?????????????????????????
?????????????????????e???:
???????UI??????????????????Activity??
??????????Activity?????????????Activity????????????
????????????????????????:
????@MediumTest
????public void testSendMessageToReceiverActivity() {
????final Button sendToReceiverButton = (Button)
????mSenderActivity.findViewById(R.id.send_message_button);
????final EditText senderMessageEditText = (EditText)
????mSenderActivity.findViewById(R.id.message_input_edit_text);
????// Set up an ActivityMonitor
????...
????// Send string input value
????...
????// Validate that ReceiverActivity is started
????...
????// Validate that ReceiverActivity has the correct data
????...
????// Remove the ActivityMonitor
????...
????}
???????????????Activity????????????????null?????ReceiverActivity?????????????????ActivityMoniter?????????????Hit???????????????????????ReceiverActivity??????????????ActivityMoniter??????????????????????????????
???????????ActivityMonitor
?????????????м??????Activity?????????????ActivityMoniter????????????????Activity????????????ActivityMoniter??????????????????
??????????????ActivityMoniter???????????????
???????getInstrumentation()????????????????Instrumentation??
???????Instrumentation?????addMonitor()????????instrumentation??????Instrumentation.ActivityMonitor?????????????????IntentFilter???????????????
??????????????Activity??
???????????????????????????
???????????????
?????????????????:
// Set up an ActivityMonitor
ActivityMonitor receiverActivityMonitor =
getInstrumentation().addMonitor(ReceiverActivity.class.getName()??
null?? false);
// Validate that ReceiverActivity is started
TouchUtils.clickView(this?? sendToReceiverButton);
ReceiverActivity receiverActivity = (ReceiverActivity)
receiverActivityMonitor.waitForActivityWithTimeout(TIMEOUT_IN_MS);
assertNotNull("ReceiverActivity is null"?? receiverActivity);
assertEquals("Monitor for ReceiverActivity has not been called"??
1?? receiverActivityMonitor.getHits());
assertEquals("Activity is of wrong type"??
ReceiverActivity.class?? receiverActivity.getClass());
// Remove the ActivityMonitor
getInstrumentation().removeMonitor(receiverActivityMonitor);
???????Instrumentation???????????????
???????Activity?????EditText??????????????????????EditText?????????????
?????????ActivityInstrumentationTestCase2?и?EditText???????????????????????????
???????runOnMainSync()??????????????????????requestFocus()?????????????UI???????y??????????????
????????waitForIdleSync()??????????????У?????????и????????????????
????????sendStringSync()??????EditText?????????????????????????
????????:
// Send string input value
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
senderMessageEditText.requestFocus();
}
});
getInstrumentation().waitForIdleSync();
getInstrumentation().sendStringSync("Hello Android!");
getInstrumentation().waitForIdleSync();