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 StyleMedia}.
23   *
24   * @author Ahmed Ashour
25   * @author Ronald Brill
26   */
27  public class StyleMediaTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts(DEFAULT = {"[object StyleMedia]", "screen"},
34              FF = "undefined",
35              FF_ESR = "undefined")
36      public void type() throws Exception {
37          final String html = DOCTYPE_HTML
38              + "<html><head>\n"
39              + "<script>\n"
40              + LOG_TITLE_FUNCTION
41              + "  function test() {\n"
42              + "    log(window.styleMedia);\n"
43              + "    if (window.styleMedia) {\n"
44              + "      log(window.styleMedia.type);\n"
45              + "    }\n"
46              + "  }\n"
47              + "</script></head><body onload='test()'>\n"
48              + "</body></html>";
49  
50          loadPageVerifyTitle2(html);
51      }
52  
53      /**
54       * @throws Exception if the test fails
55       */
56      @Test
57      @Alerts(DEFAULT = {"true", "true", "true", "false"},
58              FF = {},
59              FF_ESR = {})
60      public void matchMedium() throws Exception {
61          final String html = DOCTYPE_HTML
62              + "<html><head>\n"
63              + "<script>\n"
64              + LOG_TITLE_FUNCTION
65              + "  function test() {\n"
66              + "    if (window.styleMedia) {\n"
67              + "      log(window.styleMedia.matchMedium('screen'));\n"
68              + "      log(window.styleMedia.matchMedium('SCREEN'));\n"
69              + "      log(window.styleMedia.matchMedium('screen, handheld'));\n"
70              + "      log(window.styleMedia.matchMedium('handheld'));\n"
71              + "    }\n"
72              + "  }\n"
73              + "</script></head><body onload='test()'>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts(DEFAULT = {"true", "[object StyleMedia]", "[object StyleMedia]", "undefined", "[object StyleMedia]",
84                         "false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError"},
85              FF = {"false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError",
86                    "false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError"},
87              FF_ESR = {"false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError",
88                        "false", "undefined", "ReferenceError", "ReferenceError", "ReferenceError"})
89      public void windowScope() throws Exception {
90          final String html = DOCTYPE_HTML
91              + "<html></body>\n"
92              + "<script>\n"
93              + LOG_TITLE_FUNCTION
94              + "  log('styleMedia' in window);\n"
95              + "  log(window.styleMedia);\n"
96              + "  try { log(styleMedia); } catch(e) { logEx(e); };\n"
97              + "  try { log(styleMedia.prototype); } catch(e) { logEx(e); };\n"
98              + "  try { log(styleMedia.__proto__); } catch(e) { logEx(e); };\n"
99  
100             + "  log('StyleMedia' in window);\n"
101             + "  log(window.StyleMedia);\n"
102             + "  try { log(StyleMedia); } catch(e) { logEx(e); };\n"
103             + "  try { log(StyleMedia.prototype); } catch(e) { logEx(e); };\n"
104             + "  try { log(StyleMedia.__proto__); } catch(e) { logEx(e); };\n"
105             + "</script>\n"
106             + "</body></html>";
107 
108         loadPageVerifyTitle2(html);
109     }
110 }