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