1 /* 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.stage; 27 28 import javafx.scene.Group; 29 import javafx.scene.Scene; 30 import com.sun.javafx.pgstub.StubStage; 31 import com.sun.javafx.pgstub.StubToolkit; 32 import com.sun.javafx.pgstub.StubToolkit.ScreenConfiguration; 33 import com.sun.javafx.tk.Toolkit; 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import static junit.framework.Assert.assertEquals; 38 import static junit.framework.Assert.assertFalse; 39 import static junit.framework.Assert.assertTrue; 40 41 public class StageTest { 42 43 private StubToolkit toolkit; 44 private Stage s; 45 private StubStage peer; 46 47 private int initialNumTimesSetSizeAndLocation; 48 49 @Before 50 public void setUp() { 51 toolkit = (StubToolkit) Toolkit.getToolkit(); 52 s = new Stage(); 53 s.show(); 54 peer = (StubStage) s.impl_getPeer(); 55 initialNumTimesSetSizeAndLocation = peer.numTimesSetSizeAndLocation; 56 } 57 58 @After 59 public void tearDown() { 60 s.hide(); 61 } 62 63 private void pulse() { 64 toolkit.fireTestPulse(); 65 } 66 67 /** 68 * Simple test which checks whether changing the x/y position of the Stage 69 * ends up invoking the appropriate methods on the TKStage interface. 70 */ 71 public @Test void testMovingStage() { 72 s.setX(100); 73 pulse(); 74 assertEquals(100f, peer.x); 75 // Setting X should result in a single call to peer.setBounds() 76 assertEquals(1, peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation); 77 } 78 79 /** 80 * Simple test which checks whether changing the w/h size of the Stage 81 * ends up invoking the appropriate methods on the TKStage interface. 82 */ 83 public @Test void testResizingStage() { 84 s.setWidth(100); 85 s.setHeight(100); 86 pulse(); 87 assertEquals(100f, peer.width); 88 assertEquals(100f, peer.height); 89 // Setting W and H should result in a single call to peer.setBounds() 90 assertEquals(1, peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation); 91 } 92 93 /** 94 * Simple test which checks whether changing the w/h size and x/y position of the Stage 95 * ends up invoking the appropriate methods on the TKStage interface. 96 */ 97 public @Test void testMovingAndResizingStage() { 98 s.setX(101); 99 s.setY(102); 100 s.setWidth(103); 101 s.setHeight(104); 102 pulse(); 103 assertEquals(101f, peer.x); 104 assertEquals(102f, peer.y); 105 assertEquals(103f, peer.width); 106 assertEquals(104f, peer.height); 107 // Setting X, Y, W and H should result in a single call to peer.setBounds() 108 assertEquals(1, peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation); 109 } 110 111 /** 112 * Simple test which checks whether changing the minimum w/h of the Stage 113 * resize the window if necessary 114 */ 115 public @Test void testResizingTooSmallStage() { 116 s.setWidth(60); 117 s.setHeight(70); 118 s.setMinWidth(150); 119 s.setMinHeight(140); 120 pulse(); 121 assertEquals(150.0, peer.width, 0.0001); 122 assertEquals(140.0, peer.height, 0.0001); 123 } 124 125 /** 126 * Simple test which checks whether changing the maximum w/h of the Stage 127 * resize the window if necessary 128 */ 129 public @Test void testResizingTooBigStage() { 130 s.setWidth(100); 131 s.setHeight(100); 132 s.setMaxWidth(60); 133 s.setMaxHeight(70); 134 pulse(); 135 assertEquals(60.0, peer.width, 0.0001); 136 assertEquals(70.0, peer.height, 0.0001); 137 } 138 139 /** 140 * Test to make sure that when we initialize, the Stage doesn't notify 141 * the peer of size and location more than once. 142 */ 143 public @Test void testSizeAndLocationChangedOverTime() { 144 pulse(); 145 assertTrue((peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation) <= 1); 146 initialNumTimesSetSizeAndLocation = peer.numTimesSetSizeAndLocation; 147 // Oncethe width/height is set it is synced once on pulse 148 s.setWidth(300); 149 s.setHeight(400); 150 pulse(); 151 assertEquals(300f, peer.width); 152 assertEquals(400f, peer.height); 153 assertEquals(1, peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation); 154 // Setting y will trigger one more sync 155 s.setY(200); 156 pulse(); 157 assertEquals(200f, peer.y); 158 assertEquals(2, peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation); 159 // .. same for setting x 160 s.setX(100); 161 pulse(); 162 assertEquals(100f, peer.x); 163 assertEquals(3, peer.numTimesSetSizeAndLocation - initialNumTimesSetSizeAndLocation); 164 } 165 166 @Test 167 public void testSecondCenterOnScreenNotIgnored() { 168 s.centerOnScreen(); 169 170 s.setX(0); 171 s.setY(0); 172 173 s.centerOnScreen(); 174 175 pulse(); 176 177 assertTrue(Math.abs(peer.x) > 0.0001); 178 assertTrue(Math.abs(peer.y) > 0.0001); 179 } 180 181 @Test 182 public void testSecondSizeToSceneNotIgnored() { 183 final Scene scene = new Scene(new Group(), 200, 100); 184 s.setScene(scene); 185 186 s.sizeToScene(); 187 188 s.setWidth(400); 189 s.setHeight(300); 190 191 s.sizeToScene(); 192 193 pulse(); 194 195 assertTrue(Math.abs(peer.width - 400) > 0.0001); 196 assertTrue(Math.abs(peer.height - 300) > 0.0001); 197 } 198 199 @Test 200 public void testCenterOnScreenForWindowOnSecondScreen() { 201 toolkit.setScreens( 202 new ScreenConfiguration(0, 0, 1920, 1200, 0, 0, 1920, 1172, 96), 203 new ScreenConfiguration(1920, 160, 1440, 900, 204 1920, 160, 1440, 900, 96)); 205 206 try { 207 s.setX(1920); 208 s.setY(160); 209 s.setWidth(300); 210 s.setHeight(200); 211 212 s.centerOnScreen(); 213 pulse(); 214 215 assertTrue(peer.x > 1930); 216 assertTrue(peer.y > 170); 217 } finally { 218 toolkit.resetScreens(); 219 } 220 } 221 222 @Test 223 public void testCenterOnScreenForOwnerOnSecondScreen() { 224 toolkit.setScreens( 225 new ScreenConfiguration(0, 0, 1920, 1200, 0, 0, 1920, 1172, 96), 226 new ScreenConfiguration(1920, 160, 1440, 900, 227 1920, 160, 1440, 900, 96)); 228 229 try { 230 s.setX(1920); 231 s.setY(160); 232 s.setWidth(300); 233 s.setHeight(200); 234 235 final Stage childStage = new Stage(); 236 childStage.setWidth(100); 237 childStage.setHeight(100); 238 childStage.initOwner(s); 239 childStage.show(); 240 241 childStage.centerOnScreen(); 242 243 assertTrue(childStage.getX() > 1930); 244 assertTrue(childStage.getY() > 170); 245 } finally { 246 toolkit.resetScreens(); 247 } 248 } 249 250 @Test 251 public void testSwitchSceneWithFixedSize() { 252 Scene scene = new Scene(new Group(), 200, 100); 253 s.setScene(scene); 254 255 s.setWidth(400); 256 s.setHeight(300); 257 258 pulse(); 259 260 assertEquals(400, peer.width, 0.0001); 261 assertEquals(300, peer.height, 0.0001); 262 assertEquals(400, scene.getWidth(), 0.0001); 263 assertEquals(300, scene.getHeight(), 0.0001); 264 265 s.setScene(scene = new Scene(new Group(), 220, 110)); 266 267 pulse(); 268 269 assertEquals(400, peer.width, 0.0001); 270 assertEquals(300, peer.height, 0.0001); 271 assertEquals(400, scene.getWidth(), 0.0001); 272 assertEquals(300, scene.getHeight(), 0.0001); 273 } 274 275 @Test 276 public void testSetBoundsNotLostForAsyncNotifications() { 277 s.setX(20); 278 s.setY(50); 279 s.setWidth(400); 280 s.setHeight(300); 281 282 peer.holdNotifications(); 283 pulse(); 284 285 s.setX(40); 286 s.setY(70); 287 s.setWidth(380); 288 s.setHeight(280); 289 290 peer.releaseNotifications(); 291 pulse(); 292 293 assertEquals(40.0, peer.x, 0.0001); 294 assertEquals(70.0, peer.y, 0.0001); 295 assertEquals(380.0, peer.width, 0.0001); 296 assertEquals(280.0, peer.height, 0.0001); 297 } 298 299 @Test 300 public void testFullscreenNotLostForAsyncNotifications() { 301 peer.holdNotifications(); 302 303 s.setFullScreen(true); 304 assertTrue(s.isFullScreen()); 305 306 s.setFullScreen(false); 307 assertFalse(s.isFullScreen()); 308 309 peer.releaseSingleNotification(); 310 assertTrue(s.isFullScreen()); 311 312 peer.releaseNotifications(); 313 314 assertFalse(s.isFullScreen()); 315 } 316 317 @Test 318 public void testFullScreenNotification() { 319 peer.setFullScreen(true); 320 assertTrue(s.isFullScreen()); 321 peer.setFullScreen(false); 322 assertFalse(s.isFullScreen()); 323 } 324 325 @Test 326 public void testResizableNotLostForAsyncNotifications() { 327 peer.holdNotifications(); 328 329 s.setResizable(false); 330 assertFalse(s.isResizable()); 331 332 s.setResizable(true); 333 assertTrue(s.isResizable()); 334 335 peer.releaseSingleNotification(); 336 assertFalse(s.isResizable()); 337 338 peer.releaseNotifications(); 339 340 assertTrue(s.isResizable()); 341 } 342 343 @Test 344 public void testResizableNotification() { 345 peer.setResizable(false); 346 assertFalse(s.isResizable()); 347 peer.setResizable(true); 348 assertTrue(s.isResizable()); 349 } 350 351 @Test 352 public void testIconifiedNotLostForAsyncNotifications() { 353 peer.holdNotifications(); 354 355 s.setIconified(true); 356 assertTrue(s.isIconified()); 357 358 s.setIconified(false); 359 assertFalse(s.isIconified()); 360 361 peer.releaseSingleNotification(); 362 assertTrue(s.isIconified()); 363 364 peer.releaseNotifications(); 365 366 assertFalse(s.isIconified()); 367 } 368 369 @Test 370 public void testIconifiedNotification() { 371 peer.setIconified(true); 372 assertTrue(s.isIconified()); 373 peer.setIconified(false); 374 assertFalse(s.isIconified()); 375 } 376 377 @Test 378 public void testMaximixedNotLostForAsyncNotifications() { 379 peer.holdNotifications(); 380 381 s.setMaximized(true); 382 assertTrue(s.isMaximized()); 383 384 s.setMaximized(false); 385 assertFalse(s.isMaximized()); 386 387 peer.releaseSingleNotification(); 388 assertTrue(s.isMaximized()); 389 390 peer.releaseNotifications(); 391 392 assertFalse(s.isMaximized()); 393 } 394 395 @Test 396 public void testMaximizedNotification() { 397 peer.setMaximized(true); 398 assertTrue(s.isMaximized()); 399 peer.setMaximized(false); 400 assertFalse(s.isMaximized()); 401 } 402 403 @Test 404 public void testAlwaysOnTopNotLostForAsyncNotifications() { 405 peer.holdNotifications(); 406 407 s.setAlwaysOnTop(true); 408 assertTrue(s.isAlwaysOnTop()); 409 410 s.setAlwaysOnTop(false); 411 assertFalse(s.isAlwaysOnTop()); 412 413 peer.releaseSingleNotification(); 414 assertTrue(s.isAlwaysOnTop()); 415 416 peer.releaseNotifications(); 417 418 assertFalse(s.isAlwaysOnTop()); 419 } 420 421 @Test 422 public void testAlwaysOnTopNotification() { 423 peer.setAlwaysOnTop(true); 424 assertTrue(s.isAlwaysOnTop()); 425 peer.setAlwaysOnTop(false); 426 assertFalse(s.isAlwaysOnTop()); 427 } 428 429 @Test 430 public void testBoundsSetAfterPeerIsRecreated() { 431 s.setX(20); 432 s.setY(50); 433 s.setWidth(400); 434 s.setHeight(300); 435 436 pulse(); 437 438 assertEquals(20.0, peer.x, 0.0001); 439 assertEquals(50.0, peer.y, 0.0001); 440 assertEquals(400.0, peer.width, 0.0001); 441 assertEquals(300.0, peer.height, 0.0001); 442 443 // recreates the peer 444 s.hide(); 445 s.show(); 446 447 pulse(); 448 449 peer = (StubStage) s.impl_getPeer(); 450 assertEquals(20.0, peer.x, 0.0001); 451 assertEquals(50.0, peer.y, 0.0001); 452 assertEquals(400.0, peer.width, 0.0001); 453 assertEquals(300.0, peer.height, 0.0001); 454 } 455 }