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