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.draganddrop;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.htmlunit.junit.annotation.HtmlUnitNYI;
20  import org.junit.jupiter.api.Test;
21  
22  /**
23   * Tests for {@link DataTransfer}.
24   *
25   * @author Ronald Brill
26   */
27  public class DataTransferTest extends WebDriverTestCase {
28  
29      /**
30       * @throws Exception if the test fails
31       */
32      @Test
33      @Alerts("[object DataTransfer]")
34      public void ctor() throws Exception {
35          final String html = DOCTYPE_HTML
36              + "<html><head><script>\n"
37              + LOG_TITLE_FUNCTION
38              + "  function test() {\n"
39              + "    var dt = new DataTransfer();\n"
40              + "    log(dt);\n"
41              + "  }\n"
42              + "</script></head>\n"
43              + "<body onload='test()'>\n"
44              + "</body></html>";
45  
46          loadPageVerifyTitle2(html);
47      }
48  
49      /**
50       * @throws Exception if the test fails
51       */
52      @Test
53      @Alerts({"[object FileList]", "0"})
54      public void filesEmpty() throws Exception {
55          final String html = DOCTYPE_HTML
56              + "<html><head><script>\n"
57              + LOG_TITLE_FUNCTION
58              + "  function test() {\n"
59              + "    var dt = new DataTransfer();\n"
60              + "    log(dt.files);\n"
61              + "    log(dt.files.length);\n"
62              + "  }\n"
63              + "</script></head>\n"
64              + "<body onload='test()'>\n"
65              + "</body></html>";
66  
67          loadPageVerifyTitle2(html);
68      }
69  
70      /**
71       * @throws Exception if the test fails
72       */
73      @Test
74      @Alerts({"0", "0", "0", "true"})
75      public void filesStringAdded() throws Exception {
76          final String html = DOCTYPE_HTML
77              + "<html><head><script>\n"
78              + LOG_TITLE_FUNCTION
79              + "  function test() {\n"
80              + "    let dt = new DataTransfer();\n"
81              + "    let f1 = dt.files;\n"
82              + "    log(f1.length);\n"
83  
84              + "    let i1 = dt.items.add('HtmlUnit', 'text/html');\n"
85              + "    log(dt.files.length);\n"
86              + "    log(f1.length);\n"
87  
88              + "    log(f1 === dt.files);\n"
89              + "  }\n"
90              + "</script></head>\n"
91              + "<body onload='test()'>\n"
92              + "</body></html>";
93  
94          loadPageVerifyTitle2(html);
95      }
96  
97      /**
98       * @throws Exception if the test fails
99       */
100     @Test
101     @Alerts({"0", "1", "1", "true", "true"})
102     public void filesFileAdded() throws Exception {
103         final String html = DOCTYPE_HTML
104             + "<html><head><script>\n"
105             + LOG_TITLE_FUNCTION
106             + "  function test() {\n"
107             + "    let dt = new DataTransfer();\n"
108             + "    let f1 = dt.files;\n"
109             + "    log(f1.length);\n"
110 
111             + "    let file = new File(['Html', 'Unit'], 'htMluniT.txt');\n"
112             + "    let i1 = dt.items.add(file);\n"
113             + "    log(dt.files.length);\n"
114             + "    log(f1.length);\n"
115 
116             + "    log(f1 === dt.files);\n"
117             + "    log(file === dt.files[0]);\n"
118             + "  }\n"
119             + "</script></head>\n"
120             + "<body onload='test()'>\n"
121             + "</body></html>";
122 
123         loadPageVerifyTitle2(html);
124     }
125 
126     /**
127      * @throws Exception if the test fails
128      */
129     @Test
130     @Alerts("true")
131     public void filesRequestTwoTimes() throws Exception {
132         final String html = DOCTYPE_HTML
133             + "<html><head><script>\n"
134             + LOG_TITLE_FUNCTION
135             + "  function test() {\n"
136             + "    var dt = new DataTransfer();\n"
137             + "    let f1 = dt.files;\n"
138             + "    log(f1 === dt.files);\n"
139             + "  }\n"
140             + "</script></head>\n"
141             + "<body onload='test()'>\n"
142             + "</body></html>";
143 
144         loadPageVerifyTitle2(html);
145     }
146 
147     /**
148      * @throws Exception if the test fails
149      */
150     @Test
151     @Alerts({"[object DataTransferItemList]", "0"})
152     public void itemsEmpty() throws Exception {
153         final String html = DOCTYPE_HTML
154             + "<html><head><script>\n"
155             + LOG_TITLE_FUNCTION
156             + "  function test() {\n"
157             + "    var dt = new DataTransfer();\n"
158             + "    log(dt.items);\n"
159             + "    log(dt.items.length);\n"
160             + "  }\n"
161             + "</script></head>\n"
162             + "<body onload='test()'>\n"
163             + "</body></html>";
164 
165         loadPageVerifyTitle2(html);
166     }
167 
168     /**
169      * @throws Exception if the test fails
170      */
171     @Test
172     @Alerts(DEFAULT = "false",
173             FF = "true",
174             FF_ESR = "true")
175     @HtmlUnitNYI(CHROME = "true", EDGE = "true")
176     public void itemsRequestTwoTimes() throws Exception {
177         final String html = DOCTYPE_HTML
178             + "<html><head><script>\n"
179             + LOG_TITLE_FUNCTION
180             + "  function test() {\n"
181             + "    var dt = new DataTransfer();\n"
182             + "    let i1 = dt.items;\n"
183             + "    log(i1 === dt.items);\n"
184             + "  }\n"
185             + "</script></head>\n"
186             + "<body onload='test()'>\n"
187             + "</body></html>";
188 
189         loadPageVerifyTitle2(html);
190     }
191 }