package me.alb_i986.selenium.tinafw.ui;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import static org.junit.runners.Parameterized.*;

import me.alb_i986.selenium.tinafw.domain.SupportedBrowser;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.WebDriver;

import java.util.Arrays;

@RunWith(Parameterized.class)
public class WebDriverFactoryLocalTest {

	@Parameters(name = "{0}")
	public static Iterable<SupportedBrowser> params() {
		return Arrays.asList(SupportedBrowser.values());
	}

	@Parameter
	public SupportedBrowser supportedBrowser;

	private WebDriverFactoryLocal sut = new WebDriverFactoryLocal();
	private WebDriver driver;

	@After
	public void after() {
		if (driver != null) {
			driver.quit();
		}
	}

	@Test
	public void getWebDriver() {
		assumeThat(supportedBrowser.toClass(), hasZeroArgConstructor());

		// when
		try {
			driver = sut.getWebDriver(supportedBrowser);
		} catch (Throwable t) {
			if (supportedBrowser == SupportedBrowser.HTML_UNIT) {
				throw t;
			}
			// let's consider any exception (but html unit) as an environment issue
			// E.g.: get IEDriver fails because we are not on Windows
			assumeNoException(t);
		}

		// then
		assertNotNull(driver);
		assertThat(driver, is(instanceOf(supportedBrowser.toClass())));
	}

	private static Matcher<Class<?>> hasZeroArgConstructor() {
		return new TypeSafeMatcher<Class<?>>() {
			@Override
			protected boolean matchesSafely(Class<?> aClass) {
				try {
					aClass.getConstructor();
					return true;
				} catch (NoSuchMethodException e) {
					return false;
				}
			}

			@Override
			public void describeTo(Description description) {
				description.appendText("has a 0-arg constructor");
			}
		};
	}
}

@@ -1017,74 +1017,15 @@
 %7B%0A%09%09
-assumeThat(supportedBrowser.toClass(), hasZeroArgConstructor());%0A%0A
+try %7B%0A%09
 %09%09//
@@ -1030,24 +1030,16 @@
 // when%0A
-%09%09try %7B%0A
 %09%09%09drive
@@ -1082,108 +1082,139 @@
 r);%0A
+%0A
 %09%09
-%7D catch (Throwable t) %7B%0A%09%09%09if (supportedBrowser == SupportedBrowser.HTML_UNIT) %7B%0A%09%09%09%09throw t;%0A%09%09%09%7D
+%09// then%0A%09%09%09assertNotNull(driver);%0A%09%09%09assertThat(driver, is(instanceOf(supportedBrowser.toClass())));%0A%0A%09%09%7D catch (Exception e) %7B
 %0A%09%09%09
@@ -1353,557 +1353,110 @@
 %0A%09%09%09
-assumeNoException(t);%0A%09%09%7D%0A%0A%09%09// then%0A%09%09assertNotNull(driver);%0A%09%09assertThat(driver, is(instanceOf(supportedBrowser.toClass())));%0A%09%7D%0A%0A%09private static Matcher%3CClass%3C?%3E%3E hasZeroArgConstructor() %7B%0A%09%09return new TypeSafeMatcher%3CClass%3C?%3E%3E() %7B%0A%09%09%09@Override%0A%09%09%09protected boolean matchesSafely(Class%3C?%3E aClass) %7B%0A%09%09%09%09try %7B%0A%09%09%09%09%09aClass.getConstructor();%0A%09%09%09%09%09return tru
+if (supportedBrowser == SupportedBrowser.HTML_UNIT) %7B%0A%09%09%09%09throw 
 e;%0A
-%09
 %09%09%09%7D 
-catch (NoSuchMethodException e) %7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public void describeTo(Description description) %7B%0A%09%09%09%09description.appendText(%22has a 0-arg constructor%22
+else %7B%0A%09%09%09%09assumeNoException(e
 );%0A%09
@@ -1462,15 +1462,14 @@
 %09%09%09%7D%0A%09%09%7D
-;
 %0A%09%7D%0A%7D%0A

Fix WebDriverFactoryLocalTest