package edu.cmu.sv.trailscribe.tests;

import android.test.ActivityInstrumentationTestCase2;
import edu.cmu.sv.trailscribe.view.MainActivity;
import edu.cmu.sv.trailscribe.view.MapsActivity;
import android.widget.GridView;
import android.app.Instrumentation.ActivityMonitor;
import android.app.Instrumentation;
import edu.cmu.sv.trailscribe.R;

/**
 * This is a simple framework for a test of an Application.  See
 * {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on
 * how to write and extend Application tests.
 * <p/>
 * To run this test, you can type:
 * adb shell am instrument -w \
 * -e class edu.cmu.sv.trailscribe.view.MainActivityTest \
 * edu.cmu.sv.trailscribe.tests/android.test.InstrumentationTestRunner
 */
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private static final int GRIDVIEW_ELEMENTS = 4;
    private static final String LOG_TAG = "MainActivityTest";
    private static final long IDLE_SYNC_TIMEOUT = 10L;

    private MainActivity tMainActivity;
    private GridView tGridView;

    public MainActivityTest() {
        super("edu.cmu.sv.trailscribe", MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        tMainActivity = getActivity();
        tGridView = (GridView) tMainActivity
            .findViewById(R.id.main_buttongrid);
    }

    public void testPreconditions() {
        assertNotNull("tMainActivity is null", tMainActivity);
        assertNotNull("tGridView is null", tGridView);
    }

    public void testGridView_viewCount() {
        final int expected = GRIDVIEW_ELEMENTS;
        final int actual = tGridView.getAdapter().getCount();
        assertEquals(expected, actual);
    }

    public void testButton_mapsActivity() throws InterruptedException, Throwable {
        final ActivityMonitor monitor = getInstrumentation().addMonitor(MapsActivity.class.getName(), null, false);
        runTestOnUiThread(new Runnable() {
                public void run() {
                    tGridView.performItemClick(tGridView.getAdapter().getView(1, null, null),
                                               1, tGridView.getAdapter().getItemId(1));
                }
            });
        final MapsActivity activity = (MapsActivity) monitor.waitForActivityWithTimeout(1000L);
        assertNotNull("Maps activity not started", activity);
        activity.finish();
    }
}

@@ -180,24 +180,90 @@
 psActivity;%0A
+import edu.cmu.sv.trailscribe.view.SynchronizationCenterActivity;%0A
 import andro
@@ -1071,16 +1071,207 @@
 T = 10L;
+ // seconds%0A    private static final long ACTIVITY_START_TIMEOUT = 1000L; // milliseconds%0A    private static final int MAPS_GRID_BUTTON = 1;%0A    private static final int SYNC_GRID_BUTTON = 2;
 %0A%0A    pr
@@ -2379,17 +2379,32 @@
 getView(
-1
+MAPS_GRID_BUTTON
 , null, 
@@ -2457,17 +2457,32 @@
         
-1
+MAPS_GRID_BUTTON
 , tGridV
@@ -2508,17 +2508,32 @@
 tItemId(
-1
+MAPS_GRID_BUTTON
 ));%0A    
@@ -2650,21 +2650,38 @@
 Timeout(
-1000L
+ACTIVITY_START_TIMEOUT
 );%0A     
@@ -2769,11 +2769,828 @@
 );%0A    %7D
+%0A%0A    public void testButton_syncActivity() throws InterruptedException, Throwable %7B%0A        final ActivityMonitor monitor = getInstrumentation().addMonitor(SynchronizationCenterActivity.class.getName(), null, false);%0A        runTestOnUiThread(new Runnable() %7B%0A                public void run() %7B%0A                    tGridView.performItemClick(tGridView.getAdapter().getView(SYNC_GRID_BUTTON, null, null),%0A                                               SYNC_GRID_BUTTON, tGridView.getAdapter().getItemId(SYNC_GRID_BUTTON));%0A                %7D%0A            %7D);%0A        final SynchronizationCenterActivity activity = (SynchronizationCenterActivity) monitor.waitForActivityWithTimeout(ACTIVITY_START_TIMEOUT);%0A        assertNotNull(%22Synchronization center activity not started%22, activity);%0A        activity.finish();%0A    %7D
 %0A%7D%0A

Add test for sync center button