package com.teamname.tutortrader;

import android.app.Activity;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by taylorarnett on 2016-02-10.
 */
public class TutorTraderActivityTest extends ActivityInstrumentationTestCase2 {

    Instrumentation instrumentation;
    Activity activity;
    EditText titleInput;
    EditText descriptionInput;
    EditText searchInput;

    public TutorTraderActivityTest(Class activityClass) {
        super(activityClass);
    }
    /**
     * Testing "Things" Use Cases
     */

    /**
     * Testing UseCase 01.01.01 - AddSession
     * "As an owner, I want to add a thing in my things, each denoted with a clear,
     *  suitable description."
     */

    /** USECASE 1 - AddSession
     *  createSession(title, description) fills in the input text field and
     *  clicks the 'save' button for the activity under test:
     * @param title a string input of the subject of the tutoring session
     * @param description a string input of the description of the tutoring session.
     */
    private void createSession(String title, String description) {
        assertNotNull(activity.findViewById(com.teamname.tutortrader.R.id.save));
        titleInput.setText(title);
        descriptionInput.setText(description);
        ((Button) activity.findViewById(com.teamname.tutortrader.R.id.save)).performClick();
    }

    /** USECASE 1 = AddSession
     * Testing if a session is added after all text fields are inputted correctly.
     * This Tests the UI
     */
    public void testAddSessionValid() {
        TutorTradeActivity tta = (TutorTradeActivity)getActivity();
        int oldLength = tta.getAdapter().getCount();
        createSession("Math", "Tutor for linear Algebra for all university levels");
        ArrayAdapter<Session> arrayAdapter = tta.getAdapter();
        assertEquals(oldLength +1, arrayAdapter.getCount());

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

        Session session = arrayAdapter.getItem(arrayAdapter.getCount()-1);
        assertEquals("this is the title we expected", session.getTitle(), "Math");
        assertEquals("this is the description we expected", session.getDescription(),
                "Tutor for linear Algebra for all university levels");
    }

    /** USECASE 1 = AddSession
     * This tests use case 1 but in the case where the input fields are incomplete
     * This tests the UI
     */
    public void testAddSessionIncomplete() {
        TutorTradeActivity tta = (TutorTradeActivity)getActivity();
        int oldLength = tta.getAdapter().getCount();
        createSession("Math", null);
        ArrayAdapter<Session> arrayAdapter = tta.getAdapter();
        assertEquals(oldLength, arrayAdapter.getCount());

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

    }

    /**
     * Testing UseCase 01.02.01 - ViewSessions
     * "As an owner, I want to view a list of all my sessions, and their descriptions and statuses."
     *
     */
     
     


    /**
    *	US 04.01.01 - As a borrower, I want to specify a set of keywords, and search for all things not currently
    *                 borrowed whose description contains all keywords. 
    *	Assumes that the search bar exists in the main Activity and does not have its own View.
    *	Assumes the search updates the existing Array adapter with the new search information
    *
    */
    private void createSearch(String searchText){
	    assertNotNull(activity.findViewById(com.teamneam.tutortrader.R.id.search));
	    searchInput.setText(searchText);
	    ((Button) activity.findViewById(com.teamneam.tutortrader.R.id.search)).performClick();
    }

    public 	void testSearchOneWord(){
	    TutorTradeAcitivity tta = (TutorTradeAcitivity)getActivity();
	    createSession("Math", "Tutor for highschool math classes.");
	    createSession("Math", "Tutor for university math classes.");
	    
	    createSearch("highschool")

	    ArrayAdapter<Session> arrayAdapter = tta.getAdapter();
	    assertEquals(1, arrayAdapter.getCount()); 
	
    	Session session = arrayAdapter.getItem(arrayAdapter.getCount()-1);
    	assertEquals("Should be math title",session.getTitle(), "Math");
	    assertEquals("Should be the description for highschool math session", session.getDescription(), "Tutor for highschool math classes.");	
    }

    
    /**
    *	how do we want the search to deal with multiple words?
    *	do we look for partial matches or ignore those?
    *	do we look for exact matches or just descriptions with two matching words?
    * 	test assumes search words individualy.
    **/
    public void testSearchTwoWords(){
    	TutorTradeAcitivity tta = (TutorTradeAcitivity)getActivity();
    	createSession("Math", "Tutor for highschool classes.");
    	createSession("Math", "Tutor for university math classes.");
    	createSession("Biology", "Tutor for biology 101, 102");
    	
    	createSearch("highschool math");
    
    	ArrayAdapter<Session> arrayAdapter = tta.getAdapter();
    	assertEquals(2, arrayAdapter.getCount()); 
    	
    	Session session = arrayAdapter.getItem(arrayAdapter.getCount()-1);
    	assertNotEquals("Should not be Bio title",session.getTitle(), "Biology");
    	session = arrayAdapter.getItem(arrayAdapter.getCount()-2);
    	assertNotEquals("Should not be Bio title",session.getTitle(), "Biology")
    }

    /**
    *	check that empty list is empty.
    *	
    *
    */
    public void testSearchEmpty(){
    	TutorTradeAcitivity tta = (TutorTradeAcitivity)getActivity();
    	createSession("Math", "Tutor for highschool math classes.");
    	
    	createSearch("nothing");
    	
    	ArrayAdapter<Session> arrayAdapter = tta.getAdapter();
    	assertEquals(0, arrayAdapter.getCount()); 
    	
    }
         
         
}

@@ -3700,21 +3700,21 @@
 hText)%7B%0A
-%09
     
+%09
 assertNo
@@ -3781,21 +3781,21 @@
 arch));%0A
-%09
     
+%09
 searchIn
@@ -3820,20 +3820,16 @@
 Text);%0A%09
-    
 ((Button
@@ -3925,17 +3925,16 @@
  public 
-%09
 void tes
@@ -3952,20 +3952,16 @@
 ord()%7B%0A%09
-    
 TutorTra
@@ -4011,28 +4011,24 @@
 ctivity();%0A%09
-    
 createSessio
@@ -4077,20 +4077,16 @@
 es.%22);%0A%09
-    
 createSe
@@ -4143,18 +4143,8 @@
 );%0A%09
-    %0A%09    
 crea
@@ -4168,20 +4168,16 @@
 ool%22)%0A%0A%09
-    
 ArrayAda
@@ -4220,28 +4220,24 @@
 Adapter();%0A%09
-    
 assertEquals
@@ -4268,22 +4268,16 @@
 t()); %0A%09
-%0A    %09
 Session 
@@ -4406,20 +4406,16 @@
 ath%22);%0A%09
-    
 assertEq

Update TutorTraderActivityTest.java