package com.jme.util;

import com.jme.bounding.BoundingSphere;
import com.jme.light.DirectionalLight;
import com.jme.light.Light;
import com.jme.light.PointLight;
import com.jme.light.SpotLight;
import com.jme.math.Vector3f;
import com.jme.scene.Node;
import junit.framework.TestCase;

/**
 * Tests for LightState relatet stuff.
 */
public class LightStateTest extends TestCase {
    /**
     * Test the LightStateCreator.quickSort. 
     */
    public void testQuickSort() {
        LightStateCreator lightStateCreator = new LightStateCreator();

        // create nine lights
        SpotLight a = new SpotLight();
        lightStateCreator.addLight( a );
        SpotLight b = new SpotLight();
        lightStateCreator.addLight( b );
        SpotLight c = new SpotLight();
        lightStateCreator.addLight( c );
        SpotLight spot1 = new SpotLight();
        spot1.setAttenuate( true );
        spot1.setLocation( new Vector3f( 2, 0, 0 ) );
        spot1.setDirection( new Vector3f( 0, 0.5f, 0 ) );
        spot1.setLinear( 2 );
        lightStateCreator.addLight( spot1 );
        lightStateCreator.addLight( new DirectionalLight() );
        lightStateCreator.addLight( new SpotLight() );
        lightStateCreator.addLight( new SpotLight() );
        lightStateCreator.addLight( new DirectionalLight() );
        lightStateCreator.addLight( new PointLight() );

        for ( int i = 0; i < lightStateCreator.lightList.size(); i++ ) {
            Light light = lightStateCreator.get( i );
            light.setEnabled( true );
        }

        assertEquals( "number of lights", 9, lightStateCreator.lightList.size() );

        Node node = new Node( "test" );
        node.setWorldBound( new BoundingSphere( 1, new Vector3f( 1, 0, 0 ) ) );
        node.updateGeometricState( 0, true );
        lightStateCreator.quickSort( 0, 8, node );

        assertEquals( "number of lights", 9, lightStateCreator.lightList.size() );

        float lastValue = 0;
        int aIndex = -1;
        int bIndex = -1;
        int cIndex = -1;
        for ( int i = 0; i < lightStateCreator.lightList.size(); i++ ) {
            Light light = lightStateCreator.get( i );
            float lightValue = lightStateCreator.getValueFor( light, node.getWorldBound() );
            System.out.println( lightValue );
            assertTrue( "order wrong", lightValue >= lastValue );
            if ( light == a ) {
                aIndex = i;
            }
            if ( light == b ) {
                bIndex = i;
            }
            if ( light == c ) {
                cIndex = i;
            }
            lastValue = lightValue;
        }
        assertEquals( "b after a", aIndex + 1, bIndex );
        assertEquals( "c after b", bIndex + 1, cIndex );
    }
}

@@ -245,16 +245,51 @@
 e.Node;%0A
+import com.jme.scene.shape.Sphere;%0A
 import j
@@ -1715,21 +1715,74 @@
     
-node.setWorld
+Sphere dummy = new Sphere( null, 5, 5, 1 );%0A        dummy.setModel
 Boun
@@ -1832,24 +1832,59 @@
 0, 0 ) ) );%0A
+        node.attachChild( dummy );%0A
         node

test updated (setWorldBound was removed)