|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright (c) 2005-2019, Carlos Amengual. |
| 4 | +
|
| 5 | + SPDX-License-Identifier: BSD-3-Clause |
| 6 | +
|
| 7 | + Licensed under a BSD-style License. You can find the license here: |
| 8 | + https://css4j.github.io/LICENSE.txt |
| 9 | +
|
| 10 | + */ |
| 11 | + |
| 12 | +package io.sf.carte.doc.style.css.awt; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | + |
| 18 | +import java.awt.Font; |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Locale; |
| 21 | + |
| 22 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 23 | +import javax.xml.parsers.ParserConfigurationException; |
| 24 | + |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.w3c.dom.DOMException; |
| 28 | +import org.w3c.dom.Document; |
| 29 | +import org.w3c.dom.Element; |
| 30 | +import org.w3c.dom.Node; |
| 31 | +import org.w3c.dom.stylesheets.LinkStyle; |
| 32 | + |
| 33 | +import io.sf.carte.doc.agent.HeadlessDeviceFactory; |
| 34 | +import io.sf.carte.doc.style.css.CSSDocument; |
| 35 | +import io.sf.carte.doc.style.css.CSSElement; |
| 36 | +import io.sf.carte.doc.style.css.CSSMediaException; |
| 37 | +import io.sf.carte.doc.style.css.om.AbstractCSSStyleSheet; |
| 38 | +import io.sf.carte.doc.style.css.om.FontFaceRule; |
| 39 | +import io.sf.carte.doc.style.css.om.TestCSSStyleSheetFactory; |
| 40 | + |
| 41 | +public class AWTStyleDatabaseTest { |
| 42 | + |
| 43 | + private CSSDocument cssdoc; |
| 44 | + private Node styleText; |
| 45 | + private AbstractCSSStyleSheet sheet; |
| 46 | + private AWTStyleDatabase styleDb; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setUp() throws DOMException, ParserConfigurationException, CSSMediaException { |
| 50 | + TestCSSStyleSheetFactory factory = new TestCSSStyleSheetFactory(); |
| 51 | + HeadlessDeviceFactory deviceFactory = new HeadlessDeviceFactory(); |
| 52 | + styleDb = new AWTStyleDatabase(); |
| 53 | + deviceFactory.setStyleDatabase("screen", styleDb); |
| 54 | + factory.setDeviceFactory(deviceFactory); |
| 55 | + DocumentBuilderFactory dbFac = DocumentBuilderFactory.newInstance(); |
| 56 | + Document doc = dbFac.newDocumentBuilder().getDOMImplementation().createDocument(null, "html", null); |
| 57 | + Element head = doc.createElement("head"); |
| 58 | + Element style = doc.createElement("style"); |
| 59 | + style.setAttribute("id", "styleId"); |
| 60 | + style.setIdAttribute("id", true); |
| 61 | + style.setAttribute("type", "text/css"); |
| 62 | + style.setAttribute("media", "screen"); |
| 63 | + style.setTextContent(" "); |
| 64 | + doc.getDocumentElement().appendChild(head); |
| 65 | + head.appendChild(style); |
| 66 | + Element body = doc.createElement("body"); |
| 67 | + body.setAttribute("id", "bodyId"); |
| 68 | + body.setIdAttribute("id", true); |
| 69 | + doc.getDocumentElement().appendChild(body); |
| 70 | + cssdoc = factory.createCSSDocument(doc); |
| 71 | + cssdoc.setTargetMedium("screen"); |
| 72 | + CSSElement cssStyle = cssdoc.getElementById("styleId"); |
| 73 | + sheet = (AbstractCSSStyleSheet) ((LinkStyle) cssStyle).getSheet(); |
| 74 | + styleText = cssStyle.getChildNodes().item(0); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testFontFaceRule() throws IOException { |
| 79 | + styleText.setNodeValue( |
| 80 | + "@font-face{font-family:'OpenSans Regular';src:url('http://www.example.com/fonts/OpenSans-Regular.ttf') format('truetype')}"); |
| 81 | + FontFaceRule ffrule = (FontFaceRule) sheet.getCssRules().item(0); |
| 82 | + assertEquals(2, ffrule.getStyle().getLength()); |
| 83 | + assertEquals( |
| 84 | + "@font-face {font-family: 'OpenSans Regular'; src: url('http://www.example.com/fonts/OpenSans-Regular.ttf') format('truetype'); }", |
| 85 | + ffrule.getCssText()); |
| 86 | + assertEquals("url('http://www.example.com/fonts/OpenSans-Regular.ttf') format('truetype')", |
| 87 | + ffrule.getStyle().getPropertyValue("src")); |
| 88 | + // |
| 89 | + CSSElement body = cssdoc.getElementById("bodyId"); |
| 90 | + body.getComputedStyle(null); |
| 91 | + assertTrue(styleDb.isFontFaceName("opensans regular")); |
| 92 | + Font font = styleDb.getFont("opensans regular"); |
| 93 | + assertNotNull(font); |
| 94 | + assertEquals("Open Sans", font.getFamily(Locale.ROOT)); |
| 95 | + assertEquals("Open Sans Regular", font.getFontName(Locale.ROOT)); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments