001 /* 002 * Copyright 2002 - 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: ConverterTest.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.awt.image.BufferedImage; 023 import java.io.File; 024 025 import net.sourceforge.jeuclid.MathMLParserSupport; 026 import net.sourceforge.jeuclid.MutableLayoutContext; 027 import net.sourceforge.jeuclid.context.LayoutContextImpl; 028 import net.sourceforge.jeuclid.context.Parameter; 029 import net.sourceforge.jeuclid.converter.Converter; 030 import net.sourceforge.jeuclid.converter.ConverterPlugin.DocumentWithDimension; 031 import net.sourceforge.jeuclid.converter.ConverterRegistry; 032 import net.sourceforge.jeuclid.elements.generic.DocumentElement; 033 034 import org.junit.Assert; 035 import org.junit.Test; 036 import org.w3c.dom.Document; 037 import org.w3c.dom.Element; 038 import org.w3c.dom.svg.SVGDocument; 039 040 /** 041 * @version $Revision: 92b6a7c39d7f $ 042 */ 043 public class ConverterTest { 044 045 /** 046 * Test string with xml header. 047 */ 048 public static final String TEST1 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><math mode=\"display\">" 049 + "<mrow><munderover><mo>∫</mo><mn>1</mn><mi>x</mi></munderover>" 050 + "<mfrac><mi>dt</mi><mi>t</mi></mfrac></mrow></math>"; 051 052 private void testConverterXXX(final String ext, final String mimeext) 053 throws Exception { 054 final Document doc = MathMLParserSupport 055 .parseString(ConverterTest.TEST1); 056 final File outFile = new File(this.getOutDir(), "test1." + ext); 057 final MutableLayoutContext params = new LayoutContextImpl( 058 LayoutContextImpl.getDefaultLayoutContext()); 059 params.setParameter(Parameter.MATHSIZE, 25f); 060 061 Converter.getInstance().convert(doc, outFile, "image/" + mimeext, 062 params); 063 Assert.assertTrue(outFile.exists()); 064 Assert.assertTrue(outFile.length() > 0); 065 } 066 067 /** 068 * Tests if PNG converter is available. 069 * 070 * @throws Exception 071 * if the test fails. 072 */ 073 @Test 074 public void testConverterPNG() throws Exception { 075 this.testConverterXXX("png", "png"); 076 } 077 078 /** 079 * Tests if JPEG converter is available. 080 * 081 * @throws Exception 082 * if the test fails. 083 */ 084 @Test 085 public void testConverterJPG() throws Exception { 086 this.testConverterXXX("jpg", "jpeg"); 087 } 088 089 /** 090 * Tests if BMP converter is available. 091 * 092 * @throws Exception 093 * if the test fails. 094 */ 095 @Test 096 public void testConverterBMP() throws Exception { 097 this.testConverterXXX("bmp", "bmp"); 098 } 099 100 /** 101 * Tests if SVG converter is available and creates an output file. 102 * 103 * @throws Exception 104 * if the test fails. 105 */ 106 @Test 107 public void testConverterSVG() throws Exception { 108 final Document doc = MathMLParserSupport 109 .parseString(ConverterTest.TEST1); 110 final File outFile = new File(this.getOutDir(), "test1.svg"); 111 final MutableLayoutContext params = new LayoutContextImpl( 112 LayoutContextImpl.getDefaultLayoutContext()); 113 params.setParameter(Parameter.MATHSIZE, 25f); 114 Converter.getInstance().convert(doc, outFile, 115 net.sourceforge.jeuclid.converter.Converter.TYPE_SVG, params); 116 Assert.assertTrue("SVG file was not created", outFile.exists()); 117 Assert.assertTrue("SVG file is empty", outFile.length() > 0); 118 Assert.assertTrue("SVG file is too small", outFile.length() > 2048); 119 } 120 121 /** 122 * Tests if PDF converter is available and creates an output file. 123 * 124 * @throws Exception 125 * if the test fails. 126 */ 127 @Test 128 public void testConverterPDF() throws Exception { 129 final Document doc = MathMLParserSupport 130 .parseString(ConverterTest.TEST1); 131 final File outFile = new File(this.getOutDir(), "test1.pdf"); 132 final MutableLayoutContext params = new LayoutContextImpl( 133 LayoutContextImpl.getDefaultLayoutContext()); 134 params.setParameter(Parameter.MATHSIZE, 25f); 135 Converter.getInstance().convert(doc, outFile, "application/pdf", 136 params); 137 Assert.assertTrue("PDF file was not created", outFile.exists()); 138 Assert.assertTrue("PDF file is empty", outFile.length() > 0); 139 Assert.assertTrue("PDF file is too small", outFile.length() > 2048); 140 } 141 142 /** 143 * Tests if SVG converter is available and creates an output DOM. 144 * 145 * @throws Exception 146 * if the test fails. 147 */ 148 @Test 149 public void testConverterSVGtoDOM() throws Exception { 150 final Document doc = MathMLParserSupport 151 .parseString(ConverterTest.TEST1); 152 final MutableLayoutContext params = new LayoutContextImpl( 153 LayoutContextImpl.getDefaultLayoutContext()); 154 params.setParameter(Parameter.MATHSIZE, 25f); 155 final DocumentWithDimension svgdocdim = Converter.getInstance() 156 .convert(doc, 157 net.sourceforge.jeuclid.converter.Converter.TYPE_SVG, 158 params); 159 Assert.assertNotNull(svgdocdim); 160 final Document svgdoc = svgdocdim.getDocument(); 161 Assert.assertNotNull(svgdoc); 162 Assert.assertTrue("Document is not a SVGDocument", 163 svgdoc instanceof SVGDocument); 164 final Element e = (Element) svgdoc.getFirstChild(); 165 Assert.assertEquals("svg", e.getLocalName()); 166 Assert.assertTrue("SVG Document is empty!", e.getChildNodes() 167 .getLength() > 0); 168 Assert.assertTrue(svgdocdim.getDimension().height > 1); 169 Assert.assertTrue(svgdocdim.getDimension().width > 1); 170 } 171 172 /** 173 * Create and return temp directory. 174 * 175 * @return temp directory. 176 */ 177 public File getOutDir() { 178 final File outDir = new File("temp"); 179 if (!outDir.isDirectory()) { 180 final boolean success = outDir.mkdirs(); 181 Assert 182 .assertTrue( 183 "Failed to create temp directory. Please delete all files / directories named temp", 184 success); 185 } 186 return outDir; 187 } 188 189 /** 190 * Tests the examples at resources/test/exampleX.mml. 191 * 192 * @throws Exception 193 * if an error occurs. 194 */ 195 @Test 196 public void testConvertEmbeddedExamples() throws Exception { 197 final MutableLayoutContext params = new LayoutContextImpl( 198 LayoutContextImpl.getDefaultLayoutContext()); 199 params.setParameter(Parameter.MATHSIZE, 16f); 200 201 for (int example = 1; example <= 10; example++) { 202 final String exName = "example" + example + ".mml"; 203 final File outFile = new File(this.getOutDir(), "example" 204 + example + ".png"); 205 final Document document = MathBaseTest.loadDocument(exName); 206 Converter.getInstance().convert(document, outFile, "image/png", 207 params); 208 Assert.assertTrue(outFile.exists()); 209 Assert.assertTrue(outFile.length() > 0); 210 } 211 } 212 213 /** 214 * Tests if some mime-types are registered properly. 215 * 216 * @throws Exception 217 * if the test fails. 218 */ 219 @Test 220 public void testConverterMimeTypes() throws Exception { 221 Assert 222 .assertTrue(net.sourceforge.jeuclid.converter.Converter.TYPE_SVG 223 .equalsIgnoreCase(ConverterRegistry 224 .getInstance() 225 .getMimeTypeForSuffix( 226 net.sourceforge.jeuclid.converter.Converter.EXTENSION_SVG))); 227 Assert 228 .assertTrue(net.sourceforge.jeuclid.converter.Converter.EXTENSION_SVG 229 .equalsIgnoreCase(ConverterRegistry 230 .getInstance() 231 .getSuffixForMimeType( 232 net.sourceforge.jeuclid.converter.Converter.TYPE_SVG))); 233 234 final String pngType = ConverterRegistry.getInstance() 235 .getMimeTypeForSuffix("png"); 236 Assert.assertTrue("image/png".equalsIgnoreCase(pngType) 237 || "image/x-png".equalsIgnoreCase(pngType)); 238 Assert.assertTrue("png".equalsIgnoreCase(ConverterRegistry 239 .getInstance().getSuffixForMimeType("image/png"))); 240 241 } 242 243 /** 244 * Tests if rendered creates a buffered image with alpha values. 245 * 246 * @throws Exception 247 * if the test fails. 248 */ 249 @Test 250 public void testConverterBufferedImage() throws Exception { 251 final Document doc = MathMLParserSupport 252 .parseString(ConverterTest.TEST1); 253 final MutableLayoutContext params = new LayoutContextImpl( 254 LayoutContextImpl.getDefaultLayoutContext()); 255 params.setParameter(Parameter.MATHSIZE, 25f); 256 final BufferedImage bi = Converter.getInstance().render(doc, params); 257 Assert.assertTrue("Image Created was not wide enough", 258 bi.getWidth() > 10); 259 Assert.assertTrue("Image Created was not tall enough", 260 bi.getHeight() > 10); 261 final int ltpixel = bi.getRGB(0, 0); 262 Assert.assertEquals("Expected Transparent Pixel, got " + ltpixel, 263 ltpixel, 0); 264 } 265 266 /** 267 * Tests if rendered creates a buffered image for an empty document. 268 * 269 * @throws Exception 270 * if the test fails. 271 */ 272 @Test 273 public void testConverterBufferedImageEmptyDoc() throws Exception { 274 final Document doc = new DocumentElement(); 275 276 final MutableLayoutContext params = new LayoutContextImpl( 277 LayoutContextImpl.getDefaultLayoutContext()); 278 final BufferedImage bi = Converter.getInstance().render(doc, params); 279 Assert.assertNotNull(bi); 280 } 281 282 /** 283 * Tests if rendered creates a buffered image without alpha values. 284 * 285 * @throws Exception 286 * if the test fails. 287 */ 288 @Test 289 public void testConverterBufferedImageNoAlpha() throws Exception { 290 final Document doc = MathMLParserSupport 291 .parseString(ConverterTest.TEST1); 292 final MutableLayoutContext params = new LayoutContextImpl( 293 LayoutContextImpl.getDefaultLayoutContext()); 294 params.setParameter(Parameter.MATHSIZE, 25f); 295 final BufferedImage bi = Converter.getInstance().render(doc, params, 296 BufferedImage.TYPE_3BYTE_BGR); 297 Assert.assertTrue("Image Created was not wide enough", 298 bi.getWidth() > 10); 299 Assert.assertTrue("Image Created was not tall enough", 300 bi.getHeight() > 10); 301 final int ltpixel = bi.getRGB(0, 0); 302 final int white = Color.WHITE.getRGB(); 303 Assert.assertEquals("Expected white Pixel (" + white + "), got " 304 + ltpixel, ltpixel, white); 305 } 306 307 }