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