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.junit.BrowserRunner;
21  import org.htmlunit.junit.annotation.Alerts;
22  import org.htmlunit.util.MimeType;
23  import org.junit.Test;
24  import org.junit.runner.RunWith;
25  import org.openqa.selenium.WebDriver;
26  
27  /**
28   * Tests for {@link CSSImportRule}.
29   *
30   * @author Daniel Gredler
31   * @author Marc Guillemot
32   * @author Ronald Brill
33   * @author Guy Burton
34   * @author Frank Danek
35   */
36  @RunWith(BrowserRunner.class)
37  public class CSSImportRuleTest extends WebDriverTestCase {
38  
39      /**
40       * @throws Exception if an error occurs
41       */
42      @Test
43      @Alerts("TypeError")
44      public void ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><body>\n"
47              + LOG_TEXTAREA
48              + "<script>\n"
49              + LOG_TEXTAREA_FUNCTION
50              + "try {\n"
51              + "  var rule = new CSSImportRule();\n"
52              + "  log(rule);\n"
53              + "} catch(e) { logEx(e); }\n"
54              + "</script></body></html>";
55  
56          loadPageVerifyTextArea2(html);
57      }
58  
59      /**
60       * @throws Exception if an error occurs
61       */
62      @Test
63      @Alerts({"[object CSSImportRule]", "[object CSSImportRule]"})
64      public void scriptableToString() throws Exception {
65          final String html = DOCTYPE_HTML
66              + "<html><body>\n"
67  
68              + "<style>\n"
69              + "  @import 'imp.css';\n"
70              + "</style>\n"
71  
72              + "<script>\n"
73              + LOG_TITLE_FUNCTION
74              + "  var styleSheet = document.styleSheets[0];\n"
75              + "  var rule = styleSheet.cssRules[0];\n"
76              + "  log(Object.prototype.toString.call(rule));\n"
77              + "  log(rule);\n"
78              + "</script>\n"
79  
80              + "</body></html>";
81  
82          final String css = "#d { color: green }";
83          getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
84  
85          loadPageVerifyTitle2(html);
86      }
87  
88      /**
89       * @throws Exception if an error occurs
90       */
91      @Test
92      @Alerts("@import url(\"imp.css\");")
93      public void cssText() throws Exception {
94          final String html = DOCTYPE_HTML
95              + "<html><body>\n"
96  
97              + "<style>\n"
98              + "  @import 'imp.css';\n"
99              + "</style>\n"
100 
101             + "<script>\n"
102             + LOG_TITLE_FUNCTION
103             + "  var styleSheet = document.styleSheets[0];\n"
104             + "  var rule = styleSheet.cssRules[0];\n"
105             + "  log(rule.cssText);\n"
106             + "</script>\n"
107 
108             + "</body></html>";
109 
110         final String css = "#d { color: green }";
111         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
112 
113         loadPageVerifyTitle2(html);
114     }
115 
116     /**
117      * @throws Exception if an error occurs
118      */
119     @Test
120     @Alerts("@import url(\"imp.css\");")
121     public void cssTextSet() throws Exception {
122         final String html = DOCTYPE_HTML
123             + "<html><body>\n"
124 
125             + "<style>\n"
126             + "  @import 'imp.css';\n"
127             + "</style>\n"
128 
129             + "<script>\n"
130             + LOG_TITLE_FUNCTION
131             + "  var styleSheet = document.styleSheets[0];\n"
132             + "  var rule = styleSheet.cssRules[0];\n"
133             + "  try {"
134             + "    rule.cssText = '@import \"imp2.css\";';\n"
135             + "    log(rule.cssText);\n"
136             + "  } catch(e) {\n"
137             + "    logEx(e);\n"
138             + "  }\n"
139             + "</script>\n"
140 
141             + "</body></html>";
142 
143         final String css = "#d { color: green }";
144         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
145 
146         loadPageVerifyTitle2(html);
147     }
148 
149     /**
150      * @throws Exception if an error occurs
151      */
152     @Test
153     @Alerts("null")
154     public void parentRule() throws Exception {
155         final String html = DOCTYPE_HTML
156             + "<html><body>\n"
157 
158             + "<style>\n"
159             + "  @import 'imp.css';\n"
160             + "</style>\n"
161 
162             + "<script>\n"
163             + LOG_TITLE_FUNCTION
164             + "  var styleSheet = document.styleSheets[0];\n"
165             + "  var rule = styleSheet.cssRules[0];\n"
166             + "  log(rule.parentRule);\n"
167             + "</script>\n"
168 
169             + "</body></html>";
170 
171         final String css = "#d { color: green }";
172         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
173 
174         loadPageVerifyTitle2(html);
175     }
176 
177     /**
178      * @throws Exception if an error occurs
179      */
180     @Test
181     @Alerts("null")
182     public void parentRuleSet() throws Exception {
183         final String html = DOCTYPE_HTML
184             + "<html><body>\n"
185 
186             + "<style>\n"
187             + "  @import 'imp.css';\n"
188             + "</style>\n"
189 
190             + "<script>\n"
191             + LOG_TITLE_FUNCTION
192             + "  var styleSheet = document.styleSheets[0];\n"
193             + "  var rule = styleSheet.cssRules[0];\n"
194             + "  try {"
195             + "    rule.parentRule = rule;\n"
196             + "    log(rule.parentRule);\n"
197             + "  } catch(e) {\n"
198             + "    logEx(e);\n"
199             + "  }\n"
200             + "</script>\n"
201 
202             + "</body></html>";
203 
204         final String css = "#d { color: green }";
205         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
206 
207         loadPageVerifyTitle2(html);
208     }
209 
210     /**
211      * @throws Exception if an error occurs
212      */
213     @Test
214     @Alerts("[object CSSStyleSheet]")
215     public void parentStyleSheet() throws Exception {
216         final String html = DOCTYPE_HTML
217             + "<html><body>\n"
218 
219             + "<style>\n"
220             + "  @import 'imp.css';\n"
221             + "</style>\n"
222 
223             + "<script>\n"
224             + LOG_TITLE_FUNCTION
225             + "  var styleSheet = document.styleSheets[0];\n"
226             + "  var rule = styleSheet.cssRules[0];\n"
227             + "  log(rule.parentStyleSheet);\n"
228             + "</script>\n"
229 
230             + "</body></html>";
231 
232         final String css = "#d { color: green }";
233         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
234 
235         loadPageVerifyTitle2(html);
236     }
237 
238     /**
239      * @throws Exception if an error occurs
240      */
241     @Test
242     @Alerts("[object CSSStyleSheet]")
243     public void parentStyleSheetSet() throws Exception {
244         final String html = DOCTYPE_HTML
245             + "<html><body>\n"
246 
247             + "<style>\n"
248             + "  @media screen { p { background-color:#FFFFFF; }};\n"
249             + "</style>\n"
250 
251             + "<script>\n"
252             + LOG_TITLE_FUNCTION
253             + "  var styleSheet = document.styleSheets[0];\n"
254             + "  var rule = styleSheet.cssRules[0];\n"
255             + "  try {"
256             + "    rule.parentStyleSheet = null;\n"
257             + "    log(rule.parentStyleSheet);\n"
258             + "  } catch(e) {\n"
259             + "    logEx(e);\n"
260             + "  }\n"
261             + "</script>\n"
262 
263             + "</body></html>";
264 
265         loadPageVerifyTitle2(html);
266     }
267 
268     /**
269      * @throws Exception if an error occurs
270      */
271     @Test
272     @Alerts({"imp.css", "@import url(\"imp.css\");"})
273     public void hrefSimpleRelative() throws Exception {
274         final String html = DOCTYPE_HTML
275             + "<html><body>\n"
276 
277             + "<style>\n"
278             + "  @import  'imp.css';\n"
279             + "</style>\n"
280 
281             + "<script>\n"
282             + LOG_TITLE_FUNCTION
283             + "  var styleSheet = document.styleSheets[0];\n"
284             + "  var rule = styleSheet.cssRules[0];\n"
285             + "  log(rule.href);\n"
286             + "  log(rule.cssText);\n"
287             + "</script>\n"
288 
289             + "</body></html>";
290 
291         final String css = "#d { color: green }";
292         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
293 
294         loadPageVerifyTitle2(html);
295     }
296 
297     /**
298      * @throws Exception if an error occurs
299      */
300     @Test
301     @Alerts({"§§URL§§imp.css", "@import url(\"§§URL§§imp.css\");"})
302     public void hrefSimpleAbsolute() throws Exception {
303         final String html = DOCTYPE_HTML
304             + "<html><body>\n"
305 
306             + "<style>\n"
307             + "  @import  '" + new URL(URL_FIRST, "imp.css").toExternalForm() + "';\n"
308             + "</style>\n"
309 
310             + "<script>\n"
311             + LOG_TITLE_FUNCTION
312             + "  var styleSheet = document.styleSheets[0];\n"
313             + "  var rule = styleSheet.cssRules[0];\n"
314             + "  log(rule.href);\n"
315             + "  log(rule.cssText);\n"
316             + "</script>\n"
317 
318             + "</body></html>";
319 
320         final String css = "#d { color: green }";
321         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
322 
323         expandExpectedAlertsVariables(URL_FIRST);
324         loadPageVerifyTitle2(html);
325     }
326 
327     /**
328      * @throws Exception if an error occurs
329      */
330     @Test
331     @Alerts({"imp.css", "@import url(\"imp.css\");"})
332     public void hrefUrlRelative() throws Exception {
333         final String html = DOCTYPE_HTML
334             + "<html><body>\n"
335 
336             + "<style>\n"
337             + "  @import url( 'imp.css' );\n"
338             + "</style>\n"
339 
340             + "<script>\n"
341             + LOG_TITLE_FUNCTION
342             + "  var styleSheet = document.styleSheets[0];\n"
343             + "  var rule = styleSheet.cssRules[0];\n"
344             + "  log(rule.href);\n"
345             + "  log(rule.cssText);\n"
346             + "</script>\n"
347 
348             + "</body></html>";
349 
350         final String css = "#d { color: green }";
351         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
352 
353         loadPageVerifyTitle2(html);
354     }
355 
356     /**
357      * @throws Exception if an error occurs
358      */
359     @Test
360     @Alerts({"§§URL§§imp.css", "@import url(\"§§URL§§imp.css\");"})
361     public void hrefUrlAbsolute() throws Exception {
362         final String html = DOCTYPE_HTML
363             + "<html><body>\n"
364 
365             + "<style>\n"
366             + "  @import  url('" + new URL(URL_FIRST, "imp.css").toExternalForm() + "');\n"
367             + "</style>\n"
368 
369             + "<script>\n"
370             + LOG_TITLE_FUNCTION
371             + "  var styleSheet = document.styleSheets[0];\n"
372             + "  var rule = styleSheet.cssRules[0];\n"
373             + "  log(rule.href);\n"
374             + "  log(rule.cssText);\n"
375             + "</script>\n"
376 
377             + "</body></html>";
378 
379         final String css = "#d { color: green }";
380         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
381 
382         expandExpectedAlertsVariables(URL_FIRST);
383         loadPageVerifyTitle2(html);
384     }
385 
386     /**
387      * @throws Exception if an error occurs
388      */
389     @Test
390     @Alerts({"[object MediaList]", "", "0", "", "@import url(\"imp.css\");"})
391     public void mediaNone() throws Exception {
392         final String html = DOCTYPE_HTML
393             + "<html><body>\n"
394 
395             + "<style>\n"
396             + "  @import 'imp.css';\n"
397             + "</style>\n"
398 
399             + "<script>\n"
400             + LOG_TITLE_FUNCTION
401             + "  var styleSheet = document.styleSheets[0];\n"
402             + "  var rule = styleSheet.cssRules[0];\n"
403             + "  var mediaList = rule.media;\n"
404             + "  log(Object.prototype.toString.call(mediaList));\n"
405             + "  log(mediaList);\n"
406             + "  log(mediaList.length);\n"
407             + "  for (var i = 0; i < mediaList.length; i++) {\n"
408             + "    log(mediaList.item(i));\n"
409             + "  }\n"
410             + "  log(mediaList.mediaText);\n"
411             + "  log(rule.cssText);\n"
412             + "</script>\n"
413 
414             + "</body></html>";
415 
416         final String css = "#d { color: green }";
417         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
418 
419         loadPageVerifyTitle2(html);
420     }
421 
422     /**
423      * @throws Exception if an error occurs
424      */
425     @Test
426     @Alerts({"[object MediaList]", "all", "1", "all", "all", "@import url(\"imp.css\") all;"})
427     public void mediaAll() throws Exception {
428         final String html = DOCTYPE_HTML
429             + "<html><body>\n"
430 
431             + "<style>\n"
432             + "  @import 'imp.css' all;\n"
433             + "</style>\n"
434 
435             + "<script>\n"
436             + LOG_TITLE_FUNCTION
437             + "  var styleSheet = document.styleSheets[0];\n"
438             + "  var rule = styleSheet.cssRules[0];\n"
439             + "  var mediaList = rule.media;\n"
440             + "  log(Object.prototype.toString.call(mediaList));\n"
441             + "  log(mediaList);\n"
442             + "  log(mediaList.length);\n"
443             + "  for (var i = 0; i < mediaList.length; i++) {\n"
444             + "    log(mediaList.item(i));\n"
445             + "  }\n"
446             + "  log(mediaList.mediaText);\n"
447             + "  log(rule.cssText);\n"
448             + "</script>\n"
449 
450             + "</body></html>";
451 
452         final String css = "#d { color: green }";
453         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
454 
455         loadPageVerifyTitle2(html);
456     }
457 
458     /**
459      * @throws Exception if an error occurs
460      */
461     @Test
462     @Alerts({"[object MediaList]", "screen", "1", "screen", "screen", "@import url(\"imp.css\") screen;"})
463     public void media() throws Exception {
464         final String html = DOCTYPE_HTML
465             + "<html><body>\n"
466 
467             + "<style>\n"
468             + "  @import 'imp.css' screen;\n"
469             + "</style>\n"
470 
471             + "<script>\n"
472             + LOG_TITLE_FUNCTION
473             + "  var styleSheet = document.styleSheets[0];\n"
474             + "  var rule = styleSheet.cssRules[0];\n"
475             + "  var mediaList = rule.media;\n"
476             + "  log(Object.prototype.toString.call(mediaList));\n"
477             + "  log(mediaList);\n"
478             + "  log(mediaList.length);\n"
479             + "  for (var i = 0; i < mediaList.length; i++) {\n"
480             + "    log(mediaList.item(i));\n"
481             + "  }\n"
482             + "  log(mediaList.mediaText);\n"
483             + "  log(rule.cssText);\n"
484             + "</script>\n"
485 
486             + "</body></html>";
487 
488         final String css = "#d { color: green }";
489         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
490 
491         loadPageVerifyTitle2(html);
492     }
493 
494     /**
495      * @throws Exception if an error occurs
496      */
497     @Test
498     @Alerts({"2", "only screen and (color)", "print and (max-width: 12cm) and (min-width: 30em)",
499              "only screen and (color), print and (max-width: 12cm) and (min-width: 30em)",
500              "@import url(\"imp.css\") only screen and (color), "
501                                + "print and (max-width: 12cm) and (min-width: 30em);"})
502     public void mediaQuery() throws Exception {
503         final String html = DOCTYPE_HTML
504             + "<html><body>\n"
505 
506             + "<style>\n"
507             + "  @import 'imp.css' only screen and  (color ),print and ( max-width:12cm) and (min-width: 30em);\n"
508             + "</style>\n"
509 
510             + "<script>\n"
511             + LOG_TITLE_FUNCTION
512             + "  var styleSheet = document.styleSheets[0];\n"
513             + "  var rule = styleSheet.cssRules[0];\n"
514             + "  var mediaList = rule.media;\n"
515             + "  log(mediaList.length);\n"
516             + "  for (var i = 0; i < mediaList.length; i++) {\n"
517             + "    log(mediaList.item(i));\n"
518             + "  }\n"
519             + "  log(mediaList.mediaText);\n"
520             + "  log(rule.cssText);\n"
521             + "</script>\n"
522 
523             + "</body></html>";
524 
525         final String css = "#d { color: green }";
526         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
527 
528         loadPageVerifyTitle2(html);
529     }
530 
531     /**
532      * @throws Exception if an error occurs
533      */
534     @Test
535     @Alerts({"[object HTMLStyleElement]", "[object CSSStyleSheet]", "§§URL§§imp.css",
536              "null", "div { color: green; }"})
537     public void styleSheet() throws Exception {
538         final String html = DOCTYPE_HTML
539             + "<html><body>\n"
540 
541             + "<style>\n"
542             + "  @import 'imp.css';\n"
543             + "</style>\n"
544 
545             + "<script>\n"
546             + LOG_TITLE_FUNCTION
547             + "  var styleSheet = document.styleSheets[0];\n"
548             + "  log(styleSheet.ownerNode);\n"
549             + "  var rule = styleSheet.cssRules[0];\n"
550             + "  log(rule.styleSheet);\n"
551             + "  log(rule.styleSheet.href);\n"
552             + "  log(rule.styleSheet.ownerNode);\n"
553             + "  log(rule.styleSheet.cssRules[0].cssText);\n"
554             + "</script>\n"
555 
556             + "</body></html>";
557 
558         final String css = "div { color: green }";
559         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
560 
561         expandExpectedAlertsVariables(URL_FIRST);
562         loadPageVerifyTitle2(html);
563     }
564 
565     /**
566      * @throws Exception if an error occurs
567      */
568     @Test
569     @Alerts({"[object HTMLStyleElement]", "[object CSSStyleSheet]", "§§URL§§imp.css",
570              "[object CSSRuleList]", "null", "0"})
571     public void styleSheetNotAvailable() throws Exception {
572         final String html = DOCTYPE_HTML
573             + "<html><body>\n"
574 
575             + "<style>\n"
576             + "  @import 'imp.css';\n"
577             + "</style>\n"
578 
579             + "<script>\n"
580             + LOG_TITLE_FUNCTION
581             + "  var styleSheet = document.styleSheets[0];\n"
582             + "  log(styleSheet.ownerNode);\n"
583             + "  var rule = styleSheet.cssRules[0];\n"
584             + "  log(rule.styleSheet);\n"
585             + "  log(rule.styleSheet.href);\n"
586             + "  log(rule.styleSheet.cssRules);\n"
587             + "  log(rule.styleSheet.ownerNode);\n"
588             + "  log(rule.styleSheet.cssRules.length);\n"
589             + "</script>\n"
590 
591             + "</body></html>";
592 
593         expandExpectedAlertsVariables(URL_FIRST);
594         loadPageVerifyTitle2(html);
595     }
596 
597     /**
598      * @throws Exception if an error occurs
599      */
600     @Test
601     @Alerts({"[object CSSStyleSheet]", "§§URL§§imp.css", "div { color: green; }"})
602     public void styleSheetMediaNotMatching() throws Exception {
603         final String html = DOCTYPE_HTML
604             + "<html><body>\n"
605 
606             + "<style>\n"
607             + "  @import 'imp.css' print;\n"
608             + "</style>\n"
609 
610             + "<script>\n"
611             + LOG_TITLE_FUNCTION
612             + "  var styleSheet = document.styleSheets[0];\n"
613             + "  var rule = styleSheet.cssRules[0];\n"
614             + "  log(rule.styleSheet);\n"
615             + "  log(rule.styleSheet.href);\n"
616             + "  log(rule.styleSheet.cssRules[0].cssText);\n"
617             + "</script>\n"
618 
619             + "</body></html>";
620 
621         final String css = "div { color: green }";
622         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
623 
624         expandExpectedAlertsVariables(URL_FIRST);
625         loadPageVerifyTitle2(html);
626     }
627 
628 
629     /**
630      * @throws Exception if an error occurs
631      */
632     @Test
633     @Alerts("true")
634     public void styleSheetSameObject() throws Exception {
635         final String html = DOCTYPE_HTML
636             + "<html><body>\n"
637 
638             + "<style>\n"
639             + "  @import 'imp.css' print;\n"
640             + "</style>\n"
641 
642             + "<script>\n"
643             + LOG_TITLE_FUNCTION
644             + "  var styleSheet = document.styleSheets[0];\n"
645             + "  var rule = styleSheet.cssRules[0];\n"
646             + "  var sheet = rule.styleSheet;\n"
647             + "  log(rule.styleSheet === sheet);\n"
648             + "</script>\n"
649 
650             + "</body></html>";
651 
652         final String css = "div { color: green }";
653         getMockWebConnection().setResponse(new URL(URL_FIRST, "imp.css"), css, MimeType.TEXT_CSS);
654 
655         expandExpectedAlertsVariables(URL_FIRST);
656         loadPageVerifyTitle2(html);
657     }
658 
659     /**
660      * Regression test for Bug #789.
661      * @throws Exception if an error occurs
662      */
663     @Test
664     @Alerts({"[object CSSImportRule]", "§§URL§§second/", "", "0", "[object CSSStyleSheet]"})
665     public void getImportFromCssRulesCollection_absolute() throws Exception {
666         expandExpectedAlertsVariables(URL_FIRST);
667         getImportFromCssRulesCollection(URL_FIRST, URL_SECOND.toExternalForm(), URL_SECOND);
668     }
669 
670     /**
671      * Regression test for Bug #789.
672      * @throws Exception if an error occurs
673      */
674     @Test
675     @Alerts({"[object CSSImportRule]", "foo.css", "", "0", "[object CSSStyleSheet]"})
676     public void getImportFromCssRulesCollection_relative() throws Exception {
677         final URL urlPage = new URL(URL_FIRST, "/dir1/dir2/foo.html");
678         final URL urlCss = new URL(URL_FIRST, "/dir1/dir2/foo.css");
679         getImportFromCssRulesCollection(urlPage, "foo.css", urlCss);
680     }
681 
682     private void getImportFromCssRulesCollection(final URL pageUrl, final String cssRef, final URL cssUrl)
683         throws Exception {
684         final String html = DOCTYPE_HTML
685             + "<html><body>\n"
686             + "<style>@import url('" + cssRef + "');</style><div id='d'>foo</div>\n"
687             + "<script>\n"
688             + LOG_TITLE_FUNCTION
689             + "  var item = document.styleSheets.item(0);\n"
690             + "  if (item.cssRules) {\n"
691             + "    var r = item.cssRules[0];\n"
692             + "    log(r);\n"
693             + "    log(r.href);\n"
694             + "    log(r.media);\n"
695             + "    log(r.media.length);\n"
696             + "    log(r.styleSheet);\n"
697             + "  } else {\n"
698             + "    log('cssRules undefined');\n"
699             + "  }\n"
700             + "</script>\n"
701             + "</body></html>";
702         final String css = "#d { color: green }";
703 
704         getMockWebConnection().setResponse(cssUrl, css, MimeType.TEXT_CSS);
705 
706         final WebDriver driver = loadPage2(html, pageUrl);
707         verifyTitle2(driver, getExpectedAlerts());
708     }
709 
710     /**
711      * @throws Exception if an error occurs
712      */
713     @Test
714     @Alerts("true")
715     public void importedStylesheetsLoaded() throws Exception {
716         final String html = DOCTYPE_HTML
717             + "<html><body>\n"
718             + "<style>@import url('" + URL_SECOND + "');</style>\n"
719             + "<div id='d'>foo</div>\n"
720             + "<script>\n"
721             + LOG_TITLE_FUNCTION
722             + "var d = document.getElementById('d');\n"
723             + "var s = window.getComputedStyle(d, null);\n"
724             + "log(s.color.indexOf('128') > 0);\n"
725             + "</script>\n"
726             + "</body></html>";
727         final String css = "#d { color: rgb(0, 128, 0); }";
728 
729         getMockWebConnection().setResponse(URL_SECOND, css, MimeType.TEXT_CSS);
730 
731         loadPageVerifyTitle2(html);
732     }
733 
734     /**
735      * @throws Exception if an error occurs
736      */
737     @Test
738     @Alerts("true")
739     public void importedStylesheetsURLResolution() throws Exception {
740         final String html = DOCTYPE_HTML
741             + "<html><head>\n"
742             + "<link rel='stylesheet' type='text/css' href='dir1/dir2/file1.css'></link>\n"
743             + "<body>\n"
744             + "<div id='d'>foo</div>\n"
745             + "<script>\n"
746             + LOG_TITLE_FUNCTION
747             + "var d = document.getElementById('d');\n"
748             + "var s = window.getComputedStyle(d, null);\n"
749             + "log(s.color.indexOf('128') > 0);\n"
750             + "</script>\n"
751             + "</body></html>";
752         final String css1 = "@import url('file2.css');";
753         final String css2 = "#d { color: rgb(0, 128, 0); }";
754 
755         final URL urlPage = URL_FIRST;
756         final URL urlCss1 = new URL(urlPage, "dir1/dir2/file1.css");
757         final URL urlCss2 = new URL(urlPage, "dir1/dir2/file2.css");
758         getMockWebConnection().setResponse(urlCss1, css1, MimeType.TEXT_CSS);
759         getMockWebConnection().setResponse(urlCss2, css2, MimeType.TEXT_CSS);
760 
761         final WebDriver driver = loadPage2(html, urlPage);
762         verifyTitle2(driver, getExpectedAlerts());
763     }
764 
765     /**
766      * @throws Exception if an error occurs
767      */
768     @Test
769     @Alerts("true")
770     public void circularImportedStylesheets() throws Exception {
771         final String html = DOCTYPE_HTML
772             + "<html><head>\n"
773             + "<link rel='stylesheet' type='text/css' href='dir1/dir2/file1.css'></link>\n"
774             + "<body>\n"
775             + "<div id='d'>foo</div>\n"
776             + "<script>\n"
777             + LOG_TITLE_FUNCTION
778             + "  var d = document.getElementById('d');\n"
779             + "  var s = window.getComputedStyle(d, null);\n"
780             + "  log(s.color.indexOf('128') > 0);\n"
781             + "</script>\n"
782             + "</body></html>";
783 
784         final String css1 = "@import url('file2.css');";
785         final String css2 = "@import url('file1.css');\n"
786             + "#d { color: rgb(0, 128, 0); }";
787 
788         final URL urlPage = URL_FIRST;
789         final URL urlCss1 = new URL(urlPage, "dir1/dir2/file1.css");
790         final URL urlCss2 = new URL(urlPage, "dir1/dir2/file2.css");
791         getMockWebConnection().setResponse(urlCss1, css1, MimeType.TEXT_CSS);
792         getMockWebConnection().setResponse(urlCss2, css2, MimeType.TEXT_CSS);
793 
794         final WebDriver driver = loadPage2(html, urlPage);
795         verifyTitle2(driver, getExpectedAlerts());
796     }
797 
798     /**
799      * @throws Exception if an error occurs
800      */
801     @Test
802     @Alerts({"true", "true", "true"})
803     public void circularImportedStylesheetsComplexCase() throws Exception {
804         final String html = DOCTYPE_HTML
805             + "<html><head>\n"
806             + "<link rel='stylesheet' type='text/css' href='dir1/dir2/file1.css'></link>\n"
807             + "<body>\n"
808             + "<div id='d'>foo</div>\n"
809             + "<div id='e'>foo</div>\n"
810             + "<div id='f'>foo</div>\n"
811             + "<script>\n"
812             + LOG_TITLE_FUNCTION
813             + "var d = document.getElementById('d');\n"
814             + "var s = window.getComputedStyle(d, null);\n"
815             + "log(s.color.indexOf('128') > 0);\n"
816             + "var e = document.getElementById('e');\n"
817             + "s = window.getComputedStyle(e, null);\n"
818             + "log(s.color.indexOf('127') > 0);\n"
819             + "var f = document.getElementById('f');\n"
820             + "s = window.getComputedStyle(f, null);\n"
821             + "log(s.color.indexOf('126') > 0);\n"
822             + "</script>\n"
823             + "</body></html>";
824         final String css1 = "@import url('file2.css');";
825         final String css2 = "@import url('file3.css');\n"
826             + "@import url('file4.css');";
827         final String css3 = "#d { color: rgb(0, 128, 0); }";
828         final String css4 = "@import url('file5.css');\n"
829             + "#e { color: rgb(0, 127, 0); }";
830         final String css5 = "@import url('file2.css');\n"
831             + "#f { color: rgb(0, 126, 0); }";
832 
833         final URL urlPage = URL_FIRST;
834         final URL urlCss1 = new URL(urlPage, "dir1/dir2/file1.css");
835         final URL urlCss2 = new URL(urlPage, "dir1/dir2/file2.css");
836         final URL urlCss3 = new URL(urlPage, "dir1/dir2/file3.css");
837         final URL urlCss4 = new URL(urlPage, "dir1/dir2/file4.css");
838         final URL urlCss5 = new URL(urlPage, "dir1/dir2/file5.css");
839         getMockWebConnection().setResponse(urlCss1, css1, MimeType.TEXT_CSS);
840         getMockWebConnection().setResponse(urlCss2, css2, MimeType.TEXT_CSS);
841         getMockWebConnection().setResponse(urlCss3, css3, MimeType.TEXT_CSS);
842         getMockWebConnection().setResponse(urlCss4, css4, MimeType.TEXT_CSS);
843         getMockWebConnection().setResponse(urlCss5, css5, MimeType.TEXT_CSS);
844 
845         final WebDriver driver = loadPage2(html, urlPage);
846         verifyTitle2(driver, getExpectedAlerts());
847     }
848 
849     /**
850      * Test that media specific imports work correctly.
851      * Should import the first stylesheet and not the second
852      * @throws Exception if an error occurs
853      */
854     @Test
855     @Alerts("42px")
856     public void importedStylesheetsLoadedAccordingToMediaType() throws Exception {
857         final String html = DOCTYPE_HTML
858             + "<html><head>\n"
859             + "  <style>\n"
860             + "    @import url('" + URL_SECOND  + "');\n"
861             + "    @import url('" + URL_THIRD + "') print;\n"
862             + "  </style>\n"
863             + "</head>\n"
864 
865             + "<body>\n"
866             + "  <div id='d'>foo</div>\n"
867             + "  <script>\n"
868             + LOG_TITLE_FUNCTION
869             + "    var d = document.getElementById('d');\n"
870             + "    var s = window.getComputedStyle(d, null);\n"
871             + "    log(s.fontSize);\n"
872             + "</script>\n"
873             + "</body></html>";
874         final String screenCss = "#d { font-size: 42px; }";
875         final String printCss  = "#d { font-size: 13px; }";
876 
877         getMockWebConnection().setResponse(URL_SECOND, screenCss, MimeType.TEXT_CSS);
878         getMockWebConnection().setResponse(URL_THIRD, printCss, MimeType.TEXT_CSS);
879 
880         loadPageVerifyTitle2(html);
881     }
882 }