1 /*
   2  * Copyright (c) 2009, 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 /*
  25   @test %W% %E%
  26   @key headful
  27   @bug 6637655
  28   @summary Tests whether a LW combobox correctly overlaps a HW button
  29   @author anthony.petrov@...: area=awt.mixing
  30   @library ../regtesthelpers
  31   @build Util
  32   @run main LWComboBox
  33 */
  34 
  35 
  36 /**
  37  * LWComboBox.java
  38  *
  39  * summary:  Tests whether a LW combobox correctly overlaps a HW button
  40  */
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import javax.swing.*;
  45 import java.util.Vector;
  46 import test.java.awt.regtesthelpers.Util;
  47 
  48 
  49 
  50 public class LWComboBox
  51 {
  52     static volatile boolean failed = false;
  53 
  54     private static void init()
  55     {
  56         //*** Create instructions for the user here ***
  57 
  58         String[] instructions =
  59         {
  60             "This is an AUTOMATIC test, simply wait until it is done.",
  61             "The result (passed or failed) will be shown in the",
  62             "message window below."
  63         };
  64         Sysout.createDialog( );
  65         Sysout.printInstructions( instructions );
  66 
  67         JFrame f = new JFrame("LW menu test");
  68 
  69         JComboBox ch;
  70         Button b;
  71 
  72         Vector v = new Vector();
  73         for(int i = 1 ; i <=20;i++){
  74             v.add("Item # "+i);
  75         }
  76         ch = new JComboBox(v);
  77 
  78 
  79         b = new Button("AWT Button");
  80         b.addActionListener(new ActionListener() {
  81             public void actionPerformed(ActionEvent e) {
  82                 failed = true;
  83             }
  84         });
  85 
  86         f.add(ch,BorderLayout.NORTH);
  87         f.add(b,BorderLayout.CENTER);
  88         f.setSize(300,300);
  89         f.setVisible(true);
  90 
  91         Robot robot = Util.createRobot();
  92         robot.setAutoDelay(20);
  93 
  94         Util.waitForIdle(robot);
  95 
  96         // Pop up the combobox
  97         Point lLoc = ch.getLocationOnScreen();
  98         System.err.println("lLoc: " + lLoc);
  99         robot.mouseMove(lLoc.x + 5, lLoc.y + 5);
 100 
 101         robot.mousePress(InputEvent.BUTTON1_MASK);
 102         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 103         Util.waitForIdle(robot);
 104 
 105         // Click on the combo popup.
 106         //    It's assumed that the popup item is located
 107         //    above the heavyweight button.
 108         Point bLoc = b.getLocationOnScreen();
 109         System.err.println("bLoc: " + bLoc);
 110         robot.mouseMove(bLoc.x + 10, bLoc.y + 10);
 111 
 112         robot.mousePress(InputEvent.BUTTON1_MASK);
 113         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 114         Util.waitForIdle(robot);
 115 
 116         if (failed) {
 117             fail("The LW popup did not received the click.");
 118         } else {
 119             pass();
 120         }
 121     }//End  init()
 122 
 123 
 124 
 125     /*****************************************************
 126      * Standard Test Machinery Section
 127      * DO NOT modify anything in this section -- it's a
 128      * standard chunk of code which has all of the
 129      * synchronisation necessary for the test harness.
 130      * By keeping it the same in all tests, it is easier
 131      * to read and understand someone else's test, as
 132      * well as insuring that all tests behave correctly
 133      * with the test harness.
 134      * There is a section following this for test-
 135      * classes
 136      ******************************************************/
 137     private static boolean theTestPassed = false;
 138     private static boolean testGeneratedInterrupt = false;
 139     private static String failureMessage = "";
 140 
 141     private static Thread mainThread = null;
 142 
 143     private static int sleepTime = 300000;
 144 
 145     // Not sure about what happens if multiple of this test are
 146     //  instantiated in the same VM.  Being static (and using
 147     //  static vars), it aint gonna work.  Not worrying about
 148     //  it for now.
 149     public static void main( String args[] ) throws InterruptedException
 150     {
 151         mainThread = Thread.currentThread();
 152         try
 153         {
 154             init();
 155         }
 156         catch( TestPassedException e )
 157         {
 158             //The test passed, so just return from main and harness will
 159             // interepret this return as a pass
 160             return;
 161         }
 162         //At this point, neither test pass nor test fail has been
 163         // called -- either would have thrown an exception and ended the
 164         // test, so we know we have multiple threads.
 165 
 166         //Test involves other threads, so sleep and wait for them to
 167         // called pass() or fail()
 168         try
 169         {
 170             Thread.sleep( sleepTime );
 171             //Timed out, so fail the test
 172             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 173         }
 174         catch (InterruptedException e)
 175         {
 176             //The test harness may have interrupted the test.  If so, rethrow the exception
 177             // so that the harness gets it and deals with it.
 178             if( ! testGeneratedInterrupt ) throw e;
 179 
 180             //reset flag in case hit this code more than once for some reason (just safety)
 181             testGeneratedInterrupt = false;
 182 
 183             if ( theTestPassed == false )
 184             {
 185                 throw new RuntimeException( failureMessage );
 186             }
 187         }
 188 
 189     }//main
 190 
 191     public static synchronized void setTimeoutTo( int seconds )
 192     {
 193         sleepTime = seconds * 1000;
 194     }
 195 
 196     public static synchronized void pass()
 197     {
 198         Sysout.println( "The test passed." );
 199         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 200         //first check if this is executing in main thread
 201         if ( mainThread == Thread.currentThread() )
 202         {
 203             //Still in the main thread, so set the flag just for kicks,
 204             // and throw a test passed exception which will be caught
 205             // and end the test.
 206             theTestPassed = true;
 207             throw new TestPassedException();
 208         }
 209         theTestPassed = true;
 210         testGeneratedInterrupt = true;
 211         mainThread.interrupt();
 212     }//pass()
 213 
 214     public static synchronized void fail()
 215     {
 216         //test writer didn't specify why test failed, so give generic
 217         fail( "it just plain failed! :-)" );
 218     }
 219 
 220     public static synchronized void fail( String whyFailed )
 221     {
 222         Sysout.println( "The test failed: " + whyFailed );
 223         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 224         //check if this called from main thread
 225         if ( mainThread == Thread.currentThread() )
 226         {
 227             //If main thread, fail now 'cause not sleeping
 228             throw new RuntimeException( whyFailed );
 229         }
 230         theTestPassed = false;
 231         testGeneratedInterrupt = true;
 232         failureMessage = whyFailed;
 233         mainThread.interrupt();
 234     }//fail()
 235 
 236 }// class LWComboBox
 237 
 238 //This exception is used to exit from any level of call nesting
 239 // when it's determined that the test has passed, and immediately
 240 // end the test.
 241 class TestPassedException extends RuntimeException
 242 {
 243 }
 244 
 245 //*********** End Standard Test Machinery Section **********
 246 
 247 
 248 //************ Begin classes defined for the test ****************
 249 
 250 // if want to make listeners, here is the recommended place for them, then instantiate
 251 //  them in init()
 252 
 253 /* Example of a class which may be written as part of a test
 254 class NewClass implements anInterface
 255  {
 256    static int newVar = 0;
 257 
 258    public void eventDispatched(AWTEvent e)
 259     {
 260       //Counting events to see if we get enough
 261       eventCount++;
 262 
 263       if( eventCount == 20 )
 264        {
 265          //got enough events, so pass
 266 
 267          LWComboBox.pass();
 268        }
 269       else if( tries == 20 )
 270        {
 271          //tried too many times without getting enough events so fail
 272 
 273          LWComboBox.fail();
 274        }
 275 
 276     }// eventDispatched()
 277 
 278  }// NewClass class
 279 
 280 */
 281 
 282 
 283 //************** End classes defined for the test *******************
 284 
 285 
 286 
 287 
 288 /****************************************************
 289  Standard Test Machinery
 290  DO NOT modify anything below -- it's a standard
 291   chunk of code whose purpose is to make user
 292   interaction uniform, and thereby make it simpler
 293   to read and understand someone else's test.
 294  ****************************************************/
 295 
 296 /**
 297  This is part of the standard test machinery.
 298  It creates a dialog (with the instructions), and is the interface
 299   for sending text messages to the user.
 300  To print the instructions, send an array of strings to Sysout.createDialog
 301   WithInstructions method.  Put one line of instructions per array entry.
 302  To display a message for the tester to see, simply call Sysout.println
 303   with the string to be displayed.
 304  This mimics System.out.println but works within the test harness as well
 305   as standalone.
 306  */
 307 
 308 class Sysout
 309 {
 310     private static TestDialog dialog;
 311 
 312     public static void createDialogWithInstructions( String[] instructions )
 313     {
 314         dialog = new TestDialog( new Frame(), "Instructions" );
 315         dialog.printInstructions( instructions );
 316         dialog.setVisible(true);
 317         println( "Any messages for the tester will display here." );
 318     }
 319 
 320     public static void createDialog( )
 321     {
 322         dialog = new TestDialog( new Frame(), "Instructions" );
 323         String[] defInstr = { "Instructions will appear here. ", "" } ;
 324         dialog.printInstructions( defInstr );
 325         dialog.setVisible(true);
 326         println( "Any messages for the tester will display here." );
 327     }
 328 
 329 
 330     public static void printInstructions( String[] instructions )
 331     {
 332         dialog.printInstructions( instructions );
 333     }
 334 
 335 
 336     public static void println( String messageIn )
 337     {
 338         dialog.displayMessage( messageIn );
 339         System.out.println(messageIn);
 340     }
 341 
 342 }// Sysout  class
 343 
 344 /**
 345   This is part of the standard test machinery.  It provides a place for the
 346    test instructions to be displayed, and a place for interactive messages
 347    to the user to be displayed.
 348   To have the test instructions displayed, see Sysout.
 349   To have a message to the user be displayed, see Sysout.
 350   Do not call anything in this dialog directly.
 351   */
 352 class TestDialog extends Dialog
 353 {
 354 
 355     TextArea instructionsText;
 356     TextArea messageText;
 357     int maxStringLength = 80;
 358 
 359     //DO NOT call this directly, go through Sysout
 360     public TestDialog( Frame frame, String name )
 361     {
 362         super( frame, name );
 363         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 364         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 365         add( "North", instructionsText );
 366 
 367         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 368         add("Center", messageText);
 369 
 370         pack();
 371 
 372         setVisible(true);
 373     }// TestDialog()
 374 
 375     //DO NOT call this directly, go through Sysout
 376     public void printInstructions( String[] instructions )
 377     {
 378         //Clear out any current instructions
 379         instructionsText.setText( "" );
 380 
 381         //Go down array of instruction strings
 382 
 383         String printStr, remainingStr;
 384         for( int i=0; i < instructions.length; i++ )
 385         {
 386             //chop up each into pieces maxSringLength long
 387             remainingStr = instructions[ i ];
 388             while( remainingStr.length() > 0 )
 389             {
 390                 //if longer than max then chop off first max chars to print
 391                 if( remainingStr.length() >= maxStringLength )
 392                 {
 393                     //Try to chop on a word boundary
 394                     int posOfSpace = remainingStr.
 395                         lastIndexOf( ' ', maxStringLength - 1 );
 396 
 397                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 398 
 399                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 400                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 401                 }
 402                 //else just print
 403                 else
 404                 {
 405                     printStr = remainingStr;
 406                     remainingStr = "";
 407                 }
 408 
 409                 instructionsText.append( printStr + "\n" );
 410 
 411             }// while
 412 
 413         }// for
 414 
 415     }//printInstructions()
 416 
 417     //DO NOT call this directly, go through Sysout
 418     public void displayMessage( String messageIn )
 419     {
 420         messageText.append( messageIn + "\n" );
 421         System.out.println(messageIn);
 422     }
 423 
 424 }// TestDialog  class