1 /**
2  * Copyright 2013 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.tile;
30 
31 import java.awt.BorderLayout;
32 import java.awt.Button;
33 import java.awt.Container;
34 import java.awt.Dimension;
35 import java.awt.Frame;
36 import java.awt.Label;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.print.PageFormat;
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.lang.reflect.InvocationTargetException;
44 
45 import com.jogamp.opengl.GLCapabilities;
46 import com.jogamp.opengl.GLProfile;
47 import com.jogamp.opengl.awt.GLJPanel;
48 import javax.swing.BorderFactory;
49 import javax.swing.JButton;
50 import javax.swing.JComponent;
51 import javax.swing.JFrame;
52 import javax.swing.JLayeredPane;
53 import javax.swing.JPanel;
54 import javax.swing.SwingUtilities;
55 
56 import org.junit.AfterClass;
57 import org.junit.Assert;
58 import org.junit.BeforeClass;
59 import org.junit.FixMethodOrder;
60 import org.junit.Test;
61 import org.junit.runners.MethodSorters;
62 
63 import com.jogamp.common.os.Platform;
64 import com.jogamp.newt.event.TraceKeyAdapter;
65 import com.jogamp.newt.event.TraceWindowAdapter;
66 import com.jogamp.newt.event.awt.AWTKeyAdapter;
67 import com.jogamp.newt.event.awt.AWTWindowAdapter;
68 import com.jogamp.opengl.test.junit.jogl.demos.gl2.Gears;
69 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
70 import com.jogamp.opengl.test.junit.util.MiscUtils;
71 import com.jogamp.opengl.test.junit.util.QuitAdapter;
72 import com.jogamp.opengl.util.Animator;
73 
74 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
75 public class TestTiledPrintingGearsSwingAWT2 extends TiledPrintingAWTBase  {
76 
77     static boolean waitForKey = false;
78     static GLProfile glp;
79     static int width, height;
80 
81     @BeforeClass
initClass()82     public static void initClass() {
83         if(GLProfile.isAvailable(GLProfile.GL2)) {
84             glp = GLProfile.get(GLProfile.GL2);
85             Assert.assertNotNull(glp);
86             width  = 560; // 640;
87             height = 420; // 480;
88         } else {
89             setTestSupported(false);
90         }
91         // Runtime.getRuntime().traceInstructions(true);
92         // Runtime.getRuntime().traceMethodCalls(true);
93     }
94 
95     @AfterClass
releaseClass()96     public static void releaseClass() {
97     }
98 
runTestGL(final GLCapabilities caps, final boolean addLayout, final boolean layered, final boolean skipGLOrientationVerticalFlip, final boolean useAnim)99     protected void runTestGL(final GLCapabilities caps, final boolean addLayout, final boolean layered, final boolean skipGLOrientationVerticalFlip, final boolean useAnim) throws InterruptedException, InvocationTargetException {
100         final Dimension glc_sz = new Dimension(width, height);
101         final GLJPanel glJPanel1 = new GLJPanel(caps);
102         Assert.assertNotNull(glJPanel1);
103         glJPanel1.setSkipGLOrientationVerticalFlip(skipGLOrientationVerticalFlip);
104         glJPanel1.setMinimumSize(glc_sz);
105         glJPanel1.setPreferredSize(glc_sz);
106         glJPanel1.setBounds(0, 0, glc_sz.width, glc_sz.height);
107         {
108             final Gears demo = new Gears();
109             demo.setFlipVerticalInGLOrientation(skipGLOrientationVerticalFlip);
110             glJPanel1.addGLEventListener(demo);
111         }
112 
113         final JComponent tPanel, demoPanel;
114         if( layered ) {
115             glJPanel1.setOpaque(true);
116             final JButton tb = new JButton("On Top");
117             tb.setBounds(width/2, height/2, 200, 50);
118             if( addLayout ) {
119                 tPanel = null;
120                 final Dimension lsz = new Dimension(width, height);
121                 demoPanel = new JLayeredPane();
122                 demoPanel.setMinimumSize(lsz);
123                 demoPanel.setPreferredSize(lsz);
124                 demoPanel.setBounds(0, 0, lsz.width, lsz.height);
125                 demoPanel.setBorder(BorderFactory.createTitledBorder("Layered Pane"));
126                 demoPanel.add(glJPanel1, JLayeredPane.DEFAULT_LAYER);
127                 demoPanel.add(tb, Integer.valueOf(2));
128             } else {
129                 tPanel = new TransparentPanel();
130                 tPanel.setBounds(0, 0, width, height);
131                 tPanel.setLayout(null);
132                 tPanel.add(tb);
133                 demoPanel = glJPanel1;
134             }
135         } else {
136             tPanel = null;
137             if( addLayout ) {
138                 demoPanel = new JPanel();
139                 demoPanel.add(glJPanel1);
140             } else {
141                 demoPanel = glJPanel1;
142             }
143         }
144 
145         final JFrame frame = new JFrame("Swing Print");
146         Assert.assertNotNull(frame);
147 
148         final ActionListener print72DPIAction = new ActionListener() {
149             public void actionPerformed(final ActionEvent e) {
150                 doPrintManual(frame, 72, 0, -1, -1);
151             } };
152         final ActionListener print150DPIAction = new ActionListener() {
153             public void actionPerformed(final ActionEvent e) {
154                 doPrintManual(frame, 150, -1, -1, -1);
155             } };
156         final ActionListener print300DPIAction = new ActionListener() {
157             public void actionPerformed(final ActionEvent e) {
158                 doPrintManual(frame, 300, -1, -1, -1);
159             } };
160         final Button print72DPIButton = new Button("72dpi");
161         print72DPIButton.addActionListener(print72DPIAction);
162         final Button print150DPIButton = new Button("150dpi");
163         print150DPIButton.addActionListener(print150DPIAction);
164         final Button print300DPIButton = new Button("300dpi");
165         print300DPIButton.addActionListener(print300DPIAction);
166 
167         final JPanel printPanel = new JPanel();
168         printPanel.add(print72DPIButton);
169         printPanel.add(print150DPIButton);
170         printPanel.add(print300DPIButton);
171         final JPanel southPanel = new JPanel();
172         southPanel.add(new Label("South"));
173         final JPanel eastPanel = new JPanel();
174         eastPanel.add(new Label("East"));
175         final JPanel westPanel = new JPanel();
176         westPanel.add(new Label("West"));
177 
178         final Animator animator = useAnim ? new Animator() : null;
179         if( null != animator ) {
180             animator.add(glJPanel1);
181         }
182         final QuitAdapter quitAdapter = new QuitAdapter();
183         new AWTKeyAdapter(new TraceKeyAdapter(quitAdapter), glJPanel1).addTo(glJPanel1);
184         new AWTWindowAdapter(new TraceWindowAdapter(quitAdapter), glJPanel1).addTo(frame);
185 
186         SwingUtilities.invokeAndWait(new Runnable() {
187                 public void run() {
188                     final Container fcont = frame.getContentPane();
189                     if( addLayout ) {
190                         fcont.setLayout(new BorderLayout());
191                         fcont.add(printPanel, BorderLayout.NORTH);
192                         fcont.add(demoPanel, BorderLayout.CENTER);
193                         fcont.add(southPanel, BorderLayout.SOUTH);
194                         fcont.add(eastPanel, BorderLayout.EAST);
195                         fcont.add(westPanel, BorderLayout.WEST);
196                         fcont.validate();
197                         frame.pack();
198                     } else {
199                         frame.setSize(glc_sz);
200                         fcont.setLayout(null);
201                         if( null != tPanel ) {
202                             fcont.add(tPanel);
203                         }
204                         fcont.add(demoPanel);
205                     }
206                     frame.setVisible(true);
207                 } } ) ;
208 
209         Assert.assertEquals(true,  AWTRobotUtil.waitForVisible(frame, true));
210         Assert.assertEquals(true,  AWTRobotUtil.waitForRealized(glJPanel1, true));
211 
212         if( null != animator ) {
213             animator.setUpdateFPSFrames(60, System.err);
214             animator.start();
215             Assert.assertEquals(true, animator.isAnimating());
216         }
217 
218         final long t0 = System.currentTimeMillis();
219         long t1 = t0;
220         boolean printDone = false;
221         while( !quitAdapter.shouldQuit() && ( 0 == duration || ( t1 - t0 ) < duration ) ) {
222             Thread.sleep(200);
223             if( !printDone ) {
224                 printDone = true;
225                 {
226                     // No AA needed for 150 dpi and greater :)
227                     final PrintableBase p = doPrintAuto(frame, PageFormat.PORTRAIT, null, -1 /* offscreen-type */, 150, -1, -1, -1, false);
228                     waitUntilPrintJobsIdle(p);
229                 }
230             }
231             t1 = System.currentTimeMillis();
232         }
233 
234         Assert.assertNotNull(frame);
235         Assert.assertNotNull(glJPanel1);
236 
237         if( null != animator ) {
238             animator.stop();
239             Assert.assertEquals(false, animator.isAnimating());
240         }
241         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
242             public void run() {
243                 frame.setVisible(false);
244             }});
245         Assert.assertEquals(false, frame.isVisible());
246         javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
247             public void run() {
248                 final Frame _frame = frame;
249                 _frame.remove(demoPanel);
250                 _frame.dispose();
251             }});
252     }
253 
254     @Test
test001_flip1_norm_layout0_layered0()255     public void test001_flip1_norm_layout0_layered0() throws InterruptedException, InvocationTargetException {
256         final GLCapabilities caps = new GLCapabilities(glp);
257         runTestGL(caps, false /* addLayout */, false /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
258     }
259 
260     @Test
test002_flip1_norm_layout1_layered0()261     public void test002_flip1_norm_layout1_layered0() throws InterruptedException, InvocationTargetException {
262         final GLCapabilities caps = new GLCapabilities(glp);
263         runTestGL(caps, true /* addLayout */, false /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
264     }
265 
266     @Test
test003_flip1_norm_layout0_layered1()267     public void test003_flip1_norm_layout0_layered1() throws InterruptedException, InvocationTargetException {
268         final GLCapabilities caps = new GLCapabilities(glp);
269         runTestGL(caps, false /* addLayout */, true /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
270     }
271 
272     @Test
test004_flip1_norm_layout1_layered1()273     public void test004_flip1_norm_layout1_layered1() throws InterruptedException, InvocationTargetException {
274         final GLCapabilities caps = new GLCapabilities(glp);
275         runTestGL(caps, true /* addLayout */, true /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
276     }
277 
278     @Test
test011_flip1_bitm_layout0_layered0()279     public void test011_flip1_bitm_layout0_layered0() throws InterruptedException, InvocationTargetException {
280         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
281             return;
282         }
283         final GLCapabilities caps = new GLCapabilities(glp);
284         caps.setBitmap(true);
285         runTestGL(caps, false /* addLayout */, false /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
286     }
287 
288     @Test
test012_flip1_bitm_layout1_layered0()289     public void test012_flip1_bitm_layout1_layered0() throws InterruptedException, InvocationTargetException {
290         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
291             return;
292         }
293         final GLCapabilities caps = new GLCapabilities(glp);
294         caps.setBitmap(true);
295         runTestGL(caps, true /* addLayout */, false /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
296     }
297 
298     @Test
test013_flip1_bitm_layout0_layered1()299     public void test013_flip1_bitm_layout0_layered1() throws InterruptedException, InvocationTargetException {
300         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
301             return;
302         }
303         final GLCapabilities caps = new GLCapabilities(glp);
304         caps.setBitmap(true);
305         runTestGL(caps, false /* addLayout */, true /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
306     }
307 
308     @Test
test014_flip1_bitm_layout1_layered1()309     public void test014_flip1_bitm_layout1_layered1() throws InterruptedException, InvocationTargetException {
310         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
311             return;
312         }
313         final GLCapabilities caps = new GLCapabilities(glp);
314         caps.setBitmap(true);
315         runTestGL(caps, true /* addLayout */, true /* layered */, false /* skipGLOrientationVerticalFlip */, false /* useAnim */);
316     }
317 
318     @Test
test101_flip1_norm_layout0_layered0()319     public void test101_flip1_norm_layout0_layered0() throws InterruptedException, InvocationTargetException {
320         final GLCapabilities caps = new GLCapabilities(glp);
321         runTestGL(caps, false /* addLayout */, false /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
322     }
323 
324     @Test
test102_flip1_norm_layout1_layered0()325     public void test102_flip1_norm_layout1_layered0() throws InterruptedException, InvocationTargetException {
326         final GLCapabilities caps = new GLCapabilities(glp);
327         runTestGL(caps, true /* addLayout */, false /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
328     }
329 
330     @Test
test103_flip1_norm_layout0_layered1()331     public void test103_flip1_norm_layout0_layered1() throws InterruptedException, InvocationTargetException {
332         final GLCapabilities caps = new GLCapabilities(glp);
333         runTestGL(caps, false /* addLayout */, true /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
334     }
335 
336     @Test
test104_flip1_norm_layout1_layered1()337     public void test104_flip1_norm_layout1_layered1() throws InterruptedException, InvocationTargetException {
338         final GLCapabilities caps = new GLCapabilities(glp);
339         runTestGL(caps, true /* addLayout */, true /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
340     }
341 
342     @Test
test111_flip1_bitm_layout0_layered0()343     public void test111_flip1_bitm_layout0_layered0() throws InterruptedException, InvocationTargetException {
344         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
345             return;
346         }
347         final GLCapabilities caps = new GLCapabilities(glp);
348         caps.setBitmap(true);
349         runTestGL(caps, false /* addLayout */, false /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
350     }
351 
352     @Test
test112_flip1_bitm_layout1_layered0()353     public void test112_flip1_bitm_layout1_layered0() throws InterruptedException, InvocationTargetException {
354         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
355             return;
356         }
357         final GLCapabilities caps = new GLCapabilities(glp);
358         caps.setBitmap(true);
359         runTestGL(caps, true /* addLayout */, false /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
360     }
361 
362     @Test
test113_flip1_bitm_layout0_layered1()363     public void test113_flip1_bitm_layout0_layered1() throws InterruptedException, InvocationTargetException {
364         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
365             return;
366         }
367         final GLCapabilities caps = new GLCapabilities(glp);
368         caps.setBitmap(true);
369         runTestGL(caps, false /* addLayout */, true /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
370     }
371 
372     @Test
test114_flip1_bitm_layout1_layered1()373     public void test114_flip1_bitm_layout1_layered1() throws InterruptedException, InvocationTargetException {
374         if( Platform.OSType.WINDOWS != Platform.getOSType() ) {
375             return;
376         }
377         final GLCapabilities caps = new GLCapabilities(glp);
378         caps.setBitmap(true);
379         runTestGL(caps, true /* addLayout */, true /* layered */, true /* skipGLOrientationVerticalFlip */, false /* useAnim */);
380     }
381 
382     static long duration = 500; // ms
383 
main(final String args[])384     public static void main(final String args[]) {
385         for(int i=0; i<args.length; i++) {
386             if(args[i].equals("-time")) {
387                 i++;
388                 duration = MiscUtils.atol(args[i], duration);
389             } else if(args[i].equals("-width")) {
390                 i++;
391                 width = MiscUtils.atoi(args[i], width);
392             } else if(args[i].equals("-height")) {
393                 i++;
394                 height = MiscUtils.atoi(args[i], height);
395             } else if(args[i].equals("-wait")) {
396                 waitForKey = true;
397             }
398         }
399         if(waitForKey) {
400             final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
401             System.err.println("Press enter to continue");
402             try {
403                 System.err.println(stdin.readLine());
404             } catch (final IOException e) { }
405         }
406         org.junit.runner.JUnitCore.main(TestTiledPrintingGearsSwingAWT2.class.getName());
407     }
408 }
409