1 /*
   2  * Copyright (c) 2007, 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       6492970
  27   @summary   Tests that showing a toplvel in a not foreground Java process activates it.
  28   @library   ../../regtesthelpers
  29   @build     Util
  30   @author    Anton Tarasov: area=awt-focus
  31   @run       main ShowFrameCheckForegroundTest
  32  */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import java.applet.Applet;
  37 import java.util.concurrent.atomic.AtomicBoolean;
  38 import java.lang.reflect.InvocationTargetException;
  39 import test.java.awt.regtesthelpers.Util;
  40 
  41 public class ShowFrameCheckForegroundTest extends Applet {
  42     Robot robot;
  43     Frame nofocusFrame = new Frame("Non-focusable");
  44     Frame frame = new Frame("Frame");
  45     Dialog dialog1 = new Dialog(nofocusFrame, "Owned Dialog", false);
  46     Dialog dialog2 = new Dialog((Frame)null, "Owned Dialog", false);
  47     Window testToplevel = null;
  48     Button testButton = new Button("button");
  49     Button showButton = new Button("show");
  50     Runnable action = new Runnable() {
  51         public void run() {
  52             robot.keyPress(KeyEvent.VK_SPACE);
  53             robot.delay(50);
  54             robot.keyRelease(KeyEvent.VK_SPACE);
  55         }
  56     };
  57 
  58 
  59     public static void main(String[] args) {
  60         ShowFrameCheckForegroundTest app = new ShowFrameCheckForegroundTest();
  61         app.init();
  62         app.start();
  63     }
  64 
  65     public void init() {
  66         robot = Util.createRobot();
  67     }
  68 
  69     public void start() {
  70         showButton.addActionListener(new ActionListener() {
  71             public void actionPerformed(ActionEvent e) {
  72                 testToplevel.setVisible(true);
  73             }
  74         });
  75         nofocusFrame.add(showButton);
  76         nofocusFrame.pack();
  77         nofocusFrame.setFocusableWindowState(false);
  78         nofocusFrame.setVisible(true);
  79         Util.waitForIdle(robot);
  80 
  81         robot.delay(3000);
  82 
  83         // 1. Show the toplvel without clicking into the non-focusable frame.
  84         test(frame, 1);
  85         test(dialog1, 1);
  86         test(dialog2, 1);
  87 
  88         // 2. Showing the toplvel via clicking into the non-focusable frame.
  89         test(frame, 2);
  90         test(dialog1, 2);
  91         test(dialog2, 2);
  92 
  93         System.out.println("Test passed.");
  94     }
  95 
  96     private void test(Window toplevel, int stage) {
  97         toplevel.add(testButton);
  98         toplevel.pack();
  99         toplevel.setLocation(200, 0);
 100 
 101         switch (stage) {
 102             case 1:
 103                 toplevel.setVisible(true);
 104                 break;
 105             case 2:
 106                 testToplevel = toplevel;
 107                 Util.clickOnComp(showButton, robot);
 108                 break;
 109         }
 110         Util.waitForIdle(robot);
 111 
 112         if (!Util.trackActionPerformed(testButton, action, 2000, false)) {
 113             throw new TestFailedException("Stage " + stage + ". The toplevel " + toplevel + " wasn't made foreground on showing");
 114         }
 115         System.out.println("Stage " + stage + ". Toplevel " + toplevel + " - passed");
 116         toplevel.dispose();
 117     }
 118 }
 119 
 120 class TestFailedException extends RuntimeException {
 121     TestFailedException(String msg) {
 122         super("Test failed: " + msg);
 123     }
 124 }
 125 
 126 /****************************************************
 127  * Standard Test Machinery
 128  * DO NOT modify anything below -- it's a standard
 129  * chunk of code whose purpose is to make user
 130  * interaction uniform, and thereby make it simpler
 131  * to read and understand someone else's test.
 132  ****************************************************/
 133 
 134 /**
 135  * This is part of the standard test machinery.
 136  * It creates a dialog (with the instructions), and is the interface
 137  * for sending text messages to the user.
 138  * To print the instructions, send an array of strings to Sysout.createDialog
 139  * WithInstructions method.  Put one line of instructions per array entry.
 140  * To display a message for the tester to see, simply call Sysout.println
 141  * with the string to be displayed.
 142  * This mimics System.out.println but works within the test harness as well
 143  * as standalone.
 144  */
 145 
 146 class Sysout {
 147     static TestDialog dialog;
 148 
 149     public static void createDialogWithInstructions( String[] instructions ) {
 150         dialog = new TestDialog( new Frame(), "Instructions" );
 151         dialog.printInstructions( instructions );
 152         dialog.setVisible(true);
 153         println( "Any messages for the tester will display here." );
 154     }
 155 
 156     public static void createDialog( ) {
 157         dialog = new TestDialog( new Frame(), "Instructions" );
 158         String[] defInstr = { "Instructions will appear here. ", "" } ;
 159         dialog.printInstructions( defInstr );
 160         dialog.setVisible(true);
 161         println( "Any messages for the tester will display here." );
 162     }
 163 
 164 
 165     public static void printInstructions( String[] instructions ) {
 166         dialog.printInstructions( instructions );
 167     }
 168 
 169 
 170     public static void println( String messageIn ) {
 171         dialog.displayMessage( messageIn );
 172     }
 173 
 174 }// Sysout  class
 175 
 176 /**
 177  * This is part of the standard test machinery.  It provides a place for the
 178  * test instructions to be displayed, and a place for interactive messages
 179  * to the user to be displayed.
 180  * To have the test instructions displayed, see Sysout.
 181  * To have a message to the user be displayed, see Sysout.
 182  * Do not call anything in this dialog directly.
 183  */
 184 class TestDialog extends Dialog {
 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         super( frame, name );
 193         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 194         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 195         add( "North", instructionsText );
 196 
 197         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 198         add("Center", messageText);
 199 
 200         pack();
 201 
 202         setVisible(true);
 203     }// TestDialog()
 204 
 205     //DO NOT call this directly, go through Sysout
 206     public void printInstructions( String[] instructions ) {
 207         //Clear out any current instructions
 208         instructionsText.setText( "" );
 209 
 210         //Go down array of instruction strings
 211 
 212         String printStr, remainingStr;
 213         for( int i=0; i < instructions.length; i++ ) {
 214             //chop up each into pieces maxSringLength long
 215             remainingStr = instructions[ i ];
 216             while( remainingStr.length() > 0 ) {
 217                 //if longer than max then chop off first max chars to print
 218                 if( remainingStr.length() >= maxStringLength ) {
 219                     //Try to chop on a word boundary
 220                     int posOfSpace = remainingStr.
 221                             lastIndexOf( ' ', maxStringLength - 1 );
 222 
 223                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 224 
 225                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 226                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 227                 }
 228                 //else just print
 229                 else {
 230                     printStr = remainingStr;
 231                     remainingStr = "";
 232                 }
 233 
 234                 instructionsText.append( printStr + "\n" );
 235 
 236             }// while
 237 
 238         }// for
 239 
 240     }//printInstructions()
 241 
 242     //DO NOT call this directly, go through Sysout
 243     public void displayMessage( String messageIn ) {
 244         messageText.append( messageIn + "\n" );
 245         System.out.println(messageIn);
 246     }
 247 
 248 }// TestDialog  class