package ca.ualberta.cs.lonelytwitter.test;

import android.app.Activity;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import ca.ualberta.cs.lonelytwitter.LonelyTwitterActivity;
import ca.ualberta.cs.lonelytwitter.NormalTweetModel;

public class LonelyTwitterActivityUITest extends
		ActivityInstrumentationTestCase2<LonelyTwitterActivity> {

	Instrumentation instrumentation;
	Activity activity;
	EditText textInput;
	
	public LonelyTwitterActivityUITest() {
		super(LonelyTwitterActivity.class);
	}

	protected void setUp() throws Exception {
		super.setUp();
		instrumentation = getInstrumentation();
		activity = getActivity();
		textInput = ((EditText) activity.findViewById(ca.ualberta.cs.lonelytwitter.R.id.body));
	}
	
	 //makeTweet(text) fills in the input text field and clicks the 'save' button for the activity under test:
	private void makeTweet(String text) {
		assertNotNull(activity.findViewById(ca.ualberta.cs.lonelytwitter.R.id.save));
		textInput.setText(text);
		((Button) activity.findViewById(ca.ualberta.cs.lonelytwitter.R.id.save)).performClick();
	}
	
	//To avoid error: "CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."
	//That's because "Activities" run on the UI thread. Using this annotation, all the method statements will run on the UI thread:
	@UiThreadTest
	public void testMakeTweet(){
		LonelyTwitterActivity activity = getActivity(); //Get the Activity under test, starting it if necessary. For each test method invocation, the Activity will not actually be created until the first time this method is called
		int old_length = activity.getAdapter().getCount();

		makeTweet("Hello...");
		ArrayAdapter<NormalTweetModel> adapter = activity.getAdapter();
		assertEquals("Length of array adapter is not right!", old_length+1, adapter.getCount());
		
		assertEquals("Did you add NormalTweetModel? It's not that!", true, (adapter.getItem(adapter.getCount() - 1) instanceof NormalTweetModel));
		
		NormalTweetModel tweet = adapter.getItem(adapter.getCount() - 1);
		assertEquals("This is not the text we expected!", tweet.getText(), "Hello...");		
	}
}

@@ -1250,1072 +1250,38 @@
 %7D%0A%09%0A
-%09
 //T
-o avoid error: %22CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.%22%0A%09//That's because %22Activities%22 run on the UI thread. Using this annotation, all the method statements will run on the UI thread:%0A%09@UiThreadTest%0A%09public void testMakeTweet()%7B%0A%09%09LonelyTwitterActivity activity = getActivity(); //Get the Activity under test, starting it if necessary. For each test method invocation, the Activity will not actually be created until the first time this method is called%0A%09%09int old_length = activity.getAdapter().getCount();%0A%0A%09%09makeTweet(%22Hello...%22);%0A%09%09ArrayAdapter%3CNormalTweetModel%3E adapter = activity.getAdapter();%0A%09%09assertEquals(%22Length of array adapter is not right!%22, old_length+1, adapter.getCount());%0A%09%09%0A%09%09assertEquals(%22Did you add NormalTweetModel? It's not that!%22, true, (adapter.getItem(adapter.getCount() - 1) instanceof NormalTweetModel));%0A%09%09%0A%09%09NormalTweetModel tweet = adapter.getItem(adapter.getCount() - 1);%0A%09%09assertEquals(%22This is not the text we expected!%22, tweet.getText(), %22Hello...%22);%09%09%0A%09%7D
+ODO: Add your code here:%0A%09%09%09
 %0A%7D%0A

Update LonelyTwitterActivityUITest.java