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.mm;
30 
31 import java.io.IOException;
32 import com.jogamp.opengl.GLCapabilities;
33 import com.jogamp.opengl.GLProfile;
34 
35 import com.jogamp.opengl.util.Animator;
36 
37 import org.junit.Assert;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.FixMethodOrder;
41 import org.junit.runners.MethodSorters;
42 
43 import com.jogamp.common.os.Platform;
44 import com.jogamp.newt.Display;
45 import com.jogamp.newt.MonitorDevice;
46 import com.jogamp.newt.NewtFactory;
47 import com.jogamp.newt.Screen;
48 import com.jogamp.newt.Window;
49 import com.jogamp.newt.MonitorMode;
50 import com.jogamp.newt.opengl.GLWindow;
51 import com.jogamp.newt.util.MonitorModeUtil;
52 import com.jogamp.opengl.test.junit.jogl.demos.es2.GearsES2;
53 import com.jogamp.opengl.test.junit.util.AWTRobotUtil;
54 import com.jogamp.opengl.test.junit.util.UITestCase;
55 import java.util.List;
56 import com.jogamp.nativewindow.util.Dimension;
57 
58 /**
59  * Tests MonitorMode change w/ changed rotation,
60  * w/ and w/o fullscreen, pre and post MonitorMode change.
61  * <p>
62  * MonitorMode change does not use highest resolution.
63  * </p>
64  */
65 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
66 public class TestScreenMode02aNEWT extends UITestCase {
67     static GLProfile glp;
68     static int width, height;
69 
70     static int waitTimeShort = 4000; // 4 sec
71     static int waitTimeLong = 8000; // 8 sec
72 
73     @BeforeClass
initClass()74     public static void initClass() {
75         setResetXRandRIfX11AfterClass();
76         width  = 640;
77         height = 480;
78         glp = GLProfile.getDefault();
79     }
80 
createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height, final boolean onscreen, final boolean undecorated)81     static GLWindow createWindow(final Screen screen, final GLCapabilities caps, final int width, final int height, final boolean onscreen, final boolean undecorated) {
82         Assert.assertNotNull(caps);
83         caps.setOnscreen(onscreen);
84 
85         final GLWindow window = GLWindow.create(screen, caps);
86         window.setSize(width, height);
87         window.addGLEventListener(new GearsES2(1));
88         Assert.assertNotNull(window);
89         return window;
90     }
91 
destroyWindow(final Window window)92     static void destroyWindow(final Window window) throws InterruptedException {
93         if(null!=window) {
94             window.destroy();
95             Assert.assertTrue(AWTRobotUtil.waitForRealized(window, false));
96         }
97     }
98 
99     @Test
testScreenRotationChange01_PreWin()100     public void testScreenRotationChange01_PreWin() throws InterruptedException {
101         testScreenRotationChangeImpl(true, true, false);
102     }
103 
104     @Test
testScreenRotationChange02_PreFull()105     public void testScreenRotationChange02_PreFull() throws InterruptedException {
106         testScreenRotationChangeImpl(true, true, true);
107     }
108 
109     @Test
testScreenRotationChange11_PostWin()110     public void testScreenRotationChange11_PostWin() throws InterruptedException {
111         testScreenRotationChangeImpl(true, false, false);
112     }
113 
114     @Test
testScreenRotationChange12_PostFull()115     public void testScreenRotationChange12_PostFull() throws InterruptedException {
116         testScreenRotationChangeImpl(true, false, true);
117     }
118 
testScreenRotationChangeImpl(final boolean changeMode, final boolean preVis, final boolean fullscreen)119     void testScreenRotationChangeImpl(final boolean changeMode, final boolean preVis, final boolean fullscreen) throws InterruptedException {
120         final GLCapabilities caps = new GLCapabilities(glp);
121         Assert.assertNotNull(caps);
122         final Display display = NewtFactory.createDisplay(null); // local display
123         Assert.assertNotNull(display);
124         final Screen screen  = NewtFactory.createScreen(display, 0); // screen 0
125         Assert.assertNotNull(screen);
126         final GLWindow window = createWindow(screen, caps, width, height, true /* onscreen */, false /* undecorated */);
127         Assert.assertNotNull(window);
128         if( preVis ) {
129             window.setVisible(true);
130             if( fullscreen ) {
131                 window.setFullscreen(true);
132             }
133         } else {
134             screen.createNative();
135             Assert.assertEquals(true,display.isNativeValid());
136             Assert.assertEquals(true,screen.isNativeValid());
137         }
138 
139         final Animator animator = new Animator(window);
140         animator.start();
141 
142         final MonitorDevice monitor = window.getMainMonitor();
143         final MonitorMode mmOrig = monitor.getOriginalMode();
144         Assert.assertNotNull(mmOrig);
145         if(changeMode) {
146             Thread.sleep(waitTimeShort);
147 
148             List<MonitorMode> monitorModes = monitor.getSupportedModes();
149             if(monitorModes.size()==1) {
150                 // no support ..
151                 System.err.println("Your platform has no ScreenMode change support, sorry");
152                 animator.stop();
153                 destroyWindow(window);
154                 return;
155             }
156             Assert.assertTrue(monitorModes.size()>0);
157 
158             MonitorMode mmCurrent = monitor.getCurrentMode();
159             Assert.assertNotNull(mmCurrent);
160             System.err.println("[0] orig   : "+mmOrig);
161             System.err.println("[0] current: "+mmCurrent);
162             Assert.assertEquals(mmCurrent, mmOrig);
163 
164             monitorModes = MonitorModeUtil.filterByFlags(monitorModes, 0); // no interlace, double-scan etc
165             Assert.assertNotNull(monitorModes);
166             Assert.assertTrue(monitorModes.size()>0);
167             monitorModes = MonitorModeUtil.filterByRotation(monitorModes, 90);
168             if(null==monitorModes || Platform.getOSType() == Platform.OSType.MACOS ) {
169                 // no rotation support ..
170                 System.err.println("Your platform has no rotation support, sorry");
171                 animator.stop();
172                 destroyWindow(window);
173                 return;
174             }
175             monitorModes = MonitorModeUtil.filterByResolution(monitorModes, new Dimension(801, 601));
176             Assert.assertNotNull(monitorModes);
177             Assert.assertTrue(monitorModes.size()>0);
178             monitorModes = MonitorModeUtil.filterByRate(monitorModes, mmOrig.getRefreshRate());
179             Assert.assertNotNull(monitorModes);
180             Assert.assertTrue(monitorModes.size()>0);
181             monitorModes = MonitorModeUtil.getHighestAvailableBpp(monitorModes);
182             Assert.assertNotNull(monitorModes);
183             Assert.assertTrue(monitorModes.size()>0);
184 
185             // set mode
186             {
187                 final MonitorMode mm = monitorModes.get(0);
188                 System.err.println("[0] set current: "+mm);
189                 final boolean smOk = monitor.setCurrentMode(mm);
190                 mmCurrent = monitor.getCurrentMode();
191                 System.err.println("[0] has current: "+mmCurrent+", changeOK "+smOk);
192                 Assert.assertTrue(monitor.isModeChangedByUs());
193                 Assert.assertEquals(mm, mmCurrent);
194                 Assert.assertNotSame(mmOrig, mmCurrent);
195                 Assert.assertEquals(mmCurrent, monitor.queryCurrentMode());
196                 Assert.assertTrue(smOk);
197             }
198         }
199 
200         if( !preVis ) {
201             if( fullscreen ) {
202                 window.setFullscreen(true);
203             }
204             window.setVisible(true);
205         }
206 
207         Thread.sleep(waitTimeLong);
208 
209         if( !preVis && fullscreen ) {
210             window.setFullscreen(false);
211         }
212 
213         if(changeMode) {
214             Thread.sleep(waitTimeShort);
215 
216             // manual restore!
217             {
218                 System.err.println("[1] set orig: "+mmOrig);
219                 final boolean smOk = monitor.setCurrentMode(mmOrig);
220                 final MonitorMode mmCurrent = monitor.getCurrentMode();
221                 System.err.println("[1] has orig?: "+mmCurrent+", changeOK "+smOk);
222                 Assert.assertFalse(monitor.isModeChangedByUs());
223                 Assert.assertEquals(mmOrig, mmCurrent);
224                 Assert.assertTrue(smOk);
225             }
226             Thread.sleep(waitTimeShort);
227         }
228 
229         if( preVis && fullscreen ) {
230             window.setFullscreen(false);
231         }
232 
233         Assert.assertEquals(true,display.isNativeValid());
234         Assert.assertEquals(true,screen.isNativeValid());
235         Assert.assertEquals(true,window.isNativeValid());
236         Assert.assertEquals(true,window.isVisible());
237 
238         animator.stop();
239         destroyWindow(window);
240 
241         Assert.assertEquals(false,window.isVisible());
242         Assert.assertEquals(false,window.isNativeValid());
243         Assert.assertTrue(AWTRobotUtil.waitForRealized(screen, false));
244         Assert.assertEquals(false,screen.isNativeValid());
245         Assert.assertEquals(false,display.isNativeValid());
246     }
247 
main(final String args[])248     public static void main(final String args[]) throws IOException {
249         final String tstname = TestScreenMode02aNEWT.class.getName();
250         org.junit.runner.JUnitCore.main(tstname);
251     }
252 
253 }
254