<commit_before>/*
 * $Id$
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 */
package org.jdesktop.swingx;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.jdesktop.test.PropertyChangeReport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JXTitledPanelTest extends InteractiveTestCase {
    private static final Logger LOG = Logger.getLogger(JXTitledPanelTest.class
            .getName());
    
    // flag used in setup to explicitly choose LF
    private boolean defaultToSystemLF;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // make sure we have the same default for each test
        defaultToSystemLF = false;
        setSystemLF(defaultToSystemLF);
    }

    public JXTitledPanelTest() {
        super("JXTitledPane interactive test");
    }

    /**
     * Sanity: changed ui-delegate to install the opaqueness via LookAndFeel. Doing so
     * is the equivalent of UIResource for primitive types.
     */
    @Test
    public void testOpaque() {
        JXTitledPanel titledPanel = new JXTitledPanel();
        boolean opaque = titledPanel.isOpaque(); 
        titledPanel.setOpaque(!opaque);
        assertEquals("sanity: opaqueness toggled: ", !opaque, titledPanel.isOpaque());
        titledPanel.updateUI();
        assertEquals("ui must not overwrite custom setting: ", !opaque, titledPanel.isOpaque());
    }
    /**
     * Issue #1063-swingx: JXTitledPanel must not overwrite custom border
     */
    @Test
    public void testBorderOnLFChange() {
        JXTitledPanel titledPanel = new JXTitledPanel();
        assertTrue("sanity: titledPanels default border must be ui-installable " + titledPanel.getBorder(), 
                SwingXUtilities.isUIInstallable(titledPanel.getBorder()));
        LineBorder border = new LineBorder(titledPanel.getBackground());
        titledPanel.setBorder(border);
        assertEquals("sanity: border set", border, titledPanel.getBorder());
        titledPanel.updateUI();
        assertEquals("border untouched ", border, titledPanel.getBorder());
    }
    
    @Test
    public void testLayoutOnLFChange() {
        JXTitledPanel titledPanel = new JXTitledPanel();
        assertNotNull(titledPanel.getContentContainer());
        titledPanel.getContentContainer().setLayout(new BorderLayout());
        String lf = UIManager.getLookAndFeel().getName();
        setSystemLF(!defaultToSystemLF);
        if (lf.equals(UIManager.getLookAndFeel().getName())) {
            LOG.info("cannot run layoutOnLFChange - equal LF" + lf);
            return;
        }
        SwingUtilities.updateComponentTreeUI(titledPanel);
        assertTrue(titledPanel.getContentContainer().getLayout() instanceof BorderLayout);
    }
    
    /**
     * Issue ??: notifications missing on all "title"XX properties.
     *
     */
    @Test
    public void testTitlePropertiesNotify() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener(report);
        Font deriveFont = panel.getTitleFont().deriveFont(27f);
        panel.setTitleFont(deriveFont);
        assertTrue("panel must notify on titleFont change", report.hasEvents("titleFont"));
// TODO: Whoever owns this please fix this.  The tests should run clean.        
//        fail("JXTitledPanelTest must be updated to new api");
//        panel.setTitleForeground(Color.black);
//        assertTrue("panel must notify on titleForeground change", report.hasEvents("titleForeground"));
//        panel.setTitleDarkBackground(Color.black);
//        assertTrue("panel must notify on titleDarkBackground change", report.hasEvents("titleDarkBackground"));
//        panel.setTitleLightBackground(Color.black);
//        assertTrue("panel must notify on titleLightBackground change", report.hasEvents("titleLightBackground"));
        
    }
    
    /**
     * SwingX Issue #9: missing notification on title change.
     * happens if a generic property change listener (== one who 
     * wants to get all property changes) is registered.
     */
    @Test
    public void testTitleNotify() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener(report);
        panel.setTitle("new title");
        assertTrue("panel must have fired propertyChange", report.hasEvents());
        
    }
    /**
     * SwingX Issue #9: missing notification on title change.
     * Notification is correct, if a named propertyChangeListener is
     * registered.
     */
    @Test
    public void testTitleNotifyNamed() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener( "title", report);
        panel.setTitle("new title");
        assertTrue("panel must have fired propertyChange", report.hasEvents());
        
    }
    
    /**
     * incorrect propertyChangeEvent on setTitle(null).
     *
     */
    @Test
    public void testTitleNotifyPropertyValue() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener( "title", report);
        panel.setTitle(null);
        assertTrue("panel must have fired propertyChange", report.hasEvents());
        assertEquals("new property value must be equal to getTitle", panel.getTitle(),
                report.getLastNewValue("title"));
        
    }

