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