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.javascript.host;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  import org.openqa.selenium.By;
21  import org.openqa.selenium.WebDriver;
22  
23  /**
24   * Tests for {@link Window} that use background jobs.
25   *
26   * @author Ronald Brill
27   */
28  public class WindowConcurrency2Test extends WebDriverTestCase {
29  
30      /**
31       * When <tt>setInterval()</tt> is called with a 0 millisecond delay, Internet Explorer turns it
32       * into a <tt>setTimeout()</tt> call, and Firefox imposes a minimum timer restriction.
33       *
34       * @throws Exception if an error occurs
35       */
36      @Test
37      @Alerts("xxx")
38      public void setIntervalZeroDelay() throws Exception {
39          final String html = DOCTYPE_HTML
40              + "<html><body><div id='d'></div>\n"
41              + "<script>\n"
42              + "  var count = 0;\n"
43  
44              + "  function doTimeout() {\n"
45              + "    document.getElementById('d').innerHTML += 'x';\n"
46              + "    count++;\n"
47              + "    if (count > 2) {\n"
48              + "      clearInterval(id);\n"
49              + "    }\n"
50              + "  }\n"
51  
52              + "  var id = setInterval(doTimeout, 0);\n"
53              + "</script>\n"
54              + "</body></html>";
55  
56          final WebDriver driver = loadPage2(html);
57  
58          verify(() -> driver.findElement(By.id("d")).getText(), getExpectedAlerts()[0]);
59      }
60  }