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.event;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link ProgressEvent}.
23   *
24   * @author Ronald Brill
25   */
26  public class ProgressEventTest extends WebDriverTestCase {
27  
28      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
29          + "    if (event) {\n"
30          + "      log(event);\n"
31          + "      log(event.type);\n"
32          + "      log(event.bubbles);\n"
33          + "      log(event.cancelable);\n"
34          + "      log(event.composed);\n"
35  
36          + "      log(event.lengthComputable);\n"
37          + "      log(event.loaded);\n"
38          + "      log(event.total);\n"
39          + "    } else {\n"
40          + "      log('no event');\n"
41          + "    }\n"
42          + "  }\n";
43  
44      /**
45       * @throws Exception if the test fails
46       */
47      @Test
48      @Alerts({"[object ProgressEvent]", "progress", "false", "false", "false", "false", "0", "0"})
49      public void create_ctor() throws Exception {
50          final String html = DOCTYPE_HTML
51              + "<html><head><script>\n"
52              + LOG_TITLE_FUNCTION
53              + "  function test() {\n"
54              + "    try {\n"
55              + "      var event = new ProgressEvent('progress');\n"
56              + "      dump(event);\n"
57              + "    } catch(e) { logEx(e) }\n"
58              + "  }\n"
59              + DUMP_EVENT_FUNCTION
60              + "</script></head><body onload='test()'>\n"
61              + "</body></html>";
62  
63          loadPageVerifyTitle2(html);
64      }
65  
66      /**
67       * @throws Exception if the test fails
68       */
69      @Test
70      @Alerts({"[object ProgressEvent]", "test", "true", "false", "false", "true", "234", "666"})
71      public void create_ctorWithDetails() throws Exception {
72          final String html = DOCTYPE_HTML
73              + "<html><head><script>\n"
74              + LOG_TITLE_FUNCTION
75              + "  function test() {\n"
76              + "    try {\n"
77              + "      var event = new ProgressEvent('test', {\n"
78              + "        'bubbles': true,\n"
79              + "        'lengthComputable': true,\n"
80              + "        'loaded': 234,\n"
81              + "        'total': 666\n"
82              + "      });\n"
83              + "      dump(event);\n"
84              + "    } catch(e) { logEx(e) }\n"
85              + "  }\n"
86              + DUMP_EVENT_FUNCTION
87              + "</script></head><body onload='test()'>\n"
88              + "</body></html>";
89  
90          loadPageVerifyTitle2(html);
91      }
92  
93      /**
94       * @throws Exception if the test fails
95       */
96      @Test
97      @Alerts("NotSupportedError/DOMException")
98      public void create_createEvent() throws Exception {
99          final String html = DOCTYPE_HTML
100             + "<html><head><script>\n"
101             + LOG_TITLE_FUNCTION
102             + "  function test() {\n"
103             + "    try {\n"
104             + "      var event = document.createEvent('ProgressEvent');\n"
105             + "      dump(event);\n"
106             + "    } catch(e) { logEx(e) }\n"
107             + "  }\n"
108             + DUMP_EVENT_FUNCTION
109             + "</script></head><body onload='test()'>\n"
110             + "</body></html>";
111 
112         loadPageVerifyTitle2(html);
113     }
114 }