This is the mail archive of the mauve-patches@sourceware.org mailing list for the Mauve project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: MetalComboBoxUI - new tests


I committed some tests for the MetalComboBoxUI class:

2005-11-07 David Gilbert <david.gilbert@object-refinery.com>

* gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/createArrowButton.java:
New test,
* gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getDisplaySize.java:
New test,
* gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getMinimumSize.java:
New test,
* gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getPreferredSize.java:
New test,
* gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/MyMetalComboBoxUI.java:
New support class.


Regards,

Dave

Index: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/MyMetalComboBoxUI.java
===================================================================
RCS file: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/MyMetalComboBoxUI.java
diff -N gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/MyMetalComboBoxUI.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/MyMetalComboBoxUI.java	7 Nov 2005 21:48:16 -0000
@@ -0,0 +1,46 @@
+// Tags: JDK1.2
+
+// Copyright (C) 2005 David Gilbert <david.gilbert@object-refinery.com>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+// Boston, MA 02110-1301 USA.
+
+package gnu.testlet.javax.swing.plaf.metal.MetalComboBoxUI;
+
+import java.awt.Dimension;
+
+import javax.swing.JButton;
+import javax.swing.plaf.metal.MetalComboBoxUI;
+
+/**
+ * Provides access to protected info.
+ */
+public class MyMetalComboBoxUI extends MetalComboBoxUI
+{
+  public JButton createArrowButton()
+  {
+    return super.createArrowButton();
+  }
+  public JButton getArrowButton()
+  {
+    return this.arrowButton;
+  }
+  public Dimension getDisplaySize() 
+  {
+    return super.getDisplaySize();    
+  }
+}
Index: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/createArrowButton.java
===================================================================
RCS file: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/createArrowButton.java
diff -N gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/createArrowButton.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/createArrowButton.java	7 Nov 2005 21:48:16 -0000
@@ -0,0 +1,71 @@
+// Tags: JDK1.2
+
+// Copyright (C) 2005 David Gilbert <david.gilbert@object-refinery.com>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+// Boston, MA 02110-1301 USA.
+
+package gnu.testlet.javax.swing.plaf.metal.MetalComboBoxUI;
+
+import gnu.testlet.TestHarness;
+import gnu.testlet.Testlet;
+
+import java.awt.Insets;
+
+import javax.swing.JButton;
+import javax.swing.JComboBox;
+import javax.swing.UIManager;
+import javax.swing.plaf.UIResource;
+import javax.swing.plaf.metal.MetalComboBoxButton;
+import javax.swing.plaf.metal.MetalComboBoxUI;
+import javax.swing.plaf.metal.MetalLookAndFeel;
+
+/**
+ * Some checks for the createArrowButton() method in the 
+ * {@link MetalComboBoxUI} class.  
+ */
+public class createArrowButton implements Testlet 
+{
+
+  /**
+   * Runs the test using the specified harness.
+   * 
+   * @param harness  the test harness (<code>null</code> not permitted).
+   */
+  public void test(TestHarness harness)  
+  {
+    try
+    {
+      UIManager.setLookAndFeel(new MetalLookAndFeel());
+    }
+    catch (Exception e)
+    {
+      e.printStackTrace();
+    }
+    JComboBox cb = new JComboBox();
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    JButton b = ui.createArrowButton();
+    harness.check(b instanceof MetalComboBoxButton);
+    Insets insets = b.getInsets();
+    harness.check(insets, new Insets(3, 4, 4, 6));
+    Insets margin = b.getMargin();
+    harness.check(margin, new Insets(0, 1, 1, 3));
+    harness.check(!(margin instanceof UIResource));
+  }
+
+}
Index: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getDisplaySize.java
===================================================================
RCS file: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getDisplaySize.java
diff -N gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getDisplaySize.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getDisplaySize.java	7 Nov 2005 21:48:16 -0000
@@ -0,0 +1,145 @@
+// Tags: JDK1.2
+
+// Copyright (C) 2005 David Gilbert <david.gilbert@object-refinery.com>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+// Boston, MA 02110-1301 USA.
+
+package gnu.testlet.javax.swing.plaf.metal.MetalComboBoxUI;
+
+import gnu.testlet.TestHarness;
+import gnu.testlet.Testlet;
+import gnu.testlet.javax.swing.plaf.TestLookAndFeel;
+import gnu.testlet.javax.swing.plaf.basic.BasicComboBoxUI.MyBasicComboBoxUI;
+
+import java.awt.Dimension;
+import java.awt.FontMetrics;
+
+import javax.swing.JComboBox;
+import javax.swing.JTextField;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import javax.swing.plaf.metal.MetalComboBoxUI;
+import javax.swing.plaf.metal.MetalLookAndFeel;
+
+/**
+ * Some checks for the getDisplaySize() method in the 
+ * {@link MetalComboBoxUI} class.  This is an inherited method.  
+ */
+public class getDisplaySize implements Testlet 
+{
+
+  /**
+   * Runs the test using the specified harness.
+   * 
+   * @param harness  the test harness (<code>null</code> not permitted).
+   */
+  public void test(TestHarness harness)  
+  {
+    testNotEditable(harness);
+    testEditable(harness);
+  }
+  
+  /**
+   * Run some checks for a JComboBox that is not editable.
+   * 
+   * @param harness  the test harness.
+   */
+  private void testNotEditable(TestHarness harness)
+  {
+    try
+    {
+      UIManager.setLookAndFeel(new MetalLookAndFeel());
+    }
+    catch (UnsupportedLookAndFeelException e)
+    {
+      e.printStackTrace();
+    }
+    JComboBox cb = new JComboBox();
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    int additionalHeight = 2;  // margin?  border?
+    int additionalWidth = 2;  
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    
+    // the following width calculation is a guess.  We know the value
+    // depends on the font size, and that it is relatively small, so after
+    // trying out a few candidates this one seems to give the right result
+    int width = fm.charWidth(' ') + additionalWidth;
+    int height = fm.getHeight() + additionalHeight;
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+    
+    cb.addItem("ABC");
+    width = fm.stringWidth("ABC") + additionalWidth;
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+
+    cb.addItem("A longer item");
+    width = fm.stringWidth("A longer item") + additionalWidth;
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+
+    cb.setPrototypeDisplayValue("Prototype");
+    width = fm.stringWidth("Prototype") + additionalWidth;
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+  }
+
+  /**
+   * Run some checks for a JComboBox that is editable.
+   * 
+   * @param harness  the test harness.
+   */
+  private void testEditable(TestHarness harness)
+  {
+    try
+    {
+      UIManager.setLookAndFeel(new TestLookAndFeel());
+    }
+    catch (UnsupportedLookAndFeelException e)
+    {
+      e.printStackTrace();
+    }
+    JComboBox cb = new JComboBox();
+    cb.setEditable(true);
+    MyBasicComboBoxUI ui = new MyBasicComboBoxUI();
+    cb.setUI(ui);
+    JTextField tf = (JTextField) cb.getEditor().getEditorComponent();
+    int columns = tf.getColumns();
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    
+    // for an editable JComboBox the display size is calculated from the 
+    // preferred size of the JTextField it seems, or the prototype display
+    // value in some cases...
+    int width = fm.charWidth('m') * columns;
+    int height = fm.getHeight() + 2;  // the 2 seems to be a fixed margin
+    
+    // not sure why the width here needs + 1..
+    harness.check(ui.getDisplaySize(), new Dimension(width + 1, height));
+    
+    cb.addItem("ABC");
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+
+    cb.addItem("A longer item");
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+
+    cb.setPrototypeDisplayValue("Prototype");
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+
+    cb.setPrototypeDisplayValue("Long Prototype Display Value");
+    width = fm.stringWidth("Long Prototype Display Value") + 2;
+    harness.check(ui.getDisplaySize(), new Dimension(width, height));
+  }
+
+}
Index: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getMinimumSize.java
===================================================================
RCS file: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getMinimumSize.java
diff -N gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getMinimumSize.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getMinimumSize.java	7 Nov 2005 21:48:17 -0000
@@ -0,0 +1,208 @@
+// Tags: JDK1.2
+
+// Copyright (C) 2005 David Gilbert <david.gilbert@object-refinery.com>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+// Boston, MA 02110-1301 USA.
+
+package gnu.testlet.javax.swing.plaf.metal.MetalComboBoxUI;
+
+import gnu.testlet.TestHarness;
+import gnu.testlet.Testlet;
+
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Insets;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.JTextField;
+import javax.swing.plaf.metal.MetalComboBoxButton;
+import javax.swing.plaf.metal.MetalComboBoxIcon;
+import javax.swing.plaf.metal.MetalComboBoxUI;
+
+/**
+ * Some checks for the getMinimumSize() method in the 
+ * {@link MetalComboBoxUI} class.  
+ */
+public class getMinimumSize implements Testlet 
+{
+
+  /**
+   * Runs the test using the specified harness.
+   * 
+   * @param harness  the test harness (<code>null</code> not permitted).
+   */
+  public void test(TestHarness harness)
+  {
+    test1(harness);
+    test2(harness);
+    testEditable(harness);
+    testEditableWithCustomFont(harness);
+  }
+
+  public void test1(TestHarness harness)  
+  {
+    JComboBox cb = new JComboBox();
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    Insets insets = ui.getArrowButton().getInsets();
+    int additionalWidth = insets.left + insets.right + 2;
+    int additionalHeight = insets.top + insets.bottom + 2;
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    
+    // the following width calculation is a guess.  We know the value
+    // depends on the font size, and that it is relatively small, so after
+    // trying out a few candidates this one seems to give the right result
+    int width = fm.charWidth(' ') + additionalWidth + 1 + fm.getHeight();
+    int height = fm.getHeight() + additionalHeight;
+    harness.check(ui.getMinimumSize(cb), new Dimension(width, height));
+                   // the width is the display width plus the button width and
+                   // the button width is equal to 'height'
+    
+    cb.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    width = fm.charWidth('X') + additionalWidth + 1 + fm.getHeight();
+    harness.check(ui.getMinimumSize(cb), new Dimension(width, height));
+    
+    cb.setPrototypeDisplayValue("XX");    
+    width = fm.stringWidth("XX") + additionalWidth + 1 + fm.getHeight();
+    harness.check(ui.getMinimumSize(cb), new Dimension(width, height));
+  }
+
+  /**
+   * Checks the minimum size for a non-editable JComboBox with a custom font.
+   * 
+   * @param harness
+   */
+  public void test2(TestHarness harness)  
+  {
+    JComboBox cb = new JComboBox();
+    cb.setFont(new Font("Dialog", Font.PLAIN, 24));
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    MetalComboBoxButton button = (MetalComboBoxButton) ui.getArrowButton();
+    MetalComboBoxIcon icon = (MetalComboBoxIcon) button.getComboIcon();
+    Insets insets = button.getInsets();
+    int additionalWidth = insets.left + insets.right + 2;
+    int additionalHeight = insets.top + insets.bottom + 2;
+    int iconWidth = icon.getIconWidth() + 6;  // a margin = 6 by trial and 
+                                              // error
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+
+    // the following width calculation is a guess.  We know the value
+    // depends on the font size, and that it is relatively small, so after
+    // trying out a few candidates this one seems to give the right result
+    int width = fm.charWidth(' ') + additionalWidth + iconWidth;
+    int height = fm.getHeight() + additionalHeight;
+    harness.check(ui.getMinimumSize(cb), new Dimension(width, height));
+                   // the width is the display width plus the button width and
+                   // the button width is equal to 'height'
+    
+    cb.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    width = fm.charWidth('X') + additionalWidth + iconWidth;
+    harness.check(ui.getMinimumSize(cb), new Dimension(width, height));
+    
+    cb.setPrototypeDisplayValue("XX");    
+    width = fm.stringWidth("XX") + additionalWidth + iconWidth;
+    harness.check(ui.getMinimumSize(cb), new Dimension(width, height));
+
+  }
+
+  private void testEditable(TestHarness harness)
+  {
+    harness.checkPoint("testEditable()");
+    JComboBox cb = new JComboBox();
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    JTextField tf = (JTextField) cb.getEditor().getEditorComponent();
+    cb.setEditable(true);
+    Insets tfInsets = tf.getInsets();
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    int height = fm.getHeight() + tfInsets.top + tfInsets.bottom + 1;
+    int width = fm.stringWidth("m") * tf.getColumns() + tfInsets.left 
+        + tfInsets.right + height + 3; 
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));    
+    cb.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));        
+    cb.setPrototypeDisplayValue("XX");    
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));
+    
+    // repeat the tests with a different font
+    JComboBox cb2 = new JComboBox();
+    MyMetalComboBoxUI ui2 = new MyMetalComboBoxUI();
+    cb2.setUI(ui2);
+    JTextField tf2 = (JTextField) cb2.getEditor().getEditorComponent();
+    cb2.setEditable(true);
+    Insets tfInsets2 = tf2.getInsets();
+    cb2.setFont(new Font("Dialog", Font.PLAIN, 24));
+    FontMetrics fm2 = cb2.getFontMetrics(cb2.getFont());
+    int height2 = fm2.getHeight() + tfInsets2.top + tfInsets2.bottom + 1;
+    int width2 = fm2.stringWidth("m") * tf2.getColumns() + tfInsets2.left 
+        + tfInsets2.right + height2 + 3; 
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));    
+    cb2.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));        
+    cb2.setPrototypeDisplayValue("XX");    
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));
+  }
+
+  /**
+   * Checks the minimum size for an editable JComboBox with a custom font.
+   * 
+   * @param harness
+   */
+  public void testEditableWithCustomFont(TestHarness harness)  
+  {
+    harness.checkPoint("testEditableWithCustomFont()");
+    JComboBox cb = new JComboBox();
+    cb.setFont(new Font("Dialog", Font.PLAIN, 24));
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    JTextField tf = (JTextField) cb.getEditor().getEditorComponent();
+    cb.setEditable(true);
+    Insets tfInsets = tf.getInsets();
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    int height = fm.getHeight() + tfInsets.top + tfInsets.bottom + 1;
+    int width = fm.stringWidth("m") * tf.getColumns() + tfInsets.left 
+        + tfInsets.right + height + 3; 
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));    
+    cb.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));        
+    cb.setPrototypeDisplayValue("XX");    
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));
+    
+    // repeat the tests with a different font
+    JComboBox cb2 = new JComboBox();
+    MyMetalComboBoxUI ui2 = new MyMetalComboBoxUI();
+    cb2.setUI(ui2);
+    JTextField tf2 = (JTextField) cb2.getEditor().getEditorComponent();
+    cb2.setEditable(true);
+    Insets tfInsets2 = tf2.getInsets();
+    cb2.setFont(new Font("Dialog", Font.PLAIN, 24));
+    FontMetrics fm2 = cb2.getFontMetrics(cb2.getFont());
+    int height2 = fm2.getHeight() + tfInsets2.top + tfInsets2.bottom + 1;
+    int width2 = fm2.stringWidth("m") * tf2.getColumns() + tfInsets2.left 
+        + tfInsets2.right + height2 + 3; 
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));    
+    cb2.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));        
+    cb2.setPrototypeDisplayValue("XX");    
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));
+  }
+
+}
Index: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getPreferredSize.java
===================================================================
RCS file: gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getPreferredSize.java
diff -N gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getPreferredSize.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/javax/swing/plaf/metal/MetalComboBoxUI/getPreferredSize.java	7 Nov 2005 21:48:17 -0000
@@ -0,0 +1,124 @@
+// Tags: JDK1.2
+
+// Copyright (C) 2005 David Gilbert <david.gilbert@object-refinery.com>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
+// Boston, MA 02110-1301 USA.
+
+package gnu.testlet.javax.swing.plaf.metal.MetalComboBoxUI;
+
+import gnu.testlet.TestHarness;
+import gnu.testlet.Testlet;
+
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Insets;
+
+import javax.swing.DefaultComboBoxModel;
+import javax.swing.JComboBox;
+import javax.swing.JTextField;
+import javax.swing.plaf.metal.MetalComboBoxUI;
+
+/**
+ * Some checks for the getPreferredSize() method in the 
+ * {@link MetalComboBoxUI} class.  
+ */
+public class getPreferredSize implements Testlet 
+{
+
+  /**
+   * Runs the test using the specified harness.
+   * 
+   * @param harness  the test harness (<code>null</code> not permitted).
+   */
+  public void test(TestHarness harness)  
+  {
+    testNonEditable(harness);
+    testEditable(harness);
+  }
+  
+  private void testNonEditable(TestHarness harness)  
+  {
+    harness.checkPoint("testNonEditable()");
+    JComboBox cb = new JComboBox();
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    Insets insets = ui.getArrowButton().getInsets();
+    int additionalWidth = insets.left + insets.right + 2;
+    int additionalHeight = insets.top + insets.bottom + 2;
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    
+    // the following width calculation is a guess.  We know the value
+    // depends on the font size, and that it is relatively small, so after
+    // trying out a few candidates this one seems to give the right result
+    int width = fm.charWidth(' ') + additionalWidth + 1 + fm.getHeight();
+    int height = fm.getHeight() + additionalHeight;
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));
+                   // the width is the display width plus the button width and
+                   // the button width is equal to 'height'
+    
+    cb.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    width = fm.charWidth('X') + additionalWidth + 1 + fm.getHeight();
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));
+    
+    cb.setPrototypeDisplayValue("XX");    
+    width = fm.stringWidth("XX") + additionalWidth + 1 + fm.getHeight();
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));
+    
+  }
+
+  private void testEditable(TestHarness harness)
+  {
+    harness.checkPoint("testEditable()");
+    JComboBox cb = new JComboBox();
+    MyMetalComboBoxUI ui = new MyMetalComboBoxUI();
+    cb.setUI(ui);
+    JTextField tf = (JTextField) cb.getEditor().getEditorComponent();
+    cb.setEditable(true);
+    Insets tfInsets = tf.getInsets();
+    System.out.println("tfInsets = " + tfInsets);
+    FontMetrics fm = cb.getFontMetrics(cb.getFont());
+    int height = fm.getHeight() + tfInsets.top + tfInsets.bottom + 1;
+    int width = fm.stringWidth("m") * tf.getColumns() + tfInsets.left 
+        + tfInsets.right + height + 3; 
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));    
+    cb.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));        
+    cb.setPrototypeDisplayValue("XX");    
+    harness.check(ui.getPreferredSize(cb), new Dimension(width, height));
+    
+    // repeat the tests with a different font
+    JComboBox cb2 = new JComboBox();
+    MyMetalComboBoxUI ui2 = new MyMetalComboBoxUI();
+    cb2.setUI(ui2);
+    JTextField tf2 = (JTextField) cb2.getEditor().getEditorComponent();
+    cb2.setEditable(true);
+    Insets tfInsets2 = tf2.getInsets();
+    cb2.setFont(new Font("Dialog", Font.PLAIN, 24));
+    FontMetrics fm2 = cb2.getFontMetrics(cb2.getFont());
+    int height2 = fm2.getHeight() + tfInsets2.top + tfInsets2.bottom + 1;
+    int width2 = fm2.stringWidth("m") * tf2.getColumns() + tfInsets2.left 
+        + tfInsets2.right + height2 + 3; 
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));    
+    cb2.setModel(new DefaultComboBoxModel(new Object[] {"X"}));
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));        
+    cb2.setPrototypeDisplayValue("XX");    
+    harness.check(ui2.getPreferredSize(cb2), new Dimension(width2, height2));
+  }
+  
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]