/*******************************************************************************
 * 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.api;

import org.eclipse.birt.report.engine.EngineCase;

public class TOCTest extends EngineCase
{

	static final String REPORT_DESIGN_RESOURCE = "org/eclipse/birt/report/engine/api/toc.xml";

	static final int TOC_COUNT = 21;
	public void setUp( ) throws Exception
	{
		super.setUp( );
		removeFile( REPORT_DOCUMENT );
		removeFile( REPORT_DESIGN );
		copyResource( REPORT_DESIGN_RESOURCE, REPORT_DESIGN );
	}

	public void tearDown( )
	{
		// shut down the engine.
		engine.shutdown( );
		removeFile( REPORT_DESIGN );
		removeFile( REPORT_DOCUMENT );
	}

	public void testRender( )
	{
		try
		{
			createReportDocument( );
			doTOCTest( );
		}
		catch ( Exception ex )
		{
			ex.printStackTrace( );
			fail( );
		};
	}

	protected void doTOCTest( ) throws Exception
	{
		// open the document in the archive.
		IReportDocument reportDoc = engine.openReportDocument( REPORT_DOCUMENT );
		ITOCTree tree = reportDoc.getTOCTree( "viewer", null );
		TOCNode toc = tree.getRoot( );
		assertEquals( TOC_COUNT, toc.getChildren( ).size( ) );
		// first one is the start of report
		TOCNode start = (TOCNode) toc.getChildren( ).get( 0 );
		assertEquals( 0, start.getChildren( ).size( ) );
		assertEquals( "start of report", start.getDisplayString( ) );
		assertEquals( 1, reportDoc.getPageNumber( start.getBookmark( ) ) );

		// last one is the end of report
		TOCNode end = (TOCNode) toc.getChildren( ).get( 20 );
		assertEquals( 0, end.getChildren( ).size( ) );
		assertEquals( "end of report", end.getDisplayString( ) );
		assertEquals( reportDoc.getPageCount( ), reportDoc.getPageNumber( end
				.getBookmark( ) ) );

		reportDoc.close( );
	}

}
