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.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Tests for {@link CSSFontFaceRule}.
25   *
26   * @author Marc Guillemot
27   * @author Frank Danek
28   */
29  @RunWith(BrowserRunner.class)
30  public class CSSFontFaceRuleTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception if an error occurs
34       */
35      @Test
36      @Alerts("TypeError")
37      public void ctor() throws Exception {
38          final String html = DOCTYPE_HTML
39              + "<html><body>\n"
40              + LOG_TEXTAREA
41              + "<script>\n"
42              + LOG_TEXTAREA_FUNCTION
43              + "try {\n"
44              + "  var rule = new CSSFontFaceRule();\n"
45              + "  log(rule);\n"
46              + "} catch(e) { logEx(e); }\n"
47              + "</script></body></html>";
48  
49          loadPageVerifyTextArea2(html);
50      }
51  
52      /**
53       * @throws Exception if an error occurs
54       */
55      @Test
56      @Alerts({"[object CSSFontFaceRule]", "5",
57               "@font-face { font-family: Delicious; src: url(\"Delicious-Bold.otf\"); }"})
58      public void simple() throws Exception {
59          final String html = DOCTYPE_HTML
60              + "<html><body>\n"
61              + LOG_TEXTAREA
62              + "<style>\n"
63              + "  @font-face { font-family: Delicious; src: url('Delicious-Bold.otf'); }\n"
64              + "  h3 { font-family: Delicious;  }\n"
65              + "</style>\n"
66              + "<script>\n"
67              + LOG_TEXTAREA_FUNCTION
68              + "try {\n"
69              + "  var styleSheet = document.styleSheets[0];\n"
70              + "  var rule = styleSheet.cssRules[0];\n"
71              + "  log(rule);\n"
72              + "  log(rule.type);\n"
73              + "  log(rule.cssText);\n"
74              + "}\n"
75              + "catch(e) { logEx(e); }\n"
76              + "</script></body></html>";
77  
78          loadPageVerifyTextArea2(html);
79      }
80  
81      /**
82       * @throws Exception if an error occurs
83       */
84      @Test
85      @Alerts("@font-face { font-family: Delicious; src: url(\"//:\"); }")
86      public void urlSlashSlashColon() throws Exception {
87          final String html = DOCTYPE_HTML
88              + "<html><body>\n"
89              + LOG_TEXTAREA
90              + "<style>\n"
91              + "  @font-face { font-family: Delicious; src: url(//:); }\n"
92              + "  h3 { font-family: Delicious;  }\n"
93              + "</style>\n"
94              + "<script>\n"
95              + LOG_TEXTAREA_FUNCTION
96              + "try {\n"
97              + "  var styleSheet = document.styleSheets[0];\n"
98              + "  var rule = styleSheet.cssRules[0];\n"
99              + "  log(rule.cssText);\n"
100             + "}\n"
101             + "catch(e) { logEx(e); }\n"
102             + "</script></body></html>";
103 
104         loadPageVerifyTextArea2(html);
105     }
106 
107     /**
108      * @throws Exception if an error occurs
109      */
110     @Test
111     @Alerts("@font-face { font-family: Delicious; src: url(\"/:\"); }")
112     public void urlSlashColon() throws Exception {
113         final String html = DOCTYPE_HTML
114             + "<html><body>\n"
115             + LOG_TEXTAREA
116             + "<style>\n"
117             + "  @font-face { font-family: Delicious; src: url(/:); }\n"
118             + "  h3 { font-family: Delicious;  }\n"
119             + "</style>\n"
120             + "<script>\n"
121             + LOG_TEXTAREA_FUNCTION
122             + "try {\n"
123             + "  var styleSheet = document.styleSheets[0];\n"
124             + "  var rule = styleSheet.cssRules[0];\n"
125             + "  log(rule.cssText);\n"
126             + "}\n"
127             + "catch(e) { logEx(e); }\n"
128             + "</script></body></html>";
129 
130         loadPageVerifyTextArea2(html);
131     }
132 
133     /**
134      * @throws Exception if an error occurs
135      */
136     @Test
137     @Alerts("@font-face { font-family: Delicious; src: url(\"//\"); }")
138     public void urlSlashSlash() throws Exception {
139         final String html = DOCTYPE_HTML
140             + "<html><body>\n"
141             + LOG_TEXTAREA
142             + "<style>\n"
143             + "  @font-face { font-family: Delicious; src: url(//); }\n"
144             + "  h3 { font-family: Delicious;  }\n"
145             + "</style>\n"
146             + "<script>\n"
147             + LOG_TEXTAREA_FUNCTION
148             + "try {\n"
149             + "  var styleSheet = document.styleSheets[0];\n"
150             + "  var rule = styleSheet.cssRules[0];\n"
151             + "  log(rule.cssText);\n"
152             + "}\n"
153             + "catch(e) { logEx(e); }\n"
154             + "</script></body></html>";
155 
156         loadPageVerifyTextArea2(html);
157     }
158 }