/*
 * Copyright (C) 2005-2006 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 */ 

package org.jdesktop.layout;

import java.awt.Container;
import junit.framework.*;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.border.EmptyBorder;


/**
 *
 * @author sky
 */
public class GroupLayoutTest extends TestCase {
    private static final int HORIZONTAL = SwingConstants.HORIZONTAL; // GroupLayout.HORIZONTAL
    private JPanel panel;
    private GroupLayout layout;
    
    public GroupLayoutTest(String testName) {
        super(testName);
    }
    
    protected void setUp() throws java.lang.Exception {
    }
    
    protected void tearDown() throws java.lang.Exception {
    }
    
    private void init(boolean autocreate) {
        init(autocreate, false);
    }
    
    private void init(boolean autocreate, boolean autocreateContainerPadding) {
        panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutocreateGaps(autocreate);
        layout.setAutocreateContainerGaps(autocreateContainerPadding);
    }
    
    private void layout() {
        panel.invalidate();
        panel.getPreferredSize();
        panel.setSize(panel.getPreferredSize());
        panel.doLayout();
    }

    private Component createAWTComponent(final int min, final int pref, final int max) {
        return new Component() {
            public Dimension getMinimumSize() {
                return new Dimension(min, min);
            }
            public Dimension getPreferredSize() {
                return new Dimension(pref, pref);
            }
            public Dimension getMaximumSize() {
                return new Dimension(max, max);
            }
        };
    }
    
    private JComponent createComponent(String name) {
        JPanel panel = new JPanel();
        panel.setName(name);
        return panel;
    }
    
    private JComponent createComponent(int min, int pref, int max) {
        return createComponent(min, pref, max, null);
    }
    
    private JComponent createComponent(int min, int pref, int max, String name) {
        JPanel panel = new JPanel();
        panel.setMinimumSize(new Dimension(min, min));
        panel.setPreferredSize(new Dimension(pref, pref));
        panel.setMaximumSize(new Dimension(max, max));
        panel.setName(name);
        return panel;
    }
    
