1 /**
2  * Copyright 2010 JogAmp Community. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice, this list of
8  *       conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *       of conditions and the following disclaimer in the documentation and/or other materials
12  *       provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  * or implied, of JogAmp Community.
27  */
28 
29 package com.jogamp.opengl.test.junit.newt.event;
30 
31 import org.eclipse.swt.SWT ;
32 import org.eclipse.swt.layout.FillLayout ;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Display ;
35 import org.eclipse.swt.widgets.Shell ;
36 
37 import com.jogamp.opengl.GLCapabilities ;
38 import com.jogamp.opengl.GLProfile ;
39 
40 import org.junit.AfterClass ;
41 import org.junit.Assert;
42 import org.junit.Assume;
43 import org.junit.BeforeClass ;
44 import org.junit.FixMethodOrder;
45 import org.junit.runners.MethodSorters;
46 
47 import com.jogamp.nativewindow.swt.SWTAccessor;
48 import com.jogamp.newt.opengl.GLWindow ;
49 import com.jogamp.newt.swt.NewtCanvasSWT ;
50 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
51 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
52 
53 /**
54  * Test whether or not event modifiers preserved by NEWT when
55  * the canvas is a NewtCanvasSWT.
56  */
57 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 public class TestNewtEventModifiersNewtCanvasSWTAWT extends BaseNewtEventModifiers {
59 
60     private static Display _display = null;
61     private static Shell _shell = null;
62     private static Composite _composite = null;
63     private static GLWindow _glWindow ;
64 
65     ////////////////////////////////////////////////////////////////////////////
66 
eventDispatchImpl()67     protected static void eventDispatchImpl() {
68         final int maxEvents = 10;
69         try {
70             Thread.sleep(100);
71         } catch (final InterruptedException e) { }
72         final boolean[] res = { false };
73         int i=0;
74         do {
75             SWTAccessor.invoke(_display, true, new Runnable() {
76                public void run() {
77                    if( !_display.isDisposed() ) {
78                        res[0] = _display.readAndDispatch();
79                    } else {
80                        res[0] = false;
81                    }
82                } } );
83             i++;
84         } while( i<maxEvents && res[0] );
85     }
86 
87     @Override
eventDispatch()88     protected void eventDispatch() {
89         eventDispatchImpl();
90     }
91 
92     ////////////////////////////////////////////////////////////////////////////
93 
94     @BeforeClass
beforeClass()95     public static void beforeClass() throws Exception {
96 
97         // FIXME: Hangs .. w/ Java7 .. every now and then!
98         setTestSupported(false);
99 
100         /***
101         SWTAccessor.invoke(true, new Runnable() {
102             public void run() {
103                 _display = new Display();
104             }});
105         Assert.assertNotNull( _display );
106 
107         SWTAccessor.invoke(_display, true, new Runnable() {
108             public void run() {
109                 _shell = new Shell( _display );
110                 Assert.assertNotNull( _shell );
111                 _shell.setText( "Event Modifier Test NewtCanvasSWT" ) ;
112                 _shell.setLayout( new FillLayout() );
113                 _composite = new Composite( _shell, SWT.NONE );
114                 _composite.setLayout( new FillLayout() );
115                 Assert.assertNotNull( _composite );
116             }});
117 
118         {
119             GLCapabilities caps = new GLCapabilities( GLProfile.get( GLProfile.GL2ES2 ) ) ;
120             _glWindow = GLWindow.create( caps ) ;
121             _glWindow.addGLEventListener( new RedSquareES2() ) ;
122 
123             NewtCanvasSWT.create( _composite, SWT.NO_BACKGROUND, _glWindow ) ;
124         }
125 
126         SWTAccessor.invoke(_display, true, new Runnable() {
127            public void run() {
128               _shell.setBounds( TEST_FRAME_X, TEST_FRAME_Y, TEST_FRAME_WIDTH, TEST_FRAME_HEIGHT ) ;
129               _shell.open();
130            }
131         });
132 
133         // no AWT idling, may deadlock on OSX!
134         Assert.assertNotNull(_robot);
135         _robot.setAutoWaitForIdle( false ) ;
136 
137         // no waiting for results ..
138         AWTRobotUtil.requestFocus(null, _glWindow, false); // programmatic
139         eventDispatchImpl();
140         AWTRobotUtil.requestFocus(_robot, _glWindow, INITIAL_MOUSE_X, INITIAL_MOUSE_Y);
141         eventDispatchImpl();
142 
143         _glWindow.addMouseListener( _testMouseListener ) ;
144         */
145     }
146 
147     ////////////////////////////////////////////////////////////////////////////
148 
149     @AfterClass
afterClass()150     public static void afterClass() throws Exception {
151         /**
152         _glWindow.destroy() ;
153 
154         try {
155             SWTAccessor.invoke(_display, true, new Runnable() {
156                 public void run() {
157                     if( null != _composite ) {
158                         _composite.dispose();
159                     }
160                     if( null != _shell ) {
161                         _shell.dispose();
162                     }
163                     if( null != _display && !_display.isDisposed()) {
164                         _display.dispose();
165                     }
166                 }});
167         }
168         catch( Throwable throwable ) {
169             throwable.printStackTrace();
170             Assume.assumeNoException( throwable );
171         } */
172     }
173 
174     ////////////////////////////////////////////////////////////////////////////
175 
main(final String args[])176     public static void main(final String args[]) throws Exception {
177         final String testName = TestNewtEventModifiersNewtCanvasSWTAWT.class.getName() ;
178         org.junit.runner.JUnitCore.main( testName ) ;
179     }
180 
181     ////////////////////////////////////////////////////////////////////////////
182 }
183