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