/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.engine.layout.pdf;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IPDFRenderOption;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.layout.area.impl.ContainerArea;
import org.eclipse.birt.report.engine.layout.area.impl.PageArea;
import org.eclipse.birt.report.engine.layout.area.impl.TableArea;


public class PDFPageLMTest extends PDFLayoutTest
{
	private List getPages(boolean fitToPage, boolean pagebreakPaginationOnly ) throws EngineException
	{
		String designFile = "org/eclipse/birt/report/engine/layout/pdf/fitToPage.xml";
		IReportRunnable report = openReportDesign( designFile );
		List pageAreas = new ArrayList();
		IEmitterMonitor monitor = new PageMonitor(pageAreas);
		IRunAndRenderTask runAndRenderTask = new TestRunAndRenderTask( engine,
				report, monitor );
		PDFRenderOption options = createRenderOption();
		if (fitToPage)
		{
			options.setOption( IPDFRenderOption.PAGE_OVERFLOW, new Integer(IPDFRenderOption.FIT_TO_PAGE_SIZE) );	
		}
		
		options.setOption( IPDFRenderOption.PAGEBREAK_PAGINATION_ONLY, new Boolean(pagebreakPaginationOnly) );
		runAndRenderTask.setRenderOption( options );
		runAndRenderTask.run( );
		runAndRenderTask.close( );
		return pageAreas;
	}
	
	
	public void testPagebreakPaginationOnlyFalse() throws EngineException
	{
		assertEquals( 8, getPages(false, false).size( ) );
	}
	
	public void testPagebreakPaginationOnlyTrue() throws EngineException
	{
		assertEquals( 4, getPages(false, true).size( ) );
	}
	
	public void testFitToPageFalse() throws EngineException
	{
		List pages = getPages(false, false);
		for(int i=0; i<pages.size( ); i++)
		{
			PageArea page = (PageArea) pages.get( i );
			assertEquals(new Float(1.0f), new Float(page.getScale()));
		}
		
		pages = getPages(false, true);
		for(int i=0; i<pages.size( ); i++)
		{
			PageArea page = (PageArea) pages.get( i );
			assertEquals(new Float(1.0f), new Float(page.getScale()));
		}
	}
	
	public void testFitToPageTrue() throws EngineException
	{
		List pages = getPages(true, false);
		float[] scales = new float[]{0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f, 0.75f};
		assertTrue(pages.size( )==scales.length);
		for(int i=0; i<pages.size( ); i++)
		{
			PageArea page = (PageArea) pages.get( i );
			assertEquals(new Float(scales[i]), new Float(page.getScale( )));
		}
		
		pages = getPages(true, true);
		scales = new float[]{0.75f, 0.75f, 0.75f, 0.234f};
		assertTrue(pages.size( )==scales.length);
		for(int i=0; i<pages.size( ); i++)
		{
			PageArea page = (PageArea) pages.get( i );
			float delta = scales[i]-page.getScale( );
			assertTrue(delta>-0.001 && delta<0.001);
		}
	}

	/**
	 * Tests page break interval works in PDF.
	 * @throws EngineException
	 */
	public void testPageBreakInterval( ) throws EngineException
	{
		String designFile = "org/eclipse/birt/report/engine/layout/pdf/PageBreakIntervalTest.xml";
		List pageAreas = getPageAreas( designFile );
		assertEquals( 3, pageAreas.size( ) );
		int[] recordNumberInEachPage = {3, 3, 1};
		for ( int i = 0; i < recordNumberInEachPage.length; i++ )
		{
			TableArea table = getTableArea( (ContainerArea) pageAreas.get( i ) );
			assertNotNull( table );
			assertEquals( recordNumberInEachPage[i], table.getChildrenCount( ) );
		}
	}
	
	/**
	 * Tests page break interval count is reset when page is broken by other
	 * page break events.
	 * 
	 * In this case, a page break interval 3 is set on table, while the page
	 * break set on table group will broken page every 2 records. So the page
	 * break interval takes no effect.
	 * 
	 * @throws EngineException
	 */
	public void testPageBreakInterval2( ) throws EngineException
	{
		String designFile = "org/eclipse/birt/report/engine/layout/pdf/PageBreakIntervalAndGroupPageBreak.xml";
		List pageAreas = getPageAreas( designFile );
		assertEquals( 3, pageAreas.size( ) );
	}

	private TableArea getTableArea( ContainerArea container )
	{
		Iterator children = container.getChildren( );
		while( children.hasNext( ) )
		{
			Object child = children.next( );
			if ( child instanceof TableArea )
			{
				return (TableArea) child;
			}
			TableArea result = getTableArea( (ContainerArea) child );
			if ( result != null )
			{
				return result;
			}
		}
		return null;
	}
}

@@ -2149,9 +2149,9 @@
 ls( 
-8
+4
 , ge
@@ -2814,32 +2814,61 @@
 ineException%0A%09%7B%0A
+%09%09/* FIXME support this case%0A
 %09%09List pages = g
@@ -3174,35 +3174,45 @@
 ale( )));%0A%09%09%7D%0A%09%09
+*/
 %0A%09%09
+%0A%09%09List 
 pages = getPages
@@ -3227,16 +3227,24 @@
 rue);%0A%09%09
+float%5B%5D 
 scales =
@@ -3281,17 +3281,16 @@
 5f, 0.23
-4
 f%7D;%0A%09%09as
@@ -3484,17 +3484,16 @@
 elta%3E-0.
-0
 01 && de
@@ -3498,17 +3498,16 @@
 delta%3C0.
-0
 01);%0A%09%09%7D

Fix Bugzilla  Bug 231991 Failures occurs in Junit test of org.eclipse.birt.report.engine.tests in daily build 20080514-0630