1 /*
   2  * Copyright (c) 2013, 2015, 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 /*
  25   test
  26   @bug 6401700 6412803
  27   @summary Tests that modal dialog is shown on the screen and
  28 iconified/restored correctly if some of its blocked windows are invisible
  29   @author artem.ananiev: area=awt.modal
  30   @run applet/manual=yesno InvisibleParentTest.html
  31 */
  32 
  33 import java.applet.Applet;
  34 import java.awt.BorderLayout;
  35 import java.awt.Button;
  36 import java.awt.Component;
  37 import java.awt.Dialog;
  38 import java.awt.Frame;
  39 import java.awt.TextArea;
  40 import java.awt.Window;
  41 
  42 public class InvisibleParentTest extends Applet
  43 {
  44     public void init()
  45     {
  46         setLayout(new BorderLayout());
  47 
  48         String[] instructions =
  49         {
  50             "If your system is Windows, press PASS button.",
  51             "When the test starts two windows should appear: frame G1 and",
  52             "    dialog D1. Another one frame F1 should be minimized.",
  53             "    If the dialog is not shown (minimizied), press FAIL button.",
  54             "Then minimize frame G1 and restore F1. If the dialog D1 is not",
  55             "    restored together with F1, press FAIL, else PASS"
  56         };
  57         Sysout.createDialogWithInstructions( instructions );
  58     }
  59 
  60     public void start ()
  61     {
  62         Button b;
  63 
  64         setSize (200,200);
  65         setVisible(true);
  66         validate();
  67 
  68         Component c = this;
  69         while ((c != null) && !(c instanceof Window))
  70         {
  71             c = c.getParent();
  72         }
  73         if (c != null)
  74         {
  75             ((Window)c).setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  76         }
  77 
  78         Frame f1 = new Frame("F1");
  79         f1.setBounds(100, 300, 100, 100);
  80         f1.setVisible(true);
  81         f1.setExtendedState(Frame.ICONIFIED);
  82 
  83         Frame g1 = new Frame("G1");
  84         g1.setBounds(150, 350, 100, 100);
  85         g1.setVisible(true);
  86 
  87         final Dialog d1 = new Dialog((Frame)null, "D1", Dialog.ModalityType.APPLICATION_MODAL);
  88         d1.setBounds(200, 400, 100, 100);
  89         new Thread(new Runnable()
  90         {
  91             public void run()
  92             {
  93                 d1.setVisible(true);
  94             }
  95         }).start();
  96     }
  97 }
  98 
  99 /****************************************************
 100  Standard Test Machinery
 101  DO NOT modify anything below -- it's a standard
 102   chunk of code whose purpose is to make user
 103   interaction uniform, and thereby make it simpler
 104   to read and understand someone else's test.
 105  ****************************************************/
 106 
 107 /**
 108  This is part of the standard test machinery.
 109  It creates a dialog (with the instructions), and is the interface
 110   for sending text messages to the user.
 111  To print the instructions, send an array of strings to Sysout.createDialog
 112   WithInstructions method.  Put one line of instructions per array entry.
 113  To display a message for the tester to see, simply call Sysout.println
 114   with the string to be displayed.
 115  This mimics System.out.println but works within the test harness as well
 116   as standalone.
 117  */
 118 
 119 class Sysout
 120 {
 121     private static TestDialog dialog;
 122 
 123     public static void createDialogWithInstructions( String[] instructions )
 124     {
 125         dialog = new TestDialog( new Frame(), "Instructions" );
 126         dialog.printInstructions( instructions );
 127         dialog.setVisible(true);
 128         println( "Any messages for the tester will display here." );
 129     }
 130 
 131     public static void createDialog( )
 132     {
 133         dialog = new TestDialog( new Frame(), "Instructions" );
 134         String[] defInstr = { "Instructions will appear here. ", "" } ;
 135         dialog.printInstructions( defInstr );
 136         dialog.setVisible(true);
 137         println( "Any messages for the tester will display here." );
 138     }
 139 
 140 
 141     public static void printInstructions( String[] instructions )
 142     {
 143         dialog.printInstructions( instructions );
 144     }
 145 
 146 
 147     public static void println( String messageIn )
 148     {
 149         dialog.displayMessage( messageIn );
 150     }
 151 
 152 }// Sysout  class
 153 
 154 /**
 155   This is part of the standard test machinery.  It provides a place for the
 156    test instructions to be displayed, and a place for interactive messages
 157    to the user to be displayed.
 158   To have the test instructions displayed, see Sysout.
 159   To have a message to the user be displayed, see Sysout.
 160   Do not call anything in this dialog directly.
 161   */
 162 class TestDialog extends Dialog
 163 {
 164 
 165     TextArea instructionsText;
 166     TextArea messageText;
 167     int maxStringLength = 80;
 168 
 169     //DO NOT call this directly, go through Sysout
 170     public TestDialog( Frame frame, String name )
 171     {
 172         super( frame, name );
 173         setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
 174         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 175         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 176         add( "North", instructionsText );
 177 
 178         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 179         add("Center", messageText);
 180 
 181         pack();
 182 
 183         setVisible(true);
 184     }// TestDialog()
 185 
 186     //DO NOT call this directly, go through Sysout
 187     public void printInstructions( String[] instructions )
 188     {
 189         //Clear out any current instructions
 190         instructionsText.setText( "" );
 191 
 192         //Go down array of instruction strings
 193 
 194         String printStr, remainingStr;
 195         for( int i=0; i < instructions.length; i++ )
 196         {
 197             //chop up each into pieces maxSringLength long
 198             remainingStr = instructions[ i ];
 199             while( remainingStr.length() > 0 )
 200             {
 201                 //if longer than max then chop off first max chars to print
 202                 if( remainingStr.length() >= maxStringLength )
 203                 {
 204                     //Try to chop on a word boundary
 205                     int posOfSpace = remainingStr.
 206                         lastIndexOf( ' ', maxStringLength - 1 );
 207 
 208                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 209 
 210                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 211                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 212                 }
 213                 //else just print
 214                 else
 215                 {
 216                     printStr = remainingStr;
 217                     remainingStr = "";
 218                 }
 219 
 220                 instructionsText.append( printStr + "\n" );
 221 
 222             }// while
 223 
 224         }// for
 225 
 226     }//printInstructions()
 227 
 228     //DO NOT call this directly, go through Sysout
 229     public void displayMessage( String messageIn )
 230     {
 231         messageText.append( messageIn + "\n" );
 232         System.out.println(messageIn);
 233     }
 234 
 235 }// TestDialog  class