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

import junit.framework.TestCase;

import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.metadata.DimensionValue;
import org.eclipse.birt.report.model.api.util.DimensionUtil;

/**
 * Test the method in <code>DimensionUtil</code>.
 */

public class DimensionUtilTest extends TestCase
{

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp( ) throws Exception
	{
		super.setUp( );
	}

	/**
	 * Tests whether unit is absolute.
	 */

	public void testIsAbsoluteUnit( )
	{
		assertTrue( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_CM ) );
		assertFalse( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_EM ) );
		assertFalse( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_EX ) );
		assertTrue( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_IN ) );
		assertTrue( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_MM ) );
		assertTrue( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_PC ) );
		assertFalse( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_PERCENTAGE ) );
		assertTrue( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_PT ) );
		assertFalse( DimensionUtil
				.isAbsoluteUnit( DesignChoiceConstants.UNITS_PX ) );
	}

	/**
	 * Tests the unit conversion method.
	 */

	public void testConvertTo( )
	{
		// * => in
		DimensionValue value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_CM, DesignChoiceConstants.UNITS_IN );
		assertEquals( 39.37, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_IN, DesignChoiceConstants.UNITS_IN );
		assertEquals( 100, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_MM, DesignChoiceConstants.UNITS_IN );
		assertEquals( 3.94, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PC, DesignChoiceConstants.UNITS_IN );
		assertEquals( 16.67, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PT, DesignChoiceConstants.UNITS_IN );
		assertEquals( 1.39, value.getMeasure( ), 0.01 );
		
		// * => cm

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_CM, DesignChoiceConstants.UNITS_CM );
		assertEquals( 100, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_MM, DesignChoiceConstants.UNITS_CM );
		assertEquals( 10, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_IN, DesignChoiceConstants.UNITS_CM );
		assertEquals( 254, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PT, DesignChoiceConstants.UNITS_CM );
		assertEquals( 3.53, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PC, DesignChoiceConstants.UNITS_CM );
		assertEquals( 42.33, value.getMeasure( ), 0.01 );
		
		// * => mm
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_CM, DesignChoiceConstants.UNITS_MM );
		assertEquals( 1000, value.getMeasure( ), 0.01 );
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_MM, DesignChoiceConstants.UNITS_MM );
		assertEquals( 100, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_IN, DesignChoiceConstants.UNITS_MM );
		assertEquals( 2540, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PT, DesignChoiceConstants.UNITS_MM );
		assertEquals( 35.28, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PC, DesignChoiceConstants.UNITS_MM );
		assertEquals( 423.33, value.getMeasure( ), 0.01 );

		// * => pt
		
		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_CM, DesignChoiceConstants.UNITS_PT );
		assertEquals( 2834.65, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_MM, DesignChoiceConstants.UNITS_PT );
		assertEquals( 283.46, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_IN, DesignChoiceConstants.UNITS_PT );
		assertEquals( 7200, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PT, DesignChoiceConstants.UNITS_PT );
		assertEquals( 100, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PC, DesignChoiceConstants.UNITS_PT );
		assertEquals( 1200, value.getMeasure( ), 0.01 );

		// * => pc

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_CM, DesignChoiceConstants.UNITS_PC );
		assertEquals( 236.22, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_MM, DesignChoiceConstants.UNITS_PC );
		assertEquals( 23.62, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_IN, DesignChoiceConstants.UNITS_PC );
		assertEquals( 600, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PT, DesignChoiceConstants.UNITS_PC );
		assertEquals( 8.33, value.getMeasure( ), 0.01 );

		value = DimensionUtil.convertTo( 100,
				DesignChoiceConstants.UNITS_PC, DesignChoiceConstants.UNITS_PC );
		assertEquals( 100, value.getMeasure( ), 0.01 );

	}
}