1 /*
   2  * Copyright (c) 2005, 2018, 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 ../../../../lib/testlibrary
  32   @modules java.desktop/sun.awt
  33   @build jdk.testlibrary.OSInfo
  34   @build Util
  35   @run main OpensWithNoGrab
  36 */
  37 
  38 import java.awt.*;
  39 import java.awt.event.*;
  40 
  41 import jdk.testlibrary.OSInfo;
  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 (!(OSInfo.getOSType().equals(OSInfo.OSType.LINUX)
  50                 || OSInfo.getOSType().equals(OSInfo.OSType.SOLARIS))) {
  51             System.out.println("This test is for XAWT/Motif only");
  52             OpensWithNoGrab.pass();
  53         }
  54 
  55         Choice ch = new Choice ();
  56         Frame f = new Frame ("OpensWithNoGrab");
  57         Robot robot;
  58         Point framePt, choicePt;
  59 
  60         ch.add("line 1");
  61         ch.add("line 2");
  62         ch.add("line 3");
  63         ch.add("line 4");
  64         ch.setBackground(Color.red);
  65         f.add(ch);
  66 
  67         Menu file = new Menu ("file");
  68         MenuBar mb = new MenuBar();
  69         mb.add(file);
  70 
  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         file.add(new MenuItem ("            "));
  78 
  79         f.setMenuBar(mb);
  80 
  81         f.setBackground(Color.green);
  82         f.setForeground(Color.green);
  83         f.setSize(300, 200);
  84         f.setVisible(true);
  85         try {
  86             robot = new Robot();
  87             robot.setAutoWaitForIdle(true);
  88             robot.setAutoDelay(50);
  89 
  90             Util.waitForIdle(robot);
  91             // press on Choice
  92             choicePt = ch.getLocationOnScreen();
  93             robot.mouseMove(choicePt.x + ch.getWidth()/2, choicePt.y + ch.getHeight()/2);
  94             robot.delay(delay);
  95             robot.mousePress(InputEvent.BUTTON1_MASK);
  96             robot.delay(delay);
  97             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  98             robot.delay(delay);
  99 
 100             // press on Menu
 101             framePt = f.getLocationOnScreen();
 102             robot.mouseMove(choicePt.x + 10, choicePt.y - 15);
 103             robot.delay(10*delay);
 104             robot.mousePress(InputEvent.BUTTON1_MASK);
 105             robot.delay(delay);
 106             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 107             robot.delay(delay);
 108 
 109             robot.mouseMove(choicePt.x + 15, choicePt.y + 15);
 110             Util.waitForIdle(robot);
 111 
 112             Color c = robot.getPixelColor(choicePt.x + 15, choicePt.y + 15);
 113             System.out.println("Color obtained under opened menu is: "+c );
 114             if (!c.equals(Color.red)){
 115                 OpensWithNoGrab.fail("Failed: menu was opened by first click after opened Choice.");
 116             }
 117         }catch(Exception e){
 118             e.printStackTrace();
 119             OpensWithNoGrab.fail("Failed: exception occur "+e);
 120         }
 121         OpensWithNoGrab.pass();
 122     }//End  init()
 123 
 124 
 125 
 126     /*****************************************************
 127      * Standard Test Machinery Section
 128      * DO NOT modify anything in this section -- it's a
 129      * standard chunk of code which has all of the
 130      * synchronisation necessary for the test harness.
 131      * By keeping it the same in all tests, it is easier
 132      * to read and understand someone else's test, as
 133      * well as insuring that all tests behave correctly
 134      * with the test harness.
 135      * There is a section following this for test-
 136      * classes
 137      ******************************************************/
 138     private static boolean theTestPassed = false;
 139     private static boolean testGeneratedInterrupt = false;
 140     private static String failureMessage = "";
 141 
 142     private static Thread mainThread = null;
 143 
 144     private static int sleepTime = 300000;
 145 
 146     // Not sure about what happens if multiple of this test are
 147     //  instantiated in the same VM.  Being static (and using
 148     //  static vars), it aint gonna work.  Not worrying about
 149     //  it for now.
 150     public static void main( String args[] ) throws InterruptedException
 151     {
 152         mainThread = Thread.currentThread();
 153         try
 154             {
 155                 init();
 156             }
 157         catch( TestPassedException e )
 158             {
 159                 //The test passed, so just return from main and harness will
 160                 // interepret this return as a pass
 161                 return;
 162             }
 163         //At this point, neither test pass nor test fail has been
 164         // called -- either would have thrown an exception and ended the
 165         // test, so we know we have multiple threads.
 166 
 167         //Test involves other threads, so sleep and wait for them to
 168         // called pass() or fail()
 169         try
 170             {
 171                 Thread.sleep( sleepTime );
 172                 //Timed out, so fail the test
 173                 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 174             }
 175         catch (InterruptedException e)
 176             {
 177                 //The test harness may have interrupted the test.  If so, rethrow the exception
 178                 // so that the harness gets it and deals with it.
 179                 if( ! testGeneratedInterrupt ) throw e;
 180 
 181                 //reset flag in case hit this code more than once for some reason (just safety)
 182                 testGeneratedInterrupt = false;
 183 
 184                 if ( theTestPassed == false )
 185                     {
 186                         throw new RuntimeException( failureMessage );
 187                     }
 188             }
 189 
 190     }//main
 191 
 192     public static synchronized void setTimeoutTo( int seconds )
 193     {
 194         sleepTime = seconds * 1000;
 195     }
 196 
 197     public static synchronized void pass()
 198     {
 199         System.out.println( "The test passed." );
 200         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 201         //first check if this is executing in main thread
 202         if ( mainThread == Thread.currentThread() )
 203             {
 204                 //Still in the main thread, so set the flag just for kicks,
 205                 // and throw a test passed exception which will be caught
 206                 // and end the test.
 207                 theTestPassed = true;
 208                 throw new TestPassedException();
 209             }
 210         theTestPassed = true;
 211         testGeneratedInterrupt = true;
 212         mainThread.interrupt();
 213     }//pass()
 214 
 215     public static synchronized void fail()
 216     {
 217         //test writer didn't specify why test failed, so give generic
 218         fail( "it just plain failed! :-)" );
 219     }
 220 
 221     public static synchronized void fail( String whyFailed )
 222     {
 223         System.out.println( "The test failed: " + whyFailed );
 224         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 225         //check if this called from main thread
 226         if ( mainThread == Thread.currentThread() )
 227             {
 228                 //If main thread, fail now 'cause not sleeping
 229                 throw new RuntimeException( whyFailed );
 230             }
 231         theTestPassed = false;
 232         testGeneratedInterrupt = true;
 233         failureMessage = whyFailed;
 234         mainThread.interrupt();
 235     }//fail()
 236 
 237 }// class OpensWithNoGrab
 238 
 239 //This exception is used to exit from any level of call nesting
 240 // when it's determined that the test has passed, and immediately
 241 // end the test.
 242 class TestPassedException extends RuntimeException
 243 {
 244 }