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