jdk/test/java/awt/grab/EmbeddedFrameTest1/EmbeddedFrameTest1.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25   @test
       
    26   @bug 6359129
       
    27   @summary REGRESSION: Popup menus dont respond to selections when extend outside Applet
       
    28   @author oleg.sukhodolsky area=awt.grab
       
    29   @library ../../regtesthelpers
       
    30   @build Util
       
    31   @run main EmbeddedFrameTest1
       
    32 */
       
    33 
       
    34 /**
       
    35  * EmbeddedFrameTest1.java
       
    36  *
       
    37  * summary: REGRESSION: Popup menus dont respond to selections when extend outside Applet
       
    38  */
       
    39 
       
    40 import java.awt.BorderLayout;
       
    41 import java.awt.Dialog;
       
    42 import java.awt.Frame;
       
    43 import java.awt.Panel;
       
    44 import java.awt.Robot;
       
    45 import java.awt.TextArea;
       
    46 import java.awt.Toolkit;
       
    47 
       
    48 import java.awt.event.ActionEvent;
       
    49 import java.awt.event.ActionListener;
       
    50 
       
    51 import javax.swing.JButton;
       
    52 import javax.swing.JPopupMenu;
       
    53 
       
    54 import sun.awt.SunToolkit;
       
    55 
       
    56 import test.java.awt.regtesthelpers.Util;
       
    57 
       
    58 public class EmbeddedFrameTest1
       
    59 {
       
    60     private static void init()
       
    61     {
       
    62         //*** Create instructions for the user here ***
       
    63 
       
    64         String[] instructions = {
       
    65             "This is an AUTOMATIC test, simply wait until it is done.",
       
    66             "The result (passed or failed) will be shown in the",
       
    67             "message window below."
       
    68         };
       
    69 
       
    70         Sysout.createDialog( );
       
    71         Sysout.printInstructions( instructions );
       
    72 
       
    73         SunToolkit tk = (SunToolkit) Toolkit.getDefaultToolkit();
       
    74         if ("sun.awt.motif.MToolkit".equals(tk.getClass().getName())) {
       
    75             System.out.println("We shouldn't test MToolkit.");
       
    76             EmbeddedFrameTest1.pass();
       
    77             return;
       
    78         }
       
    79 
       
    80         try {
       
    81             final Frame frame = new Frame("AWT Frame");
       
    82             frame.pack();
       
    83             frame.setSize(200,200);
       
    84 
       
    85             final Frame embedded_frame = Util.createEmbeddedFrame(frame);
       
    86             embedded_frame.setSize(200, 200);
       
    87             Sysout.println("embedded_frame = " + embedded_frame);
       
    88 
       
    89             final JPopupMenu menu = new JPopupMenu();
       
    90             JButton item = new JButton("A button in popup");
       
    91             item.addActionListener(new ActionListener() {
       
    92                     public void actionPerformed(ActionEvent e) {
       
    93                         System.out.println("Button pressed");
       
    94                     }
       
    95                 });
       
    96 
       
    97             menu.add(item);
       
    98 
       
    99             final JButton btn = new JButton("Press me to see popup");
       
   100             btn.addActionListener(new ActionListener() {
       
   101                     public void actionPerformed(ActionEvent e) {
       
   102                         menu.show(btn, 0, btn.getHeight());
       
   103                     }
       
   104                 });
       
   105             final Panel p = new Panel();
       
   106             p.setLayout(new BorderLayout());
       
   107             embedded_frame.add(p,BorderLayout.CENTER);
       
   108             embedded_frame.validate();
       
   109             p.add(btn);
       
   110             p.validate();
       
   111             frame.setVisible(true);
       
   112             Robot robot = new Robot();
       
   113             tk.realSync();
       
   114             Util.clickOnComp(btn, robot);
       
   115             tk.realSync();
       
   116 
       
   117             Util.clickOnComp(item, robot);
       
   118             tk.realSync();
       
   119             if (item.getMousePosition() == null) {
       
   120                 throw new RuntimeException("Popup was not closed (mouse above it)");
       
   121             }
       
   122             embedded_frame.remove(p);
       
   123             embedded_frame.dispose();
       
   124             frame.dispose();
       
   125         } catch (Throwable thr) {
       
   126             thr.printStackTrace();
       
   127             EmbeddedFrameTest1.fail("TEST FAILED: " + thr);
       
   128         }
       
   129         EmbeddedFrameTest1.pass();
       
   130     }//End  init()
       
   131 
       
   132     /*****************************************************
       
   133      * Standard Test Machinery Section
       
   134      * DO NOT modify anything in this section -- it's a
       
   135      * standard chunk of code which has all of the
       
   136      * synchronisation necessary for the test harness.
       
   137      * By keeping it the same in all tests, it is easier
       
   138      * to read and understand someone else's test, as
       
   139      * well as insuring that all tests behave correctly
       
   140      * with the test harness.
       
   141      * There is a section following this for test-
       
   142      * classes
       
   143      ******************************************************/
       
   144     private static boolean theTestPassed = false;
       
   145     private static boolean testGeneratedInterrupt = false;
       
   146     private static String failureMessage = "";
       
   147 
       
   148     private static Thread mainThread = null;
       
   149 
       
   150     private static int sleepTime = 300000;
       
   151 
       
   152     // Not sure about what happens if multiple of this test are
       
   153     //  instantiated in the same VM.  Being static (and using
       
   154     //  static vars), it aint gonna work.  Not worrying about
       
   155     //  it for now.
       
   156     public static void main( String args[] ) throws InterruptedException
       
   157     {
       
   158         mainThread = Thread.currentThread();
       
   159         try
       
   160         {
       
   161             init();
       
   162         }
       
   163         catch( TestPassedException e )
       
   164         {
       
   165             //The test passed, so just return from main and harness will
       
   166             // interepret this return as a pass
       
   167             return;
       
   168         }
       
   169         //At this point, neither test pass nor test fail has been
       
   170         // called -- either would have thrown an exception and ended the
       
   171         // test, so we know we have multiple threads.
       
   172 
       
   173         //Test involves other threads, so sleep and wait for them to
       
   174         // called pass() or fail()
       
   175         try
       
   176         {
       
   177             Thread.sleep( sleepTime );
       
   178             //Timed out, so fail the test
       
   179             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
       
   180         }
       
   181         catch (InterruptedException e)
       
   182         {
       
   183             //The test harness may have interrupted the test.  If so, rethrow the exception
       
   184             // so that the harness gets it and deals with it.
       
   185             if( ! testGeneratedInterrupt ) throw e;
       
   186 
       
   187             //reset flag in case hit this code more than once for some reason (just safety)
       
   188             testGeneratedInterrupt = false;
       
   189 
       
   190             if ( theTestPassed == false )
       
   191             {
       
   192                 throw new RuntimeException( failureMessage );
       
   193             }
       
   194         }
       
   195 
       
   196     }//main
       
   197 
       
   198     public static synchronized void setTimeoutTo( int seconds )
       
   199     {
       
   200         sleepTime = seconds * 1000;
       
   201     }
       
   202 
       
   203     public static synchronized void pass()
       
   204     {
       
   205         Sysout.println( "The test passed." );
       
   206         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
       
   207         //first check if this is executing in main thread
       
   208         if ( mainThread == Thread.currentThread() )
       
   209         {
       
   210             //Still in the main thread, so set the flag just for kicks,
       
   211             // and throw a test passed exception which will be caught
       
   212             // and end the test.
       
   213             theTestPassed = true;
       
   214             throw new TestPassedException();
       
   215         }
       
   216         theTestPassed = true;
       
   217         testGeneratedInterrupt = true;
       
   218         mainThread.interrupt();
       
   219     }//pass()
       
   220 
       
   221     public static synchronized void fail()
       
   222     {
       
   223         //test writer didn't specify why test failed, so give generic
       
   224         fail( "it just plain failed! :-)" );
       
   225     }
       
   226 
       
   227     public static synchronized void fail( String whyFailed )
       
   228     {
       
   229         Sysout.println( "The test failed: " + whyFailed );
       
   230         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
       
   231         //check if this called from main thread
       
   232         if ( mainThread == Thread.currentThread() )
       
   233         {
       
   234             //If main thread, fail now 'cause not sleeping
       
   235             throw new RuntimeException( whyFailed );
       
   236         }
       
   237         theTestPassed = false;
       
   238         testGeneratedInterrupt = true;
       
   239         failureMessage = whyFailed;
       
   240         mainThread.interrupt();
       
   241     }//fail()
       
   242 
       
   243 }// class EmbeddedFrameTest1
       
   244 
       
   245 //This exception is used to exit from any level of call nesting
       
   246 // when it's determined that the test has passed, and immediately
       
   247 // end the test.
       
   248 class TestPassedException extends RuntimeException
       
   249 {
       
   250 }
       
   251 
       
   252 //*********** End Standard Test Machinery Section **********
       
   253 
       
   254 
       
   255 /****************************************************
       
   256  Standard Test Machinery
       
   257  DO NOT modify anything below -- it's a standard
       
   258   chunk of code whose purpose is to make user
       
   259   interaction uniform, and thereby make it simpler
       
   260   to read and understand someone else's test.
       
   261  ****************************************************/
       
   262 
       
   263 /**
       
   264  This is part of the standard test machinery.
       
   265  It creates a dialog (with the instructions), and is the interface
       
   266   for sending text messages to the user.
       
   267  To print the instructions, send an array of strings to Sysout.createDialog
       
   268   WithInstructions method.  Put one line of instructions per array entry.
       
   269  To display a message for the tester to see, simply call Sysout.println
       
   270   with the string to be displayed.
       
   271  This mimics System.out.println but works within the test harness as well
       
   272   as standalone.
       
   273  */
       
   274 
       
   275 class Sysout
       
   276 {
       
   277     private static TestDialog dialog;
       
   278 
       
   279     public static void createDialogWithInstructions( String[] instructions )
       
   280     {
       
   281         dialog = new TestDialog( new Frame(), "Instructions" );
       
   282         dialog.printInstructions( instructions );
       
   283         dialog.setVisible(true);
       
   284         println( "Any messages for the tester will display here." );
       
   285     }
       
   286 
       
   287     public static void createDialog( )
       
   288     {
       
   289         dialog = new TestDialog( new Frame(), "Instructions" );
       
   290         String[] defInstr = { "Instructions will appear here. ", "" } ;
       
   291         dialog.printInstructions( defInstr );
       
   292         dialog.setVisible(true);
       
   293         println( "Any messages for the tester will display here." );
       
   294     }
       
   295 
       
   296 
       
   297     public static void printInstructions( String[] instructions )
       
   298     {
       
   299         dialog.printInstructions( instructions );
       
   300     }
       
   301 
       
   302 
       
   303     public static void println( String messageIn )
       
   304     {
       
   305         dialog.displayMessage( messageIn );
       
   306         System.out.println(messageIn);
       
   307     }
       
   308 
       
   309 }// Sysout  class
       
   310 
       
   311 /**
       
   312   This is part of the standard test machinery.  It provides a place for the
       
   313    test instructions to be displayed, and a place for interactive messages
       
   314    to the user to be displayed.
       
   315   To have the test instructions displayed, see Sysout.
       
   316   To have a message to the user be displayed, see Sysout.
       
   317   Do not call anything in this dialog directly.
       
   318   */
       
   319 class TestDialog extends Dialog
       
   320 {
       
   321 
       
   322     TextArea instructionsText;
       
   323     TextArea messageText;
       
   324     int maxStringLength = 80;
       
   325 
       
   326     //DO NOT call this directly, go through Sysout
       
   327     public TestDialog( Frame frame, String name )
       
   328     {
       
   329         super( frame, name );
       
   330         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   331         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
       
   332         add( "North", instructionsText );
       
   333 
       
   334         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
       
   335         add("Center", messageText);
       
   336 
       
   337         pack();
       
   338 
       
   339         setVisible(true);
       
   340     }// TestDialog()
       
   341 
       
   342     //DO NOT call this directly, go through Sysout
       
   343     public void printInstructions( String[] instructions )
       
   344     {
       
   345         //Clear out any current instructions
       
   346         instructionsText.setText( "" );
       
   347 
       
   348         //Go down array of instruction strings
       
   349 
       
   350         String printStr, remainingStr;
       
   351         for( int i=0; i < instructions.length; i++ )
       
   352         {
       
   353             //chop up each into pieces maxSringLength long
       
   354             remainingStr = instructions[ i ];
       
   355             while( remainingStr.length() > 0 )
       
   356             {
       
   357                 //if longer than max then chop off first max chars to print
       
   358                 if( remainingStr.length() >= maxStringLength )
       
   359                 {
       
   360                     //Try to chop on a word boundary
       
   361                     int posOfSpace = remainingStr.
       
   362                         lastIndexOf( ' ', maxStringLength - 1 );
       
   363 
       
   364                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
       
   365 
       
   366                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
       
   367                     remainingStr = remainingStr.substring( posOfSpace + 1 );
       
   368                 }
       
   369                 //else just print
       
   370                 else
       
   371                 {
       
   372                     printStr = remainingStr;
       
   373                     remainingStr = "";
       
   374                 }
       
   375 
       
   376                 instructionsText.append( printStr + "\n" );
       
   377 
       
   378             }// while
       
   379 
       
   380         }// for
       
   381 
       
   382     }//printInstructions()
       
   383 
       
   384     //DO NOT call this directly, go through Sysout
       
   385     public void displayMessage( String messageIn )
       
   386     {
       
   387         messageText.append( messageIn + "\n" );
       
   388         System.out.println(messageIn);
       
   389     }
       
   390 
       
   391 }// TestDialog  class