package org.sahagin.report;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

import java.io.File;

import org.junit.Test;
import org.sahagin.TestBase;
import org.sahagin.share.AcceptableLocales;
import org.sahagin.share.IllegalDataStructureException;
import org.sahagin.share.IllegalTestScriptException;
import org.sahagin.share.SysMessages;


public class HtmlReportTest extends TestBase {

    @Test
    public void reportGenerateShouldSucceedWithoutError()
            throws IllegalDataStructureException, IllegalTestScriptException {
        SysMessages.globalInitialize(AcceptableLocales.getInstance(null));
        HtmlReport report = new HtmlReport();
        String subDirName = "reportGenerateShouldSucceedWithoutError/output";
        clearWorkDir(subDirName);
        File outputDir = mkWorkDir(subDirName);
        report.generate(
                testResourceDir("reportGenerateShouldSucceedWithoutError/input"),
                outputDir);
        assertThat(new File(outputDir, "captures").exists(), is(true));
        assertThat(new File(outputDir, "css").exists(), is(true));
        assertThat(new File(outputDir, "images").exists(), is(true));
        assertThat(new File(outputDir, "js").exists(), is(true));
        assertThat(new File(outputDir,
                "reports/sample.SampleTest/shouldFail.html").exists(), is(true));
        assertThat(new File(outputDir,
                "reports/sample.SampleTest/shouldSucceed.html").exists(), is(true));
        assertThat(new File(outputDir, "index.html").exists(), is(true));
    }

}

@@ -148,20 +148,164 @@
 g.junit.
-Test
+Assume;%0Aimport org.junit.Test;%0Aimport org.openqa.selenium.By;%0Aimport org.openqa.selenium.WebDriver;%0Aimport org.openqa.selenium.firefox.FirefoxDriver
 ;%0Aimport
@@ -519,17 +519,16 @@
 sages;%0A%0A
-%0A
 public c
@@ -575,69 +575,75 @@
     
-@Test%0A    public void reportGenerateShouldSucceedWithoutErr
+// returns generated index.html%0A    private File generateTestRep
 or
+t
 ()%0A 
@@ -1423,99 +1423,341 @@
     
-assertThat(new File(outputDir,%0A                %22reports/sample.SampleTest/shouldFail.h
+File indexHtml = new File(outputDir, %22index.html%22);%0A        File shouldFailHtml = new File(outputDir, %22reports/sample.SampleTest/shouldFail.html%22);%0A        File shouldSucceedHtml = new File(outputDir, %22reports/sample.SampleTest/shouldSucceed.html%22);%0A        assertThat(indexHtml.exists(), is(true));%0A        assertThat(shouldFailH
 tml
-%22)
 .exi
@@ -1793,35 +1793,380 @@
 ertThat(
-new File(outputDir,
+shouldSucceedHtml.exists(), is(true));%0A        return indexHtml;%0A    %7D%0A%0A    private void seleniumTestRun(File indexHtml) %7B%0A        WebDriver driver = null;%0A        try %7B%0A            driver = new FirefoxDriver();%0A        %7D catch (Exception e) %7B%0A            quietQuit(driver);%0A            Assume.assumeNoException(%22FirefoxDriver can not work on this enviroment%22, e);
 %0A       
@@ -2166,24 +2166,27 @@
         
+%7D%0A%0A
         
 %22reports
@@ -2177,25 +2177,235 @@
         
-%22reports/
+// TODO need more check such as Js error check%0A        try %7B%0A            String indexHtmlUrl = %22file:///%22 + indexHtml.getAbsolutePath();%0A            driver.get(indexHtmlUrl);%0A            driver.findElement(By.linkText(%22
 sample.S
@@ -2405,33 +2405,33 @@
 ample.SampleTest
-/
+.
 shouldSucceed.ht
@@ -2431,110 +2431,787 @@
 ceed
-.html%22).exists(), is(true));%0A        assertThat(new File(outputDir, %22
+%22)).click();%0A            driver.navigate().back();%0A            driver.findElement(By.linkText(%22sample.SampleTest.shouldFail%22)).click();%0A        %7D finally %7B%0A            quietQuit(driver);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void reportGenerationShouldSucceed()%0A            throws IllegalDataStructureException, IllegalTestScriptException %7B%0A        generateTestReport();%0A    %7D%0A%0A    @Test%0A    public void generatedReportShouldWork()%0A            throws IllegalDataStructureException, IllegalTestScriptException %7B%0A        File 
 index
-.html%22).exists(), is(true));
+Html = generateTestReport();%0A        seleniumTestRun(indexHtml);%0A    %7D%0A%0A    private void quietQuit(WebDriver driver) %7B%0A        if (driver != null) %7B%0A            try %7B%0A                driver.quit();%0A            %7D catch (Exception ignored) %7B%7D%0A        %7D
 %0A   

Add HTML report test by selenium