JOGL v2.6.0-rc-20250712
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestSWTAccessor01.java
Go to the documentation of this file.
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
29package com.jogamp.opengl.test.junit.jogl.swt;
30
31import java.lang.reflect.InvocationTargetException;
32
33import org.eclipse.swt.SWT;
34import org.eclipse.swt.events.PaintEvent;
35import org.eclipse.swt.events.PaintListener;
36import org.eclipse.swt.graphics.Color;
37import org.eclipse.swt.graphics.Rectangle;
38import org.eclipse.swt.layout.FillLayout;
39import org.eclipse.swt.layout.GridData;
40import org.eclipse.swt.layout.GridLayout;
41import org.eclipse.swt.widgets.Canvas;
42import org.eclipse.swt.widgets.Composite;
43import org.eclipse.swt.widgets.Display;
44import org.eclipse.swt.widgets.Shell;
45import org.eclipse.swt.widgets.Text;
46import org.junit.Assert;
47import org.junit.Assume;
48import org.junit.BeforeClass;
49import org.junit.Test;
50import org.junit.FixMethodOrder;
51import org.junit.runners.MethodSorters;
52
53import com.jogamp.common.os.Platform;
54import com.jogamp.junit.util.JunitTracer;
55import com.jogamp.nativewindow.swt.SWTAccessor;
56import com.jogamp.opengl.test.junit.util.MiscUtils;
57import com.jogamp.opengl.test.junit.util.UITestCase;
58
59/**
60 * Tests {@link SWTAccessor#getWindowHandle(org.eclipse.swt.widgets.Control)} on a basic simple SWT window.
61 * <p>
62 * Bug 1362 inspired this unit test, i.e. finding the issue of SWT >= 4.10 + GTK3.
63 * </p>
64 */
65@FixMethodOrder(MethodSorters.NAME_ASCENDING)
66public class TestSWTAccessor01 extends UITestCase {
67
68 static int duration = 250;
69
70 Display display = null;
71 Shell shell = null;
72 Composite composite = null;
73
74 @BeforeClass
75 public static void startup() {
76 if( Platform.getOSType() == Platform.OSType.MACOS ) {
77 // NSLocking issues on OSX and AWT, able to freeze whole test suite!
78 // Since this test is merely a technical nature to validate the accessor w/ SWT
79 // we can drop it w/o bothering.
80 JunitTracer.setTestSupported(false);
81 return;
82 }
83 }
84
85 protected void init() throws InterruptedException, InvocationTargetException {
86 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
87 public void run() {
88 display = new Display();
89 Assert.assertNotNull( display );
90 SWTAccessor.printInfo(System.err, display);
91 shell = new Shell( display );
92 Assert.assertNotNull( shell );
93 shell.setLayout( new GridLayout(3, false) );
94 shell.setBackground(new Color(display, 0, 0, 255));
95 new Text(shell, SWT.NONE).setText("1");
96 new Text(shell, SWT.NONE).setText("2");
97 new Text(shell, SWT.NONE).setText("3");
98 new Text(shell, SWT.NONE).setText("4");
99 composite = new Composite( shell, SWT.NO_BACKGROUND /** | SWT.EMBEDDED */ );
100 composite.setLayout( new FillLayout() );
101 composite.setBackground(new Color(display, 0, 255, 0));
102 final GridData gd = new GridData (GridData.FILL, GridData.FILL, true /* grabExcessHorizontalSpace */, true /* grabExcessVerticalSpace */);
103 composite.setLayoutData(gd);
104 new Text(shell, SWT.NONE).setText("6");
105 new Text(shell, SWT.NONE).setText("7");
106 new Text(shell, SWT.NONE).setText("8");
107 new Text(shell, SWT.NONE).setText("9");
108 Assert.assertNotNull( composite );
109 }});
110 }
111
112 protected void release() throws InterruptedException, InvocationTargetException {
113 Assert.assertNotNull( display );
114 Assert.assertNotNull( shell );
115 Assert.assertNotNull( composite );
116
117 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
118 public void run() {
119 composite.dispose();
120 shell.close();
121 shell.dispose();
122 display.dispose();
123 display = null;
124 shell = null;
125 composite = null;
126 }});
127 }
128
129 protected void runTest() throws InterruptedException, InvocationTargetException {
130 init();
131 final Canvas canvas[] = { null };
132 try {
133 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
134 public void run() {
135 canvas[0] = new Canvas (composite, SWT.NONE);
136 canvas[0].setBackground(new Color(display, 255, 255, 255));
137 canvas[0].setForeground(new Color(display, 255, 0, 0));
138 canvas[0].addPaintListener (new PaintListener() {
139 public void paintControl(final PaintEvent e) {
140 final Rectangle r = canvas[0].getClientArea();
141 e.gc.fillRectangle(0, 0, r.width, r.height);
142 e.gc.drawRectangle(50, 50, r.width-100, r.height-100);
143 e.gc.drawString("I am a Canvas", r.width/2, r.height/2);
144 }});
145 try {
146 System.err.println("Window handle.0 0x"+Long.toHexString(SWTAccessor.getWindowHandle(canvas[0])));
147 } catch (final Exception e) {
148 System.err.println(e.getMessage());
149 }
150 shell.setText( getClass().getName() );
151 shell.setBounds( 0, 0, 700, 700 );
152 shell.open();
153 canvas[0].redraw();
154 }});
155
156 System.err.println("Window handle.1 0x"+Long.toHexString(SWTAccessor.getWindowHandle(canvas[0])));
157
158 final long lStartTime = System.currentTimeMillis();
159 final long lEndTime = lStartTime + duration;
160 try {
161 while( (System.currentTimeMillis() < lEndTime) && !composite.isDisposed() ) {
162 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
163 public void run() {
164 if( !display.readAndDispatch() ) {
165 // blocks on linux .. display.sleep();
166 try {
167 Thread.sleep(10);
168 } catch (final InterruptedException e) { }
169 }
170 }});
171 }
172 SWTAccessor.invokeOnOSTKThread(true, new Runnable() {
173 public void run() {
174 System.err.println("Window handle.X 0x"+Long.toHexString(SWTAccessor.getWindowHandle(canvas[0])));
175 }});
176 }
177 catch( final Throwable throwable ) {
178 throwable.printStackTrace();
179 Assume.assumeNoException( throwable );
180 }
181 } finally {
182 release();
183 }
184 }
185
186 @Test
187 public void test() throws InterruptedException, InvocationTargetException {
188 runTest();
189 }
190
191 public static void main(final String args[]) {
192 for(int i=0; i<args.length; i++) {
193 if(args[i].equals("-time")) {
194 duration = MiscUtils.atoi(args[++i], duration);
195 }
196 }
197 org.junit.runner.JUnitCore.main( TestSWTAccessor01.class.getName() );
198 }
199}
static void invokeOnOSTKThread(final boolean blocking, final Runnable runnable)
Runs the specified action in an SWT compatible OS toolkit thread, which is:
static long getWindowHandle(final Control swtControl)
static void printInfo(final PrintStream out, final Display d)
Tests SWTAccessor#getWindowHandle(org.eclipse.swt.widgets.Control) on a basic simple SWT window.
static int atoi(final String str, final int def)
Definition: MiscUtils.java:57