1 /*
   2  * Copyright (c) 2011, 2013, 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 static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertTrue;
  31 
  32 import com.sun.javafx.FXUnit;
  33 import org.junit.Rule;
  34 import org.junit.Test;
  35 
  36 /**
  37  * Test those attributes on Stage that should be immutable after Stage is visible.
  38  *
  39  */
  40 public class StageMutabilityTest {
  41 
  42     @Rule
  43     public FXUnit fx = new FXUnit();
  44 
  45 // ========== Tests for style
  46 
  47     /**
  48      * Tests the default value of style for a Stage
  49      */
  50     @Test public void testStyleDefault() {
  51         Stage stage = new Stage();
  52         assertEquals(StageStyle.DECORATED, stage.getStyle());
  53     }
  54 
  55     /**
  56      * Tests setting the value of style for a Stage
  57      */
  58     @Test public void testStyleSet() {
  59         Stage stage = new Stage();
  60         assertEquals(StageStyle.DECORATED, stage.getStyle());
  61         stage.initStyle(StageStyle.UNDECORATED);
  62         assertEquals(StageStyle.UNDECORATED, stage.getStyle());
  63         stage.initStyle(StageStyle.DECORATED);
  64         assertEquals(StageStyle.DECORATED, stage.getStyle());
  65         stage.initStyle(StageStyle.TRANSPARENT);
  66         assertEquals(StageStyle.TRANSPARENT, stage.getStyle());
  67         stage.initStyle(StageStyle.UTILITY);
  68         assertEquals(StageStyle.UTILITY, stage.getStyle());
  69     }
  70 
  71     /**
  72      * Tests setting the value of style for a primary Stage
  73      */
  74     @Test public void testStyleSetPrimary() {
  75         Stage stage = new Stage();
  76         assertFalse(stage.isPrimary());
  77         stage.impl_setPrimary(true);
  78         assertTrue(stage.isPrimary());
  79         assertEquals(StageStyle.DECORATED, stage.getStyle());
  80         stage.initStyle(StageStyle.UNDECORATED);
  81         assertEquals(StageStyle.UNDECORATED, stage.getStyle());
  82         stage.initStyle(StageStyle.DECORATED);
  83         assertEquals(StageStyle.DECORATED, stage.getStyle());
  84         stage.initStyle(StageStyle.TRANSPARENT);
  85         assertEquals(StageStyle.TRANSPARENT, stage.getStyle());
  86         stage.initStyle(StageStyle.UTILITY);
  87         assertEquals(StageStyle.UTILITY, stage.getStyle());
  88     }
  89 
  90     /**
  91      * Tests initializing the value of style for a Stage in the constructor
  92      */
  93     @Test public void testStyleConstructor() {
  94         Stage stage = new Stage(StageStyle.UNDECORATED);
  95         assertEquals(StageStyle.UNDECORATED, stage.getStyle());
  96         stage.initStyle(StageStyle.DECORATED);
  97         assertEquals(StageStyle.DECORATED, stage.getStyle());
  98     }
  99 
 100     /**
 101      * Tests that setting the value of style on a visible Stage throws an exception
 102      */
 103     @Test public void testStyleSetWhileVisible() {
 104         Stage stage = new Stage();
 105         assertEquals(StageStyle.DECORATED, stage.getStyle());
 106         stage.show();
 107         try {
 108             stage.initStyle(StageStyle.UNDECORATED);
 109             // Error if we get here we didn't get the expected exception
 110             assertTrue(false);
 111         } catch (IllegalStateException ex) {
 112         }
 113         assertEquals(StageStyle.DECORATED, stage.getStyle());
 114     }
 115 
 116     /**
 117      * Tests that setting the value of style on a Stage after it is
 118      * set visible throws an exception even if it is subsequently set non-visible
 119      */
 120     @Test public void testStyleSetAfterVisible() {
 121         Stage stage = new Stage(StageStyle.TRANSPARENT);
 122         assertEquals(StageStyle.TRANSPARENT, stage.getStyle());
 123         stage.initStyle(StageStyle.UNDECORATED);
 124         assertEquals(StageStyle.UNDECORATED, stage.getStyle());
 125         stage.show();
 126         stage.hide();
 127         try {
 128             stage.initStyle(StageStyle.DECORATED);
 129             // Error if we get here we didn't get the expected exception
 130             assertTrue(false);
 131         } catch (IllegalStateException ex) {
 132         }
 133         assertEquals(StageStyle.UNDECORATED, stage.getStyle());
 134     }
 135 
 136 // ========== Tests for modality
 137 
 138     /**
 139      * Tests the default value of modality for a Stage
 140      */
 141     @Test public void testModalityDefault() {
 142         Stage stage = new Stage();
 143         assertEquals(Modality.NONE, stage.getModality());
 144     }
 145 
 146     /**
 147      * Tests setting the value of modality for a Stage
 148      */
 149     @Test public void testModalitySet() {
 150         Stage stage = new Stage();
 151         assertEquals(Modality.NONE, stage.getModality());
 152         stage.initModality(Modality.WINDOW_MODAL);
 153         assertEquals(Modality.WINDOW_MODAL, stage.getModality());
 154         stage.initModality(Modality.APPLICATION_MODAL);
 155         assertEquals(Modality.APPLICATION_MODAL, stage.getModality());
 156         stage.initModality(Modality.NONE);
 157         assertEquals(Modality.NONE, stage.getModality());
 158     }
 159 
 160     /**
 161      * Tests setting the value of modality for a primary Stage
 162      */
 163     @Test public void testModalitySetPrimary() {
 164         Stage stage = new Stage();
 165         assertFalse(stage.isPrimary());
 166         stage.impl_setPrimary(true);
 167         assertTrue(stage.isPrimary());
 168         assertEquals(Modality.NONE, stage.getModality());
 169         try {
 170             stage.initModality(Modality.WINDOW_MODAL);
 171             // Error if we get here we didn't get the expected exception
 172             assertTrue(false);
 173         } catch (IllegalStateException ex) {
 174         }
 175         assertEquals(Modality.NONE, stage.getModality());
 176     }
 177 
 178 // TODO: Add more Modality test below:
 179 //    /**
 180 //     * Tests that setting the value of style on a visible Stage throws an exception
 181 //     */
 182 //    @Test public void testStyleSetWhileVisible() {
 183 //        Stage stage = new Stage();
 184 //        assertEquals(StageStyle.DECORATED, stage.getStyle());
 185 //        stage.show();
 186 //        try {
 187 //            stage.initStyle(StageStyle.UNDECORATED);
 188 //            // Error if we get here we didn't get the expected exception
 189 //            assertTrue(false);
 190 //        } catch (IllegalStateException ex) {
 191 //        }
 192 //        assertEquals(StageStyle.DECORATED, stage.getStyle());
 193 //    }
 194 //
 195 //    /**
 196 //     * Tests that setting the value of style on a Stage after it is
 197 //     * set visible throws an exception even if it is subsequently set non-visible
 198 //     */
 199 //    @Test public void testStyleSetAfterVisible() {
 200 //        Stage stage = new Stage(StageStyle.TRANSPARENT);
 201 //        assertEquals(StageStyle.TRANSPARENT, stage.getStyle());
 202 //        stage.initStyle(StageStyle.UNDECORATED);
 203 //        assertEquals(StageStyle.UNDECORATED, stage.getStyle());
 204 //        stage.show();
 205 //        stage.hide();
 206 //        try {
 207 //            stage.initStyle(StageStyle.DECORATED);
 208 //            // Error if we get here we didn't get the expected exception
 209 //            assertTrue(false);
 210 //        } catch (IllegalStateException ex) {
 211 //        }
 212 //        assertEquals(StageStyle.UNDECORATED, stage.getStyle());
 213 //    }
 214 
 215 }