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.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  import org.openqa.selenium.WebDriver;
23  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
24  
25  /**
26   * Tests for {@link HtmlObject}.
27   *
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   */
31  @RunWith(BrowserRunner.class)
32  public class HtmlObject2Test extends WebDriverTestCase {
33  
34      /**
35       * @throws Exception if the test fails
36       */
37      @Test
38      @Alerts("[object HTMLObjectElement]")
39      public void simpleScriptable() throws Exception {
40          final String html = DOCTYPE_HTML
41              + "<html><head>\n"
42              + "<script>\n"
43              + LOG_TITLE_FUNCTION
44              + "  function test() {\n"
45              + "    log(document.getElementById('myId'));\n"
46              + "  }\n"
47              + "</script>\n"
48              + "</head><body onload='test()'>\n"
49              + "  <object id='myId'>\n"
50              + "</body></html>";
51  
52          final WebDriver driver = loadPageVerifyTitle2(html);
53          if (driver instanceof HtmlUnitDriver) {
54              final HtmlPage page = (HtmlPage) getEnclosedPage();
55              assertTrue(HtmlObject.class.isInstance(page.getHtmlElementById("myId")));
56          }
57      }
58  
59      /**
60       * @throws Exception if an error occurs
61       */
62      @Test
63      @Alerts({"false", "false", "false", "false", "false"})
64      public void willValidate() throws Exception {
65          final String html = DOCTYPE_HTML
66                  + "<html><head>\n"
67                  + "  <script>\n"
68                  + LOG_TITLE_FUNCTION
69                  + "    function test() {\n"
70                  + "      log(document.getElementById('o1').willValidate);\n"
71                  + "      log(document.getElementById('o2').willValidate);\n"
72                  + "      log(document.getElementById('o3').willValidate);\n"
73                  + "      log(document.getElementById('o4').willValidate);\n"
74                  + "      log(document.getElementById('o5').willValidate);\n"
75                  + "    }\n"
76                  + "  </script>\n"
77                  + "</head>\n"
78                  + "<body onload='test()'>\n"
79                  + "  <form>\n"
80                  + "    <object id='o1'>o1</object>\n"
81                  + "    <object id='o2' disabled>o2</object>\n"
82                  + "    <object id='o3' hidden>o3</object>\n"
83                  + "    <object id='o4' readonly>o4</object>\n"
84                  + "    <object id='o5' style='display: none'>o5</object>\n"
85                  + "  </form>\n"
86                  + "</body></html>";
87  
88          loadPageVerifyTitle2(html);
89      }
90  
91      /**
92       * @throws Exception if an error occurs
93       */
94      @Test
95      @Alerts({"true",
96               "false-false-false-false-false-false-false-false-false-true-false",
97               "false"})
98      public void validationEmpty() throws Exception {
99          validation("<object id='e1'>o1</object>\n", "");
100     }
101 
102     /**
103      * @throws Exception if an error occurs
104      */
105     @Test
106     @Alerts({"true",
107              "false-true-false-false-false-false-false-false-false-false-false",
108              "false"})
109     public void validationCustomValidity() throws Exception {
110         validation("<object id='e1'>o1</object>\n", "elem.setCustomValidity('Invalid');");
111     }
112 
113     /**
114      * @throws Exception if an error occurs
115      */
116     @Test
117     @Alerts({"true",
118              "false-true-false-false-false-false-false-false-false-false-false",
119              "false"})
120     public void validationBlankCustomValidity() throws Exception {
121         validation("<object id='e1'>o1</object>\n", "elem.setCustomValidity(' ');\n");
122     }
123 
124     /**
125      * @throws Exception if an error occurs
126      */
127     @Test
128     @Alerts({"true",
129              "false-false-false-false-false-false-false-false-false-true-false",
130              "false"})
131     public void validationResetCustomValidity() throws Exception {
132         validation("<object id='e1'>o1</object>\n",
133                 "elem.setCustomValidity('Invalid');elem.setCustomValidity('');");
134     }
135 
136     /**
137      * @throws Exception if an error occurs
138      */
139     @Test
140     @Alerts({"true",
141              "false-false-false-false-false-false-false-false-false-true-false",
142              "false"})
143     public void validationRequired() throws Exception {
144         validation("<object id='e1' required></object>\n", "");
145     }
146 
147     private void validation(final String htmlPart, final String jsPart) throws Exception {
148         final String html = DOCTYPE_HTML
149                 + "<html><head>\n"
150                 + "  <script>\n"
151                 + LOG_TITLE_FUNCTION
152                 + "    function logValidityState(s) {\n"
153                 + "      log(s.badInput"
154                         + "+ '-' + s.customError"
155                         + "+ '-' + s.patternMismatch"
156                         + "+ '-' + s.rangeOverflow"
157                         + "+ '-' + s.rangeUnderflow"
158                         + "+ '-' + s.stepMismatch"
159                         + "+ '-' + s.tooLong"
160                         + "+ '-' + s.tooShort"
161                         + " + '-' + s.typeMismatch"
162                         + " + '-' + s.valid"
163                         + " + '-' + s.valueMissing);\n"
164                 + "    }\n"
165                 + "    function test() {\n"
166                 + "      var elem = document.getElementById('e1');\n"
167                 + jsPart
168                 + "      log(elem.checkValidity());\n"
169                 + "      logValidityState(elem.validity);\n"
170                 + "      log(elem.willValidate);\n"
171                 + "    }\n"
172                 + "  </script>\n"
173                 + "</head>\n"
174                 + "<body onload='test()'>\n"
175                 + "  <form>\n"
176                 + htmlPart
177                 + "  </form>\n"
178                 + "</body></html>";
179 
180         loadPageVerifyTitle2(html);
181     }
182 }