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