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