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.javascript.host.animations.AnimationEvent;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.htmlunit.junit.annotation.HtmlUnitNYI;
21  import org.junit.jupiter.api.Test;
22  import org.openqa.selenium.WebDriver;
23  
24  /**
25   * Tests for {@link AnimationEvent}.
26   *
27   * @author Ronald Brill
28   */
29  public class AnimationEventTest extends WebDriverTestCase {
30  
31      private static final String DUMP_EVENT_FUNCTION = "  function dump(event) {\n"
32              + "    log(event);\n"
33              + "    log(event.type);\n"
34              + "    log(event.bubbles);\n"
35              + "    log(event.cancelable);\n"
36              + "    log(event.composed);\n"
37              + "  }\n";
38  
39      /**
40       * @throws Exception if the test fails
41       */
42      @Test
43      @Alerts({"[object AnimationEvent]", "animationstart", "false", "false", "false"})
44      public void create_ctor() throws Exception {
45          final String html = DOCTYPE_HTML
46              + "<html><head><script>\n"
47              + LOG_TITLE_FUNCTION
48              + "  function test() {\n"
49              + "    try {\n"
50              + "      var event = new AnimationEvent('animationstart');\n"
51              + "      dump(event);\n"
52              + "    } catch(e) { logEx(e) }\n"
53              + "  }\n"
54              + DUMP_EVENT_FUNCTION
55              + "</script></head><body onload='test()'>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts(DEFAULT = {"[object AnimationEvent]", "", "false", "false", "false"},
66              FF = "NotSupportedError/DOMException",
67              FF_ESR = "NotSupportedError/DOMException")
68      public void create_createEvent() throws Exception {
69          final String html = DOCTYPE_HTML
70              + "<html><head><script>\n"
71              + LOG_TITLE_FUNCTION
72              + "  function test() {\n"
73              + "    try {\n"
74              + "      var event = document.createEvent('AnimationEvent');\n"
75              + "      dump(event);\n"
76              + "    } catch(e) { logEx(e); }\n"
77              + "  }\n"
78              + DUMP_EVENT_FUNCTION
79              + "</script></head><body onload='test()'>\n"
80              + "</body></html>";
81  
82          loadPageVerifyTitle2(html);
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      @Alerts("true")
90      public void inWindow() throws Exception {
91          final String html = DOCTYPE_HTML
92              + "<html>\n"
93              + "<head>\n"
94              + "  <script>\n"
95              + LOG_TITLE_FUNCTION
96              + "    function test() {\n"
97              + "      log('AnimationEvent' in window);\n"
98              + "    }\n"
99              + "  </script>\n"
100             + "</head>\n"
101             + "<body onload='test()'>\n"
102             + "</body>\n"
103             + "</html>";
104 
105         loadPageVerifyTitle2(html);
106     }
107 
108     /**
109      * Test for feature-request #229.
110      *
111      * @throws Exception if an error occurs
112      */
113     @Test
114     @Alerts({"animationstart", "animationend"})
115     @HtmlUnitNYI(CHROME = {},
116             EDGE = {},
117             FF = {},
118             FF_ESR = {})
119     public void animate() throws Exception {
120         final String html = DOCTYPE_HTML
121             + "<html><head>\n"
122             + "<style>\n"
123             + "  .animate {  animation: identifier .1s ; }\n"
124             + "  @keyframes identifier {\n"
125             + "    0% { width: 0px; }\n"
126             + "    100% { width: 30px; }\n"
127             + "  }\n"
128             + "</style>\n"
129             + "<script>\n"
130             + LOG_TITLE_FUNCTION
131             + "function test() {\n"
132             + "  var el = document.getElementById('div1');\n"
133             + "  el.addEventListener('animationstart', function(e) {\n"
134             + "    log(e.type);\n"
135             + "  });\n"
136             + "  el.addEventListener('animationend', function(e) {\n"
137             + "    log(e.type);\n"
138             + "  });\n"
139             + "  el.className = 'animate';\n"
140             + "}\n"
141             + "</script>\n"
142             + "</head><body onload='test()'>\n"
143             + "<div id='div1'>TXT</div>\n"
144             + "</body></html>";
145 
146         final WebDriver driver = loadPage2(html);
147         Thread.sleep(DEFAULT_WAIT_TIME.toMillis() / 2);
148         verifyTitle2(driver, getExpectedAlerts());
149     }
150 }