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