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