< prev index next >

test/jdk/java/awt/Focus/ChildWindowFocusTest/ChildWindowFocusTest.java

Print this page


   1 /*
   2  * Copyright (c) 2004, 2014, 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  */


  39     Frame frame = new Frame("Owner");
  40     Button button0 = new Button("button-0");
  41     TextField text0 = new TextField("text-0");
  42     TextField text1 = new TextField("text-1");
  43     Window win1 = new TestWindow(frame, text0, 110);
  44     Window win2 = new TestWindow(win1, text1, 220);
  45     Frame outerFrame = new Frame("Outer");
  46     Button button1 = new Button("button-1");
  47     int shift;
  48 
  49     public void init() {
  50         try {
  51             robot = new Robot();
  52         } catch (AWTException e) {
  53             throw new RuntimeException("Error: unable to create robot", e);
  54         }
  55         // Create instructions for the user here, as well as set up
  56         // the environment -- set the layout manager, add buttons,
  57         // etc.
  58         this.setLayout (new BorderLayout ());
  59         Sysout.createDialogWithInstructions(new String[]
  60             {"This is an AUTOMATIC test", "simply wait until it is done"});
  61 
  62         Rectangle bounds = Sysout.dialog.getBounds();
  63         shift = (int)(bounds.x + bounds.width + 10);
  64     }
  65 
  66     public void start() {
  67 
  68         frame.setBounds(0, 50, 400, 100);
  69         frame.setLayout(new FlowLayout());
  70         frame.add(button0);
  71 
  72         outerFrame.setBounds(0, 390, 400, 100);
  73         outerFrame.setLayout(new FlowLayout());
  74         outerFrame.add(button1);
  75 
  76         adjustAndShow(new Component[] {frame, win1, win2, outerFrame});
  77         robot.waitForIdle();
  78 
  79         test();
  80     }
  81 
  82     void adjustAndShow(Component[] comps) {
  83         for (Component comp: comps) {
  84             comp.setLocation(shift, (int)comp.getLocation().getY());
  85             comp.setVisible(true);
  86             robot.waitForIdle();
  87         }
  88     }
  89 
  90     void test() {
  91         clickOnCheckFocusOwner(button0);
  92         clickOnCheckFocusOwner(text1);
  93         clickOnCheckFocusOwner(button1);
  94         clickOn(frame);
  95         checkFocusOwner(text1);
  96         clickOnCheckFocusOwner(text0);
  97         clickOnCheckFocusOwner(button1);
  98         clickOn(frame);
  99         checkFocusOwner(text0);
 100 
 101         Sysout.println("Test passed.");
 102     }
 103 
 104     void clickOnCheckFocusOwner(Component c) {
 105         clickOn(c);
 106         if (!checkFocusOwner(c)) {
 107             throw new RuntimeException("Test failed: couldn't focus <" + c + "> by mouse click!");
 108         }
 109     }
 110 
 111     boolean checkFocusOwner(Component comp) {
 112         return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
 113     }
 114 
 115     void clickOn(Component c) {
 116         Point p = c.getLocationOnScreen();
 117         Dimension d = c.getSize();
 118 
 119         Sysout.println("Clicking " + c);
 120 
 121         if (c instanceof Frame) {
 122             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 123         } else {
 124             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 125         }
 126         robot.delay(50);
 127         robot.mousePress(InputEvent.BUTTON1_MASK);
 128         robot.delay(50);
 129         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 130         robot.waitForIdle();
 131     }
 132 
 133 }
 134 
 135 class TestWindow extends Window {
 136     TestWindow(Window owner, Component comp, int x) {
 137         super(owner);
 138         setBackground(Color.blue);
 139         setLayout(new FlowLayout());
 140         add(comp);
 141         comp.setBackground(Color.yellow);
 142         setBounds(0, x, 100, 100);
 143     }
 144 }
 145 
 146 /****************************************************
 147  Standard Test Machinery
 148  DO NOT modify anything below -- it's a standard
 149   chunk of code whose purpose is to make user
 150   interaction uniform, and thereby make it simpler
 151   to read and understand someone else's test.
 152  ****************************************************/
 153 
 154 /**
 155  This is part of the standard test machinery.
 156  It creates a dialog (with the instructions), and is the interface
 157   for sending text messages to the user.
 158  To print the instructions, send an array of strings to Sysout.createDialog
 159   WithInstructions method.  Put one line of instructions per array entry.
 160  To display a message for the tester to see, simply call Sysout.println
 161   with the string to be displayed.
 162  This mimics System.out.println but works within the test harness as well
 163   as standalone.
 164  */
 165 
 166 class Sysout
 167 {
 168     static TestDialog dialog;
 169 
 170     public static void createDialogWithInstructions( String[] instructions )
 171     {
 172         dialog = new TestDialog( new Frame(), "Instructions" );
 173         dialog.printInstructions( instructions );
 174         dialog.setVisible(true);
 175         println( "Any messages for the tester will display here." );
 176     }
 177 
 178     public static void createDialog( )
 179     {
 180         dialog = new TestDialog( new Frame(), "Instructions" );
 181         String[] defInstr = { "Instructions will appear here. ", "" } ;
 182         dialog.printInstructions( defInstr );
 183         dialog.setVisible(true);
 184         println( "Any messages for the tester will display here." );
 185     }
 186 
 187 
 188     public static void printInstructions( String[] instructions )
 189     {
 190         dialog.printInstructions( instructions );
 191     }
 192 
 193 
 194     public static void println( String messageIn )
 195     {
 196         dialog.displayMessage( messageIn );
 197     }
 198 
 199 }// Sysout  class
 200 
 201 /**
 202   This is part of the standard test machinery.  It provides a place for the
 203    test instructions to be displayed, and a place for interactive messages
 204    to the user to be displayed.
 205   To have the test instructions displayed, see Sysout.
 206   To have a message to the user be displayed, see Sysout.
 207   Do not call anything in this dialog directly.
 208   */
 209 class TestDialog extends Dialog
 210 {
 211 
 212     TextArea instructionsText;
 213     TextArea messageText;
 214     int maxStringLength = 80;
 215 
 216     //DO NOT call this directly, go through Sysout
 217     public TestDialog( Frame frame, String name )
 218     {
 219         super( frame, name );
 220         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 221         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 222         add( "North", instructionsText );
 223 
 224         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 225         add("Center", messageText);
 226 
 227         pack();
 228 
 229         setVisible(true);
 230     }// TestDialog()
 231 
 232     //DO NOT call this directly, go through Sysout
 233     public void printInstructions( String[] instructions )
 234     {
 235         //Clear out any current instructions
 236         instructionsText.setText( "" );
 237 
 238         //Go down array of instruction strings
 239 
 240         String printStr, remainingStr;
 241         for( int i=0; i < instructions.length; i++ )
 242         {
 243             //chop up each into pieces maxSringLength long
 244             remainingStr = instructions[ i ];
 245             while( remainingStr.length() > 0 )
 246             {
 247                 //if longer than max then chop off first max chars to print
 248                 if( remainingStr.length() >= maxStringLength )
 249                 {
 250                     //Try to chop on a word boundary
 251                     int posOfSpace = remainingStr.
 252                         lastIndexOf( ' ', maxStringLength - 1 );
 253 
 254                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 255 
 256                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 257                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 258                 }
 259                 //else just print
 260                 else
 261                 {
 262                     printStr = remainingStr;
 263                     remainingStr = "";
 264                 }
 265 
 266                 instructionsText.append( printStr + "\n" );
 267 
 268             }// while
 269 
 270         }// for
 271 
 272     }//printInstructions()
 273 
 274     //DO NOT call this directly, go through Sysout
 275     public void displayMessage( String messageIn )
 276     {
 277         messageText.append( messageIn + "\n" );
 278         System.out.println(messageIn);
 279     }
 280 
 281 }// TestDialog  class
   1 /*
   2  * Copyright (c) 2004, 2018, 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  */


  39     Frame frame = new Frame("Owner");
  40     Button button0 = new Button("button-0");
  41     TextField text0 = new TextField("text-0");
  42     TextField text1 = new TextField("text-1");
  43     Window win1 = new TestWindow(frame, text0, 110);
  44     Window win2 = new TestWindow(win1, text1, 220);
  45     Frame outerFrame = new Frame("Outer");
  46     Button button1 = new Button("button-1");
  47     int shift;
  48 
  49     public void init() {
  50         try {
  51             robot = new Robot();
  52         } catch (AWTException e) {
  53             throw new RuntimeException("Error: unable to create robot", e);
  54         }
  55         // Create instructions for the user here, as well as set up
  56         // the environment -- set the layout manager, add buttons,
  57         // etc.
  58         this.setLayout (new BorderLayout ());
  59         shift = 100;




  60     }
  61 
  62     public void start() {
  63 
  64         frame.setBounds(0, 50, 400, 100);
  65         frame.setLayout(new FlowLayout());
  66         frame.add(button0);
  67 
  68         outerFrame.setBounds(0, 390, 400, 100);
  69         outerFrame.setLayout(new FlowLayout());
  70         outerFrame.add(button1);
  71 
  72         adjustAndShow(new Component[] {frame, win1, win2, outerFrame});
  73         robot.waitForIdle();
  74 
  75         test();
  76     }
  77 
  78     void adjustAndShow(Component[] comps) {
  79         for (Component comp: comps) {
  80             comp.setLocation(shift, (int)comp.getLocation().getY());
  81             comp.setVisible(true);
  82             robot.waitForIdle();
  83         }
  84     }
  85 
  86     void test() {
  87         clickOnCheckFocusOwner(button0);
  88         clickOnCheckFocusOwner(text1);
  89         clickOnCheckFocusOwner(button1);
  90         clickOn(frame);
  91         checkFocusOwner(text1);
  92         clickOnCheckFocusOwner(text0);
  93         clickOnCheckFocusOwner(button1);
  94         clickOn(frame);
  95         checkFocusOwner(text0);
  96 
  97         System.out.println("Test passed.");
  98     }
  99 
 100     void clickOnCheckFocusOwner(Component c) {
 101         clickOn(c);
 102         if (!checkFocusOwner(c)) {
 103             throw new RuntimeException("Test failed: couldn't focus <" + c + "> by mouse click!");
 104         }
 105     }
 106 
 107     boolean checkFocusOwner(Component comp) {
 108         return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
 109     }
 110 
 111     void clickOn(Component c) {
 112         Point p = c.getLocationOnScreen();
 113         Dimension d = c.getSize();
 114 
 115         System.out.println("Clicking " + c);
 116 
 117         if (c instanceof Frame) {
 118             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 119         } else {
 120             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 121         }
 122         robot.delay(50);
 123         robot.mousePress(InputEvent.BUTTON1_MASK);
 124         robot.delay(50);
 125         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 126         robot.waitForIdle();
 127     }
 128 
 129 }
 130 
 131 class TestWindow extends Window {
 132     TestWindow(Window owner, Component comp, int x) {
 133         super(owner);
 134         setBackground(Color.blue);
 135         setLayout(new FlowLayout());
 136         add(comp);
 137         comp.setBackground(Color.yellow);
 138         setBounds(0, x, 100, 100);
 139     }
 140 }









































































































































< prev index next >