    public void testZeroGap() {
        // This test case corresponds to issue 37. It currently has no asserts.
        init(false, false);
        JCheckBox jCheckBox1 = new javax.swing.JCheckBox();
        JTextField jTextField1 = new javax.swing.JTextField();
        JTextField jTextField2 = new javax.swing.JTextField();

        jCheckBox1.setText("jCheckBox1");
        jCheckBox1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBox1.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jTextField1.setText("jTextField1");
        jTextField2.setText("jTextField2");
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jCheckBox1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(317, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jCheckBox1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(222, Short.MAX_VALUE))
        );
        jTextField1.setVisible(false);
    }
    
    public void testLinked2() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10);
        JComponent c2 = createComponent(20, 20, 20);
        JComponent c3 = createComponent(30, 30, 30);
        JComponent c4 = createComponent(5, 5, 5);
        JComponent c5 = createComponent(15, 15, 15);
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3).add(c4).
                add(c5));
        layout.setVerticalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3).add(c4).
                add(c5));
        layout.linkSize(new Component[] { c1, c2 });
        layout.linkSize(new Component[] { c3, c4, c5 });
        layout.linkSize(new Component[] { c3, c1 });
        assertEquals(150, panel.getPreferredSize().width);
        assertEquals(150, panel.getPreferredSize().height);
        panel.remove(c3);
        assertEquals(80, panel.getPreferredSize().width);
        assertEquals(80, panel.getPreferredSize().height);
    }

    public void testLinked() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10);
        JComponent c2 = createComponent(20, 20, 20);
        JComponent c3 = createComponent(30, 30, 30);
        JComponent c4 = createComponent(5, 5, 5);
        JComponent c5 = createComponent(15, 15, 15);
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3).add(c4).
                add(c5));
        layout.setVerticalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3).add(c4).
                add(c5));
        layout.linkSize(new Component[] { c1, c2 });
        layout.linkSize(new Component[] { c3, c4, c5 });
        layout.linkSize(new Component[] { c3, c1 });
        assertEquals(150, panel.getPreferredSize().width);
        assertEquals(150, panel.getPreferredSize().height);
        c3.setVisible(false);
        assertEquals(80, panel.getPreferredSize().width);
        assertEquals(80, panel.getPreferredSize().height);
    }
    
    public void testHonorsVisibility1() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10);
        layout.setHorizontalGroup(layout.createSequentialGroup().add(c1));
        layout.setVerticalGroup(layout.createSequentialGroup().add(c1));
        c1.setVisible(false);
        layout.setHonorsVisibility(false);
        assertEquals(10, panel.getPreferredSize().width);
        layout.setHonorsVisibility(true);
        panel.invalidate();
        assertEquals(0, panel.getPreferredSize().width);
    }
    
    public void testDynamicVisible() {
        init(false, false);
        JButton jButton1 = new javax.swing.JButton();
        JLabel jLabel1 = new javax.swing.JLabel();
        JTextField jTextField1 = new javax.swing.JTextField();

        jButton1.setText("jButton1");
        jLabel1.setText("jLabel1");
        jTextField1.setText("jTextField1");

        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(GroupLayout.LEADING)
                    .add(jButton1)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(LayoutStyle.RELATED)
                        .add(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(293, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .add(42, 42, 42)
                .add(layout.createParallelGroup(GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(204, Short.MAX_VALUE))
        );
        
        jLabel1.setVisible(false);
        jTextField1.setVisible(false);
        layout();
        jLabel1.setVisible(true);
        jTextField1.setVisible(true);
        layout();
    }
    
    public void testSingleComponentBaseline() {
        init(false, false);
        Component c1 = new BaselineComponent(4, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 2, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(layout.createParallelGroup(GroupLayout.BASELINE).
                  add(c1)).
                add(layout.createParallelGroup(GroupLayout.BASELINE).
                  add(c2)));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(12, panel.getPreferredSize().height);
        layout();
        assertEquals(0, c1.getY());
        assertEquals(12, c1.getHeight());
        assertEquals(2, c2.getY());
        assertEquals(10, c2.getHeight());
    }
    
    public void testBaselineNonvisible() {
        init(false, false);
        Component c1 = new BaselineComponent(4, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 6, Baseline.BRB_CONSTANT_ASCENT);
        Component c3 = new BaselineComponent(6, 10, 100, 2, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2).add(c3));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3));
        assertEquals(14, panel.getPreferredSize().height);
        c2.setVisible(false);
        assertEquals(12, panel.getPreferredSize().height);
        layout();
        assertEquals(0, c1.getY());
        assertEquals(12, c1.getHeight());
        assertEquals(2, c3.getY());
        assertEquals(10, c3.getHeight());
    }

    public void testResizableBaseline11() {
        init(false, false);
        Component c1 = new BaselineComponent(4, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 6, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1, 10, 10, 10).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        panel.setSize(1000, 100);
        panel.doLayout();
        assertEquals(90, c1.getY());
        assertEquals(10, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(98, c2.getHeight());
    }
    
    public void testBaselineMin5() {
        init(false, false);
        Component c1 = new BaselineComponent(4, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 6, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createBaselineGroup(true, true).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(10, panel.getMinimumSize().height);
        assertEquals(12, panel.getPreferredSize().height);

        panel.setSize(1000, 10);
        panel.doLayout();
        assertEquals(2, c1.getY());
        assertEquals(8, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(10, c2.getHeight());
    }
    
    public void testBaselineMin4() {
        init(false, false);
        Component c1 = new BaselineComponent(4, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 6, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createBaselineGroup(true, false).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(10, panel.getMinimumSize().height);
        assertEquals(12, panel.getPreferredSize().height);

        panel.setSize(1000, 10);
        panel.doLayout();
        assertEquals(0, c1.getY());
        assertEquals(10, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(8, c2.getHeight());
    }
    
    public void testBaselineMin3() {
        init(false, false);
        Component c1 = new BaselineComponent(4, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 6, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(10, panel.getMinimumSize().height);
        assertEquals(12, panel.getPreferredSize().height);

        panel.setSize(1000, 10);
        panel.doLayout();
        assertEquals(2, c1.getY());
        assertEquals(8, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(10, c2.getHeight());
    }
    
    public void testBaselineMin2() {
        init(false, false);
        Component c1 = new BaselineComponent(6, 10, 100, 6, Baseline.BRB_CONSTANT_DESCENT);
        Component c2 = new BaselineComponent(7, 10, 100, 5, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(7, panel.getMinimumSize().height);
        assertEquals(11, panel.getPreferredSize().height);

        panel.setSize(1000, 7);
        panel.doLayout();
        assertEquals(0, c1.getY());
        assertEquals(6, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(7, c2.getHeight());
    }
    
    public void testBaselineMin() {
        init(false, false);
        Component c1 = new BaselineComponent(5, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(6, 10, 100, 5, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(6, panel.getMinimumSize().height);
        assertEquals(11, panel.getPreferredSize().height);

        panel.setSize(1000, 6);
        panel.doLayout();
        assertEquals(1, c1.getY());
        assertEquals(5, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(6, c2.getHeight());
    }
    
    public void testResizableBaseline10() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 20, 20, 6, Baseline.BRB_OTHER);
        Component c3 = new BaselineComponent(10, 10, 200, 4, Baseline.BRB_OTHER);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(layout.createSequentialGroup().
                add(true, c2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE).
                add(c3)));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3));
        assertEquals(30, panel.getPreferredSize().height);

        layout();
        assertEquals(2, c1.getY());
        assertEquals(28, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(20, c2.getHeight());
        assertEquals(20, c3.getY());
        assertEquals(10, c3.getHeight());
        
        panel.setSize(1000, 100);
        panel.doLayout();
        assertEquals(2, c1.getY());
        assertEquals(98, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(20, c2.getHeight());
        assertEquals(20, c3.getY());
        assertEquals(80, c3.getHeight());
    }
    
    public void testResizableBaseline9() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 100, 4, Baseline.BRB_CONSTANT_DESCENT);
        Component c2 = createComponent(10, 20, 1000, "c2");
        Component c3 = new BaselineComponent(10, 10, 100, 4, Baseline.BRB_OTHER);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(layout.createSequentialGroup().add(c2).
                add(true, c3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3));
        assertEquals(30, panel.getPreferredSize().height);

        layout();
        assertEquals(0, c1.getY());
        assertEquals(30, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(20, c2.getHeight());
        assertEquals(20, c3.getY());
        assertEquals(10, c3.getHeight());
        
        panel.setSize(1000, 200);
        panel.doLayout();
        assertEquals(100, c1.getY());
        assertEquals(100, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(190, c2.getHeight());
        assertEquals(190, c3.getY());
        assertEquals(10, c3.getHeight());
    }
    
    public void testResizableBaseline8() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 10, 12, 8, Baseline.BRB_CONSTANT_ASCENT);
        Component c3 = new BaselineComponent(10, 10, 100, 2, Baseline.BRB_CONSTANT_DESCENT);
        Component c4 = new BaselineComponent(10, 10, 10, 4, Baseline.BRB_CONSTANT_DESCENT);
        Component c5 = createComponent(10, 20, 30, "blah");
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2).add(c3).add(c4).add(c5));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2).add(c3).add(c4).add(c5));
        assertEquals(20, panel.getPreferredSize().height);
        assertEquals(104, panel.getMaximumSize().height);

        layout();
        assertEquals(4, c1.getY());
        assertEquals(16, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(12, c2.getHeight());
        assertEquals(0, c3.getY());
        assertEquals(16, c3.getHeight());
        assertEquals(4, c4.getY());
        assertEquals(10, c4.getHeight());
        assertEquals(0, c5.getY());
        assertEquals(20, c5.getHeight());
        
        panel.setSize(100, 60);
        panel.doLayout();
        assertEquals(4, c1.getY());
        assertEquals(56, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(12, c2.getHeight());
        assertEquals(0, c3.getY());
        assertEquals(16, c3.getHeight());
        assertEquals(4, c4.getY());
        assertEquals(10, c4.getHeight());
        assertEquals(0, c5.getY());
        assertEquals(30, c5.getHeight());
    }
    
    public void testResizableBaseline7() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 10, 100, 8, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(layout.createSequentialGroup().add(10, 10, 10).
                add(true, c2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE)));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(24, panel.getPreferredSize().height);
        assertEquals(114, panel.getMaximumSize().height);

        layout();
        assertEquals(14, c1.getY());
        assertEquals(10, c1.getHeight());
        assertEquals(10, c2.getY());
        assertEquals(14, c2.getHeight());

        panel.setSize(100, 114);
        panel.doLayout();
        assertEquals(14, c1.getY());
        assertEquals(100, c1.getHeight());
        assertEquals(10, c2.getY());
        assertEquals(100, c2.getHeight());
    }
    
    public void testResizableBaseline6() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 100, 4, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 10, 100, 8, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(layout.createSequentialGroup().
                  add(true, c2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE)));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(14, panel.getPreferredSize().height);
        assertEquals(104, panel.getMaximumSize().height);

        layout();
        assertEquals(4, c1.getY());
        assertEquals(10, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(14, c2.getHeight());

        panel.setSize(100, 104);
        panel.doLayout();
        assertEquals(4, c1.getY());
        assertEquals(100, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(100, c2.getHeight());
    }
    
    public void testResizableBaseline5() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 100, 9, Baseline.BRB_CONSTANT_DESCENT);
        Component c2 = new BaselineComponent(10, 10, 100, 8, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(11, panel.getPreferredSize().height);
        assertEquals(101, panel.getMaximumSize().height);

        layout();
        assertEquals(0, c2.getY());
        assertEquals(11, c2.getHeight());
        assertEquals(0, c1.getY());
        assertEquals(10, c1.getHeight());

        panel.setSize(100, 101);
        panel.doLayout();
        assertEquals(1, c2.getY());
        assertEquals(100, c2.getHeight());
        assertEquals(0, c1.getY());
        assertEquals(100, c1.getHeight());
    }
    
    public void testResizableBaseline4() {
        init(false, false);
        Component c1 = new BaselineCenteredComponent(6, 8, 20, 1);
        Component c2 = new BaselineComponent(10, 10, 100, 8, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(11, panel.getPreferredSize().height);
        assertEquals(11, panel.getMaximumSize().height);

        layout();
        assertEquals(0, c2.getY());
        assertEquals(10, c2.getHeight());
        assertEquals(3, c1.getY());
        assertEquals(8, c1.getHeight());
    }
    
    public void testResizableBaseline3() {
        // Tests two resizable components, one with constant_descent, the other
        // with constant_ascent
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 20, 8, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 10, 100, 6, Baseline.BRB_CONSTANT_DESCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        assertEquals(12, panel.getPreferredSize().height);
        assertEquals(20, panel.getMaximumSize().height);
        layout();
        assertEquals(0, c1.getY());
        assertEquals(12, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(12, c2.getHeight());
        
        panel.setSize(100, 20);
        panel.doLayout();
        assertEquals(0, c1.getY());
        assertEquals(20, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(12, c2.getHeight());
    }
    
    public void testResizableBaseline2() {
        // Tests two resizable components, both with a constant_ascent resize
        // behavior
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 20, 8, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 10, 22, 6, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        layout();
        assertEquals(12, panel.getPreferredSize().height);
        assertEquals(24, panel.getMaximumSize().height);
        assertEquals(0, c1.getY());
        assertEquals(12, c1.getHeight());
        assertEquals(2, c2.getY());
        assertEquals(10, c2.getHeight());
        
        panel.setSize(100, 24);
        panel.doLayout();
        assertEquals(0, c1.getY());
        assertEquals(20, c1.getHeight());
        assertEquals(2, c2.getY());
        assertEquals(22, c2.getHeight());

        panel.setSize(100, 20);
        panel.doLayout();
        assertEquals(0, c1.getY());
        assertEquals(20, c1.getHeight());
        assertEquals(2, c2.getY());
        assertEquals(18, c2.getHeight());
    }

    public void testResizableBaseline1() {
        init(false, false);
        Component c1 = new BaselineComponent(10, 10, 20, 8, Baseline.BRB_CONSTANT_ASCENT);
        Component c2 = new BaselineComponent(10, 10, 20, 8, Baseline.BRB_CONSTANT_ASCENT);
        layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.BASELINE).
                add(c1).add(c2));
        layout.setHorizontalGroup(layout.createSequentialGroup().
                add(c1).add(c2));
        layout();
        assertEquals(20, panel.getMaximumSize().height);
        panel.setSize(100, 20);
        panel.doLayout();
        assertEquals(0, c1.getY());
        assertEquals(20, c1.getHeight());
        assertEquals(0, c2.getY());
        assertEquals(20, c2.getHeight());
    }
    
    public void testRTL1() {
        init(false, false);
        panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        panel.setBorder(new EmptyBorder(0, 1, 0, 2));
        JComponent c1 = createComponent(10, 10, 10, "c1");
        JComponent c2 = createComponent(10, 10, 10, "c2");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1).add(c2));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1).add(c2));
        layout();
        assertEquals(1, c2.getX());
        assertEquals(11, c1.getX());
    }

    public void testIssue39() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        JComponent c2 = createComponent(20, 20, 20, "c2");
        JComponent c3 = createComponent(30, 30, 30, "c3");
        layout.setHorizontalGroup(
          layout.createParallelGroup(GroupLayout.CENTER).
            add(c1).add(c2).add(c3));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1).add(c2).add(c3));
        layout();
        assertEquals(10, c1.getX());
        assertEquals(5, c2.getX());
        assertEquals(0, c3.getX());
    }
    
    public void testRemove3() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1));
        panel.remove(c1);
        layout();
    }
    
    
    public void testMinBiggerThanPreferred() {
        init(false, false);
        JComponent c1 = createComponent(100, 100, 1000, "c1");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1, GroupLayout.DEFAULT_SIZE, 40, GroupLayout.DEFAULT_SIZE));
        layout.setVerticalGroup(
          layout.createParallelGroup().
            add(c1));
        Dimension min = panel.getMinimumSize();
        Dimension pref = panel.getPreferredSize();
    }
    
    public void testRemove2() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        JComponent c2 = createComponent(10, 20, 20, "c2");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1).
            add(c2));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1).
            add(c2));
        layout.linkSize(new Component[] { c1, c2} );
        assertEquals(40, panel.getPreferredSize().width);
        panel.remove(c2);
        layout.invalidateLayout(panel);
        assertEquals(10, panel.getPreferredSize().width);
    }
    
    public void testRemove() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1));
        assertEquals(10, panel.getPreferredSize().width);
        panel.remove(c1);
        layout.invalidateLayout(panel);
        assertEquals(0, panel.getPreferredSize().width);
    }
    
    public void testVisibility3() {
        init(false, false);
        JComponent c1 = createComponent(10, 20, 100, "c1");
        JComponent c2 = createComponent(10, 10, 100, "c2");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1).
            add(c2));
        layout.setVerticalGroup(
          layout.createParallelGroup().
            add(c1).
            add(c2));
        layout.linkSize(new Component[] { c1, c2 });
        layout();
        Dimension pref = panel.getPreferredSize();
        assertEquals(40, pref.width);
        c1.setVisible(false);
        layout();
        pref = panel.getPreferredSize();
        assertEquals(10, pref.width);
        assertEquals(10, c2.getWidth());
        
        c1.setVisible(true);
        layout();
        pref = panel.getPreferredSize();
        assertEquals(40, pref.width);
        assertEquals(20, c1.getWidth());
        assertEquals(20, c2.getWidth());
    }

    public void testVisibility2() {
        init(true, false);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        JComponent c2 = createComponent(10, 10, 10, "c2");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1).
            add(c2));
        layout.setVerticalGroup(
          layout.createParallelGroup().
            add(c1).
            add(c2));
        layout();
        Dimension pref = panel.getPreferredSize();
        assertTrue(pref.width > 20);

        c2.setVisible(false);
        layout();
        pref = panel.getPreferredSize();
        assertEquals(10, pref.width);
        
        c2.setVisible(true);
        pref = panel.getPreferredSize();
        assertTrue(pref.width > 20);
    }
    
    public void testVisibility1() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10);
        c1.setVisible(false);
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1));
        layout();
        Dimension pref = panel.getPreferredSize();
        assertEquals(0, pref.width);
        assertEquals(0, pref.height);

        c1.setVisible(true);
        layout();
        pref = panel.getPreferredSize();
        assertEquals(10, pref.width);
        assertEquals(10, pref.height);
    }
    
    // The following tests exercise different resize paths. They are currently
    // not used as we couldn't reach a good solution with resizing linked 
    // components and have turned it off.
    
