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