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 java.text.MessageFormat;
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.htmlunit.BrowserVersion;
24  import org.htmlunit.SimpleWebTestCase;
25  import org.junit.jupiter.params.ParameterizedTest;
26  import org.junit.jupiter.params.provider.MethodSource;
27  
28  /**
29   * Tests the <code>isDisabled()</code> method on all of the elements that must implement the <code>disabled</code>
30   * attribute:  <code>button</code>, <code>input</code>, <code>optgroup</code>, <code>option</code>, <code>select</code>
31   * and <code>textarea</code>.
32   *
33   * @author David D. Kilzer
34   * @author Ahmed Ashour
35   * @author Ronald Brill
36   */
37  public class DisabledElementTest extends SimpleWebTestCase {
38  
39      /**
40       * Tests data.
41       * @return tests data
42       */
43      public static Collection<String[]> data() {
44          return Arrays.asList(new String[][] {
45              {"<button id='element1' {0}>foo</button>"},
46              {"<input type='button' id='element1' {0}>"},
47              {"<input type='checkbox' id='element1' {0}>"},
48              {"<input type='file' id='element1' {0}>"},
49              {"<input type='hidden' id='element1' {0}>"},
50              {"<input type='image' id='element1' {0}>"},
51              {"<input type='password' id='element1' {0}>"},
52              {"<input type='radio' id='element1' {0}>"},
53              {"<input type='reset' id='element1' {0}>"},
54              {"<input type='submit' id='element1' {0}>"},
55              {"<input type='text' id='element1' {0}>"},
56              {"<select><optgroup id='element1' {0}><option value='1'></option></optgroup></select>"},
57              {"<select><option id='element1' value='1' {0}></option></select>"},
58              {"<select id='element1' {0}><option value='1'></option></select>"},
59              {"<textarea id='element1' {0}></textarea>"}
60          });
61      }
62  
63      /**
64       * Tests that the <code>isDisabled()</code> method returns {@code false} when the <code>disabled</code>
65       * attribute does not exist.
66       *
67       * @throws Exception if the test fails
68       */
69      @ParameterizedTest
70      @MethodSource("data")
71      void noDisabledAttribute(final String elementHtml) throws Exception {
72          executeDisabledTest(elementHtml, "", false);
73      }
74  
75      /**
76       * Tests that the <code>isDisabled()</code> method returns {@code true} when the <code>disabled</code>
77       * attribute exists and is blank.
78       *
79       * @throws Exception if the test fails
80       */
81      @ParameterizedTest
82      @MethodSource("data")
83      void blankDisabledAttribute(final String elementHtml) throws Exception {
84          executeDisabledTest(elementHtml, "disabled=''", true);
85      }
86  
87      /**
88       * Tests that the <code>isDisabled()</code> method returns {@code false} when the <code>disabled</code>
89       * attribute exists and is <em>not</em> blank.
90       *
91       * @throws Exception if the test fails
92       */
93      @ParameterizedTest
94      @MethodSource("data")
95      void populatedDisabledAttribute(final String elementHtml) throws Exception {
96          executeDisabledTest(elementHtml, "disabled='disabled'", true);
97      }
98  
99      /**
100      * Tests the <code>isDisabled()</code> method with the given parameters.
101      *
102      * @param disabledAttribute the definition of the <code>disabled</code> attribute
103      * @param expectedIsDisabled the expected return value of the <code>isDisabled()</code> method
104      * @throws Exception if test fails
105      */
106     private void executeDisabledTest(final String elementHtml,
107             final String disabledAttribute, final boolean expectedIsDisabled)
108                     throws Exception {
109 
110         String htmlContent = DOCTYPE_HTML + "<html><body><form id='form1'>{0}</form></body></html>";
111         htmlContent = MessageFormat.format(htmlContent, new Object[]{elementHtml});
112 
113 
114         htmlContent = MessageFormat.format(htmlContent, new Object[]{disabledAttribute});
115         final List<String> collectedAlerts = new ArrayList<>();
116         final HtmlPage page = loadPage(BrowserVersion.CHROME, htmlContent, collectedAlerts, URL_FIRST);
117 
118         final DisabledElement element = (DisabledElement) page.getHtmlElementById("element1");
119         assertEquals(expectedIsDisabled, element.isDisabled());
120     }
121 
122 }