1 /*
   2  * Copyright (c) 1998, 2014, 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 1.2 98/08/05
  26   @bug 4515763
  27   @summary Tests that clicking mouse and pressing keys generates correct amount of click-counts
  28   @author andrei.dmitriev: area=awt.mouse
  29   @run main ClickDuringKeypress
  30 */
  31 
  32 /**
  33  * ClickDuringKeypress.java
  34  *
  35  * summary:
  36  */
  37 
  38 import java.applet.Applet;
  39 import java.awt.*;
  40 import java.awt.event.*;
  41 
  42 public class ClickDuringKeypress implements MouseListener
  43  {
  44    //Declare things used in the test, like buttons and labels here
  45    final static int CLICKCOUNT = 10;
  46    final static int DOUBLE_CLICK_AUTO_DELAY = 10;
  47    volatile int lastClickCount = 0;
  48    volatile boolean clicked = false;
  49    volatile boolean ready = false;
  50 
  51    Frame frame;
  52    Robot robot;
  53 
  54    public void init()
  55     {
  56       //Create instructions for the user here, as well as set up
  57       // the environment -- set the layout manager, add buttons,
  58       // etc.
  59 
  60       frame = new Frame("ClickDuringKeypress");
  61       frame.addMouseListener(this);
  62       frame.addWindowListener(new WindowAdapter() {
  63           public void windowActivated(WindowEvent e) {
  64               synchronized(ClickDuringKeypress.this) {
  65                   ready = true;
  66                   ClickDuringKeypress.this.notifyAll();
  67               }
  68           }
  69       });
  70       frame.setBounds(0, 0, 400, 400);
  71 
  72       start();
  73 
  74     }//End  init()
  75 
  76    public void start ()
  77     {
  78       try {
  79         robot = new Robot();
  80       } catch (AWTException e) {
  81         System.out.println("Could not create Robot.");
  82         throw new RuntimeException("Couldn't create Robot.  Test fails");
  83       }
  84 
  85       robot.mouseMove(200, 200);
  86       frame.show();
  87 
  88       synchronized(this) {
  89           try {
  90               if (!ready) {
  91                   wait(10000);
  92               }
  93           } catch (InterruptedException ex) {
  94           }
  95           if (!ready) {
  96               System.out.println("Not Activated. Test fails");
  97               throw new RuntimeException("Not Activated. Test fails");
  98           }
  99       }
 100 
 101       doTest();
 102 
 103       //What would normally go into main() will probably go here.
 104       //Use System.out.println for diagnostic messages that you want
 105       //to read after the test is done.
 106       //Use Sysout.println for messages you want the tester to read.
 107 
 108     }// start()
 109 
 110     // Mouse should be over the Frame by this point
 111     private void doTest() {
 112       robot.setAutoDelay(2000);
 113       robot.waitForIdle();
 114       robot.keyPress(KeyEvent.VK_B);
 115       robot.mousePress(InputEvent.BUTTON1_MASK);
 116       robot.delay(10);
 117       robot.mouseRelease(InputEvent.BUTTON1_MASK);
 118       // Should trigger mouseClicked
 119       robot.keyRelease(KeyEvent.VK_B);
 120       robot.delay(1000);
 121 
 122       robot.setAutoDelay(DOUBLE_CLICK_AUTO_DELAY);
 123       for (int i = 0; i < CLICKCOUNT / 2; i++) {
 124         robot.mousePress(InputEvent.BUTTON1_MASK);
 125         robot.delay(10);
 126         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 127         robot.keyPress(KeyEvent.VK_B);
 128         robot.delay(10);
 129         robot.keyRelease(KeyEvent.VK_B);
 130         robot.mousePress(InputEvent.BUTTON1_MASK);
 131         robot.delay(10);
 132         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 133       }
 134       robot.waitForIdle();
 135       // check results
 136       robot.delay(200);
 137       if (!clicked) {
 138           System.out.println("No MOUSE_CLICKED events received.  Test fails.");
 139           throw new RuntimeException("No MOUSE_CLICKED events received.  Test fails.");
 140       }
 141       if (lastClickCount != CLICKCOUNT) {
 142           System.out.println("Actual click count: " + lastClickCount + " does not match expected click count: " + CLICKCOUNT + ".  Test fails");
 143           throw new RuntimeException("Actual click count: " + lastClickCount + " does not match expected click count: " + CLICKCOUNT + ".  Test fails");
 144 
 145       }
 146       // else test passes.
 147     }
 148 
 149     public void mouseEntered(MouseEvent e) {}
 150     public void mouseExited(MouseEvent e) {}
 151     public void mousePressed(MouseEvent e) {}
 152     public void mouseReleased(MouseEvent e) {}
 153     public void mouseClicked(MouseEvent e) {
 154         System.out.println(e.toString());
 155         clicked = true;
 156         lastClickCount = e.getClickCount();
 157     }
 158 
 159      public static void main(String[] args) {
 160          new ClickDuringKeypress().init();
 161      }
 162 
 163  }// class ClickDuringKeypress