package com.leandog.brazenhead;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import android.app.Activity;
import android.content.res.Resources;
import android.widget.Spinner;

import com.apple.jobjc.ID;
import com.jayway.android.robotium.solo.By;
import com.jayway.android.robotium.solo.Solo;
import com.leandog.brazenhead.test.BrazenheadTestRunner;

@RunWith(BrazenheadTestRunner.class)
public class BrazenheadTest {
    
    @Mock Solo solo;
    @Mock Activity activity;
    @Mock Resources resources;
    @Mock SpinnerPresser spinnerPresser;
    @Mock ListItemFinder listItemFinder;
    @Mock ListItemPresser listItemPresser;

    private Brazenhead brazenhead;

    @Before
    public void setUp() throws IOException {
        initMocks();
        brazenhead = new Brazenhead(spinnerPresser, listItemFinder, listItemPresser);
    }

    @Test
    public void itCanFindAnIdFromTheTargetPackageResources() {
        when(activity.getPackageName()).thenReturn("com.some.package");

        brazenhead.idFromName("some_id");

        verify(resources).getIdentifier("some_id", "id", "com.some.package");
    }

    @Test
    public void itCanPressSpinnersById() {
        brazenhead.pressSpinnerItemById(123, 7);
        verify(spinnerPresser).pressSpinnerItemById(123, 7);
    }

    @Test
    public void itCanPressSpinnersByView() {
        final Spinner spinner = mock(Spinner.class);
        brazenhead.pressSpinnerItem(spinner, 7);
        verify(spinnerPresser).pressSpinnerItem(spinner, 7);
    }
    
    @Test
    public void itCanLocateListItemsByText() throws Exception {
        brazenhead.listItemByText("Some text");
        verify(listItemFinder).findByText("Some text");
    }
    
    @Test
    public void itCanLocateListItemsByIndexInTheDefaultList() {
        brazenhead.listItemByIndex(7);
        verify(listItemFinder).findByIndex(7);
    }
    
    @Test
    public void itCanLocateListItemsByIndex() {
        brazenhead.listItemByIndex(7, 1);
        verify(listItemFinder).findByIndex(7, 1);
    }
    
    @Test
    public void itCanPressListItemsByIndex() throws Exception {
        brazenhead.pressListItemByIndex(7);
        verify(listItemPresser).pressListItem(7);
    }
    
    @Test
    public void itCanPressASpecificListsItems() {
        int whichList = 1;
        brazenhead.pressListItemByIndex(7, whichList);
        verify(listItemPresser).pressListItem(7, whichList);
    }
    
    @Test
	public void itCanFindWebViewsByVariousProperties() {
		final HashMap<String, String> hows = new HashMap<String, String>() {
			{
				put("id", "Id");
				put("xpath", "Xpath");
				put("cssSelector", "CssSelector");
				put("name", "Name");
				put("className", "ClassName");
				put("textContent", "Text");
				put("tagName", "TagName");
			}
		};
		
		for(final Map.Entry<String, String> how : hows.entrySet()) {
			brazenhead.getWebViewsBy(how.getKey(), "does not matter");
			
			final ArgumentCaptor<By> byArgument = ArgumentCaptor.forClass(By.class);
			verify(solo).getCurrentWebElements(byArgument.capture());
			assertThat(byArgument.getValue().getClass().getName(), is(by(how.getValue())));
			
			reset(solo);
		}

	}
    
    @Test
    public void itCanFindWebViewsByName() {
    	brazenhead.getWebViewsBy("name", "blar");
    	
    	final ArgumentCaptor<By> byArgument = ArgumentCaptor.forClass(By.class);
    	verify(solo).getCurrentWebElements(byArgument.capture());
    	assertThat(byArgument.getValue().getClass().getName(), is(by("Name")));
    }

	private void initMocks() {
        TestRunInformation.setSolo(solo);
        when(solo.getCurrentActivity()).thenReturn(activity);
        when(activity.getResources()).thenReturn(resources);
    }

    private String by(final String how) {
    	return String.format("com.jayway.android.robotium.solo.By$%s", how);
	}

}

@@ -48,25 +48,32 @@
 org.
-mockito.Mockito.*
+hamcrest.CoreMatchers.is
 ;%0Aim
@@ -101,17 +101,26 @@
 .Assert.
-*
+assertThat
 ;%0Aimport
@@ -135,31 +135,151 @@
 org.
-hamcrest.CoreMatchers.*
+mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.reset;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when
 ;%0A%0Ai
@@ -352,40 +352,38 @@
 til.
-Iterator;%0Aimport java.util.Map;%0A
+Map;%0A%0Aimport org.junit.Before;
 %0Aimp
@@ -400,9 +400,12 @@
 nit.
-*
+Test
 ;%0Aim
@@ -599,35 +599,8 @@
 r;%0A%0A
-import com.apple.jobjc.ID;%0A
 impo

clean up imports in BrazenheadTest