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