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