001 /*
002 * Copyright 2007 - 2010 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: LayoutContextParamTest.java,v 92b6a7c39d7f 2010/08/11 20:23:17 max $ */
018
019 package net.sourceforge.jeuclid.test;
020
021 import java.awt.Color;
022 import java.util.Arrays;
023 import java.util.Collections;
024
025 import net.sourceforge.jeuclid.Constants;
026 import net.sourceforge.jeuclid.LayoutContext;
027 import net.sourceforge.jeuclid.context.Display;
028 import net.sourceforge.jeuclid.context.LayoutContextImpl;
029 import net.sourceforge.jeuclid.context.Parameter;
030 import net.sourceforge.jeuclid.elements.generic.DocumentElement;
031 import net.sourceforge.jeuclid.elements.generic.MathImpl;
032
033 import org.junit.Assert;
034 import org.junit.Test;
035
036 /**
037 * Test for {@link net.sourceforge.jeuclid.context.Parameter} enum.
038 *
039 * @version $Revision: 92b6a7c39d7f $
040 */
041 public class LayoutContextParamTest {
042 /**
043 * Default Constructor.
044 */
045 public LayoutContextParamTest() {
046 // Empty on purpose
047 }
048
049 /**
050 * Tests implementation of {@link Parameter#valid(Object)} method.
051 */
052 @Test
053 public void testValid() {
054 Assert.assertTrue(Parameter.DEBUG.valid(Boolean.TRUE));
055 Assert.assertFalse(Parameter.DEBUG.valid("true"));
056 Assert.assertTrue(Parameter.DISPLAY.valid(Display.BLOCK));
057 Assert.assertFalse(Parameter.DISPLAY.valid(null));
058 Assert.assertTrue(Parameter.FONTS_SERIF.valid(Collections
059 .singletonList("Arial")));
060 Assert.assertFalse(Parameter.FONTS_SERIF.valid("Courier"));
061 Assert.assertTrue(Parameter.MATHBACKGROUND.valid(Color.RED));
062 Assert.assertTrue(Parameter.MATHBACKGROUND.valid(null));
063 Assert.assertTrue(Parameter.MATHSIZE.valid(Float.valueOf(1)));
064 Assert.assertFalse(Parameter.MATHSIZE.valid(Integer.valueOf(1)));
065 Assert.assertTrue(Parameter.SCRIPTLEVEL.valid(Integer.valueOf(1)));
066 Assert.assertFalse(Parameter.SCRIPTLEVEL.valid(Float.valueOf(1)));
067 }
068
069 /**
070 * Tests implementation of {@link Parameter#fromString(String)} method.
071 */
072 @Test
073 public void testFromString() {
074 Assert.assertEquals(Parameter.DEBUG.fromString("true"), Boolean.TRUE);
075 Assert.assertEquals(Parameter.DEBUG.fromString(null), null);
076 Assert.assertEquals(Parameter.DISPLAY.fromString("BLOCK"),
077 Display.BLOCK);
078 try {
079 Parameter.DISPLAY.fromString("foo");
080 Assert.fail();
081 } catch (final IllegalArgumentException e) {
082 }
083 Assert.assertEquals(Parameter.FONTS_SERIF.fromString("Arial"),
084 Collections.singletonList("arial"));
085 Assert.assertEquals(
086 Parameter.FONTS_SERIF.fromString("Arial,Helvetica"), Arrays
087 .asList(new String[] { "arial", "helvetica" }));
088 Assert.assertEquals(Parameter.FONTS_SERIF.fromString("Foo, Bar"),
089 Arrays.asList(new String[] { "foo", "bar" }));
090 Assert.assertEquals(Parameter.MATHCOLOR.fromString("RED"), Color.RED);
091 try {
092 Parameter.MATHCOLOR.fromString("violet");
093 Assert.fail();
094 } catch (final IllegalArgumentException e) {
095 }
096 Assert.assertEquals(Parameter.MATHCOLOR.fromString("rgb(100,100,100)"),
097 new Color(100, 100, 100));
098 Assert.assertEquals(Parameter.MATHSIZE.fromString("0.5"), Float
099 .valueOf(0.5f));
100 Assert.assertEquals(Parameter.SCRIPTLEVEL.fromString("1"), Integer
101 .valueOf(1));
102 }
103
104 /**
105 * Tests implementation of {@link Parameter#toString(String)} method.
106 */
107 @Test
108 public void testToString() {
109 Assert.assertEquals(Parameter.DEBUG.toString(Boolean.TRUE), "true");
110 Assert.assertEquals(Parameter.DEBUG.toString(null), null);
111 Assert.assertEquals(Parameter.DISPLAY.toString(Display.INLINE),
112 "INLINE");
113 Assert.assertEquals(Parameter.FONTS_SERIF.toString(Collections
114 .singletonList("font")), "font");
115 Assert.assertEquals(Parameter.FONTS_SERIF.toString(Arrays
116 .asList(new String[] { "f1", "f2" })), "f1, f2");
117 Assert.assertEquals(Parameter.MATHCOLOR.toString(Color.RED), "red");
118 Assert.assertEquals(Parameter.MATHCOLOR.toString(null), null);
119 Assert.assertEquals(
120 Parameter.MATHCOLOR.toString(new Color(32, 32, 32)), "#202020");
121 Assert.assertEquals(Parameter.MATHSIZE.toString(Float.valueOf(0.5f)),
122 "0.5");
123 Assert.assertEquals(Parameter.SCRIPTLEVEL.toString(Integer.valueOf(2)),
124 "2");
125 }
126
127 /**
128 * Test parameter passing from String.
129 */
130 @Test
131 public void testParamFromOldNSProgrammatically() {
132 final MathImpl mi = new MathImpl(MathImpl.ELEMENT,
133 new DocumentElement());
134 final LayoutContext defaultContext = LayoutContextImpl
135 .getDefaultLayoutContext();
136 final LayoutContext testContext1 = mi.getChildLayoutContext(0,
137 defaultContext);
138 Assert.assertEquals(testContext1.getParameter(Parameter.MATHSIZE),
139 12.0f);
140 mi.setAttributeNS(Constants.NS_OLD_JEUCLID_EXT, "jeuclid:fontSize",
141 "13");
142 Assert.assertEquals(testContext1.getParameter(Parameter.MATHSIZE),
143 13.0f);
144 // Case sensitive!
145 mi.setAttributeNS(Constants.NS_OLD_JEUCLID_EXT, "jeuclid:fontsize",
146 "14");
147 Assert.assertEquals(testContext1.getParameter(Parameter.MATHSIZE),
148 13.0f);
149 }
150
151 /**
152 * Test parameter passing from String.
153 */
154 @Test
155 public void testParamFromNSProgrammatically() {
156 final MathImpl mi = new MathImpl(MathImpl.ELEMENT,
157 new DocumentElement());
158 final LayoutContext defaultContext = LayoutContextImpl
159 .getDefaultLayoutContext();
160 final LayoutContext testContext1 = mi.getChildLayoutContext(0,
161 defaultContext);
162 Assert.assertEquals(testContext1.getParameter(Parameter.MATHSIZE),
163 12.0f);
164 mi.setAttributeNS(Constants.NS_JEUCLID_EXT, "jeuclid:fontSize", "13");
165 Assert.assertEquals(testContext1.getParameter(Parameter.MATHSIZE),
166 13.0f);
167 // Case sensitive!
168 mi.setAttributeNS(Constants.NS_JEUCLID_EXT, "jeuclid:fontsize", "14");
169 Assert.assertEquals(testContext1.getParameter(Parameter.MATHSIZE),
170 13.0f);
171 }
172 }