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