1 /*
   2  * Copyright (c) 2007, 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import static jdk.testlibrary.Asserts.*;
  26 
  27 /*
  28  * @test
  29  * @key headful
  30  * @bug 8047367
  31  * @summary Check modality settings for Window and Dialog.
  32  *
  33  * @library ../../../../lib/testlibrary/
  34  * @run main ModalitySettingsTest
  35  */
  36 
  37 
  38 
  39 public class ModalitySettingsTest {
  40 
  41     private void doTest() throws Exception {
  42 
  43         Window w = new Window(new Frame());
  44 
  45         boolean unexpectedExc = false;
  46 
  47         try {
  48             Dialog d = new Dialog(w);
  49         } catch (IllegalArgumentException iae) {
  50         } catch (Exception e) {
  51             unexpectedExc = true;
  52         }
  53 
  54         assertFalse(unexpectedExc, "unexpected exception occured when a " +
  55             "Window instance was passed to Dialog constructor");
  56 
  57         Dialog d = new Dialog((Frame) null);
  58         assertTrue(d.getModalityType() == Dialog.ModalityType.MODELESS,
  59             "the default modality type returned by Dialog " +
  60             "differs from Dialog.ModalityType.MODELESS");
  61 
  62         Frame f = new Frame();
  63         assertTrue(f.getModalExclusionType() == Dialog.ModalExclusionType.NO_EXCLUDE,
  64             "the default modality exclusion type returned by Frame" +
  65             "differs from Dialog.ModalExclusionType.NO_EXCLUDE");
  66 
  67         w = new Window((Frame) null);
  68         assertTrue(w.getModalExclusionType() == Dialog.ModalExclusionType.NO_EXCLUDE,
  69             "the default modality exclusion type returned by Window " +
  70             "differs from Dialog.ModalExclusionType.NO_EXCLUDE");
  71 
  72         d = new Dialog((Frame) null);
  73         assertTrue(d.getModalExclusionType() == Dialog.ModalExclusionType.NO_EXCLUDE,
  74             "the default modality exclusion type returned by Dialog " +
  75             "differs from Dialog.ModalExclusionType.NO_EXCLUDE");
  76 
  77         d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
  78         assertTrue(d.getModalityType() == Dialog.ModalityType.TOOLKIT_MODAL,
  79             "the modality type returned by Dialog " +
  80             "differs from Dialog.ModalityType.TOOLKIT_MODAL " +
  81             "after setting the modality type to that value");
  82 
  83         d.setModal(false);
  84         assertTrue(d.getModalityType() == Dialog.ModalityType.MODELESS,
  85             "the modality type returned by Dialog differs from " +
  86             "Dialog.ModalityType.MODELESS after calling setModal(false)");
  87 
  88         d.setModal(true);
  89         assertTrue(d.getModalityType() == Dialog.ModalityType.APPLICATION_MODAL,
  90             "the modality type returned by Dialog differs from "
  91             + "Dialog.ModalityType.APPLICATION_MODAL after calling setModal(true)");
  92 
  93         w.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  94         assertTrue(w.getModalExclusionType() ==
  95                 Dialog.ModalExclusionType.APPLICATION_EXCLUDE,
  96             "getModalExclusionType method for Window did not return " +
  97             "Dialog.ModalExclusionType.APPLICATION_EXCLUDE after " +
  98             "setting it to that value");
  99 
 100         d = new Dialog((Frame) null);
 101         d.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
 102         assertTrue(d.isModal(), "method isModal for Dialog " +
 103             "returned false when the Dialog is toolkit modal");
 104 
 105         d.setModalityType(Dialog.ModalityType.MODELESS);
 106         assertFalse(d.isModal(), "method isModal for Dialog " +
 107             "returned true when the Dialog is MODELESS");
 108 
 109         d = new Dialog((Frame) null, (Dialog.ModalityType) null);
 110         assertTrue(d.getModalityType() == Dialog.ModalityType.MODELESS,
 111             "The modality type returned for a Dialog constructed " +
 112             "with null modality type differs from MODELESS");
 113 
 114         d = new Dialog((Frame) null);
 115         d.setModalityType(null);
 116         assertTrue(d.getModalityType() == Dialog.ModalityType.MODELESS,
 117             "the modality type returned for a Dialog set with null " +
 118             "modality type differs from MODELESS");
 119 
 120         d.setModalExclusionType(null);
 121         assertTrue(d.getModalExclusionType() == Dialog.ModalExclusionType.NO_EXCLUDE,
 122             "The exlcusion type returned for a Dialog set with null " +
 123             "exclusion type differs from NO_EXCLUDE");
 124 
 125         try {
 126             Dialog.ModalityType.valueOf("invalid");
 127         } catch (IllegalArgumentException iae) {
 128         } catch (Exception e) {
 129             unexpectedExc = true;
 130         }
 131 
 132         assertFalse(unexpectedExc, "unexpected exception occured when an " +
 133             "invalid value was passed to ModalityType.valueOf method");
 134     }
 135 
 136     public static void main(String[] args) throws Exception {
 137         ModalitySettingsTest test = new ModalitySettingsTest();
 138         test.doTest();
 139     }
 140 }