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