//    public void testLinkSize3() {
//        init(false, false);
//        JLabel jLabel1 = new JLabel();
//        JTextField jTextField1 = new JTextField();
//        JTextField jTextField2 = new JTextField();
//        JTextField jTextField3 = new JTextField();
//
//        jLabel1.setText("label");
//        jTextField1.setText("short");
//        jTextField2.setText("very long");
//        jTextField3.setText("X");
//
//        layout.setHorizontalGroup(
//            layout.createParallelGroup(GroupLayout.LEADING)
//            .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                .addContainerGap()
//                .add(jTextField1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
//                .addPreferredGap(LayoutStyle.RELATED)
//                .add(layout.createParallelGroup(GroupLayout.LEADING)
//                    .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                        .add(10, 10, 10)
//                        .add(jTextField2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//                    .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                        //.add(10, 10, 10)
//                        .add(5, 5, 5)
//                        .add(5, 5, 5)
//                        .add(jTextField3, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//                    .add(jLabel1))
//                .addContainerGap())
//        );
//
//        layout.linkSize(new java.awt.Component[] {jTextField1, jTextField2, jTextField3}, GroupLayout.HORIZONTAL);
//
//        layout.setVerticalGroup(
//            layout.createParallelGroup(GroupLayout.LEADING)
//            .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                .addContainerGap()
//                .add(layout.createParallelGroup(GroupLayout.BASELINE)
//                    .add(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
//                    .add(jLabel1))
//                .addPreferredGap(LayoutStyle.RELATED)
//                .add(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
//                .addPreferredGap(LayoutStyle.RELATED)
//                .add(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
//                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//        );
//        layout();
//        int size = jTextField1.getWidth();
//        assertEquals(size, jTextField2.getWidth());
//        assertEquals(size, jTextField3.getWidth());
//        
//        Dimension pref = panel.getPreferredSize();
//        panel.setSize(pref.width + 20, pref.height);
//        panel.doLayout();
//        size += 10;
//        assertEquals(size, jTextField1.getWidth());
//        assertEquals(size, jTextField2.getWidth());
//        assertEquals(size, jTextField3.getWidth());
//    }
    
//    public void testLinkSize2() {
//        init(false, false);
//        JLabel jLabel1 = new JLabel();
//        JTextField jTextField1 = new JTextField();
//        JTextField jTextField2 = new JTextField();
//
//        jLabel1.setText("label");
//        jTextField1.setText("short");
//        jTextField2.setText("very long");
//
//        layout.setHorizontalGroup(
//            layout.createParallelGroup(GroupLayout.LEADING)
//            .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                .addContainerGap()
//                .add(jTextField1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
//                .addPreferredGap(LayoutStyle.RELATED)
//                .add(layout.createParallelGroup(GroupLayout.LEADING)
//                    .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                        .add(10, 10, 10)
//                        .add(jTextField2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//                    .add(jLabel1))
//                .addContainerGap())
//        );
//
//        layout.linkSize(new java.awt.Component[] {jTextField1, jTextField2}, GroupLayout.HORIZONTAL);
//
//        layout.setVerticalGroup(
//            layout.createParallelGroup(GroupLayout.LEADING)
//            .add(GroupLayout.LEADING, layout.createSequentialGroup()
//                .addContainerGap()
//                .add(layout.createParallelGroup(GroupLayout.BASELINE)
//                    .add(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
//                    .add(jLabel1))
//                .addPreferredGap(LayoutStyle.RELATED)
//                .add(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
//                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
//        );
//        panel.invalidate();
//        Dimension panelPref = panel.getPreferredSize();
//        panelPref.width += 20;
//        panel.setSize(panelPref);
//        panel.doLayout();
//        int pref = Math.max(jTextField1.getPreferredSize().width,
//                jTextField2.getPreferredSize().width);
//        assertEquals(pref + 10, jTextField1.getWidth());
//        assertEquals(pref + 10, jTextField2.getWidth());
//    }
    
