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