< prev index next >

test/jdk/java/awt/Mixing/LWComboBox.java

Print this page


   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  */


  36 /**
  37  * LWComboBox.java
  38  *
  39  * summary:  Tests whether a LW combobox correctly overlaps a HW button
  40  */
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import javax.swing.*;
  45 import java.util.Vector;
  46 import test.java.awt.regtesthelpers.Util;
  47 
  48 
  49 
  50 public class LWComboBox
  51 {
  52     static volatile boolean failed = false;
  53 
  54     private static void init()
  55     {
  56         //*** Create instructions for the user here ***
  57 
  58         String[] instructions =
  59         {
  60             "This is an AUTOMATIC test, simply wait until it is done.",
  61             "The result (passed or failed) will be shown in the",
  62             "message window below."
  63         };
  64         Sysout.createDialog( );
  65         Sysout.printInstructions( instructions );
  66 
  67         JFrame f = new JFrame("LW menu test");
  68 
  69         JComboBox ch;
  70         Button b;
  71 
  72         Vector v = new Vector();
  73         for(int i = 1 ; i <=20;i++){
  74             v.add("Item # "+i);
  75         }
  76         ch = new JComboBox(v);
  77 
  78 
  79         b = new Button("AWT Button");
  80         b.addActionListener(new ActionListener() {
  81             public void actionPerformed(ActionEvent e) {
  82                 failed = true;
  83             }
  84         });
  85 
  86         f.add(ch,BorderLayout.NORTH);


 178             if( ! testGeneratedInterrupt ) throw e;
 179 
 180             //reset flag in case hit this code more than once for some reason (just safety)
 181             testGeneratedInterrupt = false;
 182 
 183             if ( theTestPassed == false )
 184             {
 185                 throw new RuntimeException( failureMessage );
 186             }
 187         }
 188 
 189     }//main
 190 
 191     public static synchronized void setTimeoutTo( int seconds )
 192     {
 193         sleepTime = seconds * 1000;
 194     }
 195 
 196     public static synchronized void pass()
 197     {
 198         Sysout.println( "The test passed." );
 199         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 200         //first check if this is executing in main thread
 201         if ( mainThread == Thread.currentThread() )
 202         {
 203             //Still in the main thread, so set the flag just for kicks,
 204             // and throw a test passed exception which will be caught
 205             // and end the test.
 206             theTestPassed = true;
 207             throw new TestPassedException();
 208         }
 209         theTestPassed = true;
 210         testGeneratedInterrupt = true;
 211         mainThread.interrupt();
 212     }//pass()
 213 
 214     public static synchronized void fail()
 215     {
 216         //test writer didn't specify why test failed, so give generic
 217         fail( "it just plain failed! :-)" );
 218     }
 219 
 220     public static synchronized void fail( String whyFailed )
 221     {
 222         Sysout.println( "The test failed: " + whyFailed );
 223         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 224         //check if this called from main thread
 225         if ( mainThread == Thread.currentThread() )
 226         {
 227             //If main thread, fail now 'cause not sleeping
 228             throw new RuntimeException( whyFailed );
 229         }
 230         theTestPassed = false;
 231         testGeneratedInterrupt = true;
 232         failureMessage = whyFailed;
 233         mainThread.interrupt();
 234     }//fail()
 235 
 236 }// class LWComboBox
 237 
 238 //This exception is used to exit from any level of call nesting
 239 // when it's determined that the test has passed, and immediately
 240 // end the test.
 241 class TestPassedException extends RuntimeException
 242 {
 243 }
 244 
 245 //*********** End Standard Test Machinery Section **********
 246 
 247 
 248 //************ Begin classes defined for the test ****************
 249 
 250 // if want to make listeners, here is the recommended place for them, then instantiate
 251 //  them in init()
 252 
 253 /* Example of a class which may be written as part of a test
 254 class NewClass implements anInterface
 255  {
 256    static int newVar = 0;
 257 
 258    public void eventDispatched(AWTEvent e)
 259     {
 260       //Counting events to see if we get enough
 261       eventCount++;
 262 
 263       if( eventCount == 20 )
 264        {
 265          //got enough events, so pass
 266 
 267          LWComboBox.pass();
 268        }
 269       else if( tries == 20 )
 270        {
 271          //tried too many times without getting enough events so fail
 272 
 273          LWComboBox.fail();
 274        }
 275 
 276     }// eventDispatched()
 277 
 278  }// NewClass class
 279 
 280 */
 281 
 282 
 283 //************** End classes defined for the test *******************
 284 
 285 
 286 
 287 
 288 /****************************************************
 289  Standard Test Machinery
 290  DO NOT modify anything below -- it's a standard
 291   chunk of code whose purpose is to make user
 292   interaction uniform, and thereby make it simpler
 293   to read and understand someone else's test.
 294  ****************************************************/
 295 
 296 /**
 297  This is part of the standard test machinery.
 298  It creates a dialog (with the instructions), and is the interface
 299   for sending text messages to the user.
 300  To print the instructions, send an array of strings to Sysout.createDialog
 301   WithInstructions method.  Put one line of instructions per array entry.
 302  To display a message for the tester to see, simply call Sysout.println
 303   with the string to be displayed.
 304  This mimics System.out.println but works within the test harness as well
 305   as standalone.
 306  */
 307 
 308 class Sysout
 309 {
 310     private static TestDialog dialog;
 311 
 312     public static void createDialogWithInstructions( String[] instructions )
 313     {
 314         dialog = new TestDialog( new Frame(), "Instructions" );
 315         dialog.printInstructions( instructions );
 316         dialog.setVisible(true);
 317         println( "Any messages for the tester will display here." );
 318     }
 319 
 320     public static void createDialog( )
 321     {
 322         dialog = new TestDialog( new Frame(), "Instructions" );
 323         String[] defInstr = { "Instructions will appear here. ", "" } ;
 324         dialog.printInstructions( defInstr );
 325         dialog.setVisible(true);
 326         println( "Any messages for the tester will display here." );
 327     }
 328 
 329 
 330     public static void printInstructions( String[] instructions )
 331     {
 332         dialog.printInstructions( instructions );
 333     }
 334 
 335 
 336     public static void println( String messageIn )
 337     {
 338         dialog.displayMessage( messageIn );
 339         System.out.println(messageIn);
 340     }
 341 
 342 }// Sysout  class
 343 
 344 /**
 345   This is part of the standard test machinery.  It provides a place for the
 346    test instructions to be displayed, and a place for interactive messages
 347    to the user to be displayed.
 348   To have the test instructions displayed, see Sysout.
 349   To have a message to the user be displayed, see Sysout.
 350   Do not call anything in this dialog directly.
 351   */
 352 class TestDialog extends Dialog
 353 {
 354 
 355     TextArea instructionsText;
 356     TextArea messageText;
 357     int maxStringLength = 80;
 358 
 359     //DO NOT call this directly, go through Sysout
 360     public TestDialog( Frame frame, String name )
 361     {
 362         super( frame, name );
 363         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 364         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 365         add( "North", instructionsText );
 366 
 367         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 368         add("Center", messageText);
 369 
 370         pack();
 371 
 372         setVisible(true);
 373     }// TestDialog()
 374 
 375     //DO NOT call this directly, go through Sysout
 376     public void printInstructions( String[] instructions )
 377     {
 378         //Clear out any current instructions
 379         instructionsText.setText( "" );
 380 
 381         //Go down array of instruction strings
 382 
 383         String printStr, remainingStr;
 384         for( int i=0; i < instructions.length; i++ )
 385         {
 386             //chop up each into pieces maxSringLength long
 387             remainingStr = instructions[ i ];
 388             while( remainingStr.length() > 0 )
 389             {
 390                 //if longer than max then chop off first max chars to print
 391                 if( remainingStr.length() >= maxStringLength )
 392                 {
 393                     //Try to chop on a word boundary
 394                     int posOfSpace = remainingStr.
 395                         lastIndexOf( ' ', maxStringLength - 1 );
 396 
 397                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 398 
 399                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 400                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 401                 }
 402                 //else just print
 403                 else
 404                 {
 405                     printStr = remainingStr;
 406                     remainingStr = "";
 407                 }
 408 
 409                 instructionsText.append( printStr + "\n" );
 410 
 411             }// while
 412 
 413         }// for
 414 
 415     }//printInstructions()
 416 
 417     //DO NOT call this directly, go through Sysout
 418     public void displayMessage( String messageIn )
 419     {
 420         messageText.append( messageIn + "\n" );
 421         System.out.println(messageIn);
 422     }
 423 
 424 }// TestDialog  class
   1 /*
   2  * Copyright (c) 2009, 2018, 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  */


  36 /**
  37  * LWComboBox.java
  38  *
  39  * summary:  Tests whether a LW combobox correctly overlaps a HW button
  40  */
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import javax.swing.*;
  45 import java.util.Vector;
  46 import test.java.awt.regtesthelpers.Util;
  47 
  48 
  49 
  50 public class LWComboBox
  51 {
  52     static volatile boolean failed = false;
  53 
  54     private static void init()
  55     {











  56         JFrame f = new JFrame("LW menu test");
  57 
  58         JComboBox ch;
  59         Button b;
  60 
  61         Vector v = new Vector();
  62         for(int i = 1 ; i <=20;i++){
  63             v.add("Item # "+i);
  64         }
  65         ch = new JComboBox(v);
  66 
  67 
  68         b = new Button("AWT Button");
  69         b.addActionListener(new ActionListener() {
  70             public void actionPerformed(ActionEvent e) {
  71                 failed = true;
  72             }
  73         });
  74 
  75         f.add(ch,BorderLayout.NORTH);


 167             if( ! testGeneratedInterrupt ) throw e;
 168 
 169             //reset flag in case hit this code more than once for some reason (just safety)
 170             testGeneratedInterrupt = false;
 171 
 172             if ( theTestPassed == false )
 173             {
 174                 throw new RuntimeException( failureMessage );
 175             }
 176         }
 177 
 178     }//main
 179 
 180     public static synchronized void setTimeoutTo( int seconds )
 181     {
 182         sleepTime = seconds * 1000;
 183     }
 184 
 185     public static synchronized void pass()
 186     {
 187         System.out.println( "The test passed." );
 188         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 189         //first check if this is executing in main thread
 190         if ( mainThread == Thread.currentThread() )
 191         {
 192             //Still in the main thread, so set the flag just for kicks,
 193             // and throw a test passed exception which will be caught
 194             // and end the test.
 195             theTestPassed = true;
 196             throw new TestPassedException();
 197         }
 198         theTestPassed = true;
 199         testGeneratedInterrupt = true;
 200         mainThread.interrupt();
 201     }//pass()
 202 
 203     public static synchronized void fail()
 204     {
 205         //test writer didn't specify why test failed, so give generic
 206         fail( "it just plain failed! :-)" );
 207     }
 208 
 209     public static synchronized void fail( String whyFailed )
 210     {
 211         System.out.println( "The test failed: " + whyFailed );
 212         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 213         //check if this called from main thread
 214         if ( mainThread == Thread.currentThread() )
 215         {
 216             //If main thread, fail now 'cause not sleeping
 217             throw new RuntimeException( whyFailed );
 218         }
 219         theTestPassed = false;
 220         testGeneratedInterrupt = true;
 221         failureMessage = whyFailed;
 222         mainThread.interrupt();
 223     }//fail()
 224 
 225 }// class LWComboBox
 226 
 227 //This exception is used to exit from any level of call nesting
 228 // when it's determined that the test has passed, and immediately
 229 // end the test.
 230 class TestPassedException extends RuntimeException
 231 {
 232 }





















































































































































































< prev index next >