View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
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    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.javascript.host.css;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  
24  /**
25   * Tests for {@link ComputedFont}.
26   *
27   * @author Ahmed Ashour
28   * @author Ronald Brill
29   */
30  @RunWith(BrowserRunner.class)
31  public class ComputedFontTest extends WebDriverTestCase {
32  
33      /**
34       * @throws Exception if the test fails
35       */
36      @Test
37      @Alerts({"", "16px", "2em", "32px", "150%", "24px"})
38      public void fontSizeEm() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><head>\n"
41              + "<script>\n"
42              + LOG_TITLE_FUNCTION
43              + "  function test() {\n"
44              + "    var div = document.getElementById('mydiv');\n"
45              + "    var style = window.getComputedStyle(div, null);\n"
46              + "    log(div.style.fontSize);\n"
47              + "    log(style.fontSize);\n"
48              + "    div.style.fontSize = '2em';\n"
49              + "    log(div.style.fontSize);\n"
50              + "    log(style.fontSize);\n"
51              + "    div.style.fontSize = '150%';\n"
52              + "    log(div.style.fontSize);\n"
53              + "    log(style.fontSize);\n"
54              + "  }\n"
55              + "</script>\n"
56              + "</head>\n"
57              + "<body onload='test()'>\n"
58              + "  <div id='mydiv'></div>\n"
59              + "</body></html>";
60          loadPageVerifyTitle2(html);
61      }
62  
63      /**
64       * @throws Exception if the test fails
65       */
66      @Test
67      @Alerts(DEFAULT = {"", "", "", "", "", "", "", "", "", "", "", "", "", "",
68                         "", "16px \"Times New Roman\"",
69                         "", "normal", "", "normal", "", "400", "", "16px", "", "normal", "", "\"Times New Roman\""},
70              FF = {"", "", "", "", "", "", "", "", "", "", "", "", "", "",
71                    "", "16px serif", "", "normal", "", "normal", "", "400",
72                    "", "16px", "", "normal", "", "serif"},
73              FF_ESR = {"", "", "", "", "", "", "", "", "", "", "", "", "", "",
74                        "", "16px serif", "", "normal", "", "normal", "", "400",
75                        "", "16px", "", "normal", "", "serif"})
76      public void fontInitial() throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html><head>\n"
79              + "<script>\n"
80              + LOG_TITLE_FUNCTION
81              + "  function test() {\n"
82              + "    var div = document.createElement('div');\n"
83              + "    debug(div);\n"
84              + "    document.body.appendChild(div);\n"
85              + "    debug(div);\n"
86              + "  }\n"
87              + "  function debug(div) {\n"
88              + "    var style = window.getComputedStyle(div, null);\n"
89              + "    log(div.style.font);\n"
90              + "    log(style.font);\n"
91              + "    log(div.style.fontStyle);\n"
92              + "    log(style.fontStyle);\n"
93              + "    log(div.style.fontVariant);\n"
94              + "    log(style.fontVariant);\n"
95              + "    log(div.style.fontWeight);\n"
96              + "    log(style.fontWeight);\n"
97              + "    log(div.style.fontSize);\n"
98              + "    log(style.fontSize);\n"
99              + "    log(div.style.lineHeight);\n"
100             + "    log(style.lineHeight);\n"
101             + "    log(div.style.fontFamily);\n"
102             + "    log(style.fontFamily);\n"
103             + "  }\n"
104             + "</script>\n"
105             + "</head>\n"
106             + "<body onload='test()'>\n"
107             + "</body></html>";
108         loadPageVerifyTitle2(html);
109     }
110 
111     /**
112      * @throws Exception if the test fails
113      */
114     @Test
115     @Alerts(DEFAULT = {"15px arial, sans-serif", "15px arial, sans-serif",
116                        "normal", "normal",
117                        "oblique 15px arial, sans-serif", "italic 15px arial, sans-serif",
118                        "oblique", "italic"},
119             FF = {"15px arial, sans-serif", "15px arial, sans-serif", "normal", "normal",
120                   "oblique 15px arial, sans-serif", "oblique 15px arial, sans-serif", "oblique", "oblique"},
121             FF_ESR = {"15px arial, sans-serif", "15px arial, sans-serif", "normal", "normal",
122                       "oblique 15px arial, sans-serif", "oblique 15px arial, sans-serif", "oblique", "oblique"})
123     @HtmlUnitNYI(CHROME = {"", "16px \"Times New Roman\"", "", "normal", "",
124                            "16px \"Times New Roman\"", "oblique", "oblique"},
125             EDGE = {"", "16px \"Times New Roman\"", "", "normal", "",
126                     "16px \"Times New Roman\"", "oblique", "oblique"},
127             FF = {"", "16px serif", "", "normal", "", "16px serif", "oblique", "oblique"},
128             FF_ESR = {"", "16px serif", "", "normal", "", "16px serif", "oblique", "oblique"})
129     public void fontStyle() throws Exception {
130         font("15px arial, sans-serif", "fontStyle", "oblique");
131     }
132 
133     private void font(final String fontToSet, final String property, final String value) throws Exception {
134         String html = DOCTYPE_HTML
135             + "<html><head>\n"
136             + "<script>\n"
137             + LOG_TITLE_FUNCTION
138             + "  function test() {\n"
139             + "    var div = document.getElementById('mydiv');\n"
140             + "    div.style.font = '" + fontToSet + "';\n"
141             + "    debug(div);\n";
142 
143         if (value != null) {
144             html += "    div.style." + property + " = '" + value + "';\n"
145                     + "    debug(div);\n";
146         }
147 
148         html += "  }\n"
149             + "  function debug(div) {\n"
150             + "    var style = window.getComputedStyle(div, null);\n"
151             + "    log(div.style.font);\n"
152             + "    log(style.font);\n"
153             + "    log(div.style." + property + ");\n"
154             + "    log(style." + property + ");\n"
155             + "  }\n"
156             + "</script>\n"
157             + "</head>\n"
158             + "<body onload='test()'>\n"
159             + "  <div id='mydiv'></div>\n"
160             + "</body></html>";
161         loadPageVerifyTitle2(html);
162     }
163 
164     /**
165      * @throws Exception if the test fails
166      */
167     @Test
168     @Alerts(DEFAULT = {"", "16px \"Times New Roman\"", "", "\"Times New Roman\""},
169             FF = {"", "16px serif", "", "serif"},
170             FF_ESR = {"", "16px serif", "", "serif"})
171     public void wrongFontFamily() throws Exception {
172         font("xyz", "fontFamily", null);
173     }
174 
175     /**
176      * @throws Exception if the test fails
177      */
178     @Test
179     @Alerts({"1px xyz", "1px xyz", "xyz", "xyz", "1px abc", "1px abc", "abc", "abc"})
180     public void minimalFontFamily() throws Exception {
181         font("1px xyz", "fontFamily", "abc");
182     }
183 
184     /**
185      * @throws Exception if the test fails
186      */
187     @Test
188     @Alerts(DEFAULT = {"", "16px \"Times New Roman\"",
189                        "", "\"Times New Roman\"", "", "16px abc", "abc", "abc"},
190             FF = {"", "16px serif", "", "serif", "", "16px abc", "abc", "abc"},
191             FF_ESR = {"", "16px serif", "", "serif", "", "16px abc", "abc", "abc"})
192     @HtmlUnitNYI(CHROME = {"", "16px \"Times New Roman\"",
193                            "", "\"Times New Roman\"", "", "16px \"Times New Roman\"", "abc", "abc"},
194             EDGE = {"", "16px \"Times New Roman\"",
195                     "", "\"Times New Roman\"", "", "16px \"Times New Roman\"", "abc", "abc"},
196             FF = {"", "16px serif", "", "serif", "", "16px serif", "abc", "abc"},
197             FF_ESR = {"", "16px serif", "", "serif", "", "16px serif", "abc", "abc"})
198     public void minimalFontFamilyReversed() throws Exception {
199         font("xyz 1px", "fontFamily", "abc");
200     }
201 
202     /**
203      * @throws Exception if the test fails
204      */
205     @Test
206     @Alerts({"1px / 2px xyz", "1px / 2px xyz",
207              "2px", "2px", "1px xyz", "1px xyz", "normal", "normal"})
208     public void minimalLineHeight() throws Exception {
209         font("1px/2px xyz", "lineHeight", "normal");
210     }
211 
212     /**
213      * @throws Exception if the test fails
214      */
215     @Test
216     @Alerts({"1px / 2px xyz", "1px / 2px xyz",
217              "2px", "2px", "1px xyz", "1px xyz", "normal", "normal"})
218     @HtmlUnitNYI(CHROME = {"2px xyz", "2px xyz", "", "normal", "2px xyz", "2px xyz", "normal", "normal"},
219             EDGE = {"2px xyz", "2px xyz", "", "normal", "2px xyz", "2px xyz", "normal", "normal"},
220             FF = {"2px xyz", "2px xyz", "", "normal", "2px xyz", "2px xyz", "normal", "normal"},
221             FF_ESR = {"2px xyz", "2px xyz", "", "normal", "2px xyz", "2px xyz", "normal", "normal"})
222     public void minimalLineHeightSpace() throws Exception {
223         font("1px / 2px xyz", "lineHeight", "normal");
224     }
225 
226     /**
227      * @throws Exception if the test fails
228      */
229     @Test
230     @Alerts({"1px / 2px xyz", "1px / 2px xyz",
231              "2px", "2px", "1px xyz", "1px xyz", "normal", "normal"})
232     @HtmlUnitNYI(CHROME = {"2px xyz", "2px xyz",
233                            "", "normal", "2px xyz", "2px xyz", "normal", "normal"},
234             EDGE = {"2px xyz", "2px xyz",
235                     "", "normal", "2px xyz", "2px xyz", "normal", "normal"},
236             FF = {"2px xyz", "2px xyz", "", "normal", "2px xyz", "2px xyz", "normal", "normal"},
237             FF_ESR = {"2px xyz", "2px xyz", "", "normal", "2px xyz", "2px xyz", "normal", "normal"})
238     public void minimalLineHeightSpace2() throws Exception {
239         font("1px/ 2px xyz", "lineHeight", "normal");
240     }
241 
242     /**
243      * @throws Exception if the test fails
244      */
245     @Test
246     @Alerts({"1px / 2px xyz", "1px / 2px xyz",
247              "2px", "2px", "1px xyz", "1px xyz", "normal", "normal"})
248     @HtmlUnitNYI(CHROME = {"", "16px \"Times New Roman\"", "", "normal",
249                            "", "16px \"Times New Roman\"", "normal", "normal"},
250             EDGE = {"", "16px \"Times New Roman\"", "", "normal",
251                     "", "16px \"Times New Roman\"", "normal", "normal"},
252             FF = {"", "16px serif", "", "normal", "", "16px serif", "normal", "normal"},
253             FF_ESR = {"", "16px serif", "", "normal", "", "16px serif", "normal", "normal"})
254     public void minimalLineHeightSpace3() throws Exception {
255         font("1px /2px xyz", "lineHeight", "normal");
256     }
257 
258     /**
259      * @throws Exception if the test fails
260      */
261     @Test
262     @Alerts({"1px / 2px xyz", "1px / 2px xyz",
263              "2px", "2px", "1px xyz", "1px xyz", "normal", "normal"})
264     @HtmlUnitNYI(CHROME = {"", "16px \"Times New Roman\"", "", "normal",
265                            "", "16px \"Times New Roman\"", "normal", "normal"},
266             EDGE = {"", "16px \"Times New Roman\"", "", "normal",
267                     "", "16px \"Times New Roman\"", "normal", "normal"},
268             FF = {"", "16px serif", "", "normal", "", "16px serif", "normal", "normal"},
269             FF_ESR = {"", "16px serif", "", "normal", "", "16px serif", "normal", "normal"})
270     public void minimalLineHeightSpace4() throws Exception {
271         font("1px  /2px xyz", "lineHeight", "normal");
272     }
273 }