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