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 java.net.URL;
18  
19  import org.htmlunit.WebDriverTestCase;
20  import org.htmlunit.cssparser.dom.CSSCharsetRuleImpl;
21  import org.htmlunit.junit.BrowserRunner;
22  import org.htmlunit.junit.annotation.Alerts;
23  import org.htmlunit.util.MimeType;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  
27  /**
28   * Tests for {@link CSSCharsetRuleImpl}.
29   *
30   * @author Marc Guillemot
31   * @author Ronald Brill
32   * @author Frank Danek
33   */
34  @RunWith(BrowserRunner.class)
35  public class CSSCharsetRuleTest extends WebDriverTestCase {
36  
37      /**
38       * @throws Exception if an error occurs
39       */
40      @Test
41      @Alerts("0")
42      public void inStyle() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html><body>\n"
45  
46              + "<style>@charset \"UTF-8\";</style>\n"
47  
48              + "<script>\n"
49              + LOG_TITLE_FUNCTION
50              + "  var rules = document.styleSheets[0].cssRules;\n"
51              + "  log(rules.length);\n"
52              + "</script>\n"
53  
54              + "</body></html>";
55  
56          loadPageVerifyTitle2(html);
57      }
58  
59      /**
60       * @throws Exception if an error occurs
61       */
62      @Test
63      @Alerts("0")
64      public void inLink() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><body>\n"
67  
68              + "<link rel='stylesheet' href='imp.css'>\n"
69  
70              + "<script>\n"
71              + LOG_TITLE_FUNCTION
72              + "  var rules = document.styleSheets[0].cssRules;\n"
73              + "  log(rules.length);\n"
74              + "</script>\n"
75  
76              + "</body></html>";
77  
78          final String css = "@charset \"UTF-8\";";
79          getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
80  
81          loadPageVerifyTitle2(html);
82      }
83  }