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