/**
 * Copyright 2010 JogAmp Community. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of JogAmp Community.
 */

package com.jogamp.opengl.test.junit.jogl.acore;

import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;

import com.jogamp.newt.awt.NewtCanvasAWT;
import com.jogamp.newt.event.MouseEvent;
import com.jogamp.newt.event.MouseListener;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.Animator;

import com.jogamp.opengl.test.junit.util.UITestCase;
import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
import com.jogamp.opengl.test.junit.util.AWTRobotUtil;

import java.awt.Frame;
import java.lang.reflect.InvocationTargetException;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Bug 556
 * 
 * Step to reproduce:
 *  - Press mosue inside window
 *  - drag outside window and release mouse button
 *  - move mouse inside window
 *  
 * Result: mouseDragged received
 * Expected: mouseMoved received
 */
public class NewtMouseDragged_bug extends UITestCase implements MouseListener {
	static GLProfile glp;
	static GLCapabilities caps;
	static int width, height;

	@BeforeClass
	public static void initClass() {
		glp = GLProfile.getDefault();
		Assert.assertNotNull(glp);
		caps = new GLCapabilities(glp);
		Assert.assertNotNull(caps);
		width = 256;
		height = 256;
	}
	
	@Override
	public void mouseMoved(MouseEvent arg0) {
		System.out.println("Moved");
	}
	@Override
	public void mouseDragged(MouseEvent arg0) {
		System.out.println("Dragged");
	}
	@Override
	public void mouseClicked(MouseEvent arg0) {}
	@Override
	public void mouseEntered(MouseEvent arg0) {}
	@Override
	public void mouseExited(MouseEvent arg0) {}
	@Override
	public void mousePressed(MouseEvent arg0) {}
	@Override
	public void mouseReleased(MouseEvent arg0) {}
	@Override
	public void mouseWheelMoved(MouseEvent arg0) {}

	protected NewtCanvasAWT runTestGL(final Frame frame, final Animator animator, final int x, final int y,
			final boolean vsync) throws InterruptedException, InvocationTargetException {

		Gears gears = new Gears(vsync ? 1 : 0);
		
		final GLWindow glWindow;
		final NewtCanvasAWT newtCanvas; {
			glWindow = GLWindow.create(caps);
			glWindow.addGLEventListener(gears);
			
			newtCanvas = new NewtCanvasAWT(glWindow);
			newtCanvas.setFocusTraversalKeysEnabled(false);
			
			glWindow.addMouseListener(this);
		}
		
		Assert.assertNotNull(newtCanvas);
		frame.add(newtCanvas);
		frame.setLocation(x, y);
		frame.setSize(width, height);

		animator.add(glWindow);

		javax.swing.SwingUtilities.invokeAndWait(new Runnable(){
			@Override
			public void run() {
				frame.setVisible(true);
			}
		});
		Assert.assertEquals(true, AWTRobotUtil.waitForRealized(newtCanvas, true));

		return newtCanvas;
	}

	@Test
	public void test01() throws InterruptedException, InvocationTargetException {
		final Frame f1 = new Frame();
		Animator animator = new Animator();

		final NewtCanvasAWT glc1 = runTestGL(f1, animator, 0, 0, false);

		animator.setUpdateFPSFrames(1, null);
		animator.start();
		while(animator.isAnimating() && animator.getTotalFPSDuration() < duration) {
			Thread.sleep(30000);
		}
		animator.stop();

		try {
			javax.swing.SwingUtilities.invokeAndWait(new Runnable(){
				@Override
				public void run() {
					try {
						f1.dispose();
						Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glc1, false));
					}
					catch(Throwable t) {
						throw new RuntimeException(t);
					}
				}
			});
		}
		catch(Throwable throwable) {
			throwable.printStackTrace();
			Assume.assumeNoException(throwable);
		}
	}

	static long duration = 500; // ms

	public static void main(String args[]) {
		for(int i = 0; i < args.length; i++) {
			if(args[i].equals("-time")) {
				i++;
				try {
					duration = Integer.parseInt(args[i]);
				}
				catch(Exception ex) {
					ex.printStackTrace();
				}
			}
		}
		org.junit.runner.JUnitCore.main(NewtMouseDragged_bug.class.getName());
	}
}
