ValidBounds.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2007, 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
26  @key headful
27  @bug 6637796
28  @summary Shape should be correctly updated on invalid components
29  @author anthony.petrov@...: area=awt.mixing
30  @library ../regtesthelpers
31  @build Util
32  @run main ValidBounds
33*/
34
35/**
36 * ValidBounds.java
37 *
38 * summary:  Shape should be correctly updated on invalid components
39 */
40
41import java.awt.*;
42import java.awt.event.*;
43import test.java.awt.regtesthelpers.Util;
44
45public class ValidBounds
46{
47
48    static volatile boolean clickPassed = false;
49
50    private static void init()
51    {
52        //*** Create instructions for the user here ***
53
54        String[] instructions =
55        {
56            "This is an AUTOMATIC test, simply wait until it is done.",
57            "The result (passed or failed) will be shown in the",
58            "message window below."
59        };
60        Sysout.createDialog( );
61        Sysout.printInstructions( instructions );
62
63
64        // Create the frame and the button
65        Frame f = new Frame();
66        f.setBounds(100, 100, 400, 300);
67
68        Button b = new Button("OK");
69
70        f.setLayout(null);
71        f.add(b);
72        b.setBounds(50, 50, 200, 50);
73
74        b.addActionListener(new java.awt.event.ActionListener() {
75            public void actionPerformed(java.awt.event.ActionEvent e) {
76                clickPassed = true;
77            }
78        });
79
80        f.setVisible(true);
81
82        // Let's make the button much smaller first...
83        Robot robot = Util.createRobot();
84        robot.setAutoDelay(20);
85
86        Util.waitForIdle(robot);
87
88        b.setBounds(50, 50, 5, 5);
89        Util.waitForIdle(robot);
90
91        // ... and now let's enlarge it.
92        b.setBounds(50, 50, 200, 50);
93        Util.waitForIdle(robot);
94
95        // If the button doesn't receive the click, it means that the test
96        // failed: the shape of the button was not enlarged.
97        Point heavyLoc = b.getLocationOnScreen();
98        robot.mouseMove(heavyLoc.x + 20, heavyLoc.y + 20);
99
100        robot.mousePress(InputEvent.BUTTON1_MASK);
101        robot.mouseRelease(InputEvent.BUTTON1_MASK);
102        Util.waitForIdle(robot);
103
104        if (clickPassed) {
105            pass();
106        } else {
107            fail("The button cannot be clicked.");
108        }
109    }//End  init()
110
111
112
113    /*****************************************************
114     * Standard Test Machinery Section
115     * DO NOT modify anything in this section -- it's a
116     * standard chunk of code which has all of the
117     * synchronisation necessary for the test harness.
118     * By keeping it the same in all tests, it is easier
119     * to read and understand someone else's test, as
120     * well as insuring that all tests behave correctly
121     * with the test harness.
122     * There is a section following this for test-
123     * classes
124     ******************************************************/
125    private static boolean theTestPassed = false;
126    private static boolean testGeneratedInterrupt = false;
127    private static String failureMessage = "";
128
129    private static Thread mainThread = null;
130
131    private static int sleepTime = 300000;
132
133    // Not sure about what happens if multiple of this test are
134    //  instantiated in the same VM.  Being static (and using
135    //  static vars), it aint gonna work.  Not worrying about
136    //  it for now.
137    public static void main( String args[] ) throws InterruptedException
138    {
139        mainThread = Thread.currentThread();
140        try
141        {
142            init();
143        }
144        catch( TestPassedException e )
145        {
146            //The test passed, so just return from main and harness will
147            // interepret this return as a pass
148            return;
149        }
150        //At this point, neither test pass nor test fail has been
151        // called -- either would have thrown an exception and ended the
152        // test, so we know we have multiple threads.
153
154        //Test involves other threads, so sleep and wait for them to
155        // called pass() or fail()
156        try
157        {
158            Thread.sleep( sleepTime );
159            //Timed out, so fail the test
160            throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
161        }
162        catch (InterruptedException e)
163        {
164            //The test harness may have interrupted the test.  If so, rethrow the exception
165            // so that the harness gets it and deals with it.
166            if( ! testGeneratedInterrupt ) throw e;
167
168            //reset flag in case hit this code more than once for some reason (just safety)
169            testGeneratedInterrupt = false;
170
171            if ( theTestPassed == false )
172            {
173                throw new RuntimeException( failureMessage );
174            }
175        }
176
177    }//main
178
179    public static synchronized void setTimeoutTo( int seconds )
180    {
181        sleepTime = seconds * 1000;
182    }
183
184    public static synchronized void pass()
185    {
186        Sysout.println( "The test passed." );
187        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
188        //first check if this is executing in main thread
189        if ( mainThread == Thread.currentThread() )
190        {
191            //Still in the main thread, so set the flag just for kicks,
192            // and throw a test passed exception which will be caught
193            // and end the test.
194            theTestPassed = true;
195            throw new TestPassedException();
196        }
197        theTestPassed = true;
198        testGeneratedInterrupt = true;
199        mainThread.interrupt();
200    }//pass()
201
202    public static synchronized void fail()
203    {
204        //test writer didn't specify why test failed, so give generic
205        fail( "it just plain failed! :-)" );
206    }
207
208    public static synchronized void fail( String whyFailed )
209    {
210        Sysout.println( "The test failed: " + whyFailed );
211        Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
212        //check if this called from main thread
213        if ( mainThread == Thread.currentThread() )
214        {
215            //If main thread, fail now 'cause not sleeping
216            throw new RuntimeException( whyFailed );
217        }
218        theTestPassed = false;
219        testGeneratedInterrupt = true;
220        failureMessage = whyFailed;
221        mainThread.interrupt();
222    }//fail()
223
224}// class ValidBounds
225
226//This exception is used to exit from any level of call nesting
227// when it's determined that the test has passed, and immediately
228// end the test.
229class TestPassedException extends RuntimeException
230{
231}
232
233//*********** End Standard Test Machinery Section **********
234
235
236//************ Begin classes defined for the test ****************
237
238// if want to make listeners, here is the recommended place for them, then instantiate
239//  them in init()
240
241/* Example of a class which may be written as part of a test
242class NewClass implements anInterface
243 {
244   static int newVar = 0;
245
246   public void eventDispatched(AWTEvent e)
247    {
248      //Counting events to see if we get enough
249      eventCount++;
250
251      if( eventCount == 20 )
252       {
253         //got enough events, so pass
254
255         ValidBounds.pass();
256       }
257      else if( tries == 20 )
258       {
259         //tried too many times without getting enough events so fail
260
261         ValidBounds.fail();
262       }
263
264    }// eventDispatched()
265
266 }// NewClass class
267
268*/
269
270
271//************** End classes defined for the test *******************
272
273
274
275
276/****************************************************
277 Standard Test Machinery
278 DO NOT modify anything below -- it's a standard
279  chunk of code whose purpose is to make user
280  interaction uniform, and thereby make it simpler
281  to read and understand someone else's test.
282 ****************************************************/
283
284/**
285 This is part of the standard test machinery.
286 It creates a dialog (with the instructions), and is the interface
287  for sending text messages to the user.
288 To print the instructions, send an array of strings to Sysout.createDialog
289  WithInstructions method.  Put one line of instructions per array entry.
290 To display a message for the tester to see, simply call Sysout.println
291  with the string to be displayed.
292 This mimics System.out.println but works within the test harness as well
293  as standalone.
294 */
295
296class Sysout
297{
298    private static TestDialog dialog;
299
300    public static void createDialogWithInstructions( String[] instructions )
301    {
302        dialog = new TestDialog( new Frame(), "Instructions" );
303        dialog.printInstructions( instructions );
304        dialog.setVisible(true);
305        println( "Any messages for the tester will display here." );
306    }
307
308    public static void createDialog( )
309    {
310        dialog = new TestDialog( new Frame(), "Instructions" );
311        String[] defInstr = { "Instructions will appear here. ", "" } ;
312        dialog.printInstructions( defInstr );
313        dialog.setVisible(true);
314        println( "Any messages for the tester will display here." );
315    }
316
317
318    public static void printInstructions( String[] instructions )
319    {
320        dialog.printInstructions( instructions );
321    }
322
323
324    public static void println( String messageIn )
325    {
326        dialog.displayMessage( messageIn );
327        System.out.println(messageIn);
328    }
329
330}// Sysout  class
331
332/**
333  This is part of the standard test machinery.  It provides a place for the
334   test instructions to be displayed, and a place for interactive messages
335   to the user to be displayed.
336  To have the test instructions displayed, see Sysout.
337  To have a message to the user be displayed, see Sysout.
338  Do not call anything in this dialog directly.
339  */
340class TestDialog extends Dialog
341{
342
343    TextArea instructionsText;
344    TextArea messageText;
345    int maxStringLength = 80;
346
347    //DO NOT call this directly, go through Sysout
348    public TestDialog( Frame frame, String name )
349    {
350        super( frame, name );
351        int scrollBoth = TextArea.SCROLLBARS_BOTH;
352        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
353        add( "North", instructionsText );
354
355        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
356        add("Center", messageText);
357
358        pack();
359
360        setVisible(true);
361    }// TestDialog()
362
363    //DO NOT call this directly, go through Sysout
364    public void printInstructions( String[] instructions )
365    {
366        //Clear out any current instructions
367        instructionsText.setText( "" );
368
369        //Go down array of instruction strings
370
371        String printStr, remainingStr;
372        for( int i=0; i < instructions.length; i++ )
373        {
374            //chop up each into pieces maxSringLength long
375            remainingStr = instructions[ i ];
376            while( remainingStr.length() > 0 )
377            {
378                //if longer than max then chop off first max chars to print
379                if( remainingStr.length() >= maxStringLength )
380                {
381                    //Try to chop on a word boundary
382                    int posOfSpace = remainingStr.
383                        lastIndexOf( ' ', maxStringLength - 1 );
384
385                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
386
387                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
388                    remainingStr = remainingStr.substring( posOfSpace + 1 );
389                }
390                //else just print
391                else
392                {
393                    printStr = remainingStr;
394                    remainingStr = "";
395                }
396
397                instructionsText.append( printStr + "\n" );
398
399            }// while
400
401        }// for
402
403    }//printInstructions()
404
405    //DO NOT call this directly, go through Sysout
406    public void displayMessage( String messageIn )
407    {
408        messageText.append( messageIn + "\n" );
409        System.out.println(messageIn);
410    }
411
412}// TestDialog  class
413