test/jdk/java/awt/Focus/ModalBlockedStealsFocusTest/ModalBlockedStealsFocusTest.java
changeset 50834 9cf279436b9d
parent 47216 71c04702a3d5
child 54231 e4813eded7cb
equal deleted inserted replaced
50833:97852c0a7a91 50834:9cf279436b9d
     1 /*
     1 /*
     2  * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    50     public void init() {
    50     public void init() {
    51         // Create instructions for the user here, as well as set up
    51         // Create instructions for the user here, as well as set up
    52         // the environment -- set the layout manager, add buttons,
    52         // the environment -- set the layout manager, add buttons,
    53         // etc.
    53         // etc.
    54         this.setLayout (new BorderLayout ());
    54         this.setLayout (new BorderLayout ());
    55         Sysout.createDialogWithInstructions(new String[]
       
    56             {"This is an automatic test. Simply wait until it is done."
       
    57             });
       
    58     }
    55     }
    59 
    56 
    60     public void start() {
    57     public void start() {
    61         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
    58         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
    62             Sysout.println("The test is not for MToolkit.");
    59             System.out.println("The test is not for MToolkit.");
    63             return;
    60             return;
    64         }
    61         }
    65 
    62 
    66         dialog.setBounds(800, 0, 200, 100);
    63         dialog.setBounds(800, 0, 200, 100);
    67         frame.setBounds(800, 150, 200, 100);
    64         frame.setBounds(800, 150, 200, 100);
    68 
    65 
    69         dialog.addWindowFocusListener(new WindowAdapter() {
    66         dialog.addWindowFocusListener(new WindowAdapter() {
    70                 public void windowLostFocus(WindowEvent e) {
    67                 public void windowLostFocus(WindowEvent e) {
    71                     Sysout.println(e.toString());
    68                     System.out.println(e.toString());
    72                     synchronized (lostFocus) {
    69                     synchronized (lostFocus) {
    73                         lostFocus.set(true);
    70                         lostFocus.set(true);
    74                         lostFocus.notifyAll();
    71                         lostFocus.notifyAll();
    75                     }
    72                     }
    76                 }
    73                 }
   104         frame.toFront();
   101         frame.toFront();
   105 
   102 
   106         if (Util.waitForCondition(lostFocus, 2000L)) {
   103         if (Util.waitForCondition(lostFocus, 2000L)) {
   107             throw new TestFailedException("the modal blocked frame stole focus on its bringing to front!");
   104             throw new TestFailedException("the modal blocked frame stole focus on its bringing to front!");
   108         } else {
   105         } else {
   109             Sysout.println("Test passed");
   106             System.out.println("Test passed");
   110         }
   107         }
   111     }
   108     }
   112 }
   109 }
   113 
   110 
   114 class TestFailedException extends RuntimeException {
   111 class TestFailedException extends RuntimeException {
   115     TestFailedException(String msg) {
   112     TestFailedException(String msg) {
   116         super("Test failed: " + msg);
   113         super("Test failed: " + msg);
   117     }
   114     }
   118 }
   115 }
   119 
       
   120 /****************************************************
       
   121  Standard Test Machinery
       
   122  DO NOT modify anything below -- it's a standard
       
   123   chunk of code whose purpose is to make user
       
   124   interaction uniform, and thereby make it simpler
       
   125   to read and understand someone else's test.
       
   126  ****************************************************/
       
   127 
       
   128 /**
       
   129  This is part of the standard test machinery.
       
   130  It creates a dialog (with the instructions), and is the interface
       
   131   for sending text messages to the user.
       
   132  To print the instructions, send an array of strings to Sysout.createDialog
       
   133   WithInstructions method.  Put one line of instructions per array entry.
       
   134  To display a message for the tester to see, simply call Sysout.println
       
   135   with the string to be displayed.
       
   136  This mimics System.out.println but works within the test harness as well
       
   137   as standalone.
       
   138  */
       
   139 
       
   140 class Sysout
       
   141 {
       
   142     static TestDialog dialog;
       
   143 
       
   144     public static void createDialogWithInstructions( String[] instructions )
       
   145     {
       
   146         dialog = new TestDialog( new Frame(), "Instructions" );
       
   147         dialog.printInstructions( instructions );
       
   148         dialog.setVisible(true);
       
   149         println( "Any messages for the tester will display here." );
       
   150     }
       
   151 
       
   152     public static void createDialog( )
       
   153     {
       
   154         dialog = new TestDialog( new Frame(), "Instructions" );
       
   155         String[] defInstr = { "Instructions will appear here. ", "" } ;
       
   156         dialog.printInstructions( defInstr );
       
   157         dialog.setVisible(true);
       
   158         println( "Any messages for the tester will display here." );
       
   159     }
       
   160 
       
   161 
       
   162     public static void printInstructions( String[] instructions )
       
   163     {
       
   164         dialog.printInstructions( instructions );
       
   165     }
       
   166 
       
   167 
       
   168     public static void println( String messageIn )
       
   169     {
       
   170         dialog.displayMessage( messageIn );
       
   171     }
       
   172 
       
   173 }// Sysout  class
       
   174 
       
   175 /**
       
   176   This is part of the standard test machinery.  It provides a place for the
       
   177    test instructions to be displayed, and a place for interactive messages
       
   178    to the user to be displayed.
       
   179   To have the test instructions displayed, see Sysout.
       
   180   To have a message to the user be displayed, see Sysout.
       
   181   Do not call anything in this dialog directly.
       
   182   */
       
   183 class TestDialog extends Dialog
       
   184 {
       
   185 
       
   186     TextArea instructionsText;
       
   187     TextArea messageText;
       
   188     int maxStringLength = 80;
       
   189 
       
   190     //DO NOT call this directly, go through Sysout
       
   191     public TestDialog( Frame frame, String name )
       
   192     {
       
   193         super( frame, name );
       
   194         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   195         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
       
   196         add( "North", instructionsText );
       
   197 
       
   198         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
       
   199         add("Center", messageText);
       
   200 
       
   201         pack();
       
   202 
       
   203         setVisible(true);
       
   204     }// TestDialog()
       
   205 
       
   206     //DO NOT call this directly, go through Sysout
       
   207     public void printInstructions( String[] instructions )
       
   208     {
       
   209         //Clear out any current instructions
       
   210         instructionsText.setText( "" );
       
   211 
       
   212         //Go down array of instruction strings
       
   213 
       
   214         String printStr, remainingStr;
       
   215         for( int i=0; i < instructions.length; i++ )
       
   216         {
       
   217             //chop up each into pieces maxSringLength long
       
   218             remainingStr = instructions[ i ];
       
   219             while( remainingStr.length() > 0 )
       
   220             {
       
   221                 //if longer than max then chop off first max chars to print
       
   222                 if( remainingStr.length() >= maxStringLength )
       
   223                 {
       
   224                     //Try to chop on a word boundary
       
   225                     int posOfSpace = remainingStr.
       
   226                         lastIndexOf( ' ', maxStringLength - 1 );
       
   227 
       
   228                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
       
   229 
       
   230                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
       
   231                     remainingStr = remainingStr.substring( posOfSpace + 1 );
       
   232                 }
       
   233                 //else just print
       
   234                 else
       
   235                 {
       
   236                     printStr = remainingStr;
       
   237                     remainingStr = "";
       
   238                 }
       
   239 
       
   240                 instructionsText.append( printStr + "\n" );
       
   241 
       
   242             }// while
       
   243 
       
   244         }// for
       
   245 
       
   246     }//printInstructions()
       
   247 
       
   248     //DO NOT call this directly, go through Sysout
       
   249     public void displayMessage( String messageIn )
       
   250     {
       
   251         messageText.append( messageIn + "\n" );
       
   252         System.out.println(messageIn);
       
   253     }
       
   254 
       
   255 }// TestDialog  class