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.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link CSSRuleList}.
23   *
24   * @author Ronald Brill
25   * @author Frank Danek
26   * @author Ahmed Ashour
27   */
28  public class CSSRuleListTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if an error occurs
32       */
33      @Test
34      @Alerts("TypeError")
35      public void ctor() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><body>\n"
38              + LOG_TEXTAREA
39              + "<script>\n"
40              + LOG_TEXTAREA_FUNCTION
41              + "try {\n"
42              + "  var rule = new CSSRuleList();\n"
43              + "  log(rule);\n"
44              + "} catch(e) { logEx(e); }\n"
45              + "</script></body></html>";
46  
47          loadPageVerifyTextArea2(html);
48      }
49  
50      /**
51       * @throws Exception on test failure
52       */
53      @Test
54      @Alerts({"1", "[object CSSStyleRule]"})
55      public void ruleList() throws Exception {
56          final String html = DOCTYPE_HTML
57                  + "<html><head>\n"
58                  + "<style>\n"
59                  + "  BODY { font-size: 1234px; }\n"
60                  + "</style>\n"
61                  + "<script>\n"
62                  + LOG_TITLE_FUNCTION
63                  + "  function test() {\n"
64                  + "    var rules = document.styleSheets[0].cssRules;\n"
65                  + "    log(rules.length);\n"
66                  + "    log(rules[0]);\n"
67                  + "  }\n"
68                  + "</script>\n"
69                  + "</head><body onload='test()'>\n"
70                  + "</body></html>";
71          loadPageVerifyTitle2(html);
72      }
73  
74      /**
75       * @throws Exception on test failure
76       */
77      @Test
78      @Alerts("undefined")
79      public void wrongRuleListAccess() throws Exception {
80          final String html = DOCTYPE_HTML
81                  + "<html><head>\n"
82                  + "<style>\n"
83                  + "  BODY { font-size: 1234px; }\n"
84                  + "</style>\n"
85                  + "<script>\n"
86                  + LOG_TITLE_FUNCTION
87                  + "  function test() {\n"
88                  + "    var rules = document.styleSheets[0].cssRules;\n"
89                  + "    var r = rules[1];\n"
90                  + "    log(r);\n"
91                  + "  }\n"
92                  + "</script>\n"
93                  + "</head><body onload='test()'>\n"
94                  + "</body></html>";
95          loadPageVerifyTitle2(html);
96      }
97  
98      /**
99       * @throws Exception on test failure
100      */
101     @Test
102     @Alerts("true")
103     public void has() throws Exception {
104         final String html = DOCTYPE_HTML
105                 + "<html><head>\n"
106                 + "<style>\n"
107                 + "  BODY { font-size: 1234px; }\n"
108                 + "</style>\n"
109                 + "<script>\n"
110                 + LOG_TITLE_FUNCTION
111                 + "  function test() {\n"
112                 + "    var rules = document.styleSheets[0].cssRules;\n"
113                 + "    log(0 in rules);\n"
114                 + "  }\n"
115                 + "</script>\n"
116                 + "</head><body onload='test()'>\n"
117                 + "</body></html>";
118         loadPageVerifyTitle2(html);
119     }
120 
121     /**
122      * @throws Exception on test failure
123      */
124     @Test
125     @Alerts({"0", "undefined"})
126     public void ruleListUnknownAtRule() throws Exception {
127         final String html = DOCTYPE_HTML
128                 + "<html><head>\n"
129                 + "<style>\n"
130                 + "  @UnknownAtRule valo-animate-in-fade {0 {opacity: 0;}}\n"
131                 + "</style>\n"
132                 + "<script>\n"
133                 + LOG_TITLE_FUNCTION
134                 + "  function test() {\n"
135                 + "    var rules = document.styleSheets[0].cssRules;\n"
136                 + "    log(rules.length);\n"
137                 + "    log(rules[0]);\n"
138                 + "  }\n"
139                 + "</script>\n"
140                 + "</head><body onload='test()'>\n"
141                 + "</body></html>";
142         loadPageVerifyTitle2(html);
143     }
144 
145     /**
146      * @throws Exception on test failure
147      */
148     @Test
149     @Alerts({"1", "[object CSSKeyframesRule]"})
150     public void ruleListKeyframes() throws Exception {
151         final String html = DOCTYPE_HTML
152                 + "<html><head>\n"
153                 + "<style>\n"
154                 + "  @keyframes mymove {from {top: 0px;} to {top: 200px;}}\n"
155                 + "</style>\n"
156                 + "<script>\n"
157                 + LOG_TITLE_FUNCTION
158                 + "  function test() {\n"
159                 + "    var rules = document.styleSheets[0].cssRules;\n"
160                 + "    log(rules.length);\n"
161                 + "    log(rules[0]);\n"
162                 + "  }\n"
163                 + "</script>\n"
164                 + "</head><body onload='test()'>\n"
165                 + "</body></html>";
166         loadPageVerifyTitle2(html);
167     }
168 
169     /**
170      * @throws Exception on test failure
171      */
172     @Test
173     @Alerts({"1", "false", "true", "false", "false"})
174     public void in() throws Exception {
175         final String html = DOCTYPE_HTML
176                 + "<html><head>\n"
177                 + "<style>\n"
178                 + "  BODY { font-size: 1234px; }\n"
179                 + "</style>\n"
180                 + "<script>\n"
181                 + LOG_TITLE_FUNCTION
182                 + "  function test() {\n"
183                 + "    var rules = document.styleSheets[0].cssRules;\n"
184                 + "    log(rules.length);\n"
185                 + "    log(-1 in rules);\n"
186                 + "    log(0 in rules);\n"
187                 + "    log(1 in rules);\n"
188                 + "    log(42 in rules);\n"
189                 + "  }\n"
190                 + "</script>\n"
191                 + "</head><body onload='test()'>\n"
192                 + "</body></html>";
193         loadPageVerifyTitle2(html);
194     }
195 
196     /**
197      * @throws Exception on test failure
198      */
199     @Test
200     @Alerts("[object CSSStyleRule]")
201     public void index() throws Exception {
202         final String html = DOCTYPE_HTML
203                 + "<html><head>\n"
204                 + "<style>\n"
205                 + "  BODY { font-size: 1234px; }\n"
206                 + "</style>\n"
207                 + "<script>\n"
208                 + LOG_TITLE_FUNCTION
209                 + "  function test() {\n"
210                 + "    var rules = document.styleSheets[0].cssRules;\n"
211                 + "    log(rules[0]);\n"
212                 + "  }\n"
213                 + "</script>\n"
214                 + "</head><body onload='test()'>\n"
215                 + "</body></html>";
216         loadPageVerifyTitle2(html);
217     }
218 
219     /**
220      * @throws Exception on test failure
221      */
222     @Test
223     @Alerts("undefined")
224     public void indexNotFound() throws Exception {
225         final String html = DOCTYPE_HTML
226                 + "<html><head>\n"
227                 + "<style>\n"
228                 + "  BODY { font-size: 1234px; }\n"
229                 + "</style>\n"
230                 + "<script>\n"
231                 + LOG_TITLE_FUNCTION
232                 + "  function test() {\n"
233                 + "    var rules = document.styleSheets[0].cssRules;\n"
234                 + "    log(rules[17]);\n"
235                 + "  }\n"
236                 + "</script>\n"
237                 + "</head><body onload='test()'>\n"
238                 + "</body></html>";
239         loadPageVerifyTitle2(html);
240     }
241 
242     /**
243      * @throws Exception on test failure
244      */
245     @Test
246     @Alerts("[object CSSStyleRule]")
247     public void item() throws Exception {
248         final String html = DOCTYPE_HTML
249                 + "<html><head>\n"
250                 + "<style>\n"
251                 + "  BODY { font-size: 1234px; }\n"
252                 + "</style>\n"
253                 + "<script>\n"
254                 + LOG_TITLE_FUNCTION
255                 + "  function test() {\n"
256                 + "    var rules = document.styleSheets[0].cssRules;\n"
257                 + "    log(rules.item(0));\n"
258                 + "  }\n"
259                 + "</script>\n"
260                 + "</head><body onload='test()'>\n"
261                 + "</body></html>";
262         loadPageVerifyTitle2(html);
263     }
264 
265     /**
266      * @throws Exception on test failure
267      */
268     @Test
269     @Alerts("null")
270     public void itemNotFound() throws Exception {
271         final String html = DOCTYPE_HTML
272                 + "<html><head>\n"
273                 + "<style>\n"
274                 + "  BODY { font-size: 1234px; }\n"
275                 + "</style>\n"
276                 + "<script>\n"
277                 + LOG_TITLE_FUNCTION
278                 + "  function test() {\n"
279                 + "    var rules = document.styleSheets[0].cssRules;\n"
280                 + "    log(rules.item(17));\n"
281                 + "  }\n"
282                 + "</script>\n"
283                 + "</head><body onload='test()'>\n"
284                 + "</body></html>";
285         loadPageVerifyTitle2(html);
286     }
287 }