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.html;
16  
17  import org.htmlunit.SimpleWebTestCase;
18  import org.junit.jupiter.api.Test;
19  
20  /**
21   * Tests for {@link HtmlRangeInput}.
22   *
23   * @author Anton Demydenko
24   */
25  public class HtmlRangeInput2Test extends SimpleWebTestCase {
26  
27      /**
28       * @throws Exception if the test fails
29       */
30      @Test
31      public void testDefault() throws Exception {
32          final String htmlContent = DOCTYPE_HTML
33                  + "<html>\n"
34                  + "<head></head>\n"
35                  + "<body>\n"
36                  + "<form id='form1'>\n"
37                  + "  <input type='range' id='first'>\n"
38                  + "  <input type='range' id='second' min='-100' max='100'>\n"
39                  + "  <input type='range' id='third' min='-15' max='85'>\n"
40                  + "  <input type='range' id='forth' min='foo' max='bar'>\n"
41                  + "</form>\n"
42                  + "</body></html>";
43  
44          final HtmlPage page = loadPage(htmlContent);
45  
46          final HtmlRangeInput first = (HtmlRangeInput) page.getElementById("first");
47          final HtmlRangeInput second = (HtmlRangeInput) page.getElementById("second");
48          final HtmlRangeInput third = (HtmlRangeInput) page.getElementById("third");
49          final HtmlRangeInput forth = (HtmlRangeInput) page.getElementById("forth");
50  
51          assertEquals("", first.getValueAttribute());
52          assertEquals("50", first.getValue());
53          assertEquals("", second.getValueAttribute());
54          assertEquals("0", second.getValue());
55          assertEquals("", third.getValueAttribute());
56          assertEquals("35", third.getValue());
57          assertEquals("", forth.getValueAttribute());
58          assertEquals("50", forth.getValue());
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      public void minValidation() throws Exception {
66          final String htmlContent = DOCTYPE_HTML
67                  + "<html>\n"
68                  + "<head></head>\n"
69                  + "<body>\n"
70                  + "<form id='form1'>\n"
71                  + "  <input type='range' id='first' min='10'>\n"
72                  + "  <input type='range' id='second'>\n"
73                  + "  <input type='range' id='third' min='foo'>\n"
74                  + "</form>\n"
75                  + "</body></html>";
76  
77          final HtmlPage page = loadPage(htmlContent);
78  
79          final HtmlRangeInput first = (HtmlRangeInput) page.getElementById("first");
80          final HtmlRangeInput second = (HtmlRangeInput) page.getElementById("second");
81          final HtmlRangeInput third = (HtmlRangeInput) page.getElementById("third");
82  
83          final String defaultFirstValue = first.getValue();
84  
85          // empty
86          assertTrue(first.isValid());
87          // invalid
88          first.setValue("foo");
89          assertTrue(first.isValid());
90          assertEquals("", first.getValueAttribute());
91          assertEquals(defaultFirstValue, first.getValue());
92          // lesser
93          first.setValue("1");
94          assertTrue(first.isValid());
95          assertEquals("", first.getValueAttribute());
96          assertEquals("10", first.getValue());
97          // equal
98          first.setValue("10");
99          assertTrue(first.isValid());
100         assertEquals("", first.getValueAttribute());
101         assertEquals("10", first.getValue());
102         // bigger
103         first.setValue("100");
104         assertTrue(first.isValid());
105         assertEquals("", first.getValueAttribute());
106         assertEquals("100", first.getValue());
107 
108         second.setValue("0");
109         assertTrue(second.isValid());
110         assertEquals("", second.getValueAttribute());
111         assertEquals("0", second.getValue());
112 
113         third.setValue("0");
114         assertTrue(third.isValid());
115         assertEquals("", third.getValueAttribute());
116         assertEquals("0", third.getValue());
117     }
118 
119     /**
120      * @throws Exception if the test fails
121      */
122     @Test
123     public void maxValidation() throws Exception {
124         final String htmlContent = DOCTYPE_HTML
125                 + "<html>\n"
126                 + "<head></head>\n"
127                 + "<body>\n"
128                 + "<form id='form1'>\n"
129                 + "  <input type='range' id='first' max='10'>\n"
130                 + "  <input type='range' id='second'>\n"
131                 + "  <input type='range' id='third' max='foo'>\n"
132                 + "</form>\n"
133                 + "</body></html>";
134 
135         final HtmlPage page = loadPage(htmlContent);
136 
137         final HtmlRangeInput first = (HtmlRangeInput) page.getElementById("first");
138         final HtmlRangeInput second = (HtmlRangeInput) page.getElementById("second");
139         final HtmlRangeInput third = (HtmlRangeInput) page.getElementById("third");
140 
141         final String defaultFirstValue = first.getValue();
142 
143         // empty
144         assertTrue(first.isValid());
145         // invalid
146         first.setValue("foo");
147         assertTrue(first.isValid());
148         assertEquals("", first.getValueAttribute());
149         assertEquals(defaultFirstValue, first.getValue());
150         // lesser
151         first.setValue("1");
152         assertTrue(first.isValid());
153         assertEquals("", first.getValueAttribute());
154         assertEquals("1", first.getValue());
155         // equal
156         first.setValue("10");
157         assertTrue(first.isValid());
158         assertEquals("", first.getValueAttribute());
159         assertEquals("10", first.getValue());
160         // bigger
161         first.setValue("100");
162         assertTrue(first.isValid());
163         assertEquals("", first.getValueAttribute());
164         assertEquals("10", first.getValue());
165 
166         second.setValue("0");
167         assertTrue(second.isValid());
168         assertEquals("", second.getValueAttribute());
169         assertEquals("0", second.getValue());
170 
171         third.setValue("0");
172         assertTrue(third.isValid());
173         assertEquals("", third.getValueAttribute());
174         assertEquals("0", third.getValue());
175     }
176 }