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 CSSPageRule}.
24   *
25   * @author Ronald Brill
26   */
27  public class CSSTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if an error occurs
31       */
32      @Test
33      @Alerts({"[object CSS]", "undefined"})
34      public void global() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><body>\n"
37              + "<style>@charset 'UTF-8';</style>\n"
38              + "<script>\n"
39              + LOG_TITLE_FUNCTION
40              + "  try {\n"
41              + "    log(CSS);"
42              + "    log(CSS.prototype);"
43              + "  } catch(e) { logEx(e); }\n"
44              + "</script>\n"
45              + "</body></html>";
46  
47          loadPageVerifyTitle2(html);
48      }
49  
50      /**
51       * @throws Exception if an error occurs
52       */
53      @Test
54      @Alerts("TypeError")
55      public void constructor() throws Exception {
56          final String html = DOCTYPE_HTML
57              + "<html><body>\n"
58              + "<style>@charset 'UTF-8';</style>\n"
59              + "<script>\n"
60              + LOG_TITLE_FUNCTION
61              + "  try {\n"
62              + "    var o = Object.create(CSS.prototype);\n"
63              + "    log(o);"
64              + "  } catch(e) { logEx(e); }\n"
65              + "</script>\n"
66              + "</body></html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if an error occurs
73       */
74      @Test
75      @Alerts({"true", "true", "true"})
76      public void supports() throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html><body>\n"
79              + "<script>\n"
80              + LOG_TITLE_FUNCTION
81              + "  try {\n"
82              + "    log(CSS.supports('display', 'flex'));"
83              + "    log(CSS.supports('display', 'grid'));"
84              + "    log(CSS.supports('color', 'red'));"
85              + "  } catch(e) { logEx(e); }\n"
86              + "</script>\n"
87              + "</body></html>";
88  
89          loadPageVerifyTitle2(html);
90      }
91  
92      /**
93       * @throws Exception if an error occurs
94       */
95      @Test
96      @Alerts({"true", "true"})
97      public void supportsCondition() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html><body>\n"
100             + "<script>\n"
101             + LOG_TITLE_FUNCTION
102             + "  try {\n"
103             + "    log(CSS.supports('display: flex'));"
104             + "    log(CSS.supports('color: red'));"
105             + "  } catch(e) { logEx(e); }\n"
106             + "</script>\n"
107             + "</body></html>";
108 
109         loadPageVerifyTitle2(html);
110     }
111 
112     /**
113      * @throws Exception if an error occurs
114      */
115     @Test
116     @Alerts({"true", "false"})
117     @HtmlUnitNYI(CHROME = {"true", "true"},
118             EDGE = {"true", "true"},
119             FF = {"true", "true"},
120             FF_ESR = {"true", "true"})
121     public void supportsSelector() throws Exception {
122         final String html = DOCTYPE_HTML
123             + "<html><body>\n"
124             + "<script>\n"
125             + LOG_TITLE_FUNCTION
126             + "  try {\n"
127             + "    log(CSS.supports('selector(div)'));"
128             + "    log(CSS.supports('selector(div, span)'));"
129             + "  } catch(e) { logEx(e); }\n"
130             + "</script>\n"
131             + "</body></html>";
132 
133         loadPageVerifyTitle2(html);
134     }
135 }