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;
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 {@link Screen}.
23   *
24   * @author Daniel Gredler
25   * @author Marc Guillemot
26   * @author Ronald Brill
27   * @see <a href="http://msdn.microsoft.com/en-us/library/ms535868.aspx">MSDN documentation</a>
28   * @see <a href="http://www.mozilla.org/docs/dom/domref/dom_window_ref.html">Mozilla documentation</a>
29   */
30  public class ScreenTest extends WebDriverTestCase {
31  
32      /**
33       * @throws Exception on test failure
34       */
35      @Test
36      @Alerts({"1040", "1040"})
37      public void availHeight() throws Exception {
38          testNumericProperty("availHeight");
39      }
40  
41      /**
42       * @throws Exception on test failure
43       */
44      @Test
45      @Alerts({"0", "0"})
46      public void availLeft() throws Exception {
47          testNumericProperty("availLeft");
48      }
49  
50      /**
51       * @throws Exception on test failure
52       */
53      @Test
54      @Alerts({"0", "0"})
55      public void availTop() throws Exception {
56          testNumericProperty("availTop");
57      }
58  
59      /**
60       * @throws Exception on test failure
61       */
62      @Test
63      @Alerts({"1920", "1920"})
64      public void availWidth() throws Exception {
65          testNumericProperty("availWidth");
66      }
67  
68      /**
69       * @throws Exception on test failure
70       */
71      @Test
72      @Alerts({"undefined", "1234"})
73      public void bufferDepth() throws Exception {
74          testNumericProperty("bufferDepth");
75      }
76  
77      /**
78       * @throws Exception on test failure
79       */
80      @Test
81      @Alerts({"24", "24"})
82      public void colorDepth() throws Exception {
83          testNumericProperty("colorDepth");
84      }
85  
86      /**
87       * @throws Exception on test failure
88       */
89      @Test
90      @Alerts({"undefined", "1234"})
91      public void deviceXDPI() throws Exception {
92          testNumericProperty("deviceXDPI");
93      }
94  
95      /**
96       * @throws Exception on test failure
97       */
98      @Test
99      @Alerts({"undefined", "1234"})
100     public void deviceYDPI() throws Exception {
101         testNumericProperty("deviceYDPI");
102     }
103 
104     /**
105      * @throws Exception on test failure
106      */
107     @Test
108     @Alerts({"undefined", "false"})
109     public void fontSmoothingEnabled() throws Exception {
110         testBooleanProperty("fontSmoothingEnabled");
111     }
112 
113     /**
114      * @throws Exception on test failure
115      */
116     @Test
117     @Alerts({"1080", "1080"})
118     public void height() throws Exception {
119         testNumericProperty("height");
120     }
121 
122     /**
123      * @throws Exception on test failure
124      */
125     @Test
126     @Alerts(DEFAULT = {"undefined", "1234"},
127             FF = {"0", "0"},
128             FF_ESR = {"0", "0"})
129     public void left() throws Exception {
130         testNumericProperty("left");
131     }
132 
133     /**
134      * @throws Exception on test failure
135      */
136     @Test
137     @Alerts(DEFAULT = {"undefined", "1234"},
138             FF = {"0", "0"},
139             FF_ESR = {"0", "0"})
140     public void top() throws Exception {
141         testNumericProperty("top");
142     }
143 
144     /**
145      * @throws Exception on test failure
146      */
147     @Test
148     @Alerts({"undefined", "1234"})
149     public void logicalXDPI() throws Exception {
150         testNumericProperty("logicalXDPI");
151     }
152 
153     /**
154      * @throws Exception on test failure
155      */
156     @Test
157     @Alerts({"undefined", "1234"})
158     public void logicalYDPI() throws Exception {
159         testNumericProperty("logicalYDPI");
160     }
161 
162     /**
163      * @throws Exception on test failure
164      */
165     @Test
166     @Alerts({"24", "24"})
167     public void pixelDepth() throws Exception {
168         testNumericProperty("pixelDepth");
169     }
170 
171     /**
172      * @throws Exception on test failure
173      */
174     @Test
175     @Alerts({"undefined", "1234"})
176     public void systemXDPI() throws Exception {
177         testNumericProperty("systemXDPI");
178     }
179 
180     /**
181      * @throws Exception on test failure
182      */
183     @Test
184     @Alerts({"undefined", "1234"})
185     public void systemYDPI() throws Exception {
186         testNumericProperty("systemYDPI");
187     }
188 
189     /**
190      * @throws Exception on test failure
191      */
192     @Test
193     @Alerts({"undefined", "1234"})
194     public void updateInterval() throws Exception {
195         testNumericProperty("updateInterval");
196     }
197 
198     /**
199      * @throws Exception on test failure
200      */
201     @Test
202     @Alerts({"1920", "1920"})
203     public void width() throws Exception {
204         testNumericProperty("width");
205     }
206 
207     private void testBooleanProperty(final String prop) throws Exception {
208         final String html = DOCTYPE_HTML
209             + "<html><head>\n"
210             + "  <script>\n"
211             + LOG_TITLE_FUNCTION
212             + "    function doTest() {\n"
213             + "      try {\n"
214             + "        log(window.screen." + prop + ");\n"
215             + "      } catch(e) { log('get exception') }\n"
216 
217             + "      try {\n"
218             + "        window.screen." + prop + " = false;\n"
219             + "        log(window.screen." + prop + ");\n"
220             + "      } catch(e) { log('set exception') }\n"
221             + "    }\n"
222             + "  </script>\n"
223             + "</head>\n"
224             + "<body onload='doTest()'>\n"
225             + "</body></html>";
226 
227         loadPageVerifyTitle2(html);
228     }
229 
230     private void testNumericProperty(final String prop) throws Exception {
231         final String html = DOCTYPE_HTML
232             + "<html><head>\n"
233             + "  <script>\n"
234             + LOG_TITLE_FUNCTION
235             + "    function doTest() {\n"
236             + "      try {\n"
237             + "        log(window.screen." + prop + ");\n"
238             + "      } catch(e) { log('get exception') }\n"
239 
240             + "      try {\n"
241             + "        window.screen." + prop + " = 1234;\n"
242             + "        log(window.screen." + prop + ");\n"
243             + "      } catch(e) { log('set exception') }\n"
244             + "    }\n"
245             + "  </script>\n"
246             + "</head>\n"
247             + "<body onload='doTest()'>\n"
248             + "</body></html>";
249 
250         loadPageVerifyTitle2(html);
251     }
252 
253     /**
254      * @throws Exception on test failure
255      */
256     @Test
257     @Alerts({"[object ScreenOrientation]", "landscape-primary", "0"})
258     public void orientation() throws Exception {
259         final String html = DOCTYPE_HTML
260             + "<html><head>\n"
261             + "  <script>\n"
262             + LOG_TITLE_FUNCTION
263             + "    function doTest() {\n"
264             + "      try {\n"
265             + "        var o = window.screen.orientation;"
266             + "        log(o);\n"
267             + "        log(o.type);\n"
268             + "        log(o.angle);\n"
269             + "      } catch(e) { logEx(e) }\n"
270             + "    }\n"
271             + "  </script>\n"
272             + "</head>\n"
273             + "<body onload='doTest()'>\n"
274             + "</body></html>";
275 
276         loadPageVerifyTitle2(html);
277     }
278 
279     /**
280      * @throws Exception on test failure
281      */
282     @Test
283     @Alerts(DEFAULT = "undefined",
284             FF = "landscape-primary",
285             FF_ESR = "landscape-primary")
286     public void mozOrientation() throws Exception {
287         final String html = DOCTYPE_HTML
288             + "<html><head>\n"
289             + "  <script>\n"
290             + LOG_TITLE_FUNCTION
291             + "    function doTest() {\n"
292             + "      try {\n"
293             + "        var o = window.screen.mozOrientation;"
294             + "        log(o);\n"
295             + "      } catch(e) { logEx(e) }\n"
296             + "    }\n"
297             + "  </script>\n"
298             + "</head>\n"
299             + "<body onload='doTest()'>\n"
300             + "</body></html>";
301 
302         loadPageVerifyTitle2(html);
303     }
304 
305     /**
306      * @throws Exception on test failure
307      */
308     @Test
309     @Alerts(DEFAULT = "false",
310             FF = "undefined",
311             FF_ESR = "undefined")
312     public void isExtended() throws Exception {
313         final String html = DOCTYPE_HTML
314             + "<html><head>\n"
315             + "  <script>\n"
316             + LOG_TITLE_FUNCTION
317             + "    function doTest() {\n"
318             + "      try {\n"
319             + "        log(window.screen.isExtended);\n"
320             + "      } catch(e) { logEx(e) }\n"
321             + "    }\n"
322             + "  </script>\n"
323             + "</head>\n"
324             + "<body onload='doTest()'>\n"
325             + "</body></html>";
326 
327         loadPageVerifyTitle2(html);
328     }
329 }