//--------------------- interactive tests
    /**
     * Issue #1063-swingx: JXTitledPanel must respect custom border.
     */
    public void interactiveBorderOnUpdateUI() {
        final JXTitledPanel titledPanel = new JXTitledPanel("Search");
        titledPanel.setBorder(new LineBorder(titledPanel.getBackground()));
        showInFrame(titledPanel, "border on laf change");
    }

    public  void interactiveRToL() {
        String title = "starting title";
        JXTitledPanel titledPane = new JXTitledPanel(title);
        titledPane.setLeftDecoration(new JLabel("Leading"));
        titledPane.setRightDecoration(new JLabel("Trailing"));
//        panel.getContentContainer().setLayout(new BoxLayout(panel.getContentContainer(), BoxLayout.PAGE_AXIS));
        Icon icon = new ImageIcon(getClass().getResource("resources/images/wellBottom.gif"));
        final JLabel label = new JLabel(title);
        label.setIcon(icon);<commit_msg>sanity commit: some time ago forgot an updated test for JXTitledPanel<commit_after>/*
 * $Id$
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 */
package org.jdesktop.swingx;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.LinearGradientPaint;
import java.awt.event.ActionEvent;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.jdesktop.swingx.painter.MattePainter;
import org.jdesktop.test.PropertyChangeReport;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class JXTitledPanelTest extends InteractiveTestCase {
    private static final Logger LOG = Logger.getLogger(JXTitledPanelTest.class
            .getName());
    
    // flag used in setup to explicitly choose LF
    private boolean defaultToSystemLF;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // make sure we have the same default for each test
        defaultToSystemLF = false;
        setSystemLF(defaultToSystemLF);
    }

    public JXTitledPanelTest() {
        super("JXTitledPane interactive test");
    }

    /**
     * Sanity: changed ui-delegate to install the opaqueness via LookAndFeel. Doing so
     * is the equivalent of UIResource for primitive types.
     */
    @Test
    public void testOpaque() {
        JXTitledPanel titledPanel = new JXTitledPanel();
        boolean opaque = titledPanel.isOpaque(); 
        titledPanel.setOpaque(!opaque);
        assertEquals("sanity: opaqueness toggled: ", !opaque, titledPanel.isOpaque());
        titledPanel.updateUI();
        assertEquals("ui must not overwrite custom setting: ", !opaque, titledPanel.isOpaque());
    }
    /**
     * Issue #1063-swingx: JXTitledPanel must not overwrite custom border
     */
    @Test
    public void testBorderOnLFChange() {
        JXTitledPanel titledPanel = new JXTitledPanel();
        assertTrue("sanity: titledPanels default border must be ui-installable " + titledPanel.getBorder(), 
                SwingXUtilities.isUIInstallable(titledPanel.getBorder()));
        LineBorder border = new LineBorder(titledPanel.getBackground());
        titledPanel.setBorder(border);
        assertEquals("sanity: border set", border, titledPanel.getBorder());
        titledPanel.updateUI();
        assertEquals("border untouched ", border, titledPanel.getBorder());
    }
    
    @Test
    public void testLayoutOnLFChange() {
        JXTitledPanel titledPanel = new JXTitledPanel();
        assertNotNull(titledPanel.getContentContainer());
        titledPanel.getContentContainer().setLayout(new BorderLayout());
        String lf = UIManager.getLookAndFeel().getName();
        setSystemLF(!defaultToSystemLF);
        if (lf.equals(UIManager.getLookAndFeel().getName())) {
            LOG.info("cannot run layoutOnLFChange - equal LF" + lf);
            return;
        }
        SwingUtilities.updateComponentTreeUI(titledPanel);
        assertTrue(titledPanel.getContentContainer().getLayout() instanceof BorderLayout);
    }
    
    /**
     * Issue ??: notifications missing on all "title"XX properties.
     *
     */
    @Test
    public void testTitlePropertiesNotify() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener(report);
        Font deriveFont = panel.getTitleFont().deriveFont(27f);
        panel.setTitleFont(deriveFont);
        assertTrue("panel must notify on titleFont change", report.hasEvents("titleFont"));
// TODO: Whoever owns this please fix this.  The tests should run clean.        
//        fail("JXTitledPanelTest must be updated to new api");
//        panel.setTitleForeground(Color.black);
//        assertTrue("panel must notify on titleForeground change", report.hasEvents("titleForeground"));
//        panel.setTitleDarkBackground(Color.black);
//        assertTrue("panel must notify on titleDarkBackground change", report.hasEvents("titleDarkBackground"));
//        panel.setTitleLightBackground(Color.black);
//        assertTrue("panel must notify on titleLightBackground change", report.hasEvents("titleLightBackground"));
        
    }
    
    /**
     * SwingX Issue #9: missing notification on title change.
     * happens if a generic property change listener (== one who 
     * wants to get all property changes) is registered.
     */
    @Test
    public void testTitleNotify() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener(report);
        panel.setTitle("new title");
        assertTrue("panel must have fired propertyChange", report.hasEvents());
        
    }
    /**
     * SwingX Issue #9: missing notification on title change.
     * Notification is correct, if a named propertyChangeListener is
     * registered.
     */
    @Test
    public void testTitleNotifyNamed() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener( "title", report);
        panel.setTitle("new title");
        assertTrue("panel must have fired propertyChange", report.hasEvents());
        
    }
    
    /**
     * incorrect propertyChangeEvent on setTitle(null).
     *
     */
    @Test
    public void testTitleNotifyPropertyValue() {
        String title = "starting title";
        final JXTitledPanel panel = new JXTitledPanel(title);
        PropertyChangeReport report = new PropertyChangeReport();
        panel.addPropertyChangeListener( "title", report);
        panel.setTitle(null);
        assertTrue("panel must have fired propertyChange", report.hasEvents());
        assertEquals("new property value must be equal to getTitle", panel.getTitle(),
                report.getLastNewValue("title"));
        
    }

