View Javadoc

1   /*
2    * Copyright 2010 - 2010 JEuclid, http://jeuclid.sf.net
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: JMathComponentTest.java,v b7608960db6b 2010/08/13 09:22:12 max $ */
18  
19  package net.sourceforge.jeuclid.test.swing;
20  
21  import java.awt.Dimension;
22  import java.awt.GraphicsEnvironment;
23  
24  import net.sourceforge.jeuclid.swing.JMathComponent;
25  
26  import org.junit.Assert;
27  import org.junit.Assume;
28  import org.junit.BeforeClass;
29  import org.junit.Test;
30  import org.w3c.dom.Node;
31  
32  /**
33   * Tests for {@link JMathComponent}.
34   * 
35   * @version $Revision: b7608960db6b $
36   */
37  public class JMathComponentTest {
38  
39      /** Run these tests only on non-headless nodes. */
40      @BeforeClass
41      public static void checkHeadless() {
42          Assume.assumeTrue(!GraphicsEnvironment.isHeadless());
43      }
44  
45      /**
46       * Checks if {@link JMathComponent#getPreferredSize() works and updates if
47       * the content changes.
48       */
49      @Test
50      public void testPreferredSize() {
51          final JMathComponent jmc = new JMathComponent();
52          final Dimension d = jmc.getPreferredSize();
53          Assert.assertEquals(d.getHeight(), 0.0, 0.01);
54          Assert.assertEquals(d.getWidth(), 0.0, 0.01);
55          jmc.setContent("<math><mtext>x</mtext></math>");
56          final Dimension d2 = jmc.getPreferredSize();
57          Assert.assertTrue(d2.getHeight() > 1);
58          Assert.assertTrue(d2.getWidth() > 1);
59          final Node mtext = jmc.getDocument().getFirstChild().getFirstChild();
60          mtext.setTextContent("xxx");
61          final Dimension d3 = jmc.getPreferredSize();
62          Assert.assertEquals(d3.getHeight(), d2.getHeight(), 0.001);
63          Assert.assertTrue(d3.getWidth() > d2.getWidth());
64      }
65  }