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