View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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  
52          int i = 0;
53          for (final String parent : jsClassNames) {
54              if (predicate.test(parent)) {
55                  for (final String child : jsClassNames) {
56                      list.add(Arguments.of(parent, child));
57                  }
58                  if (++i > 100) {
59                      return list;
60                  }
61              }
62          }
63          return list;
64      }
65  
66      /**
67       * Runs the test.
68       *
69       * @param parent the parent host name
70       * @param child the child host name
71       * @throws Exception if an error occurs
72       */
73      @ParameterizedTest(name = "_{0}_{1}", quoteTextArguments = false)
74      @MethodSource("data")
75      @Alerts("false/false")
76      protected void test(final String parent, final String child) throws Exception {
77          final String html = DOCTYPE_HTML
78              + "<html>\n"
79              + "<head>\n"
80              + "<title>-</title>\n"
81              + "</head>\n"
82              + "<body>\n"
83              + "<script>\n"
84  
85              + "  function isParentOf(p, c) {\n"
86              + "    detector = function() {};\n"
87              + "    p.prototype.myCustomFunction = detector;\n"
88              + "    return (c.prototype.myCustomFunction === detector) + '/'"
89              + "      + (Object.getPrototypeOf(c) === p);\n"
90              + "  }\n"
91  
92              + "  try {\n"
93              + "    document.title = isParentOf(" + parent + ", " + child + ");\n"
94              + "  } catch(e) { document.title = 'false/false'; }\n"
95              + "</script>\n"
96              + "</body></html>";
97  
98          ServerRestartCount_++;
99          if (ServerRestartCount_ == 200) {
100             stopWebServers();
101             ServerRestartCount_ = 0;
102         }
103         final WebDriver driver = loadPage2(html);
104         assertTitle(driver, getExpectedAlerts()[0]);
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     protected boolean isWebClientCached() {
112         return true;
113     }
114 }