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