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