1 /*
   2  * Copyright (c) 2005, 2020, 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   @library /test/lib
  32   @modules java.desktop/sun.awt
  33   @build jdk.test.lib.Platform
  34   @build Util
  35   @run main OpensWithNoGrab
  36 */
  37 
  38 import java.awt.*;
  39 import java.awt.event.*;
  40 
  41 import jdk.test.lib.Platform;
  42 import test.java.awt.regtesthelpers.Util;
  43 
  44 public class OpensWithNoGrab
  45 {
  46     final static int delay = 50;
  47     private static void init()
  48     {
  49         if (!Platform.isLinux()) {
  50             System.out.println("This test is for XAWT/Motif only");
  51             OpensWithNoGrab.pass();
  52         }
  53 
  54         Choice ch = new Choice ();
  55         Frame f = new Frame ("OpensWithNoGrab");
  56         Robot robot;
  57         Point framePt, choicePt;
  58 
  59         ch.add("line 1");
  60         ch.add("line 2");
  61         ch.add("line 3");
  62         ch.add("line 4");
  63         ch.setBackground(Color.red);
  64         f.add(ch);
  65 
  66         Menu file = new Menu ("file");
  67         MenuBar mb = new MenuBar();
  68         mb.add(file);
  69 
  70         file.add(new MenuItem ("            "));
  71         file.add(new MenuItem ("            "));
  72         file.add(new MenuItem ("            "));
  73         file.add(new MenuItem ("            "));
  74         file.add(new MenuItem ("            "));
  75         file.add(new MenuItem ("            "));
  76         file.add(new MenuItem ("            "));
  77 
  78         f.setMenuBar(mb);
  79 
  80         f.setBackground(Color.green);
  81         f.setForeground(Color.green);
  82         f.setSize(300, 200);
  83         f.setVisible(true);
  84         try {
  85             robot = new Robot();
  86             robot.setAutoWaitForIdle(true);
  87             robot.setAutoDelay(50);
  88 
  89             Util.waitForIdle(robot);
  90             // press on Choice
  91             choicePt = ch.getLocationOnScreen();
  92             robot.mouseMove(choicePt.x + ch.getWidth()/2, choicePt.y + ch.getHeight()/2);
  93             robot.delay(delay);
  94             robot.mousePress(InputEvent.BUTTON1_MASK);
  95             robot.delay(delay);
  96             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  97             robot.delay(delay);
  98 
  99             // press on Menu
 100             framePt = f.getLocationOnScreen();
 101             robot.mouseMove(choicePt.x + 10, choicePt.y - 15);
 102             robot.delay(10*delay);
 103             robot.mousePress(InputEvent.BUTTON1_MASK);
 104             robot.delay(delay);
 105             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 106             robot.delay(delay);
 107 
 108             robot.mouseMove(choicePt.x + 15, choicePt.y + 15);
 109             Util.waitForIdle(robot);
 110 
 111             Color c = robot.getPixelColor(choicePt.x + 15, choicePt.y + 15);
 112             System.out.println("Color obtained under opened menu is: "+c );
 113             if (!c.equals(Color.red)){
 114                 OpensWithNoGrab.fail("Failed: menu was opened by first click after opened Choice.");
 115             }
 116         }catch(Exception e){
 117             e.printStackTrace();
 118             OpensWithNoGrab.fail("Failed: exception occur "+e);
 119         }
 120         OpensWithNoGrab.pass();
 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         System.out.println( "The test passed." );
 199         System.out.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         System.out.println( "The test failed: " + whyFailed );
 223         System.out.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 OpensWithNoGrab
 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 }