1 /*
   2  * Copyright (c) 2007, 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  */
  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         // Turn off the dynamic layouting
  60         Toolkit.getDefaultToolkit().setDynamicLayout(false);
  61         System.out.println("isDynamicLayoutActive(): " + Toolkit.getDefaultToolkit().isDynamicLayoutActive());
  62 
  63 
  64         final Frame frame = new Frame("Test Frame");
  65 
  66         // Add some components to check their position later
  67         JPanel panel = new JPanel();
  68         panel.setBackground(Color.RED);
  69 
  70         JTextField jf = new JTextField (10);
  71         JTextField jf1 = new JTextField (10);
  72         JButton jb = new JButton("Test");
  73 
  74         panel.add(jf);
  75         panel.add(jf1);
  76         panel.add(jb);
  77         frame.add(panel);
  78 
  79         frame.setSize(400, 400);
  80         frame.setVisible(true);
  81 
  82         Robot robot = Util.createRobot();
  83         robot.setAutoDelay(20);
  84 
  85         // To be sure the window is shown and packed
  86         Util.waitForIdle(robot);
  87 
  88         // The initial JTextField position. While resizing the position supposed to stay the same.
  89         Point loc1 = jf1.getLocation();
  90 
  91         System.out.println("The initial position of the JTextField is: " + loc1);
  92 
  93         Insets insets = frame.getInsets();
  94         if (insets.right == 0 || insets.bottom == 0) {
  95             System.out.println("The test environment must have non-zero right & bottom insets! The current insets are: " + insets);
  96             pass();
  97             return;
  98         }
  99 
 100         // Let's move the mouse pointer to the bottom-right coner of the frame (the "size-grip")
 101         Rectangle bounds = frame.getBounds();
 102 
 103         robot.mouseMove(bounds.x + bounds.width - 1, bounds.y + bounds.height - 1);
 104 
 105         // ... and start resizing
 106         robot.mousePress( InputEvent.BUTTON1_MASK );
 107         robot.mouseMove(bounds.x + bounds.width + 20, bounds.y + bounds.height + 15);
 108         Util.waitForIdle(robot);
 109 
 110         // And check whether the location of the JTextField has changed.
 111         Point loc2 = jf1.getLocation();
 112         System.out.println("Position of the JTextField while resizing is: " + loc2);
 113 
 114         robot.mouseRelease( InputEvent.BUTTON1_MASK );
 115 
 116         // Location of the component changed if relayouting has happened.
 117         if (!loc2.equals(loc1)) {
 118             fail("Location of a component has been changed.");
 119             return;
 120         }
 121 
 122         DynamicLayout.pass();
 123 
 124     }//End  init()
 125 
 126 
 127 
 128     /*****************************************************
 129      * Standard Test Machinery Section
 130      * DO NOT modify anything in this section -- it's a
 131      * standard chunk of code which has all of the
 132      * synchronisation necessary for the test harness.
 133      * By keeping it the same in all tests, it is easier
 134      * to read and understand someone else's test, as
 135      * well as insuring that all tests behave correctly
 136      * with the test harness.
 137      * There is a section following this for test-
 138      * classes
 139      ******************************************************/
 140     private static boolean theTestPassed = false;
 141     private static boolean testGeneratedInterrupt = false;
 142     private static String failureMessage = "";
 143 
 144     private static Thread mainThread = null;
 145 
 146     private static int sleepTime = 300000;
 147 
 148     // Not sure about what happens if multiple of this test are
 149     //  instantiated in the same VM.  Being static (and using
 150     //  static vars), it aint gonna work.  Not worrying about
 151     //  it for now.
 152     public static void main( String args[] ) throws InterruptedException
 153     {
 154         mainThread = Thread.currentThread();
 155         try
 156         {
 157             init();
 158         }
 159         catch( TestPassedException e )
 160         {
 161             //The test passed, so just return from main and harness will
 162             // interepret this return as a pass
 163             return;
 164         }
 165         //At this point, neither test pass nor test fail has been
 166         // called -- either would have thrown an exception and ended the
 167         // test, so we know we have multiple threads.
 168 
 169         //Test involves other threads, so sleep and wait for them to
 170         // called pass() or fail()
 171         try
 172         {
 173             Thread.sleep( sleepTime );
 174             //Timed out, so fail the test
 175             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 176         }
 177         catch (InterruptedException e)
 178         {
 179             //The test harness may have interrupted the test.  If so, rethrow the exception
 180             // so that the harness gets it and deals with it.
 181             if( ! testGeneratedInterrupt ) throw e;
 182 
 183             //reset flag in case hit this code more than once for some reason (just safety)
 184             testGeneratedInterrupt = false;
 185 
 186             if ( theTestPassed == false )
 187             {
 188                 throw new RuntimeException( failureMessage );
 189             }
 190         }
 191 
 192     }//main
 193 
 194     public static synchronized void setTimeoutTo( int seconds )
 195     {
 196         sleepTime = seconds * 1000;
 197     }
 198 
 199     public static synchronized void pass()
 200     {
 201         System.out.println( "The test passed." );
 202         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 203         //first check if this is executing in main thread
 204         if ( mainThread == Thread.currentThread() )
 205         {
 206             //Still in the main thread, so set the flag just for kicks,
 207             // and throw a test passed exception which will be caught
 208             // and end the test.
 209             theTestPassed = true;
 210             throw new TestPassedException();
 211         }
 212         theTestPassed = true;
 213         testGeneratedInterrupt = true;
 214         mainThread.interrupt();
 215     }//pass()
 216 
 217     public static synchronized void fail()
 218     {
 219         //test writer didn't specify why test failed, so give generic
 220         fail( "it just plain failed! :-)" );
 221     }
 222 
 223     public static synchronized void fail( String whyFailed )
 224     {
 225         System.out.println( "The test failed: " + whyFailed );
 226         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 227         //check if this called from main thread
 228         if ( mainThread == Thread.currentThread() )
 229         {
 230             //If main thread, fail now 'cause not sleeping
 231             throw new RuntimeException( whyFailed );
 232         }
 233         theTestPassed = false;
 234         testGeneratedInterrupt = true;
 235         failureMessage = whyFailed;
 236         mainThread.interrupt();
 237     }//fail()
 238 
 239 }// class DynamicLayout
 240 
 241 //This exception is used to exit from any level of call nesting
 242 // when it's determined that the test has passed, and immediately
 243 // end the test.
 244 class TestPassedException extends RuntimeException
 245 {
 246 }