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.general.huge;
16  
17  import java.util.ArrayList;
18  import java.util.Collection;
19  import java.util.List;
20  import java.util.Set;
21  import java.util.function.Predicate;
22  
23  import org.htmlunit.TestCaseTest;
24  import org.htmlunit.WebDriverTestCase;
25  import org.htmlunit.junit.annotation.Alerts;
26  import org.junit.jupiter.params.ParameterizedTest;
27  import org.junit.jupiter.params.provider.Arguments;
28  import org.junit.jupiter.params.provider.MethodSource;
29  import org.openqa.selenium.WebDriver;
30  
31  /**
32   * Tests two Host classes, if one prototype is parent of another.
33   *
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   */
37  public abstract class HostParentOf extends WebDriverTestCase {
38  
39      private static int ServerRestartCount_ = 0;
40  
41      /**
42       * Returns the parameterized data.
43       * @param predicate the predicate, which determines whether or not to include the parent
44       * @return the parameterized data
45       * @throws Exception if an error occurs
46       */
47      protected static Collection<Arguments> data(final Predicate<String> predicate)  throws Exception {
48          final Set<String> jsClassNames = TestCaseTest.getAllConfiguredJsConstructorNames();
49  
50          final List<Arguments> list = new ArrayList<>(jsClassNames.size() * jsClassNames.size() / 10);
51          for (final String parent : jsClassNames) {
52              if (predicate.test(parent)) {
53                  for (final String child : jsClassNames) {
54                      list.add(Arguments.of(parent, child));
55                  }
56              }
57          }
58          return list;
59      }
60  
61      /**
62       * Runs the test.
63       *
64       * @param parent the parent host name
65       * @param child the child host name
66       * @throws Exception if an error occurs
67       */
68      @ParameterizedTest(name = "_{0}_{1}")
69      @MethodSource("data")
70      @Alerts("false/false")
71      protected void test(final String parent, final String child) throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html>\n"
74              + "<head>\n"
75              + "<title>-</title>\n"
76              + "</head>\n"
77              + "<body>\n"
78              + "<script>\n"
79  
80              + "  function isParentOf(p, c) {\n"
81              + "    detector = function() {};\n"
82              + "    p.prototype.myCustomFunction = detector;\n"
83              + "    return (c.prototype.myCustomFunction === detector) + '/'"
84              + "      + (Object.getPrototypeOf(c) === p);\n"
85              + "  }\n"
86  
87              + "  try {\n"
88              + "    document.title = isParentOf(" + parent + ", " + child + ");\n"
89              + "  } catch(e) { document.title = 'false/false'; }\n"
90              + "</script>\n"
91              + "</body></html>";
92  
93          ServerRestartCount_++;
94          if (ServerRestartCount_ == 200) {
95              stopWebServers();
96              ServerRestartCount_ = 0;
97          }
98          final WebDriver driver = loadPage2(html);
99          assertTitle(driver, getExpectedAlerts()[0]);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     protected boolean isWebClientCached() {
107         return true;
108     }
109 }