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.dom;
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 RadioNodeList}.
25   *
26   * @author Lai Quang Duong
27   * @author Ronald Brill
28   */
29  @RunWith(BrowserRunner.class)
30  public class RadioNodeListTest extends WebDriverTestCase {
31  
32      private static final String FORM_HTML = "<form name='form'>\n"
33              + "<input type='text' name='first' value='0'/>\n"
34              + "<input type='radio' name='first'/>\n"
35              + "<input type='radio' name='first' value='2' checked/>\n"
36              + "<input type='radio' name='first' value='3'/>\n"
37              + "\n"
38              + "<input type='radio' name='second' value='1'/>\n"
39              + "\n"
40              + "<input type='radio' name='third' value='1' checked/>\n"
41              + "\n"
42              + "<input type='radio' name='fourth' value='1'/>\n"
43              + "<input type='radio' name='fourth' value='2'/>\n"
44              + "\n"
45              + "<input type='radio' name='fifth' value='1'/>\n"
46              + "<input type='radio' name='fifth' checked/>\n"
47              + "</form>";
48  
49      /**
50       * @throws Exception on test failure
51       */
52      @Test
53      @Alerts({"true", "true", "true"})
54      public void instanceOf() throws Exception {
55          final String html = DOCTYPE_HTML
56                  + "<html><head>\n"
57                  + "<script>\n"
58                  + LOG_TITLE_FUNCTION
59                  + "  function test() {\n"
60                  + "    log(document.form.first instanceof RadioNodeList);\n"
61                  + "    log(document.form.fourth instanceof RadioNodeList);\n"
62                  + "    log(document.form.fifth instanceof RadioNodeList);\n"
63                  + "  }\n"
64                  + "</script>\n"
65                  + "</head><body onload='test()'>\n"
66                  + FORM_HTML
67                  + "</body></html>\n";
68  
69          loadPageVerifyTitle2(html);
70      }
71  
72      /**
73       * @throws Exception on test failure
74       */
75      @Test
76      @Alerts({"2", "1", "1", "", "on"})
77      public void getValue() throws Exception {
78          final String html = DOCTYPE_HTML
79                  + "<html><head>\n"
80                  + "<script>\n"
81                  + LOG_TITLE_FUNCTION
82                  + "  function test() {\n"
83                  + "    log(document.form.first.value);\n"
84                  + "    log(document.form.second.value);\n"
85                  + "    log(document.form.third.value);\n"
86                  + "    log(document.form.fourth.value);\n"
87                  + "    log(document.form.fifth.value);\n"
88                  + "  }\n"
89                  + "</script>\n"
90                  + "</head><body onload='test()'>\n"
91                  + FORM_HTML
92                  + "</body></html>\n";
93  
94          loadPageVerifyTitle2(html);
95      }
96  
97      /**
98       * @throws Exception on test failure
99       */
100     @Test
101     @Alerts({"2", "on", "true"})
102     public void setValue() throws Exception {
103         final String html = DOCTYPE_HTML
104                 + "<html><head>\n"
105                 + "<script>\n"
106                 + LOG_TITLE_FUNCTION
107                 + "  function test() {\n"
108                 + "    log(document.form.first.value);\n"
109                 + "    document.form.first.value = 'on';\n"
110                 + "    log(document.form.first[1].value);\n"
111                 + "    log(document.form.first[1].checked);\n"
112                 + "  }\n"
113                 + "</script>\n"
114                 + "</head><body onload='test()'>\n"
115                 + FORM_HTML
116                 + "</body></html>\n";
117 
118         loadPageVerifyTitle2(html);
119     }
120 
121     /**
122      * @throws Exception on test failure
123      */
124     @Test
125     @Alerts({"first", "first", "first", "first"})
126     public void iterable() throws Exception {
127         final String html = DOCTYPE_HTML
128                 + "<html><head>\n"
129                 + "<script>\n"
130                 + LOG_TITLE_FUNCTION
131                 + "  function test() {\n"
132                 + "    for (let elem of form.first) {\n"
133                 + "      log(elem.name)\n"
134                 + "    }\n"
135                 + "  }\n"
136                 + "</script>\n"
137                 + "</head><body onload='test()'>\n"
138                 + FORM_HTML
139                 + "</body></html>\n";
140 
141         loadPageVerifyTitle2(html);
142     }
143 }