//--------------------- interactive tests
 
    
    /**
     * Issue #157-swingx: JXTitledPanel not fully bidi-compliant.
     * 
     * Last issue: gradient not adjusted - should be handled by 
     * the painter. See Issue #1163-swingx
     */
    public void interactiveGradient() {
        LinearGradientPaint l;
        GradientPaint paint = new GradientPaint(0f, 0f, Color.WHITE, 500f, 
                500f, Color.BLUE);
        JXTitledPanel panel = new JXTitledPanel("want a gradient");
        panel.setTitlePainter(new MattePainter(paint, true));
//        ImagePainter p = new ImagePainter(XTestUtils.loadDefaultImage());
//        p.setScaleType(ScaleType.OutsideFit);
//        p.setHorizontalAlignment(HorizontalAlignment.LEFT);
//        panel.setTitlePainter(p);
        JXFrame frame = wrapInFrame(panel, "gradient");
        addComponentOrientationToggle(frame);
        show(frame, 500, 500);
    }

    /**
     * Issue #1063-swingx: JXTitledPanel must respect custom border.
     */
    public void interactiveBorderOnUpdateUI() {
        final JXTitledPanel titledPanel = new JXTitledPanel("Search");
        titledPanel.setBorder(new LineBorder(titledPanel.getBackground()));
        showInFrame(titledPanel, "border on laf change");
    }

    public  void interactiveRToL() {
        String title = "starting title";
        JXTitledPanel titledPane = new JXTitledPanel(title);
        titledPane.setLeftDecoration(new JLabel("Leading"));
        titledPane.setRightDecoration(new JLabel("Trailing"));
//        panel.getContentContainer().setLayout(new BoxLayout(panel.getContentContainer(), BoxLayout.PAGE_AXIS));
        Icon icon = new ImageIcon(getClass().getResource("resources/images/wellBottom.gif"));
        final JLabel label = new JLabel(title);
        label.setIcon(icon);
{'fraction_non_alphanumeric': 0.10299340436326737, 'fraction_numerical': 0.004756468797564688, 'mean_word_length': 3.1247711221553756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6955204', 'n_tokens_mistral': 4261, 'n_tokens_neox': 4161, 'n_words': 1061}