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.javascript.host.css;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.annotation.Alerts;
19  import org.junit.jupiter.api.Test;
20  
21  /**
22   * Tests for css pseudo selectors :placeholder-shown and :-ms-input-placeholder.
23   *
24   * @author Thorsten Wendelmuth
25   * @author Ronald Brill
26   *
27   */
28  public class CSSSelector2Test extends WebDriverTestCase {
29  
30      private static final String PLACEHOLDER_SHOWN_HTML_HEAD = DOCTYPE_HTML
31              + "<html><head>\n"
32              + "<style>:placeholder-shown {border: 10px solid;}</style>\n"
33              + "<script>\n"
34              + "  function test() {\n"
35              + LOG_TITLE_FUNCTION
36              + "    try {\n"
37              + "      log(document.querySelector(':placeholder-shown'));\n"
38              + "    } catch (e) { logEx(e); }\n"
39              + "  }\n"
40              + "</script>\n"
41              + "</head>\n";
42  
43      private static final String MS_PLACEHOLDER_HTML_HEAD = DOCTYPE_HTML
44              + "<html><head>\n"
45              + "<style>:-ms-input-placeholder {border: 10px solid;}</style>"
46              + "<script>\n"
47              + "  function test() {\n"
48              + LOG_TITLE_FUNCTION
49              + "    try {\n"
50              + "      log(document.querySelector(':-ms-input-placeholder'));\n"
51              + "    } catch (e) { logEx(e); }\n"
52              + "  }\n"
53              + "</script>\n"
54              + "</head>\n";
55  
56      /**
57       * @throws Exception if the test fails
58       */
59      @Test
60      @Alerts("[object HTMLInputElement]")
61      public void placeholderShown() throws Exception {
62          final String html = PLACEHOLDER_SHOWN_HTML_HEAD
63                  + "<body onload='test();'>\n"
64                  + "<form>\n"
65                  + "  <input placeholder='htmlUnit supports placeholder-shown'>\n"
66                  + "</form></body></html>";
67  
68          loadPageVerifyTitle2(html);
69      }
70  
71      /**
72       * @throws Exception if the test fails
73       */
74      @Test
75      @Alerts("[object HTMLInputElement]")
76      public void placeholderShown_number() throws Exception {
77          final String html = PLACEHOLDER_SHOWN_HTML_HEAD
78                  + "<body onload='test();'>\n"
79                  + "<form>\n"
80                  + "  <input type='number' placeholder='200'>\n"
81                  + "</form></body></html>";
82  
83          loadPageVerifyTitle2(html);
84      }
85  
86      /**
87       * @throws Exception if the test fails
88       */
89      @Test
90      @Alerts("[object HTMLInputElement]")
91      public void placeholderShown_displayNone() throws Exception {
92          final String html = PLACEHOLDER_SHOWN_HTML_HEAD
93                  + "<body onload='test();'>\n"
94                  + "<form style='display:none;'>\n"
95                  + "  <input placeholder='htmlUnit supports placeholder-shown'>\n"
96                  + "</form></body></html>";
97          loadPageVerifyTitle2(html);
98      }
99  
100     /**
101      * @throws Exception if the test fails
102      */
103     @Test
104     @Alerts("null")
105     public void placeholderShown_hasValue() throws Exception {
106         final String html = PLACEHOLDER_SHOWN_HTML_HEAD
107                 + "<body onload='test();'>\n"
108                 + "<form style='display:none;'>\n"
109                 + "  <input placeholder='htmlUnit supports placeholder-shown' value='dont show placeholder'>\n"
110                 + "</form></body></html>";
111         loadPageVerifyTitle2(html);
112     }
113 
114     /**
115      * @throws Exception if the test fails
116      */
117     @Test
118     @Alerts("null")
119     public void placeholderShown_noInput() throws Exception {
120         final String html = PLACEHOLDER_SHOWN_HTML_HEAD
121                 + "<body onload='test();'>\n"
122                 + "<form style='display:none;'>\n"
123                 + "  <div placeholder='htmlUnit supports placeholder-shown' value='dont show placeholder'>\n"
124                 + "</form></body></html>";
125         loadPageVerifyTitle2(html);
126     }
127 
128     /**
129      * @throws Exception if the test fails
130      */
131     @Test
132     @Alerts("SyntaxError/DOMException")
133     public void msPlaceholder() throws Exception {
134         final String html = MS_PLACEHOLDER_HTML_HEAD
135                 + "<body onload='test();'>\n"
136                 + "<form>\n"
137                 + "  <input placeholder='htmlUnit supports placeholder-shown'>\n"
138                 + "</form></body></html>";
139 
140         loadPageVerifyTitle2(html);
141     }
142 
143     /**
144      * @throws Exception if the test fails
145      */
146     @Test
147     @Alerts("SyntaxError/DOMException")
148     public void msPlaceholder_number() throws Exception {
149         final String html = MS_PLACEHOLDER_HTML_HEAD
150                 + "<body onload='test();'>\n"
151                 + "<form>\n"
152                 + "  <input type='number' placeholder='2'>\n"
153                 + "</form></body></html>";
154 
155         loadPageVerifyTitle2(html);
156     }
157 
158     /**
159      * @throws Exception if the test fails
160      */
161     @Test
162     @Alerts("SyntaxError/DOMException")
163     public void msPlaceholder_displayNone() throws Exception {
164         final String html = MS_PLACEHOLDER_HTML_HEAD
165                 + "<body onload='test();'>\n"
166                 + "<form style='display:none;'>\n"
167                 + "  <input placeholder='htmlUnit supports placeholder-shown'>\n"
168                 + "</form></body></html>";
169         loadPageVerifyTitle2(html);
170     }
171 
172     /**
173      * @throws Exception if the test fails
174      */
175     @Test
176     @Alerts("SyntaxError/DOMException")
177     public void msPlaceholder_hasValue() throws Exception {
178         final String html = MS_PLACEHOLDER_HTML_HEAD
179                 + "<body onload='test();'>\n"
180                 + "<form style='display:none;'>\n"
181                 + "  <input placeholder='htmlUnit supports placeholder-shown' value='dont show placeholder'>\n"
182                 + "</form></body></html>";
183         loadPageVerifyTitle2(html);
184     }
185 
186     /**
187      * @throws Exception if the test fails
188      */
189     @Test
190     @Alerts("SyntaxError/DOMException")
191     public void msPlaceholder_noInput() throws Exception {
192         final String html = MS_PLACEHOLDER_HTML_HEAD
193                 + "<body onload='test();'>\n"
194                 + "<form style='display:none;'>\n"
195                 + "  <div placeholder='htmlUnit supports placeholder-shown' value='dont show placeholder'>\n"
196                 + "</form></body></html>";
197         loadPageVerifyTitle2(html);
198     }
199 
200 }