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 java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.List;
20  
21  import org.htmlunit.BrowserVersion;
22  import org.htmlunit.WebDriverTestCase;
23  import org.htmlunit.css.StyleAttributes.Definition;
24  import org.htmlunit.junit.BrowserParameterizedRunner;
25  import org.htmlunit.junit.BrowserParameterizedRunner.Default;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.junit.runners.Parameterized.Parameter;
29  import org.junit.runners.Parameterized.Parameters;
30  
31  /**
32   * Tests for iterability of CSS style attributes defined in {@link StyleAttributes}.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   */
37  @RunWith(BrowserParameterizedRunner.class)
38  public class StyleAttributesIterableTest extends WebDriverTestCase {
39  
40      private static int ServerRestartCount_ = 0;
41  
42      /**
43       * Returns the parameterized data.
44       * @return the parameterized data
45       * @throws Exception if an error occurs
46       */
47      @Parameters
48      public static Collection<Object[]> data() throws Exception {
49          final List<Object[]> list = new ArrayList<>();
50          for (final Definition definition : StyleAttributes.Definition.values()) {
51              list.add(new Object[] {definition});
52          }
53          return list;
54      }
55  
56      /**
57       * {@inheritDoc}
58       */
59      @Override
60      protected String[] getExpectedAlerts() {
61          if (definition_ == null) {
62              return super.getExpectedAlerts();
63          }
64          final BrowserVersion browserVersion = getBrowserVersion();
65          return new String[] {Boolean.toString(definition_.isAvailable(browserVersion, true))};
66      }
67  
68      /**
69       * The {@link Definition} to test.
70       */
71      @Parameter
72      public Definition definition_;
73  
74      /**
75       * The default test.
76       * @throws Exception if an error occurs
77       */
78      @Test
79      @Default
80      public void test() throws Exception {
81          test(definition_.getPropertyName());
82      }
83  
84      private void test(final String propertyName) throws Exception {
85          ServerRestartCount_++;
86          if (ServerRestartCount_ == 200) {
87              stopWebServers();
88              ServerRestartCount_ = 0;
89          }
90  
91          final String html = DOCTYPE_HTML
92              + "<html><head><script>\n"
93              + LOG_TITLE_FUNCTION
94              + "  function test() {\n"
95              + "    var e = document.getElementById('myDiv');\n"
96              + "    for (var i in e.style) {\n"
97              + "      if (i == '" + propertyName + "') {\n"
98              + "        log('true');\n"
99              + "        return;\n"
100             + "      }\n"
101             + "    }\n"
102             + "    log('false');\n"
103             + "  }\n"
104             + "</script></head>\n"
105             + "<body onload='test()'>\n"
106             + "  <div id='myDiv'></div>\n"
107             + "</body></html>";
108 
109         loadPageVerifyTitle2(html);
110     }
111 }