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