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

package org.jdesktop.layout;

import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.lang.reflect.Field;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;
import junit.framework.*;

/**
 *
 * @author sky
 */
public class BaselineTest extends TestCase {
    public BaselineTest(String testName) {
        super(testName);
    }
    
    public static void main(String[] args) {
        new BaselineTest("foo").testBaselineResizeBehavior();
    }
    
    public void testBRBOverride() {
        BaselineComponent c = new BaselineComponent();
        assertEquals(c.brb, Baseline.getBaselineResizeBehavior(c));
        c.brb = Baseline.BRB_CONSTANT_ASCENT;
        assertEquals(c.brb, Baseline.getBaselineResizeBehavior(c));
        c.brb = Baseline.BRB_CONSTANT_DESCENT;
        assertEquals(c.brb, Baseline.getBaselineResizeBehavior(c));
        c.brb = Baseline.BRB_CENTER_OFFSET;
        assertEquals(c.brb, Baseline.getBaselineResizeBehavior(c));
    }
    
    public void testBaselineResizeBehavior() {
        Baseline.getBaselineResizeBehavior(new JButton("blah"));
        Baseline.getBaselineResizeBehavior(new JCheckBox("blah"));
        Baseline.getBaselineResizeBehavior(new JRadioButton("blah"));
        Baseline.getBaselineResizeBehavior(new JToggleButton("blah"));
        Baseline.getBaselineResizeBehavior(new JComboBox(new String[] { "blah" }));
        Baseline.getBaselineResizeBehavior(new JTextArea());
        Baseline.getBaselineResizeBehavior(new JTextField("blah"));
        Baseline.getBaselineResizeBehavior(new JFormattedTextField());
        Baseline.getBaselineResizeBehavior(new JPasswordField());
        Baseline.getBaselineResizeBehavior(new JList());
        Baseline.getBaselineResizeBehavior(new JPanel());
        Baseline.getBaselineResizeBehavior(new JProgressBar());
        Baseline.getBaselineResizeBehavior(new JSlider());
        Baseline.getBaselineResizeBehavior(new JSpinner());
        Baseline.getBaselineResizeBehavior(new JScrollPane());
        Baseline.getBaselineResizeBehavior(new JTabbedPane());
        Baseline.getBaselineResizeBehavior(new JTable());
        Baseline.getBaselineResizeBehavior(new JTree());
    }
    
    public void testCustomComponent() {
        BaselineComponent c = new BaselineComponent();
        assertEquals(-1, Baseline.getBaseline(c, 100, 100));
        c.baseline = 10;
        assertEquals(10, Baseline.getBaseline(c, 100, 100));
    }
    
    public void testMetalSubclass() {
        try {
            UIManager.setLookAndFeel(new MetalLookAndFeel() {
                public String getID() {
                    return "foo";
                }});
            int baseline = Baseline.getBaseline(new JLabel("xxx"));
            assertTrue(baseline > 0);
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException ulfe) {
        }
    }
    
    public void testWindowsSubclass() {
        try {
            UIManager.setLookAndFeel(new WindowsLookAndFeel() {
                public String getID() {
                    return "foo";
                }});
            int baseline = Baseline.getBaseline(new JLabel("xxx"));
            assertTrue(baseline > 0);
            UIManager.setLookAndFeel(new MetalLookAndFeel());
        } catch (UnsupportedLookAndFeelException ulfe) {
        }
    }
    
    
    private static class BaselineComponent extends JComponent {
        int baseline;
        int brb;
        
        BaselineComponent() {
            this(-1, Baseline.BRB_OTHER);
        }
        
        BaselineComponent(int baseline, int brb) {
            this.baseline = baseline;
            this.brb = brb;
        }
        
        public int getBaselineResizeBehaviorInt() {
            return brb;
        }
        
        public int getBaseline(int w, int h) {
            return baseline;
        }
    }
    
    public void testCustomBaseline() {
        Baseline myBaseline = new Baseline() {
            public int getComponentBaseline(JComponent component, int width, int height) {
                return 100;
            }
        };
        UIManager.put("Baseline.instance", myBaseline);
        int baseline = Baseline.getBaseline(new JPanel());
        assertEquals(100, baseline);
        UIManager.put("Baseline.instance", null);
        assertTrue(Baseline.getBaseline(new JPanel()) != 100);
    }

    public void testSecurity() {
        System.setSecurityManager(new TestSecurityManager());
        Baseline.getBaseline(new JComboBox(new String[] { "one", "two" }));
        System.setSecurityManager(null);
    }
    
    
    private static final class TestSecurityManager extends SecurityManager {
        public boolean checkTopLevelWindow(Object window) {
            return true;
        }

        public void checkPermission(java.security.Permission perm, Object context) {
        }

        public void checkPermission(java.security.Permission perm) {
        }

        public void checkMulticast(java.net.InetAddress maddr) {
        }

        public void checkAccess(ThreadGroup g) {
        }

        public void checkWrite(String file) {
        }

        public void checkSecurityAccess(String target) {
        }

        public void checkLink(String lib) {
        }

        public void checkExec(String cmd) {
        }

        public void checkDelete(String file) {
        }

        public void checkPackageAccess(String pkg) {
        }

        public void checkPackageDefinition(String pkg) {
        }

        public void checkPropertyAccess(String key) {
        }

        public void checkRead(String file) {
        }

        public void checkWrite(java.io.FileDescriptor fd) {
        }

        public void checkListen(int port) {
        }

        public void checkExit(int status) {
        }

        public void checkRead(java.io.FileDescriptor fd) {
        }

        public void checkMulticast(java.net.InetAddress maddr, byte ttl) {
        }

        public void checkAccess(Thread t) {
        }

        public void checkConnect(String host, int port, Object context) {
        }

        public void checkRead(String file, Object context) {
        }

        public void checkConnect(String host, int port) {
        }

        public void checkAccept(String host, int port) {
        }

        public void checkMemberAccess(Class clazz, int which) {
            if (which == Field.DECLARED) {
                throw new SecurityException();
            }
        }

        public void checkSystemClipboardAccess() {
        }

        public void checkSetFactory() {
        }

        public void checkCreateClassLoader() {
        }

        public void checkAwtEventQueueAccess() {
        }

        public void checkPrintJobAccess() {
        }

        public void checkPropertiesAccess() {
        }
    }
}
