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

import org.eclipse.birt.report.model.api.FreeFormHandle;
import org.eclipse.birt.report.model.api.TableHandle;

/**
 * Tests <code>NativeElementDefinition</code>.
 */

public class NativeElementDefinitionTest extends ScriptTestCase
{

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp( ) throws Exception
	{
		super.setUp( );

		FreeFormHandle freeFormHandle = designHandle.getElementFactory( )
				.newFreeForm( "form1" ); //$NON-NLS-1$
		designHandle.getBody( ).add( freeFormHandle );

		TableHandle tableHandle = designHandle.getElementFactory( )
				.newTableItem( "table1", 3 ); //$NON-NLS-1$
		freeFormHandle.getReportItems( ).add( tableHandle );
		tableHandle.setCaption( "company" ); //$NON-NLS-1$

		context.registerBean( "table", //$NON-NLS-1$
				new ElementDefinition( tableHandle ) );
	}

	/**
	 * Testing getters.
	 * 
	 * @throws Exception
	 */

	public void test( ) throws Exception
	{
		String script1 = "table.name "; //$NON-NLS-1$
		Object value1 = eval( script1 );
		assertEquals( "table1", value1 ); //$NON-NLS-1$

		// TODO: extends is keyword of JavaScript
		// script1 = "table.extends"; //$NON-NLS-1$
		// value1 = eval( script1 );
		// assertEquals( "", value1 ); //$NON-NLS-1$

		script1 = "table.hasStyle"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "true", value1.toString( ) ); //$NON-NLS-1$

		script1 = "table.container.name"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "form1", value1 ); //$NON-NLS-1$

		script1 = "table.containerSlot"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "reportItems", value1 ); //$NON-NLS-1$

		script1 = "table.propertyDefns[0] "; //$NON-NLS-1$
		value1 = eval( script1 );
		PropertyDefinition propDefn = (PropertyDefinition) value1;
		assertEquals( "caption", propDefn.getName( ) ); //$NON-NLS-1$

		script1 = "table.propertyDefns['caption'] "; //$NON-NLS-1$
		value1 = eval( script1 );
		propDefn = (PropertyDefinition) value1;
		assertEquals( "caption", propDefn.getName( ) ); //$NON-NLS-1$

		script1 = "table.propertyDefns.caption "; //$NON-NLS-1$
		value1 = eval( script1 );
		propDefn = (PropertyDefinition) value1;
		assertEquals( "caption", propDefn.getName( ) ); //$NON-NLS-1$

		// Note: length is not supported.

		// script1 = "table.propertyDefns.length "; //$NON-NLS-1$
		// value1 = eval( script1 );
		// assertEquals( 88, ( (Number) value1 ).intValue( ) ); //$NON-NLS-1$

		script1 = "table.caption "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "company", value1 ); //$NON-NLS-1$

		script1 = "table.properties['caption'] "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "company", value1 ); //$NON-NLS-1$
	}

	/**
	 * Tests setters. All members are read-only, so any set will be ignored by
	 * script runtime.
	 * 
	 * @throws Exception
	 */

	public void testSetters( ) throws Exception
	{
		String script1 = "table.name='abc'; table.name "; //$NON-NLS-1$
		Object value1 = eval( script1 );
		assertEquals( "table1", value1 ); //$NON-NLS-1$

		// script1 = "table.extends='abc'; table.extends"; //$NON-NLS-1$
		// value1 = eval( script1 );
		// assertEquals( "", value1 ); //$NON-NLS-1$

		script1 = "table.hasStyle='false'; table.hasStyle"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "true", value1.toString( ) ); //$NON-NLS-1$

		script1 = "table.container.name=''; table.container.name"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "form1", value1 ); //$NON-NLS-1$

		script1 = "table.containerSlot=-1; table.containerSlot"; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "reportItems", value1 ); //$NON-NLS-1$

	}
}