package pt.fccn.mobile.arquivo.tests.imagesearch;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import pt.fccn.arquivo.selenium.WebDriverTestBaseParalell;

/**
 *
 * @author ivo.branco@fccn.pt
 *
 */
public class ImageSearchDirectUrlTest extends WebDriverTestBaseParalell {

	public ImageSearchDirectUrlTest(String os, String version, String browser, String deviceName,
			String deviceOrientation) {
		super(os, version, browser, deviceName, deviceOrientation);
	}

	@Test
	public void testImageSearchDirectUrl() throws Exception {

		String url = testURL
				+ "/images.jsp?l=en&size=all&type=&tools=off&safeSearch=on&query=fccn&btnSubmit=Search&dateStart=26%2F06%2F2007&dateEnd=27%2F06%2F2007";
		driver.get(url);

		WebElement firstImage = driver.findElement(By.id("imageResults0"));
		assertNotNull("Should exist at least one image", firstImage);

		appendError(() -> {
			List<WebElement> photoDivList = driver.findElements(By.xpath("//*[@id=\"photos\"]/div"));
			assertEquals("Verify results count", 1, photoDivList.size());
		});

		appendError(() -> assertThat("Check image original origin/domain",
				driver.findElement(By.xpath("//*[@id=\"imageResults0\"]/p[1]")).getText(),
				containsString("sag.fccn.pt")));
	}
}

@@ -267,16 +267,43 @@
 il.List;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -569,16 +569,207 @@
 lell %7B%0A%0A
+%09private static final String IMAGE_SEARCH_DIRECT_URL = %22/images.jsp?l=en&size=all&type=&tools=off&safeSearch=on&query=fccn&btnSubmit=Search&dateStart=26%252F06%252F2007&dateEnd=27%252F06%252F2007%22;%0A%0A
 %09public 
@@ -996,16 +996,26 @@
 irectUrl
+NoLanguage
 () throw
@@ -1032,172 +1032,510 @@
 n %7B%0A
-%0A
 %09%09
-String url = testURL%0A%09%09%09%09+ %22/images.jsp?l=en&size=all&type=&tools=off&safeSearch=on&query=fccn&btnSubmit=Search&dateStart=26%252F06%252F2007&dateEnd=27%252F06%252F2007%22;
+testImageSearchDirectUrl(testURL + IMAGE_SEARCH_DIRECT_URL, Optional.empty());%0A%09%7D%0A%0A%09@Test%0A%09public void testImageSearchDirectUrlPt() throws Exception %7B%0A%09%09testImageSearchDirectUrl(testURL + IMAGE_SEARCH_DIRECT_URL + %22&l=pt%22, Optional.empty());%0A%09%7D%0A%0A%09@Test%0A%09public void testImageSearchDirectUrlEn() throws Exception %7B%0A%09%09testImageSearchDirectUrl(testURL + IMAGE_SEARCH_DIRECT_URL + %22&l=en%22, Optional.of(%22Images%22));%0A%09%7D%0A%0A%09private void testImageSearchDirectUrl(String url, Optional%3CString%3E imageButtonText) %7B
 %0A%09%09d
@@ -2058,13 +2058,224 @@
 pt%22)));%0A
-%09%7D
+%0A%09%09if (imageButtonText.isPresent()) %7B%0A%09%09%09appendError(() -%3E assertEquals(%22Check page language by verifying images button text%22, imageButtonText.get(),%0A%09%09%09%09%09driver.findElementById(%22ImageButton%22).getText()));%0A%09%09%7D%0A%09%7D%0A
 %0A%7D%0A

Improve image search direct url test with: no language, PT and EN language