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

package org.jdesktop.layout;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import junit.framework.*;

/**
 *
 * @author sky
 */
public class LayoutStyleTest extends TestCase {
    public LayoutStyleTest(String testName) {
        super(testName);
    }

    public void testSetSharedInstance() {
        LayoutStyle style = new LayoutStyle() {
            public int getPreferredGap(JComponent component1,
                    JComponent component2, int type, int position,
                    java.awt.Container parent) {
                return 100;
            }
        };
        JLabel l1 = new JLabel();
        JLabel l2 = new JLabel();
        JPanel parent = new JPanel();
        UIManager.getDefaults().put("LayoutStyle.instance", style);
        assertEquals(100, LayoutStyle.getSharedInstance().getPreferredGap(
                l1, l2, LayoutStyle.RELATED, SwingConstants.EAST, parent));
        UIManager.getDefaults().put("LayoutStyle.instance", null);
        assertTrue(LayoutStyle.getSharedInstance().getPreferredGap(
                l1, l2, LayoutStyle.RELATED, SwingConstants.EAST, parent) != 100);
    }
    
    public void testAssertions() {
        doTestAssertions("javax.swing.plaf.metal");
        doTestAssertions("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
        doTestAssertions("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException ulfe) {
        }
    }
    
    private void doTestAssertions(String laf) {
        try {
            UIManager.setLookAndFeel(laf);
        } catch (Exception e) {
            // Not a supported look and feel, bail.
            return;
        }
        JPanel parent = new JPanel();
        JComponent c1 = new JPanel();
        JComponent c2 = new JPanel();
        LayoutStyle style = LayoutStyle.getSharedInstance();
        int[] types = new int[] { LayoutStyle.RELATED,
                LayoutStyle.UNRELATED, LayoutStyle.INDENT };
        int[] axis = new int[] { SwingConstants.NORTH, SwingConstants.SOUTH,
                SwingConstants.EAST, SwingConstants.WEST };
        for (int type = 0; type < types.length; type++) {
            for (int x = 0; x < axis.length; x++) {
                assert(style.getPreferredGap(c1, c2, types[type], axis[x],
                        parent) != 0);
            }
        }
    }
}
