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 6273031
  28   @summary  PIT. Choice drop down does not close once it is right clicked to show a popup menu
  29   @author andrei.dmitriev: area=awt.window
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main GrabSequence
  33 */
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 import test.java.awt.regtesthelpers.Util;
  38 
  39 public class GrabSequence
  40 {
  41     private static void init()
  42     {
  43         String toolkit = Toolkit.getDefaultToolkit().getClass().getName();
  44         if ( toolkit.equals("sun.awt.motif.MToolkit")){
  45             System.out.println("This test is for XToolkit and WToolkit only. Now using " + toolkit + ". Automatically passed.");
  46             return;
  47         }
  48         Frame frame = new Frame("Frame");
  49         frame.setBackground(Color.green);
  50         frame.setForeground(Color.green);
  51         frame.setLayout(new FlowLayout());
  52         Choice ch = new Choice();
  53         ch.setBackground(Color.red);
  54         ch.setForeground(Color.red);
  55         ch.addItem("One");
  56         ch.addItem("Two");
  57         ch.addItem("Three");
  58         ch.addItem("Four");
  59         ch.addItem("Five");
  60         final PopupMenu pm = new PopupMenu();
  61 
  62         MenuItem i1 = new MenuItem("Item1");
  63         MenuItem i2 = new MenuItem("Item2");
  64         MenuItem i3 = new MenuItem("Item3");
  65         pm.add(i1);
  66         pm.add(i2);
  67         pm.add(i3);
  68         ch.add(pm);
  69         ch.addMouseListener(new MouseAdapter() {
  70             public void mousePressed(MouseEvent event) {
  71                 if (event.isPopupTrigger()) {
  72                     System.out.println("mousePressed"+event);
  73                     pm.show((Choice)event.getSource(), event.getX(), event.getY());
  74                 }
  75             }
  76 
  77             public void mouseReleased(MouseEvent event) {
  78                 if (event.isPopupTrigger()) {
  79                     System.out.println("mouseReleased"+event);
  80                     pm.show((Choice)event.getSource(), event.getX(), event.getY());
  81                 }
  82             }
  83         });
  84         frame.add(ch);
  85         frame.setSize(200, 200);
  86 
  87 
  88         frame.setVisible(true);
  89 
  90         try {
  91             Robot robot = new Robot();
  92             Util.waitForIdle(robot);
  93             robot.mouseMove(ch.getLocationOnScreen().x + ch.getWidth()/2,
  94                             ch.getLocationOnScreen().y + ch.getHeight()/2 );
  95             robot.mousePress(InputEvent.BUTTON1_MASK);
  96             robot.delay(100);
  97             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  98             Util.waitForIdle(robot);
  99 
 100             Color color = robot.getPixelColor(ch.getLocationOnScreen().x + ch.getWidth()/2,
 101                                               ch.getLocationOnScreen().y + ch.getHeight()*4);
 102             System.out.println("1. Color is " + color);
 103             if (!color.equals(Color.red)){
 104                 GrabSequence.fail("Test failed. Choice is still not opened. ");
 105             }
 106 
 107             robot.mousePress(InputEvent.BUTTON3_MASK);
 108             robot.delay(100);
 109             robot.mouseRelease(InputEvent.BUTTON3_MASK);
 110             Util.waitForIdle(robot);
 111 
 112             color = robot.getPixelColor(ch.getLocationOnScreen().x + ch.getWidth()/2,
 113                                               ch.getLocationOnScreen().y + ch.getHeight()*2);
 114             System.out.println("2. Color is " + color);
 115             if (color.equals(Color.green)){
 116                 GrabSequence.fail("Test failed. popup menu is not opened. ");
 117             }
 118 
 119             color = robot.getPixelColor(ch.getLocationOnScreen().x + ch.getWidth()/2,
 120                                               ch.getLocationOnScreen().y + ch.getHeight()*4);
 121             System.out.println("3. Color is " + color);
 122             if (!color.equals(Color.green) && !Toolkit.getDefaultToolkit().getClass().getName().equals("sun.awt.windows.WToolkit")){
 123                 GrabSequence.fail("Test failed. Choice is still opened. ");
 124             }
 125         } catch(AWTException e){
 126             GrabSequence.fail("Test interrupted."+e);
 127         }
 128 
 129         GrabSequence.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         System.out.println( "The test passed." );
 208         System.out.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         System.out.println( "The test failed: " + whyFailed );
 232         System.out.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
 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 }