@UiThreadTest
  public void testMakeTweet() {
    LonelyTwitterActivity lta = (LonelyTwitterActivity) getActivity();
    int oldLength = lta.getAdapter().getCount();

    makeTweet("test string");
    ArrayAdapter<Tweet> arrayAdapter = lta.getAdapter();
    assertEquals(oldLength + 1, arrayAdapter.getCount());

    assertTrue(
        "Did you add a Tweet object?",
        arrayAdapter.getItem(arrayAdapter.getCount() - 1) instanceof Tweet);

    Tweet tweet = arrayAdapter.getItem(arrayAdapter.getCount() - 1);
    assertEquals("This is not the text we expected!", tweet.getMessage(), "test string");
  }
  public void testEditTweet() throws Exception {
    // When you call getActivity() android will start up the app and the activity
    Activity myActivity = getActivity();
    LonelyTwitterActivity activity = (LonelyTwitterActivity) myActivity;

    // Reset the app to known state
    activity.getTweets().clear();

    // Add a tweet using UI
    bodyText = activity.getBodyText();
    activity.runOnUiThread(
        new Runnable() {
          public void run() {
            bodyText.setText("So Lonely");
          }
        });
    getInstrumentation().waitForIdleSync();

    // Activate OnClickListener
    saveButton = activity.getSaveButton();
    activity.runOnUiThread(
        new Runnable() {
          public void run() {
            saveButton.performClick();
          }
        });
    getInstrumentation().waitForIdleSync();

    // Make sure the tweet was actually saved
    oldTweetsList = activity.getOldTweetsList();
    Tweet tweet = (Tweet) oldTweetsList.getItemAtPosition(0);

    assertEquals("So Lonely", tweet.getText());

    // Ensure the Tweet Editor activity has begun

    // https://developer.android.com/training/activity-testing/activity-functional-testing.html
    // Set up an ActivityMonitor
    Instrumentation.ActivityMonitor receiverActivityMonitor =
        getInstrumentation().addMonitor(EditTweetActivity.class.getName(), null, false);

    // Validate that ReceiverActivity is started
    // They Clicked Here
    // -----------------

    // Click on The Tweet To Edit
    activity.runOnUiThread(
        new Runnable() {
          public void run() {
            View v = oldTweetsList.getChildAt(0);
            oldTweetsList.performItemClick(v, 0, v.getId());
          }
        });
    getInstrumentation().waitForIdleSync();

    // -----------------
    EditTweetActivity receiverActivity =
        (EditTweetActivity) receiverActivityMonitor.waitForActivityWithTimeout(1000);
    assertNotNull("EditTweetActivity is null", receiverActivity);
    assertEquals(
        "Monitor for ReceiverActivity has not been called", 1, receiverActivityMonitor.getHits());
    assertEquals("Activity is of wrong type", EditTweetActivity.class, receiverActivity.getClass());

    // Remove the ActivityMonitor
    getInstrumentation().removeMonitor(receiverActivityMonitor);

    // test that the editor starts up with the right tweet
    textbox = receiverActivity.getTextbox();
    String text = textbox.getText().toString();

    assertEquals("So Lonely", text);

    // test that it can be edited
    activity.runOnUiThread(
        new Runnable() {
          public void run() {
            textbox.performClick();
            textbox.append("!");
          }
        });
    getInstrumentation().waitForIdleSync();

    text = textbox.getText().toString();

    assertEquals("So Lonely!", text);

    // test that we can push a save button for that tweet
    saveEdit = receiverActivity.getSaveEdit();
    activity.runOnUiThread(
        new Runnable() {
          public void run() {
            saveEdit.performClick();
          }
        });
    getInstrumentation().waitForIdleSync();

    // test that the new modified tweet was saved
    myActivity = getActivity();
    activity = (LonelyTwitterActivity) myActivity;

    assertEquals("So Lonely!", activity.getTweets().get(0).getText());

    // test that the new modified tweet is displayed on previous activity
    assertEquals("So Lonely!", activity.getTweets().get(0).getText());

    // Clean up all activities at the end of the test
    receiverActivity.finish();
    activity.finish();
  }