1 /**
2  * Copyright 2011 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;
30 
31 import java.io.IOException;
32 import java.util.Properties;
33 
34 import com.jogamp.nativewindow.NativeWindowFactory;
35 import com.jogamp.opengl.GLCapabilities;
36 
37 import org.junit.AfterClass;
38 import org.junit.Assert;
39 import org.junit.FixMethodOrder;
40 import org.junit.Test;
41 import org.junit.runners.MethodSorters;
42 
43 import com.jogamp.common.util.IOUtil;
44 import com.jogamp.junit.util.SingletonJunitCase;
45 import com.jogamp.newt.Display;
46 import com.jogamp.newt.Display.PointerIcon;
47 import com.jogamp.newt.opengl.GLWindow;
48 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
49 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
50 import com.jogamp.opengl.test.junit.util.MiscUtils;
51 import com.jogamp.opengl.test.junit.util.QuitAdapter;
52 import com.jogamp.opengl.util.Animator;
53 import com.jogamp.opengl.util.AnimatorBase;
54 
55 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
56 public class TestWindowAndPointerIconNEWT extends SingletonJunitCase {
57 
58     static long duration = 1000; // ms
59 
60     // As early as possible
61     static {
setPointerIcons()62         setPointerIcons();
63     }
64 
setPointerIcons()65     static void setPointerIcons() {
66         final Properties sysp = System.getProperties();
67         sysp.put("jnlp.newt.window.icons", "red-16x16.png red-32x32.png");
68     }
69 
70     @AfterClass
unsetPointerIcons()71     public static void unsetPointerIcons() {
72         final Properties sysp = System.getProperties();
73         sysp.remove("jnlp.newt.window.icons");
74     }
75 
76     @Test
test()77     public void test() throws InterruptedException {
78         final GLWindow glWindow = GLWindow.create(new GLCapabilities(null));
79         Assert.assertNotNull(glWindow);
80 
81         glWindow.setSize(800, 600);
82 
83         final GearsES2 demo = new GearsES2(1);
84         glWindow.addGLEventListener(demo);
85 
86         final QuitAdapter quitAdapter = new QuitAdapter();
87         glWindow.addKeyListener(quitAdapter);
88         glWindow.addWindowListener(quitAdapter);
89 
90         final PointerIcon pointerIcon;
91         {
92             final Display disp = glWindow.getScreen().getDisplay();
93             disp.createNative();
94             final int idx = 0;
95             {
96                 PointerIcon _pointerIcon = null;
97                 final IOUtil.ClassResources res = new IOUtil.ClassResources(new String[] { "arrow-red-alpha-64x64.png" }, glWindow.getClass().getClassLoader(), null);
98                 try {
99                     _pointerIcon = disp.createPointerIcon(res, 0, 0);
100                     System.err.printf("Create PointerIcon #%02d: %s%n", idx, _pointerIcon.toString());
101                 } catch (final Exception e) {
102                     e.printStackTrace();
103                 }
104                 pointerIcon = _pointerIcon;
105             }
106         }
107         glWindow.setPointerIcon(pointerIcon);
108         System.err.println("Set PointerIcon: "+glWindow.getPointerIcon());
109 
110         final Animator animator = new Animator();
111         animator.setModeBits(false, AnimatorBase.MODE_EXPECT_AWT_RENDERING_THREAD);
112         animator.add(glWindow);
113         animator.start();
114 
115         glWindow.setVisible(true);
116         glWindow.warpPointer(3*glWindow.getSurfaceWidth()/4, 3*glWindow.getSurfaceHeight()/4);
117 
118         final long t0 = System.currentTimeMillis();
119         long t1 = t0;
120         while(!quitAdapter.shouldQuit() && t1-t0<duration) {
121             Thread.sleep(100);
122             t1 = System.currentTimeMillis();
123         }
124 
125         animator.stop();
126 
127         glWindow.destroy();
128         if( NativeWindowFactory.isAWTAvailable() ) {
129             Assert.assertEquals(true,  AWTRobotUtil.waitForRealized(glWindow, false));
130         }
131     }
132 
main(final String args[])133     public static void main(final String args[]) throws IOException {
134         for(int i=0; i<args.length; i++) {
135             if(args[i].equals("-time")) {
136                 i++;
137                 duration = MiscUtils.atol(args[i], duration);
138             }
139         }
140         org.junit.runner.JUnitCore.main(TestWindowAndPointerIconNEWT.class.getName());
141     }
142 }
143