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