package org.eclipse.birt.report.tests.engine.regression;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;

import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.tests.engine.EngineCase;

/**
 * <b>Bug description</b>
 * <p>
 * Support HTML render option SupportCollapseGroups
 * 
 * <p>
 * <b>Test description</b>
 * <p>
 * This test make sure that setEnableMetadata and setDisplayFilterIcon can be set, and an image is add before table group.
 * 
 */

public class Regression_137406 extends EngineCase
{
	private final String input = "regression_137406.rptdesign";
	private final String output = "regression_137406.html";
	
	
	public void test_Regression_137406( ) throws EngineException, Exception
	{
		String outputFile = this.getClassFolder( ) + "/" + OUTPUT_FOLDER + "/" + output;
		String inputFile = this.getClassFolder( ) + "/" + INPUT_FOLDER + "/" + input;
		
		IReportRunnable runnable = engine.openReportDesign( inputFile );
		IRunAndRenderTask task = engine.createRunAndRenderTask( runnable );
		
		engineTask = task;
		task.setLocale( Locale.ENGLISH );
		
		IRenderOption options = null;
		options = new HTMLRenderOption( );
		options.setOutputFileName( outputFile );
		
		( ( HTMLRenderOption ) options ).setEnableMetadata( true );
		( ( HTMLRenderOption ) options ).setDisplayGroupIcon( true );
		HashMap appContext = new HashMap( );
		task.setAppContext( appContext );
		
		options.setOutputFormat( "HTML" );
		options.getOutputSetting( ).put( HTMLRenderOption.URL_ENCODING , "UTF-8" );
		task.setRenderOption( options );
		task.run( );
		task.close( );
		
		testCompare( outputFile );
		
	}


	protected void testCompare( String fileHTMLName ) throws IOException
	{
		BufferedReader reader = new BufferedReader( new FileReader( new File( fileHTMLName ) ) );
		String line = null;
		int i = 0;
		while ((line = reader.readLine()) != null) 
		{
			if (line.indexOf( "<img src=\"" ) > 0 ) 
			{
				i++;
			}
		}
		assertEquals( "Incorrect count of image!" , 5 , i );
	}
}
