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 
30 package com.jogamp.opengl.test.junit.newt.parenting;
31 
32 import org.junit.Assert;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.junit.FixMethodOrder;
36 import org.junit.runners.MethodSorters;
37 
38 import com.jogamp.opengl.*;
39 import com.jogamp.nativewindow.*;
40 import com.jogamp.nativewindow.util.Point;
41 import com.jogamp.nativewindow.util.PointImmutable;
42 import com.jogamp.newt.*;
43 import com.jogamp.newt.event.*;
44 import com.jogamp.newt.opengl.*;
45 
46 import java.io.IOException;
47 
48 import com.jogamp.opengl.test.junit.util.*;
49 import com.jogamp.opengl.test.junit.jogl.demos.es2.RedSquareES2;
50 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
51 // import com.jogamp.opengl.test.junit.jogl.demos.es1.RedSquareES1;
52 // import com.jogamp.opengl.test.junit.jogl.demos.es1.GearsES1;
53 
54 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
55 public class TestParenting02NEWT extends UITestCase {
56     static int width, height;
57     static long durationPerTest = 500;
58 
59     @BeforeClass
initClass()60     public static void initClass() {
61         width  = 640;
62         height = 480;
63     }
64 
createWindow(final Screen screen, final Capabilities caps)65     static Window createWindow(final Screen screen, final Capabilities caps) {
66         Assert.assertNotNull(caps);
67         final Window window = NewtFactory.createWindow(screen, caps) ;
68         Assert.assertNotNull(window);
69         return window;
70     }
71 
createWindow(final NativeWindow parent, final Capabilities caps)72     static Window createWindow(final NativeWindow parent, final Capabilities caps) {
73         Assert.assertNotNull(caps);
74         final Window window = NewtFactory.createWindow(parent, caps);
75         window.setUndecorated(true);
76         Assert.assertNotNull(window);
77         return window;
78     }
79 
destroyWindow(final Display display, final Screen screen, final Window window, final GLWindow glWindow)80     static void destroyWindow(final Display display, final Screen screen, final Window window, final GLWindow glWindow) {
81         if(null!=glWindow) {
82             glWindow.destroy();
83         }
84         if(null!=window) {
85             window.destroy();
86         }
87         if(null!=screen) {
88             screen.destroy();
89         }
90         if(null!=display) {
91             display.destroy();
92         }
93     }
94 
95     @Test
test01NewtOnNewtParentChildDraw()96     public void test01NewtOnNewtParentChildDraw() throws InterruptedException {
97         final GLCapabilities caps = new GLCapabilities(null);
98         Assert.assertNotNull(caps);
99         final Display display = NewtFactory.createDisplay(null); // local display
100         Assert.assertNotNull(display);
101         final Screen screen  = NewtFactory.createScreen(display, 0); // screen 0
102         Assert.assertNotNull(screen);
103 
104         int x = 1;
105         int y = 1;
106 
107         final NEWTEventFiFo eventFifo = new NEWTEventFiFo();
108 
109         final Window window1 = createWindow(screen, caps);
110         Assert.assertNotNull(window1);
111         final GLWindow glWindow1 = GLWindow.create(window1);
112         Assert.assertNotNull(glWindow1);
113         glWindow1.setSize(width, height);
114         Assert.assertEquals(width,glWindow1.getWidth());
115         Assert.assertEquals(height,glWindow1.getHeight());
116         glWindow1.setTitle("test01NewtOnNewtParentChildDraw - PARENT");
117         glWindow1.setPosition(x,y);
118         //glWindow1.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
119         //glWindow1.addWindowListener(new TraceWindowAdapter());
120 
121         final GLEventListener demo1 = new RedSquareES2();
122         setDemoFields(demo1, window1, glWindow1, false);
123         // glWindow1.addGLEventListener(demo1);
124 
125         glWindow1.setVisible(true);
126         CapabilitiesImmutable capsChosen = glWindow1.getGraphicsConfiguration().getChosenCapabilities();
127         Assert.assertNotNull(capsChosen);
128         Assert.assertTrue(capsChosen.isOnscreen()==true);
129 
130         final Window window2 = createWindow(window1, caps);
131         Assert.assertNotNull(window2);
132         final GLWindow glWindow2 = GLWindow.create(window2);
133         Assert.assertNotNull(glWindow2);
134         glWindow2.setSize(width/2, height/2);
135         //Assert.assertEquals(width/2,glWindow2.getWidth());
136         //Assert.assertEquals(height/2,glWindow2.getHeight());
137         glWindow2.setTitle("test01NewtOnNewtParentChildDraw - CHILD");
138         glWindow2.setPosition(glWindow1.getWidth()/2, glWindow1.getHeight()/2);
139         //glWindow2.addKeyListener(new TraceKeyAdapter(new KeyAction(eventFifo)));
140         //glWindow2.addWindowListener(new TraceWindowAdapter(new WindowAction(eventFifo)));
141         // glWindow2.addMouseListener(new TraceMouseAdapter());
142 
143         final GLEventListener demo2 = new GearsES2();
144         setDemoFields(demo2, window2, glWindow2, false);
145         // glWindow2.addGLEventListener(demo2);
146 
147         glWindow2.setVisible(true);
148         capsChosen = glWindow2.getGraphicsConfiguration().getChosenCapabilities();
149         Assert.assertNotNull(capsChosen);
150         Assert.assertTrue(capsChosen.isOnscreen()==true);
151 
152         glWindow1.addGLEventListener(demo1);
153         glWindow2.addGLEventListener(demo2);
154         glWindow2.addWindowListener(windowMoveDetection);
155 
156         boolean shouldQuit = false;
157         long duration = durationPerTest;
158         final long step = 20;
159         NEWTEvent event;
160 
161         while (duration>0 && !shouldQuit) {
162             glWindow1.display();
163             glWindow2.display();
164             duration -= step;
165             x += 1;
166             y += 1;
167             // glWindow1.setPosition(x,y);
168             final PointImmutable expPos = new Point(glWindow1.getWidth()/2, glWindow1.getHeight()/2-y);
169             glWindow2.setPosition(expPos.getX(), expPos.getY());
170             {
171                 int waitCount=0;
172                 do {
173                     Thread.sleep(step);
174                     waitCount++;
175                 } while( !windowMoved && waitCount < 10);
176                 final boolean didWindowMove = windowMoved;
177                 windowMoved = false;
178                 final PointImmutable hasPos = new Point(glWindow2.getX(), glWindow2.getY());
179                 System.err.println("Moved: exp "+expPos+", has "+hasPos+", equals "+expPos.equals(hasPos)+", didWindowMove "+didWindowMove+", waitCount "+waitCount);
180             }
181 
182             while( null != ( event = eventFifo.get() ) ) {
183                 final Window source = (Window) event.getSource();
184                 if(WindowEvent.EVENT_WINDOW_DESTROY_NOTIFY == event.getEventType()) {
185                     shouldQuit = true;
186                 } else if(event instanceof KeyEvent) {
187                     final KeyEvent keyEvent = (KeyEvent) event;
188                     switch(keyEvent.getKeyChar()) {
189                         case 'q':
190                             shouldQuit = true;
191                             break;
192                         case 'f':
193                             source.setFullscreen(!source.isFullscreen());
194                             break;
195                     }
196                 }
197             }
198         }
199         destroyWindow(null, null, window2, glWindow2);
200         destroyWindow(display, screen, window1, glWindow1);
201     }
202     volatile boolean windowMoved = false;
203     final WindowListener windowMoveDetection = new WindowAdapter() {
204             @Override
205             public void windowMoved(final WindowEvent e) {
206                 windowMoved = true;
207             }
208     };
209 
setDemoFields(final GLEventListener demo, final Window window, final GLWindow glWindow, final boolean debug)210     public static void setDemoFields(final GLEventListener demo, final Window window, final GLWindow glWindow, final boolean debug) {
211         Assert.assertNotNull(demo);
212         Assert.assertNotNull(window);
213         if(debug) {
214             MiscUtils.setFieldIfExists(demo, "glDebug", true);
215             MiscUtils.setFieldIfExists(demo, "glTrace", true);
216         }
217         if(!MiscUtils.setFieldIfExists(demo, "window", window)) {
218             MiscUtils.setFieldIfExists(demo, "glWindow", glWindow);
219         }
220     }
221 
atoi(final String a)222     static int atoi(final String a) {
223         int i=0;
224         try {
225             i = Integer.parseInt(a);
226         } catch (final Exception ex) { ex.printStackTrace(); }
227         return i;
228     }
229 
main(final String args[])230     public static void main(final String args[]) throws IOException {
231         for(int i=0; i<args.length; i++) {
232             if(args[i].equals("-time")) {
233                 durationPerTest = atoi(args[++i]);
234             }
235         }
236         final String tstname = TestParenting02NEWT.class.getName();
237         org.junit.runner.JUnitCore.main(tstname);
238     }
239 
240 }
241