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

import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.birt.report.model.api.metadata.IElementDefn;
import org.eclipse.birt.report.model.elements.Style;
import org.eclipse.birt.report.model.elements.TextItem;
import org.eclipse.birt.report.model.metadata.MetaDataDictionary;
import org.eclipse.birt.report.model.metadata.PropertyDefn;

import com.ibm.icu.util.ULocale;

/**
 * Tests <code>NativePropertyDefinition</code>.
 */

public class NativePropertyDefinitionTest extends ScriptTestCase
{

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

		DesignEngine.newSession( (ULocale) null );

		MetaDataDictionary dd = MetaDataDictionary.getInstance( );
		IElementDefn elementDefn = dd
				.getElement( ReportDesignConstants.TEXT_ITEM );
		PropertyDefinition propDefn = new PropertyDefinition(
				(PropertyDefn) elementDefn.getProperty( TextItem.CONTENT_PROP ) );
		scope.put( "contentProp", scope, propDefn ); //$NON-NLS-1$
		context.registerBean( "lineStyleProp", //$NON-NLS-1$
				new PropertyDefinition( (PropertyDefn) elementDefn
						.getProperty( Style.BORDER_BOTTOM_STYLE_PROP ) ) );
	}

	/**
	 * Tests the getters.
	 */

	public void testGetters( ) throws Exception
	{
		// contentProp

		String script1 = "contentProp.name "; //$NON-NLS-1$
		Object value1 = eval( script1 );
		assertEquals( "content", value1.toString( ) ); //$NON-NLS-1$

		script1 = "contentProp.type "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "string", value1.toString( ) ); //$NON-NLS-1$

		script1 = "contentProp.isUserProperty "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "false", value1.toString( ) ); //$NON-NLS-1$

		script1 = "contentProp.isStyle "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "false", value1.toString( ) ); //$NON-NLS-1$

		script1 = "contentProp.group "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "", value1.toString( ) ); //$NON-NLS-1$

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

		script1 = "contentProp.defaultValue "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( null, value1 ); //$NON-NLS-1$

		script1 = "contentProp.choices == null "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "true", value1.toString( ) ); //$NON-NLS-1$

		script1 = "contentProp.choices == undefined "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "true", value1.toString( ) ); //$NON-NLS-1$

		// lineStyleProp

		script1 = "lineStyleProp.name "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "borderBottomStyle", value1.toString( ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.type "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "choice", value1.toString( ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.isUserProperty "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "false", value1.toString( ) ); //$NON-NLS-1$

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

		script1 = "lineStyleProp.group "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "Box", value1.toString( ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.canInherit "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "false", value1.toString( ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.defaultValue "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "none", value1 ); //$NON-NLS-1$

		script1 = "lineStyleProp.choices "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertNotNull( value1 );
		assertEquals( 9, ( (List) value1 ).size( ) ); //$NON-NLS-1$
		assertEquals( "none", ( (List) value1 ).get( 0 ) ); //$NON-NLS-1$
		assertEquals( "solid", ( (List) value1 ).get( 1 ) ); //$NON-NLS-1$
		assertEquals( "dotted", ( (List) value1 ).get( 2 ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.choices[0] "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "none", value1.toString( ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.choices[1] "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "solid", value1.toString( ) ); //$NON-NLS-1$

		script1 = "lineStyleProp.choices[2] "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "dotted", value1.toString( ) ); //$NON-NLS-1$
	}

	/**
	 * Tests the setters. All members are read-only, so any setter will be
	 * ignored by script runtime.
	 */

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

		script1 = "contentProp.type='integer'; contentProp.type "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "string", value1.toString( ) ); //$NON-NLS-1$

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

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

		script1 = "contentProp.group='abc'; contentProp.group "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( "", value1.toString( ) ); //$NON-NLS-1$

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

		script1 = "contentProp.defaultValue='abc'; contentProp.defaultValue "; //$NON-NLS-1$
		value1 = eval( script1 );
		assertEquals( null, value1 ); //$NON-NLS-1$

	}
}