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.css;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.javascript.host.css.CSSStyleDeclaration;
19  import org.htmlunit.javascript.host.css.ComputedCSSStyleDeclaration;
20  import org.htmlunit.junit.annotation.Alerts;
21  import org.junit.jupiter.api.Test;
22  
23  /**
24   * Tests for {@link StyleAttributes}.
25   *
26   * @author Ahmed Ashour
27   * @author Ronald Brill
28   */
29  public class StyleAttributesIterable2Test extends WebDriverTestCase {
30  
31      /**
32       * Some properties exist only in {@link CSSStyleDeclaration}, not in {@link ComputedCSSStyleDeclaration}.
33       * Seems Rhino checks for prototype, so they will always be available, even for {@link ComputedCSSStyleDeclaration}.
34       *
35       * @throws Exception if an error occurs
36       */
37      @Test
38      @Alerts("done")
39      public void notInComputed() throws Exception {
40          styleVsComputed("pixelBottom");
41      }
42  
43      /**
44       * @throws Exception if an error occurs
45       */
46      @Test
47      @Alerts({"in style", "in computed style", "done"})
48      public void inComputed() throws Exception {
49          styleVsComputed("wordBreak");
50      }
51  
52      private void styleVsComputed(final String property) throws Exception {
53          final String html = DOCTYPE_HTML
54              + "<html><head>\n"
55              + "<script>\n"
56              + LOG_TITLE_FUNCTION
57              + "  function test() {\n"
58              + "    var e = document.getElementById('myDiv');\n"
59              + "    for (var i in e.style) {\n"
60              + "      if (i == '" + property + "') {\n"
61              + "        log('in style');\n"
62              + "      }\n"
63              + "    }\n"
64              + "    for (var i in window.getComputedStyle(e, null)) {\n"
65              + "      if (i == '" + property + "') {\n"
66              + "        log('in computed style');\n"
67              + "      }\n"
68              + "    }\n"
69              + "    log('done');\n"
70              + "  }\n"
71              + "</script></head><body onload='test()'>\n"
72              + "  <div id='myDiv'></div>\n"
73              + "</body></html>";
74  
75          loadPageVerifyTitle2(html);
76      }
77  }