//    public void testResize2() {
//        JComponent c1 = createComponent(10, 20, 30);
//        JComponent c2 = createComponent(5, 10, 50);
//        init(false, false);
//        layout.setHorizontalGroup(
//                layout.createSequentialGroup().
//                add(layout.createParallelGroup().add(c1)).add(c2));
//        layout.setVerticalGroup(
//                layout.createParallelGroup().add(c1).add(c2));
//        layout.linkSize(new Component[] { c1, c2 });
//        assertEquals(panel.getMinimumSize().width, 20);
//        assertEquals(panel.getPreferredSize().width, 40);
//        panel.setBounds(0, 0, 500, 500);
//        panel.doLayout();
//        assertEquals(50, c1.getWidth());
//        assertEquals(50, c2.getWidth());
//    }
    
//    public void testResize() {
//        JComponent c1 = createComponent(10, 20, 30);
//        JComponent c2 = createComponent(5, 10, 50);
//        init(false, false);
//        layout.setHorizontalGroup(
//                layout.createSequentialGroup().add(c1).add(c2));
//        layout.setVerticalGroup(
//                layout.createParallelGroup().add(c1).add(c2));
//        layout.linkSize(new Component[] { c1, c2 });
//        assertEquals(panel.getMinimumSize().width, 20);
//        assertEquals(panel.getPreferredSize().width, 40);
//        panel.setBounds(0, 0, 500, 500);
//        panel.doLayout();
//        assertEquals(50, c1.getWidth());
//        assertEquals(50, c2.getWidth());
//    }
    
    public void testLinkSize1() {
        // Test case from quick start guide
        init(false, false);
        
        JLabel jLabel1 = new JLabel();
        JLabel jLabel2 = new JLabel();
        JLabel jLabel3 = new JLabel();
        JLabel jLabel4 = new JLabel();
        JTextField jTextField1 = new JTextField();
        JTextField jTextField2 = new JTextField();
        JTextField jTextField3 = new JTextField();
        JTextField jTextField4 = new JTextField();
        
        jLabel1.setText("First label:");
        jLabel2.setText("Second label:");
        jLabel3.setText("Third label:");
        jLabel4.setText("Fourth label:");
        jTextField1.setText("one");
        jTextField2.setText("one plus one");
        jTextField3.setText("three");
        jTextField4.setText("four");

        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel3))
                .addPreferredGap(LayoutStyle.RELATED)
                .add(layout.createParallelGroup(GroupLayout.LEADING)
                    .add(jTextField1, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jTextField3, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(LayoutStyle.RELATED)
                .add(layout.createParallelGroup(GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(jLabel4))
                .addPreferredGap(LayoutStyle.RELATED)
                .add(layout.createParallelGroup(GroupLayout.LEADING)
                    .add(jTextField2, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jTextField4, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {jTextField1, jTextField2, jTextField3, jTextField4}, GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.RELATED)
                .add(layout.createParallelGroup(GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4)
                    .add(jTextField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout();
        int pref = Math.max(jTextField1.getPreferredSize().width,
                Math.max(jTextField2.getPreferredSize().width,
                Math.max(jTextField3.getPreferredSize().width,
                jTextField4.getPreferredSize().width)));
        assertEquals(pref, jTextField1.getWidth());
        assertEquals(pref, jTextField2.getWidth());
        assertEquals(pref, jTextField3.getWidth());
        assertEquals(pref, jTextField4.getWidth());
    }
    
    public void testResize3() {
        JComponent c1 = createComponent(10, 20, 30);
        JComponent c2 = createComponent(5, 10, 50);
        init(false, false);
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(layout.createParallelGroup(GroupLayout.LEADING, false).
              add(c1)).
            add(c2));
        layout.setVerticalGroup(
                layout.createParallelGroup().add(c1).add(c2));
        layout.linkSize(new Component[] { c1, c2 });
        assertEquals(40, panel.getMinimumSize().width);
        assertEquals(40, panel.getPreferredSize().width);
        panel.setBounds(0, 0, 500, 500);
        panel.doLayout();
        assertEquals(20, c1.getWidth());
        assertEquals(20, c2.getWidth());
    }
    
    public void testNegativeGap() {
        JButton b1 = new javax.swing.JButton();
        JButton b2 = new javax.swing.JButton();
        init(true, false);
        layout.setHorizontalGroup(
                layout.createSequentialGroup().add(b1).add(b2));
        layout.setVerticalGroup(
                layout.createParallelGroup().add(b1).add(b2));
        LayoutStyle.setSharedInstance(new LayoutStyle() {
            public int getPreferredGap(JComponent source, JComponent target,
                    int type, int position, Container parent) {
                return -10;
            }
        });
        layout();
        assertEquals(b1.getWidth() - 10, b2.getX());
        LayoutStyle.setSharedInstance(null);
    }
    
    public void testMultiLink() {
        JButton jButton1 = new javax.swing.JButton();
        JButton jButton2 = new javax.swing.JButton();
        JButton jButton3 = new javax.swing.JButton();

        jButton1.setName("b1");
        jButton2.setName("b2");
        jButton3.setName("b3");
        
        jButton1.setText("A");
        jButton2.setText("BB");
        jButton3.setText("CCC");

        init(false, false);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(GroupLayout.LEADING)
                    .add(GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jButton1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(LayoutStyle.RELATED)
                        .add(jButton2))
                    .add(jButton3))
                .addContainerGap())
        );
        
        layout.linkSize(new Component[] {jButton1, jButton3},
                GroupLayout.HORIZONTAL);
        
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.LEADING)
            .add(GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2))
                .addPreferredGap(LayoutStyle.RELATED)
                .add(jButton3)
                .addContainerGap())
        );
        layout();
        assertEquals(jButton1.getWidth(), jButton3.getWidth());
    }

    public void testGroupingBug() {
        init(false, false);
        JButton jButton1 = new javax.swing.JButton();
        JButton jButton2 = new javax.swing.JButton();
        JButton jButton3 = new javax.swing.JButton();

        jButton1.setName("button1");
        jButton2.setName("button2");
        jButton3.setName("button3");

        jButton1.setText("jButton1");
        jButton2.setText("jButton2");
        jButton3.setText("jButton3");

        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(LayoutStyle.RELATED))
                    .add(layout.createSequentialGroup()
                        .add(jButton2)
                        .addPreferredGap(LayoutStyle.RELATED)))
                .add(jButton3)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2)
                .addContainerGap())
        );
        int p1 = LayoutStyle.getSharedInstance().getPreferredGap(
                jButton1, jButton3, LayoutStyle.RELATED, SwingConstants.EAST, panel);
        int p2 = LayoutStyle.getSharedInstance().getPreferredGap(
                jButton1, jButton3, LayoutStyle.RELATED, SwingConstants.EAST, panel);
        int size = Math.max(p1 + jButton1.getPreferredSize().width,
                p2 + jButton2.getPreferredSize().width);
        size += jButton3.getPreferredSize().width;
        size += Math.max(LayoutStyle.getSharedInstance().getContainerGap(jButton1, SwingConstants.WEST, panel),
                LayoutStyle.getSharedInstance().getContainerGap(jButton2, SwingConstants.WEST, panel));
        size += LayoutStyle.getSharedInstance().getContainerGap(jButton3, SwingConstants.EAST, panel);
        panel.getPreferredSize();
        assertEquals(panel.getPreferredSize().width, size);
    }

    public void testAutoContainerBorder() {
        init(false, true);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        panel.setBorder(new EmptyBorder(1, 20, 3, 40));
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.add(c1);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1);
        LayoutStyle style = LayoutStyle.getSharedInstance();
        int leading = style.getContainerGap(c1, SwingConstants.WEST, panel);
        int trailing = style.getContainerGap(c1, SwingConstants.EAST, panel);
        layout();
        assertEquals(20 + leading + 10 + trailing + 40, panel.getPreferredSize().width);
        layout();
        assertEquals(20 + leading, c1.getX());
    }

    public void testComponent3() {
        init(false, true);
        Component c1 = createAWTComponent(10, 10, 10);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.add(c1);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1);
        assertEquals(30, panel.getPreferredSize().width);
        panel.doLayout();
    }
    
    public void testComponent2() {
        init(false);
        Container c1 = new Container();
        Container c2 = new Container();
        Container c3 = new Container();
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.add(c1)
              .addPreferredGap(LayoutStyle.RELATED, 50,  5000)
              .add(c2)
              .add(c3);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1).add(c2).add(c3);
        panel.setSize(100, 100);
        panel.doLayout();
    }
    
    public void testComponent() {
        Container panel = new Container();
        layout = new GroupLayout(panel);
        layout.setAutocreateContainerGaps(true);
        layout.setAutocreateGaps(true);
        panel.setLayout(layout);
        Container c1 = new Container();
        Container c2 = new Container();
        Container c3 = new Container();
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.add(c1)
              .addPreferredGap(LayoutStyle.RELATED, 50,  5000)
              .add(c2)
              .add(c3);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1).add(c2).add(c3);
        panel.setSize(100, 100);
        panel.doLayout();
    }
    
    public void testAutopaddingResize() {
        init(false);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        JComponent c2 = createComponent(10, 10, 10, "c2");
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.add(c1)
              .addPreferredGap(LayoutStyle.RELATED, 50,  5000)
              .add(c2);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        vGroup.add(c1).add(c2);
        layout.setHorizontalGroup(hGroup);
        layout.setVerticalGroup(vGroup);
        int gap = LayoutStyle.getSharedInstance().
                getPreferredGap(c1, c2, LayoutStyle.RELATED,
                SwingConstants.EAST, panel);
        panel.setSize(c1.getPreferredSize().width + gap + c2.getPreferredSize().width,
                10);
        panel.doLayout();
        assertTrue(c2.getX() >= c1.getX() + c1.getWidth() + gap);
        panel.setSize(c1.getPreferredSize().width + gap  - 1 + c2.getPreferredSize().width,
                10);
        panel.doLayout();
        assertTrue(c2.getX() >= c1.getX() + c1.getWidth() + gap);
    }

    public void testSingleAxis() {
        init(true, true);
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        JComponent c1 = new JLabel("blah");
        JComponent c2 = new JLabel("blah");
        group.add(c1).add(c2);
        layout.setHorizontalGroup(group);
        panel.invalidate();
        boolean gotException = false;
        try {
            panel.getPreferredSize();
        } catch (IllegalStateException ise) {
            gotException = true;
        }
        if (!gotException) {
            fail();
        }
    }
    
    public void testContainerPadding() {
        init(false);
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        JComponent c1 = new JLabel("blah");
        group.addContainerGap().add(c1).addContainerGap();
        layout.setHorizontalGroup(group);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.add(c1);
        layout.setVerticalGroup(vGroup);

        panel.invalidate();
        Dimension pref = panel.getPreferredSize();
        LayoutStyle p = LayoutStyle.getSharedInstance();
        panel.getPreferredSize();
        assertEquals(c1.getPreferredSize().width +
                p.getContainerGap(c1,
                    SwingConstants.EAST, panel) +
                p.getContainerGap(c1,
                    SwingConstants.WEST, panel),
                panel.getPreferredSize().width);
    }
    
    public void testReplace2() {
        init(false, false);
        JComponent c1 = createComponent(10, 10, 10);
        JComponent c2 = createComponent(20, 20, 20);

        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        group.add(c1);
        layout.setHorizontalGroup(group);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.add(c1);
        layout.setVerticalGroup(vGroup);
        layout.replace(c1, c2);
        panel.getPreferredSize();
    }

    public void testReplace() {
        init(false);
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        JComponent c1 = new JLabel();
        group.add(c1, 100, 200, 300);
        layout.setHorizontalGroup(group);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.add(c1);
        layout.setVerticalGroup(vGroup);

        panel.setBounds(0, 0, 500, 500);
        panel.doLayout();
        assertEquals(300, c1.getWidth());
        
        JComponent c2 = new JButton("blah");
        layout.replace(c1, c2);
        assertEquals(null, c1.getParent());
        assertEquals(panel, c2.getParent());
        panel.setBounds(0, 0, 500, 500);
        panel.doLayout();
        assertEquals(300, c2.getWidth());
    }
    
    public void testAutocreate2() {
        panel = new JPanel();
        layout = new GroupLayout(panel);
        layout.setAutocreateGaps(false);
        panel.setLayout(layout);
        JLabel label = new javax.swing.JLabel();
        JTextField textField = new javax.swing.JTextField();

        label.setText("Label");
        textField.setText("Some text");

        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .add(label)
                .addPreferredGap(LayoutStyle.RELATED)                    
                .add(textField)
        );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.BASELINE)
                .add(label)
                .add(textField)
        );
        layout.invalidateLayout(panel);
        Dimension pref = layout.preferredLayoutSize(panel);
        
        int padding = LayoutStyle.getSharedInstance().getPreferredGap(
                label, textField, LayoutStyle.RELATED, SwingConstants.EAST, panel);
        assertEquals(pref.width,
                label.getPreferredSize().width + padding +
                textField.getPreferredSize().width);
        layout.invalidateLayout(panel);
        panel.setSize(115, 20);
        layout.layoutContainer(panel);
        assertEquals(label.getPreferredSize().width, label.getWidth());
        assertEquals(label.getPreferredSize().width + padding,
                textField.getX());
    }

    // does nothing
    public void testUnused() {
        init(true);
        JComponent c1 = createComponent(10, 10, 10, "c1");
        JComponent c2 = createComponent(20, 20, 20, "c2");
        JComponent c3 = createComponent(30, 30, 30, "c3");
        JComponent c4 = createComponent(40, 40, 40, "c4");
        JComponent c5 = createComponent(50, 50, 50, "c5");
        JComponent c6 = createComponent(50, 50, 50, "c6");
        JComponent c7 = createComponent(50, 50, 50, "c7");
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        hGroup.add(
          layout.createParallelGroup().
            add(layout.createSequentialGroup().
              add(c1).
              add(c2)).
            add(layout.createSequentialGroup().
              add(layout.createSequentialGroup().
                add(c3).
                add(c4)).
              add(c5).
              add(c6))).
          add(c7);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1).add(c2).add(c3).add(c4).add(c5).add(c6).add(c7);
        layout.preferredLayoutSize(panel);
    }

    // does nothing 2
    public void testUnused2() {
        init(true);
        JComponent t1 = createComponent(10, 10, 10, "t1");
        JComponent l1 = createComponent(20, 20, 20, "l1");
        JComponent t2 = createComponent(30, 30, 30, "t2");
        JComponent s = createComponent(40, 40, 40, "s");
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        hGroup.add(
          layout.createParallelGroup().
            add(t1).
            add(layout.createSequentialGroup().
              add(l1).
              add(t2))).
          add(s);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(
          layout.createSequentialGroup().
            add(t1).
            add(layout.createParallelGroup().
              add(l1).
              add(t2))).
          add(s);
        layout.preferredLayoutSize(panel);
    }

    public void testParallelGroup() {
        init(false);
        JButton button1 = new JButton("Hi");
        JButton button2 = new JButton("YOW!");
        GroupLayout.ParallelGroup group = layout.createParallelGroup(GroupLayout.LEADING);
        layout.setHorizontalGroup(group);
        group.add(button1).add(button2);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(button1).add(button2);
        layout();
        
        assertEquals(Math.max(
                button1.getPreferredSize().width,
                button2.getPreferredSize().width),
                panel.getPreferredSize().width);
        assertEquals(Math.max(
                button1.getMinimumSize().width,
                button2.getMinimumSize().width),
                panel.getMinimumSize().width);
        assertEquals(Math.max(
                button1.getMaximumSize().width,
                button2.getMaximumSize().width),
                panel.getMaximumSize().width);
        assertEquals(0, button1.getX());
        assertEquals(button1.getPreferredSize().width, button1.getWidth());
        assertEquals(0, button2.getX());
        assertEquals(button2.getPreferredSize().width, button2.getWidth());
        
        JLabel l1 = new JLabel("a long label");
        JLabel l2 = new JLabel("X");
        group.add(l1).add(l2);
        vGroup.add(l1).add(l2);
        layout();
        assertEquals(Math.max(l1.getPreferredSize().width,
                Math.max(l2.getPreferredSize().width,
                Math.max(button1.getPreferredSize().width,
                button2.getPreferredSize().width))),
                panel.getPreferredSize().width);
        assertEquals(0, l1.getX());
        assertEquals(l1.getPreferredSize().width, l1.getWidth());
        assertEquals(0, l2.getX());
        assertEquals(l2.getPreferredSize().width, l2.getWidth());
        
        assertEquals(0, button1.getX());
        assertEquals(button1.getPreferredSize().width, button1.getWidth());
        assertEquals(0, button2.getX());
        assertEquals(button2.getPreferredSize().width, button2.getWidth());
    }
    
    public void testParallelGroupTrailing() {
        init(false);
        JButton button1 = new JButton("Hi");
        JButton button2 = new JButton("YOW asdf!");
        GroupLayout.ParallelGroup group = layout.createParallelGroup(GroupLayout.LEADING);
        layout.setHorizontalGroup(group);
        group.add(GroupLayout.TRAILING, button1).add(button2);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(button1).add(button2);
        layout();
        
        assertEquals(Math.max(
                button1.getPreferredSize().width,
                button2.getPreferredSize().width),
                panel.getPreferredSize().width);
        assertEquals(panel.getPreferredSize().width - button1.getWidth(), button1.getX());
        assertEquals(0, button2.getX());
    }
    
    public void testParallelGroupCenter() {
        init(false);
        JButton button1 = new JButton("Hi");
        JButton button2 = new JButton("YOW asdf!");
        GroupLayout.ParallelGroup group = layout.createParallelGroup(GroupLayout.LEADING);
        layout.setHorizontalGroup(group);
        group.add(GroupLayout.CENTER, button1).add(button2);
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(button1).add(button2);
        layout();
        
        assertEquals(Math.max(
                button1.getPreferredSize().width,
                button2.getPreferredSize().width),
                panel.getPreferredSize().width);
        assertEquals((panel.getPreferredSize().width - button1.getWidth()) / 2, button1.getX());
        assertEquals(0, button2.getX());
    }
    
    public void testSerialGroupBasic() {
        init(false);
        JButton button1 = new JButton("Hi");
        JButton button2 = new JButton("YOW asdf!");
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        layout.setHorizontalGroup(group);
        group.add(button1).add(button2);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(button1).add(button2);
        layout();
        
        assertEquals(button1.getPreferredSize().width +
                button2.getPreferredSize().width,
                panel.getPreferredSize().width);
        assertEquals(button1.getMinimumSize().width +
                button2.getMinimumSize().width,
                panel.getMinimumSize().width);
        assertEquals(button1.getMaximumSize().width +
                button2.getMaximumSize().width,
                panel.getMinimumSize().width);
        assertEquals(0, button1.getX());
        assertEquals(button1.getPreferredSize().width, button2.getX());
    }
    
    
    public void testSerialGroupResize2() {
        init(false);
        JButton button1 = new JButton("Hi");
        JButton button2 = new JButton("YOW asdf!");
        JButton button3 = new JButton("aaaaa");
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        layout.setHorizontalGroup(group);
        group.add(button1, 3, 5, 10).add(button2, 3, 4, 5).add(button3, 3, 3, 3);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(button1).add(button2).add(button3);
        layout();
        assertEquals(12, panel.getPreferredSize().width);
        assertEquals(0, button1.getX());
        assertEquals(5, button1.getWidth());
        assertEquals(5, button2.getX());
        assertEquals(4, button2.getWidth());
        assertEquals(9, button3.getX());
        assertEquals(3, button3.getWidth());
        assertEquals(9, panel.getMinimumSize().width);
        assertEquals(18, panel.getMaximumSize().width);
    }
    
    public void testLink() {
        init(false);
        JButton b1 = new JButton("Hi");
        JButton b2 = new JButton("YOW asdf!");
        JButton b3 = new JButton("aaaaa");
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        layout.setHorizontalGroup(group);
        group.add(b1).add(b2).add(b3);
        GroupLayout.ParallelGroup pg = layout.createParallelGroup();
        pg.add(b1).add(b2).add(b3);
        layout.setVerticalGroup(pg);
        layout();
        assertEquals(panel.getPreferredSize(),
                new Dimension(b1.getPreferredSize().width +
                b2.getPreferredSize().width +
                b3.getPreferredSize().width,
                Math.max(b1.getPreferredSize().height,
                Math.max(b2.getPreferredSize().height,
                b3.getPreferredSize().height))));
        layout.linkSize(new Component[] { b1, b3 });
        layout();
        assertEquals(panel.getPreferredSize(),
                new Dimension(2 * Math.max(b1.getPreferredSize().width,
                b3.getPreferredSize().width) +
                b2.getPreferredSize().width,
                Math.max(b1.getPreferredSize().height,
                b2.getPreferredSize().height)));
    }
    
    public void testHorizontalLink() {
        init(false);
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        p1.setPreferredSize(new Dimension(10, 20));
        p2.setPreferredSize(new Dimension(30, 40));
        layout.setHorizontalGroup(group);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(p1).add(p2);
        layout.linkSize(new Component[] { p1, p2}, GroupLayout.HORIZONTAL);
        group.add(p1).add(p2);
        layout();
        assertEquals(panel.getPreferredSize().width, 60);
        assertEquals(p1.getWidth(), 30);
        assertEquals(p2.getWidth(), 30);
    }
    
    public void testVerticalLink() {
        init(false);
        GroupLayout.ParallelGroup group = layout.createParallelGroup();
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        p1.setPreferredSize(new Dimension(10, 20));
        p2.setPreferredSize(new Dimension(30, 40));
        layout.setVerticalGroup(group);
        layout.linkSize(new Component[] { p1, p2}, GroupLayout.VERTICAL);
        group.add(p1).add(p2);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        hGroup.add(p1).add(p2);
        layout();
        assertEquals(p1.getHeight(), 40);
        assertEquals(p2.getHeight(), 40);
        assertEquals(panel.getPreferredSize().height, 40);
    }
    
    public void testMultiAdd() {
        init(false);
        layout.createParallelGroup(GroupLayout.LEADING)
        .add(new JLabel("one"))
        .add(GroupLayout.TRAILING, new JLabel("two"));
    }
    
    public void testLinkedBaseline() {
        init(false);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup(
                GroupLayout.BASELINE);
        JLabel l1 = new JLabel("label");
        JButton b1 = new JButton("button");
        layout.setHorizontalGroup(hGroup);
        layout.setVerticalGroup(vGroup);
        layout.linkSize(new Component[] { l1, b1}, GroupLayout.VERTICAL);
        hGroup.add(l1).add(b1);
        vGroup.add(l1).add(b1);
        layout();
        int height = Math.max(l1.getPreferredSize().height,
                b1.getPreferredSize().height);
        assertEquals(l1.getHeight(), height);
        assertEquals(b1.getHeight(), height);
        
        int l1Baseline = Baseline.getBaseline(l1, l1.getPreferredSize().width,
                height);
        int b1Baseline = Baseline.getBaseline(b1, b1.getPreferredSize().width,
                height);
        
        assertEquals(panel.getPreferredSize().height,
                Math.max(height - l1Baseline,
                height - b1Baseline) +
                Math.max(l1Baseline, b1Baseline));
    }
    
    public void testSizes() {
        init(false);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        Component c1 = createComponent(10, 20, 30);
        hGroup.add(c1, GroupLayout.PREFERRED_SIZE,
                GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1);
        assertEquals(panel.getMinimumSize().width, 20);
        assertEquals(panel.getPreferredSize().width, 20);
        assertEquals(panel.getMaximumSize().width, 30);
        
        init(false);
        hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        c1 = createComponent(10, 20, 30);
        hGroup.add(c1, 6,
                GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
        vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1);
        assertEquals(panel.getMinimumSize().width, 6);
        assertEquals(panel.getPreferredSize().width, 20);
        assertEquals(panel.getMaximumSize().width, 20);
        
        init(false);
        hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        c1 = createComponent(10, 20, 30);
        hGroup.add(c1, GroupLayout.DEFAULT_SIZE,
                15, GroupLayout.PREFERRED_SIZE);
        vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(c1);
        assertEquals(panel.getMinimumSize().width, 10);
        assertEquals(panel.getPreferredSize().width, 15);
        assertEquals(panel.getMaximumSize().width, 15);
    }
    
    public void testHorizontalAutocreate() {
        init(true);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        
        JTextField tf = new JTextField(5);
        JButton button = new JButton("hi");
        hGroup.add(tf).add(button);
        vGroup.add(tf).add(button);
        
        int padding = LayoutStyle.getSharedInstance().getPreferredGap(
                tf, button, LayoutStyle.RELATED, SwingConstants.EAST, panel);

        assertEquals(panel.getPreferredSize().width,
                tf.getPreferredSize().width +
                button.getPreferredSize().width +
                padding);
        
        layout();
        
        assertEquals(tf.getWidth(), tf.getPreferredSize().width);
        assertEquals(button.getX(), tf.getWidth() + padding);
        assertEquals(button.getWidth(), button.getPreferredSize().width);
    }
    
    public void testHorizontalAutocreate2() {
        init(true);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        
        JTextField tf = new JTextField(5);
        JButton button = new JButton("hi");
        hGroup.add(tf).addPreferredGap(LayoutStyle.UNRELATED).add(button);
        vGroup.add(tf).add(button);
        
        layout();
        
        int padding = LayoutStyle.getSharedInstance().getPreferredGap(
                tf, button, LayoutStyle.UNRELATED, SwingConstants.EAST, panel);
        assertEquals(tf.getWidth(), tf.getPreferredSize().width);
        assertEquals(button.getX(), tf.getWidth() + padding);
        assertEquals(button.getWidth(), button.getPreferredSize().width);
    }
    
    public void testSetLayoutStyle() {
        init(true, true);
        layout.setLayoutStyle(new LayoutStyle() {
            public int getContainerGap(JComponent component, int position,
                    Container parent) {
                return 100;
            }
        });
        JComponent c1 = createComponent(10, 10, 10, "c1");
        layout.setHorizontalGroup(
          layout.createSequentialGroup().
            add(c1));
        layout.setVerticalGroup(
          layout.createSequentialGroup().
            add(c1));
        panel.remove(c1);
        assertEquals(210, panel.getPreferredSize().width);
        layout.setLayoutStyle(null);
        LayoutStyle style = LayoutStyle.getSharedInstance();
        assertTrue(style.getContainerGap(c1, SwingConstants.EAST, panel) != 100);
        assertEquals(10 + style.getContainerGap(c1, SwingConstants.EAST, panel) +
                style.getContainerGap(c1, SwingConstants.WEST, panel),
                panel.getPreferredSize().width);
    }
    
    public void testHorizontalAutocreate3() {
        init(true);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        layout.setVerticalGroup(vGroup);
        
        JTextField tf = new JTextField(5);
        JButton button = new JButton("hi");
        JButton b2 = new JButton("hixxx");
        hGroup.add(tf).addPreferredGap(LayoutStyle.UNRELATED).add(button).add(b2);
        vGroup.add(tf).add(button).add(b2);
        
        layout();
        
        int padding = LayoutStyle.getSharedInstance().getPreferredGap(
                tf, button, LayoutStyle.UNRELATED, SwingConstants.EAST, panel);
        int p2 = LayoutStyle.getSharedInstance().getPreferredGap(
                button, b2, LayoutStyle.RELATED, SwingConstants.EAST, panel);
        assertEquals(tf.getWidth(), tf.getPreferredSize().width);
        assertEquals(button.getX(), tf.getWidth() + padding);
        assertEquals(button.getWidth(), button.getPreferredSize().width);
        assertEquals(b2.getX(), button.getX() + button.getWidth() + p2);
    }
        
    public void testVerticalAutocreate() {
        init(true);
        GroupLayout.ParallelGroup hGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        
        JTextField tf = new JTextField(5);
        JButton button = new JButton("hi");
        hGroup.add(tf).add(button);
        vGroup.add(tf).add(button);
        
        layout();
        
        int padding = LayoutStyle.getSharedInstance().getPreferredGap(
                tf, button, LayoutStyle.RELATED, SwingConstants.SOUTH, panel);
        assertEquals(tf.getHeight(), tf.getPreferredSize().height);
        assertEquals(button.getY(), tf.getHeight() + padding);
        assertEquals(button.getHeight(), button.getPreferredSize().height);
    }
    
    public void testVerticalAutocreate2() {
        init(true);
        GroupLayout.ParallelGroup hGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        
        JTextField tf = new JTextField(5);
        JButton button = new JButton("hi");
        hGroup.add(tf).add(button);
        vGroup.add(tf).addPreferredGap(LayoutStyle.UNRELATED).add(button);
        
        layout();
        
        int padding = LayoutStyle.getSharedInstance().getPreferredGap(
                tf, button, LayoutStyle.UNRELATED, SwingConstants.SOUTH,
                panel);
        assertEquals(tf.getHeight(), tf.getPreferredSize().height);
        assertEquals(button.getY(), tf.getHeight() + padding);
        assertEquals(button.getHeight(), button.getPreferredSize().height);
    }
    
    public void testBaselineAutocreate() {
        init(true);
        LayoutStyle.setSharedInstance(new LayoutStyle() {
            public int getPreferredGap(JComponent source, JComponent target,
                    int type, int position, Container parent) {
                if (source instanceof JLabel && target instanceof JLabel) {
                    return 10;
                }
                return 2;
            }
            
        });
        GroupLayout.ParallelGroup hGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        
        JLabel l1 = new JLabel("l1");
        JLabel l2 = new JLabel("l2");
        JTextField t1 = new JTextField(5);
        hGroup.add(layout.createSequentialGroup()
        .add(l1).add(t1))
        .add(l2);
        
        vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE)
        .add(l1).add(t1))
        .add(l2);
        
        layout();
        
        assertEquals(t1.getX(), l1.getWidth() + 2);
        int l1Baseline = Baseline.getBaseline(l1);
        int t1Baseline = Baseline.getBaseline(t1);
        assertEquals(l1.getY(), Math.max(l1Baseline, t1Baseline) - l1Baseline);
        assertEquals(l2.getY(), Math.max(l1.getY() + l1.getHeight() + 10,
                t1.getHeight() + 2));
        LayoutStyle.setSharedInstance(null);
    }
    
    public void testBaselineAutocreate2() {
        init(false);
        LayoutStyle.setSharedInstance(new LayoutStyle() {
            public int getPreferredGap(JComponent source, JComponent target,
                    int type, int position, Container parent) {
                if (source instanceof JLabel && target instanceof JLabel) {
                    return 10;
                }
                return 2;
            }
            
        });
        GroupLayout.ParallelGroup hGroup = layout.createParallelGroup();
        layout.setHorizontalGroup(hGroup);
        
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        
        JLabel l1 = new JLabel("l1");
        JLabel l2 = new JLabel("l2");
        JTextField t1 = new JTextField(5);
        hGroup.add(layout.createSequentialGroup()
        .add(l1).addPreferredGap(LayoutStyle.RELATED).add(t1))
        .add(l2);
        
        vGroup.add(layout.createParallelGroup(GroupLayout.BASELINE)
        .add(l1).add(t1))
        .addPreferredGap(LayoutStyle.RELATED).add(l2);
        
        layout();
        
        assertEquals(t1.getX(), l1.getWidth() + 2);
        int l1Baseline = Baseline.getBaseline(l1);
        int t1Baseline = Baseline.getBaseline(t1);
        assertEquals(l1.getY(), Math.max(l1Baseline, t1Baseline) - l1Baseline);
        assertEquals(l2.getY(), Math.max(l1.getY() + l1.getHeight() + 10,
                t1.getHeight() + 2));
        LayoutStyle.setSharedInstance(null);
    }
    
    public void testMinConstraints() {
        init(false);
        JLabel l = new JLabel("blah");
        l.setPreferredSize(new Dimension(10, 10));
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        layout.setHorizontalGroup(group);
        group.add(l, 100, GroupLayout.PREFERRED_SIZE,
                GroupLayout.PREFERRED_SIZE);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        layout.setVerticalGroup(vGroup);
        vGroup.add(l);
        layout();
        assertEquals(panel.getPreferredSize().width, 100);
        assertEquals(panel.getMinimumSize().width, 100);
    }
    
    public void testContainerAutoPadding() {
        init(false, true);
        final JComponent c1 = createComponent(100, 100, 100);
        final JComponent c2 = createComponent(200, 200, 200);
        LayoutStyle.setSharedInstance(new LayoutStyle() {
            public int getContainerGap(JComponent source,
                    int position, Container parent) {
                super.getContainerGap(source, position, parent);
                if (source == c1) {
                    return 10;
                }
                return 5;
            }
            
        });
        GroupLayout.ParallelGroup group = layout.createParallelGroup();
        group.add(GroupLayout.TRAILING, c1);
        group.add(c2);
        layout.setHorizontalGroup(group);

        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        vGroup.add(c1);
        vGroup.add(c2);
        layout.setVerticalGroup(vGroup);
        
        layout();
        assertEquals(215, panel.getPreferredSize().width);
        assertEquals(5, c2.getX());
        assertEquals(105, c1.getX());
        LayoutStyle.setSharedInstance(null);
    }
    
    public void testMax() {
        init(false);
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        JComponent c1 = new JLabel();
        group.add(c1, 100, 200, 300);
        layout.setHorizontalGroup(group);
        panel.setBounds(0, 0, 500, 500);
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.add(c1);
        layout.setVerticalGroup(vGroup);
        panel.doLayout();
        assertEquals(300, c1.getWidth());
    }
    
    public void testNestedAutopadding() {
        init(false);
        JComponent c1 = createComponent("c1");
        JComponent c2 = createComponent("c2");
        JComponent c3 = createComponent("c3");
        JComponent c4 = createComponent("c4");
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        layout.setHorizontalGroup(hGroup);
        hGroup.add(
          layout.createParallelGroup().
            add(c1).
            add(c2)).
          add(layout.createSequentialGroup().
            addPreferredGap(LayoutStyle.RELATED).
            add(c3)).
          add(layout.createSequentialGroup().
            addPreferredGap(LayoutStyle.RELATED).
            add(c4));
        GroupLayout.ParallelGroup vGroup = layout.createParallelGroup();
        vGroup.add(c1).add(c2).add(c3).add(c4);
        layout.setVerticalGroup(vGroup);
        panel.setBounds(0, 0, 500, 500);
        panel.doLayout();
        LayoutStyle layoutStyle = LayoutStyle.getSharedInstance();
        int gap = layoutStyle.getPreferredGap(c1, c3, LayoutStyle.RELATED,
                SwingConstants.EAST, panel);
        assertEquals(gap, c3.getX() - c1.getX() - c1.getWidth());

        gap = layoutStyle.getPreferredGap(c3, c4, LayoutStyle.RELATED,
                SwingConstants.EAST, panel);
        assertEquals(gap, c4.getX() - c3.getX() - c3.getWidth());
    }

    private int getBaseline(Component c) {
        if (c instanceof JComponent) {
            return Baseline.getBaseline((JComponent)c);
        }
        return -1;
    }


    private static class TestComponent extends JComponent {
        final int min, pref, max;
        
        public TestComponent(int min, int pref, int max) {
            this.min = min;
            this.pref = pref;
            this.max = max;
        }
        
        public Dimension getMinimumSize() {
            return new Dimension(10, min);
        }

        public Dimension getPreferredSize() {
            return new Dimension(10, pref);
        }

        public Dimension getMaximumSize() {
            return new Dimension(10, max);
        }
    }

    
    private static class BaselineComponent extends TestComponent {
        private final int baseline;
        private final int brb;
        
        public BaselineComponent(int min, int pref, int max, int baseline,
                int brb) {
            super(min, pref, max);
            this.baseline = baseline;
            this.brb = brb;
        }
        
        public int getBaselineResizeBehaviorInt() {
            return brb;
        }

        public int getBaseline(int width, int height) {
            if (height == pref) {
                return baseline;
            }
            switch(brb) {
                case Baseline.BRB_CONSTANT_ASCENT:
                    return baseline;
                case Baseline.BRB_CONSTANT_DESCENT:
                    return height - (pref - baseline);
                case Baseline.BRB_OTHER:
                    return baseline;
                default:
                    throw new RuntimeException("not implemented!");
            }
        }
    }


    private static class BaselineCenteredComponent extends TestComponent {
        private int offset;
        
        public BaselineCenteredComponent(int min, int pref, int max, int offset) {
            super(min, pref, max);
            this.offset = offset;
        }
        
        public int getBaselineResizeBehavior() {
            return Baseline.BRB_CENTER_OFFSET;
        }

        public int getBaseline(int width, int height) {
            return height / 2 + offset;
        }
    }

    // UNUSED TESTS!
    /*
 public void testAutocreate() {
        init(true);
        GroupLayout.SequentialGroup group = layout.createSequentialGroup();
        group.
          add(layout.createSequentialGroup().
            add(layout.createParallelGroup().
              add(createComponent("a")).
              add(createComponent("b"))).
            add(createComponent("c")).
            add(layout.createParallelGroup().
              add(createComponent("d")).
              add(layout.createSequentialGroup().
                add(createComponent("e")).
                add(createComponent("f"))).
              add(createComponent("g"))).
            add(createComponent("h"))).
          add(layout.createSequentialGroup().
            add(createComponent("i")).
            add(createComponent("j"))).
          add(layout.createParallelGroup().
            add(layout.createParallelGroup().
              add(createComponent("k")).
              add(createComponent("l"))).
            add(createComponent("m")));
        layout.setHorizontalGroup(group);
        layout.dump(group, "", GroupLayout.HORIZONTAL);
        panel.getMinimumSize();
        layout.insertAutopadding();
    }
     */

/*
     public void testInteractive() {
        JFrame frame = new JFrame();
        Container container = frame.getContentPane();
        GroupLayout layout = new GroupLayout(container);
        layout.setAutocreateGaps(false);
        container.setLayout(layout);

        JLabel label = new javax.swing.JLabel();
        JTextField textField = new javax.swing.JTextField();

        label.setText("Label");
        textField.setText("Some text");

        frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .add(label)
                .addPreferredGap(Padding.RELATED)                    
                .add(textField)
        );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.BASELINE)
                .add(label)
                .add(textField)
        );

        frame.pack();
        frame.show();
        synchronized(this) {
            while (frame.isShowing()) {
                try {
                    wait(1000);
                } catch (InterruptedException ie) {
                }
            }
        }
    }
*/
}
