package cgeo.geocaching.files;

import com.jayway.android.robotium.solo.Solo;

import android.annotation.TargetApi;
import android.os.Build;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox;

import java.util.ArrayList;

@TargetApi(Build.VERSION_CODES.FROYO)
public class SimpleDirChooserUITest extends ActivityInstrumentationTestCase2<SimpleDirChooser> {

    private Solo solo;

    public SimpleDirChooserUITest() {
        super(SimpleDirChooser.class);
    }

    @Override
    public void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testSingleSelection() throws InterruptedException {
        assertChecked("Newly opened activity", 0);
        solo.scrollToBottom();
        final int lastIndex = solo.getCurrentCheckBoxes().size() - 1;

        solo.clickOnCheckBox(lastIndex);
        assertTrue(solo.getCurrentCheckBoxes().get(lastIndex).isChecked());
        assertFalse(solo.getCurrentCheckBoxes().get(0).isChecked());
        assertChecked("Clicked last checkbox", 1);

        solo.scrollUp();
        Thread.sleep(20);
        solo.scrollToBottom();
        Thread.sleep(20);
        assertChecked("Refreshing last checkbox", 1);

        solo.scrollToTop();
        solo.clickOnCheckBox(0);
        assertChecked("Clicked first checkbox", 1);
        assertTrue(solo.getCurrentCheckBoxes().get(0).isChecked());
        solo.clickOnCheckBox(1);
        assertChecked("Clicked second checkbox", 1);
        assertTrue(solo.getCurrentCheckBoxes().get(1).isChecked());
    }

    private void assertChecked(String message, int expectedChecked) {
        int checked = 0;
        final ArrayList<CheckBox> boxes = solo.getCurrentCheckBoxes();
        assertNotNull("Could not get checkboxes", boxes);
        assertTrue("There are no checkboxes", boxes.size() > 1);
        for (CheckBox checkBox : boxes) {
            if (checkBox.isChecked()) {
                checked++;
            }
        }
        assertEquals(message, expectedChecked, checked);
    }

}

@@ -762,32 +762,229 @@
 rollToBottom();%0A
+        pause();%0A        // according to the documentation, automatic pauses only happen in the clickXYZ() methods.%0A        // Therefore lets introduce a manual pause after the scrolling methods.%0A%0A
         final in
@@ -1037,16 +1037,16 @@
 () - 1;%0A
-
 %0A       
@@ -1305,39 +1305,30 @@
 ();%0A        
-Thread.sleep(20
+pause(
 );%0A        s
@@ -1361,23 +1361,14 @@
     
-Thread.sleep(20
+pause(
 );%0A 
@@ -1445,24 +1445,41 @@
 ollToTop();%0A
+        pause();%0A
         solo
@@ -1697,32 +1697,32 @@
  checkbox%22, 1);%0A
-
         assertTr
@@ -1776,24 +1776,120 @@
 ());%0A    %7D%0A%0A
+    private static void pause() throws InterruptedException %7B%0A        Thread.sleep(500);%0A    %7D%0A%0A
     private 

introduce pause after scrolling in UI test