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.newt;
30 
31 
32 import org.junit.Assert;
33 import org.junit.Assume;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.junit.FixMethodOrder;
37 import org.junit.runners.MethodSorters;
38 
39 import com.jogamp.nativewindow.*;
40 
41 import com.jogamp.newt.*;
42 import java.io.IOException;
43 
44 import com.jogamp.opengl.test.junit.util.UITestCase;
45 
46 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
47 public class TestRemoteWindow01NEWT extends UITestCase {
48     static int width, height;
49     static String remoteDisplay = "localhost:0.0";
50 
51     @BeforeClass
initClass()52     public static void initClass() {
53         NativeWindowFactory.initSingleton();
54         width  = 640;
55         height = 480;
56     }
57 
createWindow(final Screen screen, final Capabilities caps, final int width, final int height, final boolean onscreen, final boolean undecorated)58     static Window createWindow(final Screen screen, final Capabilities caps, final int width, final int height, final boolean onscreen, final boolean undecorated) {
59         Assert.assertNotNull(caps);
60         caps.setOnscreen(onscreen);
61         // System.out.println("Requested: "+caps);
62 
63         //
64         // Create native windowing resources .. X11/Win/OSX
65         //
66         final Window window = NewtFactory.createWindow(screen, caps);
67         Assert.assertNotNull(window);
68         window.setUndecorated(onscreen && undecorated);
69         window.setSize(width, height);
70         Assert.assertEquals(false,window.isNativeValid());
71         Assert.assertEquals(false,window.isVisible());
72         window.setVisible(true);
73         Assert.assertEquals(true,window.isVisible());
74         Assert.assertEquals(true,window.isNativeValid());
75         // Assert.assertEquals(width,window.getWidth());
76         // Assert.assertEquals(height,window.getHeight());
77         // System.out.println("Created: "+window);
78 
79         //
80         // Create native OpenGL resources .. XGL/WGL/CGL ..
81         // equivalent to GLAutoDrawable methods: setVisible(true)
82         //
83         final CapabilitiesImmutable chosenCapabilities = window.getGraphicsConfiguration().getChosenCapabilities();
84         Assert.assertNotNull(chosenCapabilities);
85         Assert.assertTrue(chosenCapabilities.getGreenBits()>5);
86         Assert.assertTrue(chosenCapabilities.getBlueBits()>5);
87         Assert.assertTrue(chosenCapabilities.getRedBits()>5);
88         Assert.assertEquals(chosenCapabilities.isOnscreen(),onscreen);
89 
90         return window;
91     }
92 
destroyWindow(final Display display, final Screen screen, final Window window)93     static void destroyWindow(final Display display, final Screen screen, final Window window) {
94         if(null!=window) {
95             window.destroy();
96         }
97         if(null!=screen) {
98             screen.destroy();
99         }
100         if(null!=display) {
101             display.destroy();
102         }
103     }
104 
105     @Test
testRemoteWindow01()106     public void testRemoteWindow01() throws InterruptedException {
107         final Capabilities caps = new Capabilities();
108         final Display display1 = NewtFactory.createDisplay(null); // local display
109         final Screen screen1  = NewtFactory.createScreen(display1, 0); // screen 0
110         final Window window1 = createWindow(screen1, caps, width, height, true /* onscreen */, false /* undecorated */);
111         window1.setVisible(true);
112 
113         Assert.assertEquals(true,window1.isNativeValid());
114         Assert.assertEquals(true,window1.isVisible());
115 
116         // Remote Display/Device/Screen/Window ..
117         Display display2;
118         final AbstractGraphicsDevice device2;
119         Screen screen2;
120         Window window2;
121         try {
122             display2 = NewtFactory.createDisplay(remoteDisplay);
123             display2.createNative();
124             screen2  = NewtFactory.createScreen(display2, 0); // screen 0
125             window2 = createWindow(screen2, caps, width, height, true /* onscreen */, false /* undecorated */);
126             window2.setVisible(true);
127         } catch (final NativeWindowException nwe) {
128             System.err.println(nwe);
129             Assume.assumeNoException(nwe);
130             destroyWindow(display1, screen1, window1);
131             return;
132         }
133 
134         Assert.assertEquals(true,window2.isNativeValid());
135         Assert.assertEquals(true,window2.isVisible());
136 
137         Thread.sleep(500); // 500 ms
138 
139         destroyWindow(display1, screen1, window1);
140         destroyWindow(display2, screen2, window2);
141     }
142 
main(final String args[])143     public static void main(final String args[]) throws IOException {
144         for(int i=0; i<args.length; i++) {
145             if(args[i].equals("-display")) {
146                 remoteDisplay = args[++i];
147             }
148         }
149         System.out.println("display: "+remoteDisplay);
150         final String tstname = TestRemoteWindow01NEWT.class.getName();
151         org.junit.runner.JUnitCore.main(tstname);
152     }
153 
154 }
155