package com.pennapps.labs.pennmobile;

import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.view.ContextThemeWrapper;
import android.widget.ListView;

import com.pennapps.labs.pennmobile.api.Labs;
import com.pennapps.labs.pennmobile.classes.Course;
import com.pennapps.labs.pennmobile.classes.NewDiningHall;
import com.pennapps.labs.pennmobile.classes.Person;
import com.pennapps.labs.pennmobile.classes.Venue;

import java.util.List;

/**
 * Unit testing for MainActivity
 * Created by Adel on 12/9/14.
 */
public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {
    private MainActivity activity;

    public MainActivityTest() {
        super(MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        ContextThemeWrapper context = new ContextThemeWrapper(getInstrumentation().getTargetContext(), R.style.AppTheme);
        setActivityContext(context);
        Intent intent = new Intent(getInstrumentation().getTargetContext(),
                MainActivity.class);
        startActivity(intent, null, null);
        activity = getActivity();
    }

    public void testMainLayout() {
        assertNotNull(activity.findViewById(R.id.left_drawer));
    }

    public void testMainDrawerLayout() {
        assertTrue(activity.findViewById(R.id.left_drawer) instanceof ListView);
    }

    public void testLabsCoursesAPI() {
        Labs mLabs = activity.getLabsInstance();
        List<Course> courses = mLabs.courses("CIS 110");
        assertEquals(110, courses.get(0).course_number);
    }

    public void testLabsCoursesInstructors() {
        Labs mLabs = activity.getLabsInstance();
        List<Course> courses = mLabs.courses("CIS 110");
        assertEquals("Eric Eaton", courses.get(0).instructors.get(0).name);
    }

    public void testCourseMeetings() {
        Labs mLabs = activity.getLabsInstance();
        List<Course> courses = mLabs.courses("CIS 110");
        assertEquals("Towne Building", courses.get(0).meetings.get(0).building_name);
    }

    public void testDirectorySearch() {
        Labs mLabs = activity.getLabsInstance();
        List<Person> people = mLabs.people("adel");
        assertEquals("ADELMAN, STEPHEN R", people.get(0).name);
    }

    public void testDirectorySearchGetName() {
        Labs mLabs = activity.getLabsInstance();
        List<Person> people = mLabs.people("adel");
        assertEquals("Stephen R Adelman", people.get(0).getName());
    }

    public void testCourseMeetingSection() {
        Labs mLabs = activity.getLabsInstance();
        List<Course> courses = mLabs.courses("BIBB 109");
        assertEquals("BIBB109401", courses.get(0).meetings.get(0).section_id);
    }

    public void testDiningVenues() {
        Labs mLabs = activity.getLabsInstance();
        List<Venue> venues = mLabs.venues();
        assertEquals("1920 Commons", venues.get(0).name);
    }

    public void testDiningMenuMeals() {
        Labs mLabs = activity.getLabsInstance();
        List<Venue> venues = mLabs.venues();
        NewDiningHall commons = mLabs.daily_menu(venues.get(0).id);
        assertTrue(commons.menus.size() > 0);
    }
}

@@ -1530,32 +1530,70 @@
 urses(%22CIS 110%22)
+.toList().toBlocking().single().get(0)
 ;%0A        assert
@@ -1785,32 +1785,70 @@
 urses(%22CIS 110%22)
+.toList().toBlocking().single().get(0)
 ;%0A        assert
@@ -1859,17 +1859,21 @@
 ls(%22
-Eric Eato
+Benedict Brow
 n%22, 
@@ -2067,46 +2067,116 @@
 10%22)
-;%0A        assertEquals(%22Towne Building
+.toList().toBlocking().single().get(0);%0A        // Course locations not announced yet%0A        assertEquals(%22
 %22, c
@@ -2359,32 +2359,70 @@
 s.people(%22adel%22)
+.toList().toBlocking().single().get(0)
 ;%0A        assert
@@ -2624,16 +2624,54 @@
 (%22adel%22)
+.toList().toBlocking().single().get(0)
 ;%0A      
@@ -2889,16 +2889,54 @@
 BB 109%22)
+.toList().toBlocking().single().get(0)
 ;%0A      

Fix tests for RxJava