PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/test/com/jogamp/opengl/test/junit/jogl/swt/TestBug672NewtCanvasSWTSashFormComposite.java

https://github.com/s6rapala/jogl
Java | 339 lines | 274 code | 35 blank | 30 comment | 32 complexity | 238be75ab08c725754680b022c53b9e0 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, Apache-2.0, GPL-3.0
  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. package com.jogamp.opengl.test.junit.jogl.swt;
  29. import java.io.IOException;
  30. import java.lang.reflect.InvocationTargetException;
  31. import com.jogamp.nativewindow.swt.SWTAccessor;
  32. import com.jogamp.newt.NewtFactory;
  33. import com.jogamp.newt.event.KeyAdapter;
  34. import com.jogamp.newt.event.KeyEvent;
  35. import com.jogamp.newt.event.WindowEvent;
  36. import com.jogamp.newt.event.WindowAdapter;
  37. import com.jogamp.newt.opengl.GLWindow;
  38. import com.jogamp.newt.swt.NewtCanvasSWT;
  39. import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
  40. import com.jogamp.opengl.test.junit.util.MiscUtils;
  41. import com.jogamp.opengl.test.junit.util.UITestCase;
  42. import com.jogamp.opengl.test.junit.util.QuitAdapter;
  43. import com.jogamp.opengl.util.Animator;
  44. import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
  45. import javax.media.nativewindow.util.Dimension;
  46. import javax.media.nativewindow.util.Point;
  47. import javax.media.nativewindow.util.PointImmutable;
  48. import javax.media.nativewindow.util.DimensionImmutable;
  49. import javax.media.opengl.GLCapabilities;
  50. import javax.media.opengl.GLCapabilitiesImmutable;
  51. import javax.media.opengl.GLProfile;
  52. import org.eclipse.swt.SWT;
  53. import org.eclipse.swt.custom.SashForm;
  54. import org.eclipse.swt.layout.FillLayout;
  55. import org.eclipse.swt.widgets.Composite;
  56. import org.eclipse.swt.widgets.Display;
  57. import org.eclipse.swt.widgets.Shell;
  58. import org.junit.After;
  59. import org.junit.Assert;
  60. import org.junit.Assume;
  61. import org.junit.Before;
  62. import org.junit.BeforeClass;
  63. import org.junit.AfterClass;
  64. import org.junit.Test;
  65. import org.junit.FixMethodOrder;
  66. import org.junit.runners.MethodSorters;
  67. @FixMethodOrder(MethodSorters.NAME_ASCENDING)
  68. public class TestBug672NewtCanvasSWTSashFormComposite extends UITestCase {
  69. static int screenIdx = 0;
  70. static PointImmutable wpos;
  71. static DimensionImmutable wsize, rwsize = null;
  72. static long duration = 500; // ms
  73. @BeforeClass
  74. public static void initClass() {
  75. if(null == wsize) {
  76. wsize = new Dimension(640, 480);
  77. }
  78. }
  79. @AfterClass
  80. public static void releaseClass() {
  81. }
  82. Display display = null;
  83. Shell shell = null;
  84. Composite composite = null;
  85. SashForm sash = null;
  86. Composite innerComposite = null;
  87. com.jogamp.newt.Display swtNewtDisplay = null;
  88. @Before
  89. public void init() {
  90. SWTAccessor.invoke(true, new Runnable() {
  91. public void run() {
  92. display = new Display();
  93. Assert.assertNotNull( display );
  94. }});
  95. display.syncExec(new Runnable() {
  96. public void run() {
  97. shell = new Shell( display );
  98. Assert.assertNotNull( shell );
  99. shell.setLayout( new FillLayout() );
  100. composite = new Composite( shell, SWT.NONE );
  101. composite.setLayout( new FillLayout() );
  102. Assert.assertNotNull( composite );
  103. sash = new SashForm(composite, SWT.NONE);
  104. Assert.assertNotNull( sash );
  105. final org.eclipse.swt.widgets.Label c = new org.eclipse.swt.widgets.Label(sash, SWT.NONE);
  106. c.setText("Left cell");
  107. innerComposite = new Composite(sash, SWT.NONE);
  108. Assert.assertNotNull( innerComposite );
  109. innerComposite.setLayout( new FillLayout() );
  110. }});
  111. swtNewtDisplay = NewtFactory.createDisplay(null, false); // no-reuse
  112. }
  113. @After
  114. public void release() {
  115. Assert.assertNotNull( display );
  116. Assert.assertNotNull( shell );
  117. Assert.assertNotNull( composite );
  118. Assert.assertNotNull( sash );
  119. Assert.assertNotNull( innerComposite );
  120. try {
  121. display.syncExec(new Runnable() {
  122. public void run() {
  123. innerComposite.dispose();
  124. sash.dispose();
  125. composite.dispose();
  126. shell.dispose();
  127. }});
  128. SWTAccessor.invoke(true, new Runnable() {
  129. public void run() {
  130. display.dispose();
  131. }});
  132. }
  133. catch( Throwable throwable ) {
  134. throwable.printStackTrace();
  135. Assume.assumeNoException( throwable );
  136. }
  137. swtNewtDisplay = null;
  138. display = null;
  139. shell = null;
  140. composite = null;
  141. sash = null;
  142. innerComposite = null;
  143. }
  144. class WaitAction implements Runnable {
  145. private final long sleepMS;
  146. WaitAction(long sleepMS) {
  147. this.sleepMS = sleepMS;
  148. }
  149. public void run() {
  150. if( !display.readAndDispatch() ) {
  151. // blocks on linux .. display.sleep();
  152. try {
  153. Thread.sleep(sleepMS);
  154. } catch (InterruptedException e) { }
  155. }
  156. }
  157. }
  158. final WaitAction awtRobotWaitAction = new WaitAction(AWTRobotUtil.TIME_SLICE);
  159. final WaitAction generalWaitAction = new WaitAction(10);
  160. protected void runTestGL(GLCapabilitiesImmutable caps) throws InterruptedException, InvocationTargetException {
  161. com.jogamp.newt.Screen screen = NewtFactory.createScreen(swtNewtDisplay, screenIdx);
  162. final GLWindow glWindow = GLWindow.create(screen, caps);
  163. Assert.assertNotNull(glWindow);
  164. final GearsES2 demo = new GearsES2(1);
  165. glWindow.addGLEventListener(demo);
  166. Animator animator = new Animator();
  167. animator.setModeBits(false, Animator.MODE_EXPECT_AWT_RENDERING_THREAD);
  168. QuitAdapter quitAdapter = new QuitAdapter();
  169. //glWindow.addKeyListener(new TraceKeyAdapter(quitAdapter));
  170. //glWindow.addWindowListener(new TraceWindowAdapter(quitAdapter));
  171. glWindow.addKeyListener(quitAdapter);
  172. glWindow.addWindowListener(quitAdapter);
  173. glWindow.addWindowListener(new WindowAdapter() {
  174. public void windowResized(WindowEvent e) {
  175. System.err.println("window resized: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight());
  176. }
  177. public void windowMoved(WindowEvent e) {
  178. System.err.println("window moved: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight());
  179. }
  180. });
  181. glWindow.addKeyListener(new KeyAdapter() {
  182. public void keyReleased(KeyEvent e) {
  183. if( !e.isPrintableKey() || e.isAutoRepeat() ) {
  184. return;
  185. }
  186. if(e.getKeyChar()=='f') {
  187. new Thread() {
  188. public void run() {
  189. final Thread t = glWindow.setExclusiveContextThread(null);
  190. System.err.println("[set fullscreen pre]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
  191. glWindow.setFullscreen(!glWindow.isFullscreen());
  192. System.err.println("[set fullscreen post]: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", f "+glWindow.isFullscreen()+", a "+glWindow.isAlwaysOnTop()+", "+glWindow.getInsets());
  193. glWindow.setExclusiveContextThread(t);
  194. } }.start();
  195. }
  196. }
  197. });
  198. animator.add(glWindow);
  199. animator.start();
  200. Assert.assertTrue(animator.isStarted());
  201. Assert.assertTrue(animator.isAnimating());
  202. animator.setUpdateFPSFrames(60, null);
  203. final NewtCanvasSWT canvas1 = NewtCanvasSWT.create( innerComposite, 0, glWindow );
  204. Assert.assertNotNull( canvas1 );
  205. display.syncExec( new Runnable() {
  206. public void run() {
  207. shell.setText( getSimpleTestName(".") );
  208. shell.setSize( wsize.getWidth(), wsize.getHeight() );
  209. if( null != wpos ) {
  210. shell.setLocation( wpos.getX(), wpos.getY() );
  211. }
  212. shell.open();
  213. }
  214. });
  215. Assert.assertTrue("GLWindow didn't become visible natively!", AWTRobotUtil.waitForRealized(glWindow, awtRobotWaitAction, true));
  216. Assert.assertNotNull( canvas1.getNativeWindow() );
  217. System.err.println("NW chosen: "+glWindow.getDelegatedWindow().getChosenCapabilities());
  218. System.err.println("GL chosen: "+glWindow.getChosenCapabilities());
  219. System.err.println("window pos/siz.0: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets());
  220. System.err.println("GLWindow LOS.0: "+glWindow.getLocationOnScreen(null));
  221. System.err.println("NewtCanvasSWT LOS.0: "+canvas1.getNativeWindow().getLocationOnScreen(null));
  222. if( null != rwsize ) {
  223. for(int i=0; i<50; i++) { // 500 ms dispatched delay
  224. generalWaitAction.run();
  225. }
  226. display.syncExec( new Runnable() {
  227. public void run() {
  228. shell.setSize( rwsize.getWidth(), rwsize.getHeight() );
  229. }
  230. });
  231. System.err.println("window resize pos/siz.1: "+glWindow.getX()+"/"+glWindow.getY()+" "+glWindow.getWidth()+"x"+glWindow.getHeight()+", "+glWindow.getInsets());
  232. System.err.println("GLWindow LOS.1: "+glWindow.getLocationOnScreen(null));
  233. System.err.println("NewtCanvasSWT LOS.1: "+canvas1.getNativeWindow().getLocationOnScreen(null));
  234. }
  235. final PointImmutable pSashRightClient = new Point(wsize.getWidth(), 0);
  236. final PointImmutable pNatWinLOS = canvas1.getNativeWindow().getLocationOnScreen(null);
  237. final PointImmutable pGLWinLOS = glWindow.getLocationOnScreen(null);
  238. System.err.println("GLWindow LOS: "+pGLWinLOS);
  239. System.err.println("NewtCanvasSWT LOS: "+pNatWinLOS);
  240. Assert.assertTrue( "NewtCanvasAWT LOS "+pNatWinLOS+" not >= sash-right "+pSashRightClient, pNatWinLOS.compareTo(pSashRightClient) >= 0 );
  241. Assert.assertTrue( "GLWindow LOS "+pGLWinLOS+" not >= sash-right "+pSashRightClient, pGLWinLOS.compareTo(pSashRightClient) >= 0 );
  242. while( !quitAdapter.shouldQuit() && animator.isAnimating() && animator.getTotalFPSDuration()<duration ) {
  243. generalWaitAction.run();
  244. }
  245. animator.stop();
  246. Assert.assertFalse(animator.isAnimating());
  247. Assert.assertFalse(animator.isStarted());
  248. Assert.assertEquals(null, glWindow.getExclusiveContextThread());
  249. canvas1.dispose();
  250. glWindow.destroy();
  251. Assert.assertEquals(true, AWTRobotUtil.waitForRealized(glWindow, false));
  252. }
  253. @Test
  254. public void test01() throws InterruptedException, InvocationTargetException {
  255. final GLProfile glp = GLProfile.getGL2ES2();
  256. final GLCapabilities caps = new GLCapabilities( glp );
  257. runTestGL(caps);
  258. }
  259. public static void main(String args[]) throws IOException {
  260. int x=0, y=0, w=640, h=480, rw=-1, rh=-1;
  261. boolean usePos = false;
  262. for(int i=0; i<args.length; i++) {
  263. if(args[i].equals("-time")) {
  264. i++;
  265. duration = MiscUtils.atol(args[i], duration);
  266. } else if(args[i].equals("-width")) {
  267. i++;
  268. w = MiscUtils.atoi(args[i], w);
  269. } else if(args[i].equals("-height")) {
  270. i++;
  271. h = MiscUtils.atoi(args[i], h);
  272. } else if(args[i].equals("-x")) {
  273. i++;
  274. x = MiscUtils.atoi(args[i], x);
  275. usePos = true;
  276. } else if(args[i].equals("-y")) {
  277. i++;
  278. y = MiscUtils.atoi(args[i], y);
  279. usePos = true;
  280. } else if(args[i].equals("-rwidth")) {
  281. i++;
  282. rw = MiscUtils.atoi(args[i], rw);
  283. } else if(args[i].equals("-rheight")) {
  284. i++;
  285. rh = MiscUtils.atoi(args[i], rh);
  286. } else if(args[i].equals("-screen")) {
  287. i++;
  288. screenIdx = MiscUtils.atoi(args[i], 0);
  289. }
  290. }
  291. wsize = new Dimension(w, h);
  292. if( 0 < rw && 0 < rh ) {
  293. rwsize = new Dimension(rw, rh);
  294. }
  295. if(usePos) {
  296. wpos = new Point(x, y);
  297. }
  298. System.err.println("position "+wpos);
  299. System.err.println("size "+wsize);
  300. System.err.println("resize "+rwsize);
  301. System.err.println("screen "+screenIdx);
  302. org.junit.runner.JUnitCore.main(TestBug672NewtCanvasSWTSashFormComposite.class.getName());
  303. }
  304. }