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 DataTransferItem}.
23   *
24   * @author Ronald Brill
25   */
26  public class DataTransferItemTest extends WebDriverTestCase {
27  
28      /**
29       * @throws Exception if the test fails
30       */
31      @Test
32      @Alerts({"string", "null", "string", "text/html", "string", "undefined",
33               "string", "something really special", "string", "text/plain",
34               "string", "text/xml",
35               "HtmlUnit", "HtmlUnit", "HtmlUnit", "HtmlUnit", "1234", "[object Object]"})
36      public void string() 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 i1 = dt.items.add('HtmlUnit', null);\n"
43              + "    log(i1.kind);\n"
44              + "    log(i1.type);\n"
45              + "    i1.getAsString((s) => log(s));\n"
46  
47              + "    let i2 = dt.items.add('HtmlUnit', 'text/html');\n"
48              + "    log(i2.kind);\n"
49              + "    log(i2.type);\n"
50              + "    i2.getAsString((s) => log(s));\n"
51  
52              + "    let i3 = dt.items.add('HtmlUnit', undefined);\n"
53              + "    log(i3.kind);\n"
54              + "    log(i3.type);\n"
55              + "    i3.getAsString((s) => log(s));\n"
56  
57              + "    let i4 = dt.items.add('HtmlUnit', 'something really special');\n"
58              + "    log(i4.kind);\n"
59              + "    log(i4.type);\n"
60              + "    i4.getAsString((s) => log(s));\n"
61  
62              + "    let i5 = dt.items.add(1234, 'text/plain');\n"
63              + "    log(i5.kind);\n"
64              + "    log(i5.type);\n"
65              + "    i5.getAsString((s) => log(s));\n"
66  
67              + "    let i6 = dt.items.add({ab: 17}, 'text/xml');\n"
68              + "    log(i6.kind);\n"
69              + "    log(i6.type);\n"
70              + "    i6.getAsString((s) => log(s));\n"
71              + "  }\n"
72              + "</script></head>\n"
73              + "<body onload='test()'>\n"
74              + "</body></html>";
75  
76          loadPageVerifyTitle2(html);
77      }
78  
79      /**
80       * @throws Exception if the test fails
81       */
82      @Test
83      @Alerts({"file", "", "file", "text/html", "TypeError"})
84      public void file() throws Exception {
85          final String html = DOCTYPE_HTML
86              + "<html><head><script>\n"
87              + LOG_TITLE_FUNCTION
88              + "  function test() {\n"
89              + "    let dt = new DataTransfer();\n"
90              + "    let file = new File(['Html', 'Unit'], 'htMluniT.txt');\n"
91              + "    let i1 = dt.items.add(file);\n"
92              + "    log(i1.kind);\n"
93              + "    log(i1.type);\n"
94  
95              + "    file = new File(['Html', 'Unit'], 'htMluniT.txt', { type: 'text/html' });\n"
96              + "    let i2 = dt.items.add(file);\n"
97              + "    log(i2.kind);\n"
98              + "    log(i2.type);\n"
99  
100             + "    try {"
101             + "      dt.items.add(undefined);\n"
102             + "    } catch(e) { logEx(e); }\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({"[object File]", "true", "null"})
116     public void getAsFile() 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 file = new File(['Html', 'Unit'], 'htMluniT.txt');\n"
123             + "    let i1 = dt.items.add(file);\n"
124             + "    let f1 = i1.getAsFile();\n"
125             + "    log(f1);\n"
126             + "    log(f1 === file);\n"
127 
128             + "    let i2 = dt.items.add('HtmlUnit', 'text/html');\n"
129             + "    let f2 = i2.getAsFile();\n"
130             + "    log(f2);\n"
131 
132             + "  }\n"
133             + "</script></head>\n"
134             + "<body onload='test()'>\n"
135             + "</body></html>";
136 
137         loadPageVerifyTitle2(html);
138     }
139 
140     /**
141      * @throws Exception if the test fails
142      */
143     @Test
144     @Alerts({"done", "HtmlUnit"})
145     public void getAsString() throws Exception {
146         final String html = DOCTYPE_HTML
147             + "<html><head><script>\n"
148             + LOG_TITLE_FUNCTION
149             + "  function test() {\n"
150             + "    let dt = new DataTransfer();\n"
151             + "    let i1 = dt.items.add('HtmlUnit', 'text/html');\n"
152             + "    i1.getAsString((s) => log(s));\n"
153 
154             + "    let file = new File(['Html', 'Unit'], 'htMluniT.txt');\n"
155             + "    let i2 = dt.items.add(file);\n"
156             + "    i2.getAsString((s) => log(s));\n"
157             + "    log('done');\n"
158             + "  }\n"
159             + "</script></head>\n"
160             + "<body onload='test()'>\n"
161             + "</body></html>";
162 
163         loadPageVerifyTitle2(html);
164     }
165 }