@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"); }
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { tweetList = new ArrayList<Tweet>(); Tweet importantTweet = new ImportantTweet(""); try { importantTweet.setTweet("Hah!"); // controller tweetList.add(importantTweet); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } super.onCreate(savedInstanceState); setContentView(R.layout.main); // view bodyText = (EditText) findViewById(R.id.body); // model saveButton = (Button) findViewById(R.id.save); oldTweetsList = (ListView) findViewById(R.id.oldTweetsList); // view saveButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { setResult(RESULT_OK); // model String text = bodyText.getText().toString(); // model saveInFile(text, new Date(System.currentTimeMillis())); // controller finish(); // controller } }); oldTweetsList.setOnItemClickListener( new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(LonelyTwitterActivity.this, EditTweetActivity.class); startActivity(i); } }); }
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(); }
/** * Compares the tweet objects * * @param o A tweet object * @return -1 for < than, 0 for equal, 1 for greater **I think */ public int compareTo(Tweet o) { return getDate().compareTo(o.getDate()); }