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.jogl.awt; 30 31 import com.jogamp.opengl.GLProfile; 32 import com.jogamp.opengl.awt.GLCanvas; 33 34 import com.jogamp.opengl.util.Animator; 35 import com.jogamp.opengl.test.junit.util.UITestCase; 36 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2; 37 import com.jogamp.opengl.test.junit.util.MiscUtils; 38 39 import java.awt.Dimension; 40 import java.awt.Frame; 41 import java.awt.Label; 42 43 import jogamp.nativewindow.SurfaceScaleUtils; 44 45 import org.junit.Assert; 46 import org.junit.Assume; 47 import org.junit.Before; 48 import org.junit.BeforeClass; 49 import org.junit.After; 50 import org.junit.Test; 51 import org.junit.FixMethodOrder; 52 import org.junit.runners.MethodSorters; 53 54 55 @FixMethodOrder(MethodSorters.NAME_ASCENDING) 56 public class TestAWT03GLCanvasRecreate01 extends UITestCase { 57 static long durationPerTest = 500; // ms 58 59 final static int sizeEps = 64; 60 final static Dimension size1 = new Dimension(512, 512-sizeEps-1); 61 final static Dimension size2 = new Dimension(512+sizeEps+1+256, 512+256); 62 final static Dimension size3 = new Dimension(512-256, 512-sizeEps-1-256); 63 64 Frame frame1=null; 65 Frame frame2=null; 66 Frame frame3=null; 67 GLCanvas glComp=null; 68 Label label1 = null; 69 Label label2 = null; 70 Label label3 = null; 71 Animator animator = null; 72 73 @BeforeClass startup()74 public static void startup() { 75 System.out.println("GLProfile "+GLProfile.glAvailabilityToString()); 76 } 77 78 @Before init()79 public void init() { 80 glComp = new GLCanvas(); 81 Assert.assertNotNull(glComp); 82 glComp.addGLEventListener(new GearsES2()); 83 84 animator = new Animator(glComp); 85 animator.start(); 86 87 label1 = new Label("L1 - No GLCanvas"); 88 label1.setMinimumSize(size1); 89 label1.setPreferredSize(size1); 90 frame1 = new Frame("Frame 1"); 91 Assert.assertNotNull(frame1); 92 frame1.add(label1); 93 frame1.setLocation(0, 0); 94 95 label2 = new Label("L2 - No GLCanvas"); 96 label2.setMinimumSize(size2); 97 label2.setPreferredSize(size2); 98 frame2 = new Frame("Frame 2"); 99 Assert.assertNotNull(frame2); 100 frame2.add(label2); 101 frame2.setLocation(size1.width + size1.width/2, 0); 102 103 label3 = new Label("L3 - No GLCanvas"); 104 label3.setMinimumSize(size3); 105 label3.setPreferredSize(size3); 106 frame3 = new Frame("Frame 3"); 107 Assert.assertNotNull(frame3); 108 frame3.add(label3); 109 frame3.setLocation(0, size1.height + size1.height/2); 110 } 111 112 @After release()113 public void release() { 114 Assert.assertNotNull(frame1); 115 Assert.assertNotNull(frame2); 116 Assert.assertNotNull(glComp); 117 try { 118 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 119 public void run() { 120 glComp.destroy(); 121 frame1.dispose(); 122 frame2.dispose(); 123 frame3.dispose(); 124 }}); 125 } catch (final Throwable t) { 126 t.printStackTrace(); 127 Assume.assumeNoException(t); 128 } 129 frame1=null; 130 frame2=null; 131 frame3=null; 132 glComp=null; 133 134 animator.stop(); 135 animator=null; 136 } 137 addCanvas(final Frame frame, final Label label, final Dimension size)138 private void addCanvas(final Frame frame, final Label label, final Dimension size) { 139 try { 140 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 141 public void run() { 142 frame.remove(label); 143 glComp.setPreferredSize(size); 144 glComp.setMinimumSize(size); 145 frame.add(glComp); 146 frame.pack(); 147 }}); 148 } catch (final Throwable t) { 149 t.printStackTrace(); 150 Assume.assumeNoException(t); 151 } 152 } 153 removeCanvas(final Frame frame, final Label label)154 private void removeCanvas(final Frame frame, final Label label) { 155 try { 156 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 157 public void run() { 158 frame.remove(glComp); 159 frame.add(label); 160 frame.pack(); 161 frame.repaint(); 162 }}); 163 } catch (final Throwable t) { 164 t.printStackTrace(); 165 Assume.assumeNoException(t); 166 } 167 } 168 setVisible(final Frame frame, final boolean v)169 private void setVisible(final Frame frame, final boolean v) { 170 try { 171 javax.swing.SwingUtilities.invokeAndWait(new Runnable() { 172 public void run() { 173 frame.pack(); 174 frame.setVisible(v); 175 }}); 176 } catch (final Throwable t) { 177 t.printStackTrace(); 178 Assume.assumeNoException(t); 179 } 180 } 181 assertSize(final Dimension expSize)182 private void assertSize(final Dimension expSize) { 183 final float[] scale = { 1f, 1f }; 184 glComp.getMaximumSurfaceScale(scale); 185 186 final Dimension hasSize = glComp.getSize(null); 187 188 Assert.assertTrue("AWT Size.width mismatch: expected "+expSize+", has "+hasSize, 189 Math.abs(expSize.width-hasSize.width) <= sizeEps); 190 Assert.assertTrue("AWT Size.height mismatch: expected "+expSize+", has "+hasSize, 191 Math.abs(expSize.height-hasSize.height) <= sizeEps); 192 193 final int expSurfWidth = SurfaceScaleUtils.scale(expSize.width, scale[0]); 194 final int expSurfHeight = SurfaceScaleUtils.scale(expSize.height, scale[0]); 195 final int hasSurfWidth = glComp.getSurfaceWidth(); 196 final int hasSurfHeight = glComp.getSurfaceHeight(); 197 198 Assert.assertTrue("GL Size.width mismatch: expected "+expSurfWidth+", has "+hasSurfWidth, 199 Math.abs(expSurfWidth-hasSurfWidth) <= sizeEps); 200 Assert.assertTrue("GL Size.height mismatch: expected "+expSurfHeight+", has "+hasSurfHeight, 201 Math.abs(expSurfHeight-hasSurfHeight) <= sizeEps); 202 } 203 204 @Test testAddRemove3Times()205 public void testAddRemove3Times() throws InterruptedException { 206 setVisible(frame1, true); 207 setVisible(frame2, true); 208 setVisible(frame3, true); 209 210 // Init Frame 1 211 addCanvas(frame1, label1, size1); 212 Thread.sleep(durationPerTest); 213 assertSize(size1); 214 215 // Frame 1 -> Frame 2 216 removeCanvas(frame1, label1); 217 addCanvas(frame2, label2, size2); 218 Thread.sleep(durationPerTest); 219 assertSize(size2); 220 221 // Frame 2 -> Frame 3 222 removeCanvas(frame2, label2); 223 addCanvas(frame3, label3, size3); 224 Thread.sleep(durationPerTest); 225 assertSize(size3); 226 227 // Frame 3 -> Frame 1 228 removeCanvas(frame3, label3); 229 addCanvas(frame1, label1, size1); 230 Thread.sleep(durationPerTest); 231 assertSize(size1); 232 } 233 main(final String args[])234 public static void main(final String args[]) { 235 for(int i=0; i<args.length; i++) { 236 if(args[i].equals("-time")) { 237 durationPerTest = MiscUtils.atoi(args[++i], (int)durationPerTest); 238 } 239 } 240 org.junit.runner.JUnitCore.main(TestAWT03GLCanvasRecreate01.class.getName()); 241 } 242 } 243