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.html;
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 Enumerator}.
23   *
24   * @author Ahmed Ashour
25   * @author Frank Danek
26   * @author Ronald Brill
27   */
28  public class EnumeratorTest extends WebDriverTestCase {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      @Alerts("Enumerator not supported")
35      public void basicEmptyEnumerator() throws Exception {
36          final String html = DOCTYPE_HTML
37              + "<html><head><script>\n"
38              + LOG_TITLE_FUNCTION
39              + "function test() {\n"
40              + "  if (typeof(Enumerator) != 'undefined') {\n"
41              + "    try {\n"
42              + "      var en = new Enumerator();\n"
43              + "      log(en.atEnd());\n"
44              + "      log(en.item());\n"
45              + "      log(en.moveNext());\n"
46              + "      log(en.item());\n"
47              + "      log(en.atEnd());\n"
48              + "    } catch(e) { logEx(e); }\n"
49              + "  } else {\n"
50              + "    log('Enumerator not supported');\n"
51              + "  }\n"
52              + "}\n"
53              + "</script></head>\n"
54              + "<body onload='test()'>\n"
55              + "  <form/>\n"
56              + "</body></html>";
57  
58          loadPageVerifyTitle2(html);
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      @Alerts("Enumerator not supported")
66      public void basicEnumerator() throws Exception {
67          final String html = DOCTYPE_HTML
68              + "<html><head><script>\n"
69              + LOG_TITLE_FUNCTION
70              + "function test() {\n"
71              + "  if (typeof(Enumerator) != 'undefined') {\n"
72              + "    try {\n"
73              + "      var en = new Enumerator(document.forms);\n"
74              + "      log(en.atEnd());\n"
75              + "      log(en.item());\n"
76              + "      log(en.moveNext());\n"
77              + "      log(en.item());\n"
78              + "      log(en.atEnd());\n"
79              + "    } catch(e) { logEx(e); }\n"
80              + "  } else {\n"
81              + "    log('Enumerator not supported');\n"
82              + "  }\n"
83              + "}\n"
84              + "</script></head>\n"
85              + "<body onload='test()'>\n"
86              + "  <form/>\n"
87              + "</body></html>";
88  
89          loadPageVerifyTitle2(html);
90      }
91  
92      /**
93       * @throws Exception if the test fails
94       */
95      @Test
96      @Alerts("Enumerator not supported")
97      public void basicEnumeratorWrongType() throws Exception {
98          final String html = DOCTYPE_HTML
99              + "<html><head><script>\n"
100             + LOG_TITLE_FUNCTION
101             + "function test() {\n"
102             + "  if (typeof(Enumerator) != 'undefined') {\n"
103             + "    try {\n"
104             + "      var en = new Enumerator(window);\n"
105             + "    } catch(e) { logEx(e); }\n"
106             + "  } else {\n"
107             + "    log('Enumerator not supported');\n"
108             + "  }\n"
109             + "}\n"
110             + "</script></head>\n"
111             + "<body onload='test()'>\n"
112             + "  <form/>\n"
113             + "</body></html>";
114 
115         loadPageVerifyTitle2(html);
116     }
117 
118     /**
119      * Verifies that the enumerator constructor can take a form argument and then enumerate over the
120      * form's input elements (bug 2645480).
121      * @throws Exception if an error occurs
122      */
123     @Test
124     @Alerts({"f t1 t2", "Enumerator not supported"})
125     public void formEnumerator() throws Exception {
126         final String html = DOCTYPE_HTML
127             + "<html>\n"
128             + "<body>\n"
129             + "  <form id='f'>\n"
130             + "    <input type='text' name='t1' id='t1' />\n"
131             + "    <input type='text' name='t2' id='t2' />\n"
132             + "    <div id='d'>d</div>\n"
133             + "  </form>\n"
134             + "<script>\n"
135             + LOG_TITLE_FUNCTION
136             + "  var f = document.forms.f, t1 = document.all.t1, t2 = document.all.t2;\n"
137             + "  log(f.id + ' ' + t1.id + ' ' + t2.id);\n"
138             + "  if (typeof(Enumerator) != 'undefined') {\n"
139             + "    try {\n"
140             + "      var e = new Enumerator(f);\n"
141             + "      for( ; !e.atEnd(); e.moveNext()) {\n"
142             + "        log(e.item().id);\n"
143             + "      }\n"
144             + "    } catch(e) { logEx(e); }\n"
145             + "  } else {\n"
146             + "    log('Enumerator not supported');\n"
147             + "  }\n"
148             + "</script>\n"
149             + "</body></html>";
150 
151         loadPageVerifyTitle2(html);
152     }
153 
154     /**
155      * @throws Exception if the test fails
156      */
157     @Test
158     @Alerts({"undefined", "Enumerator not supported"})
159     public void item() throws Exception {
160         final String html = DOCTYPE_HTML
161             + "<html><head>\n"
162             + "<script>\n"
163             + LOG_TITLE_FUNCTION
164             + "function test() {\n"
165             + "  var form = document.forms.myForm;\n"
166             + "  log(form.elements.item(0).TyPe);\n"
167             + "  if (typeof(Enumerator) != 'undefined') {\n"
168             + "    try {\n"
169             + "      log(new Enumerator(form).item().TyPe);\n"
170             + "    } catch(e) { logEx(e); }\n"
171             + "  } else {\n"
172             + "    log('Enumerator not supported');\n"
173             + "  }\n"
174             + "}\n"
175             + "</script></head>\n"
176             + "<body onload='test()'>\n"
177             + "  <form name='myForm'>\n"
178             + "    <input name='myInput'>\n"
179             + "  </form>\n"
180             + "</body></html>";
181 
182         loadPageVerifyTitle2(html);
183     }
184 }