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