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