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.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for {@link DataTransferItemList}.
23   *
24   * @author Ronald Brill
25   */
26  public class DataTransferItemListTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"0", "1"})
33      public void length() throws Exception {
34          final String html = DOCTYPE_HTML
35              + "<html><head><script>\n"
36              + LOG_TITLE_FUNCTION
37              + "  function test() {\n"
38              + "    let dt = new DataTransfer();\n"
39              + "    let l1 = dt.items;\n"
40              + "    log(l1.length);\n"
41  
42              + "    dt.items.add('HtmlUnit', 'text/html');\n"
43              + "    log(l1.length);\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({"0", "0", "1", "0"})
57      public void clear() throws Exception {
58          final String html = DOCTYPE_HTML
59              + "<html><head><script>\n"
60              + LOG_TITLE_FUNCTION
61              + "  function test() {\n"
62              + "    let dt = new DataTransfer();\n"
63              + "    let l1 = dt.items;\n"
64              + "    log(l1.length);\n"
65              + "    l1.clear();\n"
66              + "    log(l1.length);\n"
67  
68              + "    dt.items.add('HtmlUnit', 'text/html');\n"
69              + "    log(l1.length);\n"
70              + "    l1.clear();\n"
71              + "    log(l1.length);\n"
72              + "  }\n"
73              + "</script></head>\n"
74              + "<body onload='test()'>\n"
75              + "</body></html>";
76  
77          loadPageVerifyTitle2(html);
78      }
79  
80      /**
81       * @throws Exception if the test fails
82       */
83      @Test
84      @Alerts({"0", "1", "2"})
85      public void add() throws Exception {
86          final String html = DOCTYPE_HTML
87              + "<html><head><script>\n"
88              + LOG_TITLE_FUNCTION
89              + "  function test() {\n"
90              + "    let dt = new DataTransfer();\n"
91              + "    let l1 = dt.items;\n"
92              + "    log(l1.length);\n"
93  
94              + "    l1.add('HtmlUnit', 'text/html');\n"
95              + "    log(l1.length);\n"
96  
97              + "    let file = new File(['Html', 'Unit'], 'htMluniT.txt', { type: 'text/html' });\n"
98              + "    l1.add(file);\n"
99              + "    log(l1.length);\n"
100             + "  }\n"
101             + "</script></head>\n"
102             + "<body onload='test()'>\n"
103             + "</body></html>";
104 
105         loadPageVerifyTitle2(html);
106     }
107 
108     /**
109      * @throws Exception if the test fails
110      */
111     @Test
112     @Alerts({"0", "1", "0"})
113     public void remove() throws Exception {
114         final String html = DOCTYPE_HTML
115             + "<html><head><script>\n"
116             + LOG_TITLE_FUNCTION
117             + "  function test() {\n"
118             + "    let dt = new DataTransfer();\n"
119             + "    let l1 = dt.items;\n"
120             + "    log(l1.length);\n"
121 
122             + "    l1.add('HtmlUnit', 'text/html');\n"
123             + "    log(l1.length);\n"
124 
125             + "    l1.remove(0);\n"
126             + "    log(l1.length);\n"
127             + "  }\n"
128             + "</script></head>\n"
129             + "<body onload='test()'>\n"
130             + "</body></html>";
131 
132         loadPageVerifyTitle2(html);
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts({"0", "1", "1", "1"})
140     public void removeInvalid() throws Exception {
141         final String html = DOCTYPE_HTML
142             + "<html><head><script>\n"
143             + LOG_TITLE_FUNCTION
144             + "  function test() {\n"
145             + "    let dt = new DataTransfer();\n"
146             + "    let l1 = dt.items;\n"
147             + "    log(l1.length);\n"
148 
149             + "    l1.add('HtmlUnit', 'text/html');\n"
150             + "    log(l1.length);\n"
151 
152             + "    l1.remove(10);\n"
153             + "    log(l1.length);\n"
154 
155             + "    l1.remove(-1);\n"
156             + "    log(l1.length);\n"
157             + "  }\n"
158             + "</script></head>\n"
159             + "<body onload='test()'>\n"
160             + "</body></html>";
161 
162         loadPageVerifyTitle2(html);
163     }
164 
165     /**
166      * @throws Exception on test failure
167      */
168     @Test
169     @Alerts({"2", "[object DataTransferItem]", "[object DataTransferItem]"})
170     public void indexed() throws Exception {
171         final String html = DOCTYPE_HTML
172             + "<html><head><script>\n"
173             + LOG_TITLE_FUNCTION
174             + "  function test() {\n"
175             + "    let dt = new DataTransfer();\n"
176             + "    let items = dt.items;\n"
177             + "    items.add('HtmlUnit', 'text/html');\n"
178             + "    items.add('1234', 'text/plain');\n"
179             + "    log(items.length);\n"
180             + "    log(items[0]);\n"
181             + "    log(items[1]);\n"
182             + "  }\n"
183             + "</script></head>\n"
184             + "<body onload='test()'>\n"
185             + "</body></html>";
186 
187         loadPageVerifyTitle2(html);
188     }
189 
190     /**
191      * @throws Exception on test failure
192      */
193     @Test
194     @Alerts({"2", "undefined", "undefined"})
195     public void indexedWrong() throws Exception {
196         final String html = DOCTYPE_HTML
197             + "<html><head><script>\n"
198             + LOG_TITLE_FUNCTION
199             + "  function test() {\n"
200             + "    let dt = new DataTransfer();\n"
201             + "    let items = dt.items;\n"
202             + "    items.add('HtmlUnit', 'text/html');\n"
203             + "    items.add('1234', 'text/plain');\n"
204             + "    log(items.length);\n"
205             + "    log(items[-1]);\n"
206             + "    log(items[2]);\n"
207             + "  }\n"
208             + "</script></head>\n"
209             + "<body onload='test()'>\n"
210             + "</body></html>";
211 
212         loadPageVerifyTitle2(html);
213     }
214 
215     /**
216      * @throws Exception on test failure
217      */
218     @Test
219     @Alerts({"2", "[object DataTransferItem]", "[object DataTransferItem]"})
220     public void iterator() throws Exception {
221         final String html = DOCTYPE_HTML
222             + "<html><head><script>\n"
223             + LOG_TITLE_FUNCTION
224             + "  function test() {\n"
225             + "    let dt = new DataTransfer();\n"
226             + "    let items = dt.items;\n"
227             + "    items.add('HtmlUnit', 'text/html');\n"
228             + "    items.add('1234', 'text/plain');\n"
229             + "    log(items.length);\n"
230 
231             + "    for (var i of items) {\n"
232             + "      log(i);\n"
233             + "    }\n"
234             + "  }\n"
235             + "</script></head>\n"
236             + "<body onload='test()'>\n"
237             + "</body></html>";
238 
239         loadPageVerifyTitle2(html);
240     }
241 }