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.BrowserParameterizedRunner.Default;
26  import org.htmlunit.junit.annotation.Alerts;
27  import org.junit.After;
28  import org.junit.Test;
29  import org.junit.runners.Parameterized.Parameter;
30  import org.openqa.selenium.WebDriver;
31  
32  /**
33   * Tests two Host classes, if one prototype is parent of another.
34   *
35   * @author Ahmed Ashour
36   * @author Ronald Brill
37   */
38  public abstract class HostParentOf extends WebDriverTestCase {
39  
40      private static int ServerRestartCount_ = 0;
41  
42      /**
43       * Returns the parameterized data.
44       * @param predicate the predicate, which determines whether or not to include the parent
45       * @return the parameterized data
46       * @throws Exception if an error occurs
47       */
48      protected static Collection<Object[]> data(final Predicate<String> predicate)  throws Exception {
49          final Set<String> jsClassNames = TestCaseTest.getAllConfiguredJsConstructorNames();
50  
51          final List<Object[]> list = new ArrayList<>(jsClassNames.size() * jsClassNames.size() / 10);
52          for (final String parent : jsClassNames) {
53              if (predicate.test(parent)) {
54                  for (final String child : jsClassNames) {
55                      list.add(new Object[] {parent, child});
56                  }
57              }
58          }
59          return list;
60      }
61  
62      /**
63       * The parent element name.
64       */
65      @Parameter
66      public String parent_;
67  
68      /**
69       * The child element name.
70       */
71      @Parameter(1)
72      public String child_;
73  
74      /**
75       * The default test.
76       * @throws Exception if an error occurs
77       */
78      @Test
79      @Alerts("false")
80      @Default
81      public void isParentOf() throws Exception {
82          test(parent_, child_);
83      }
84  
85      /**
86       * Runs the test.
87       *
88       * @param parent the parent host name
89       * @param child the child host name
90       * @throws Exception if an error occurs
91       */
92      protected void test(final String parent, final String child) throws Exception {
93          final String html = DOCTYPE_HTML
94              + "<html>\n"
95              + "<head>\n"
96              + "<title>-</title>\n"
97              + "</head>\n"
98              + "<body>\n"
99              + "<script>\n"
100 
101             + "  function isParentOf(o1, o2) {"
102             + "    detector = function() {};\n"
103             + "    o1.prototype.myCustomFunction = detector;\n"
104             + "    return o2.prototype.myCustomFunction === detector;\n"
105             + "  }\n"
106 
107             + "  try {\n"
108             + "    document.title = isParentOf(" + parent + ", " + child + ");\n"
109             + "  } catch(e) { document.title = 'false'; }\n"
110             + "</script>\n"
111             + "</body></html>";
112 
113         ServerRestartCount_++;
114         if (ServerRestartCount_ == 200) {
115             stopWebServers();
116             ServerRestartCount_ = 0;
117         }
118         final WebDriver driver = loadPage2(html);
119         assertTitle(driver, getExpectedAlerts()[0]);
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     protected boolean isWebClientCached() {
127         return true;
128     }
129 
130     /**
131      * Cleanup.
132      */
133     @After
134     public void after() {
135         parent_ = null;
136         child_ = null;
137     }
138 }