1/*
2 * Copyright (c) 2000, 2006, 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  @bug 4350402
27  @summary Tests that mouse behavior on LW component
28*/
29
30/**
31 * RobotLWTest.java
32 *
33 * summary:
34 */
35
36import java.applet.Applet;
37import java.awt.*;
38import java.awt.event.*;
39import test.java.awt.regtesthelpers.Util;
40
41public class RobotLWTest extends Applet
42{
43    //Declare things used in the test, like buttons and labels here
44
45    public void init()
46    {
47    }//End  init()
48
49    public void start ()
50    {
51        //What would normally go into main() will probably go here.
52        //Use System.out.println for diagnostic messages that you want
53        //to read after the test is done.
54        //Use Sysout.println for messages you want the tester to read.
55        Frame frame = new Frame();
56        MyLWContainer c = new MyLWContainer();
57        MyLWComponent b = new MyLWComponent();
58        c.add(b);
59        frame.add(c);
60        frame.setSize(400,400);
61        frame.setVisible(true);
62
63        try {
64            Robot robot = new Robot();
65            robot.setAutoWaitForIdle(true);
66            robot.setAutoDelay(100);
67            robot.waitForIdle();
68
69            Util.waitForIdle(robot);
70
71            Point pt = frame.getLocationOnScreen();
72            Point pt1 = b.getLocationOnScreen();
73
74            //Testing capture with multiple buttons
75            robot.mouseMove(pt1.x + b.getWidth()/2, pt1.y + b.getHeight()/2);
76            robot.mousePress(InputEvent.BUTTON1_MASK);
77            robot.mousePress(InputEvent.BUTTON2_MASK);
78            robot.mouseMove(pt.x + frame.getWidth()+10, pt.y + frame.getHeight()+10);
79            robot.mouseRelease(InputEvent.BUTTON2_MASK);
80            Util.waitForIdle(robot);
81
82            b.last = null;
83            robot.mouseRelease(InputEvent.BUTTON1_MASK);
84            Util.waitForIdle(robot);
85
86            if (b.last == null) {
87                throw new RuntimeException("RobotLWTest failed. Mouse Capture failed");
88            }
89            //Enter/Exit
90            b.last = null;
91            robot.mouseMove(pt1.x + b.getWidth()/2, pt1.y + b.getHeight()/2);
92            Util.waitForIdle(robot);
93
94            if (b.last == null || b.last.getID() != MouseEvent.MOUSE_ENTERED) {
95                throw new RuntimeException("RobotLWTest failed. Enter/Exit failed");
96            }
97            b.last = b.prev = null;
98            robot.mousePress(InputEvent.BUTTON1_MASK);
99            Util.waitForIdle(robot);
100
101            if (b.prev != null && b.prev.getID() == MouseEvent.MOUSE_ENTERED) {
102                robot.mouseRelease(InputEvent.BUTTON1_MASK);
103                throw new RuntimeException("RobotLWTest failed. Enter/Exit failed");
104            }
105            robot.mouseRelease(InputEvent.BUTTON1_MASK);
106
107        } catch (Exception e) {
108            throw new RuntimeException("The test was not completed.", e);
109        }
110    }// start()
111
112}// class RobotLWTest
113
114class MyLWContainer extends Container {
115    public MouseEvent last = null;
116    public MouseEvent prev = null;
117
118    MyLWContainer() {
119        enableEvents(MouseEvent.MOUSE_MOTION_EVENT_MASK);
120    }
121
122    public void processMouseEvent(MouseEvent e) {
123        prev = last;
124        last = e;
125        System.out.println(e.toString());
126        super.processMouseEvent(e);
127    }
128}
129
130class MyLWComponent extends Component {
131    public MouseEvent last = null;
132    public MouseEvent prev = null;
133
134    MyLWComponent() {
135        setSize(50,30);
136        enableEvents(MouseEvent.MOUSE_EVENT_MASK);
137    }
138
139    public void processMouseEvent(MouseEvent e) {
140        prev = last;
141        last = e;
142        System.out.println(e.toString());
143        super.processMouseEvent(e);
144    }
145
146    public void paint(Graphics g) {
147        Dimension d = getSize();
148        setBackground(isEnabled() ? Color.red : Color.gray);
149        g.clearRect(0, 0, d.width - 1, d.height -1);
150    }
151}
152
153/****************************************************
154 Standard Test Machinery
155 DO NOT modify anything below -- it's a standard
156  chunk of code whose purpose is to make user
157  interaction uniform, and thereby make it simpler
158  to read and understand someone else's test.
159 ****************************************************/
160
161/**
162 This is part of the standard test machinery.
163 It creates a dialog (with the instructions), and is the interface
164  for sending text messages to the user.
165 To print the instructions, send an array of strings to Sysout.createDialog
166  WithInstructions method.  Put one line of instructions per array entry.
167 To display a message for the tester to see, simply call Sysout.println
168  with the string to be displayed.
169 This mimics System.out.println but works within the test harness as well
170  as standalone.
171 */
172
173class Sysout
174 {
175   private static TestDialog dialog;
176
177   public static void createDialogWithInstructions( String[] instructions )
178    {
179      dialog = new TestDialog( new Frame(), "Instructions" );
180      dialog.printInstructions( instructions );
181      dialog.show();
182      println( "Any messages for the tester will display here." );
183    }
184
185   public static void createDialog( )
186    {
187      dialog = new TestDialog( new Frame(), "Instructions" );
188      String[] defInstr = { "Instructions will appear here. ", "" } ;
189      dialog.printInstructions( defInstr );
190      dialog.show();
191      println( "Any messages for the tester will display here." );
192    }
193
194
195   public static void printInstructions( String[] instructions )
196    {
197      dialog.printInstructions( instructions );
198    }
199
200
201   public static void println( String messageIn )
202    {
203      dialog.displayMessage( messageIn );
204    }
205
206 }// Sysout  class
207
208/**
209  This is part of the standard test machinery.  It provides a place for the
210   test instructions to be displayed, and a place for interactive messages
211   to the user to be displayed.
212  To have the test instructions displayed, see Sysout.
213  To have a message to the user be displayed, see Sysout.
214  Do not call anything in this dialog directly.
215  */
216class TestDialog extends Dialog
217 {
218
219   TextArea instructionsText;
220   TextArea messageText;
221   int maxStringLength = 80;
222
223   //DO NOT call this directly, go through Sysout
224   public TestDialog( Frame frame, String name )
225    {
226      super( frame, name );
227      int scrollBoth = TextArea.SCROLLBARS_BOTH;
228      instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
229      add( "North", instructionsText );
230
231      messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
232      add("South", messageText);
233
234      pack();
235
236      show();
237    }// TestDialog()
238
239   //DO NOT call this directly, go through Sysout
240   public void printInstructions( String[] instructions )
241    {
242      //Clear out any current instructions
243      instructionsText.setText( "" );
244
245      //Go down array of instruction strings
246
247      String printStr, remainingStr;
248      for( int i=0; i < instructions.length; i++ )
249       {
250         //chop up each into pieces maxSringLength long
251         remainingStr = instructions[ i ];
252         while( remainingStr.length() > 0 )
253          {
254            //if longer than max then chop off first max chars to print
255            if( remainingStr.length() >= maxStringLength )
256             {
257               //Try to chop on a word boundary
258               int posOfSpace = remainingStr.
259                  lastIndexOf( ' ', maxStringLength - 1 );
260
261               if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
262
263               printStr = remainingStr.substring( 0, posOfSpace + 1 );
264               remainingStr = remainingStr.substring( posOfSpace + 1 );
265             }
266            //else just print
267            else
268             {
269               printStr = remainingStr;
270               remainingStr = "";
271             }
272
273            instructionsText.append( printStr + "\n" );
274
275          }// while
276
277       }// for
278
279    }//printInstructions()
280
281   //DO NOT call this directly, go through Sysout
282   public void displayMessage( String messageIn )
283    {
284      messageText.append( messageIn + "\n" );
285    }
286
287 }// TestDialog  class
288