1 /*
   2  * Copyright (c) 2013, 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 4816114
  28  * @summary REGRESSION: Regression in divider location behavior when JSplitPane is resized
  29  * @author Andrey Pikalev
  30  * @run main bug4816114
  31  */
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.lang.reflect.*;
  36 
  37 
  38 public class bug4816114 {
  39 
  40     JFrame fr;
  41     JSplitPane splitPane;
  42 
  43     boolean[] resized = new boolean[] { false, false, false,
  44                                         false, false, false };
  45     static int step = 0;
  46     boolean h_passed = false;
  47     boolean v_passed = false;
  48 
  49     static bug4816114 test = new bug4816114();
  50 
  51     public static void main(String[] args) throws InterruptedException, InvocationTargetException, AWTException {
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             public void run() {
  54                 test.createAndShowGUI();
  55             }
  56         });
  57         Robot robot = new Robot();
  58         robot.waitForIdle();
  59         Thread.sleep(1000);
  60         Thread.sleep(2000);
  61 
  62         step++;
  63         test.doTest(150, 300);
  64 
  65         step++;
  66         test.doTest(650, 300);
  67 
  68         SwingUtilities.invokeAndWait(new Runnable() {
  69             public void run() {
  70                 test.splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
  71             }
  72         });
  73 
  74         step++;
  75         test.doTest(300, 650);
  76 
  77         step++;
  78         test.doTest(300, 150);
  79 
  80         step++;
  81         test.doTest(300, 650);
  82 
  83         if ( !test.isPassed() ) {
  84             throw new Error("The divider location is wrong.");
  85         }
  86     }
  87     public void createAndShowGUI() {
  88         fr = new JFrame("Test");
  89 
  90         splitPane = new TestSplitPane();
  91         splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
  92         splitPane.setResizeWeight(0);
  93         splitPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
  94 
  95         JButton leftButton = new JButton("LEFT");
  96         leftButton.setPreferredSize(new Dimension(300, 300));
  97         leftButton.setMinimumSize(new Dimension(150, 150));
  98         splitPane.setLeftComponent(leftButton);
  99 
 100         JButton rightButton = new JButton("RIGHT");
 101         rightButton.setPreferredSize(new Dimension(300, 300));
 102         rightButton.setMinimumSize(new Dimension(150, 150));
 103         splitPane.setRightComponent(rightButton);
 104 
 105         fr.getContentPane().add(splitPane, BorderLayout.CENTER);
 106 
 107         fr.pack();
 108         fr.setVisible(true);
 109     }
 110 
 111     void doTest(final int width, final int height)  throws InterruptedException, InvocationTargetException {
 112         SwingUtilities.invokeAndWait(new Runnable() {
 113             public void run() {
 114                 splitPane.setPreferredSize(new Dimension(width, height));
 115                 fr.pack();
 116             }
 117         });
 118 
 119         synchronized (bug4816114.this) {
 120             while (!resized[step]) {
 121                 bug4816114.this.wait();
 122             }
 123         }
 124     }
 125 
 126    synchronized void setPassed(int orientation, boolean passed) {
 127        if (orientation == JSplitPane.HORIZONTAL_SPLIT) {
 128            this.h_passed = passed;
 129        }
 130        else {
 131            this.v_passed = passed;
 132        }
 133    }
 134 
 135     synchronized boolean isPassed() {
 136         return h_passed && v_passed;
 137     }
 138 
 139 
 140     class TestSplitPane extends JSplitPane {
 141         public void setDividerLocation(int location) {
 142             super.setDividerLocation(location);
 143 
 144             if ( splitPane.getDividerLocation() == 151 ) {
 145                 setPassed(getOrientation(), true);
 146             }
 147 
 148             synchronized (bug4816114.this) {
 149                 resized[step] = true;
 150                 bug4816114.this.notifyAll();
 151             }
 152         }
 153     }
 154 }