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.intl;
16  
17  import java.util.Locale;
18  import java.util.TimeZone;
19  
20  import org.htmlunit.BrowserVersion;
21  import org.htmlunit.WebDriverTestCase;
22  import org.htmlunit.junit.BrowserRunner;
23  import org.htmlunit.junit.annotation.Alerts;
24  import org.htmlunit.junit.annotation.BuggyWebDriver;
25  import org.htmlunit.junit.annotation.HtmlUnitNYI;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  
29  /**
30   * Tests for {@link DateTimeFormat}.
31   *
32   * @author Ronald Brill
33   * @author Ahmed Ashour
34   */
35  @RunWith(BrowserRunner.class)
36  public class DateTimeFormatTest extends WebDriverTestCase {
37  
38      /**
39       * @throws Exception if the test fails
40       */
41      @Test
42      @Alerts({"zh-CN", "gregory", "latn", "UTC", "undefined", "undefined", "undefined",
43               "numeric", "numeric", "numeric", "undefined", "undefined", "undefined", "undefined"})
44      @HtmlUnitNYI(CHROME = {"zh-CN", "undefined", "undefined", "America/New_York", "undefined", "undefined", "undefined",
45                             "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"},
46              EDGE = {"zh-CN", "undefined", "undefined", "America/New_York", "undefined", "undefined", "undefined",
47                      "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"},
48              FF = {"zh-CN", "undefined", "undefined", "America/New_York", "undefined", "undefined", "undefined",
49                    "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"},
50              FF_ESR = {"zh-CN", "undefined", "undefined", "America/New_York", "undefined", "undefined", "undefined",
51                        "undefined", "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"})
52      public void resolvedOptionsValues() throws Exception {
53          final String html = DOCTYPE_HTML
54                  + "<html><head>\n"
55                  + "<script>\n"
56                  + LOG_TITLE_FUNCTION
57                  + "  function test() {\n"
58                  + "    var region1 = new Intl.DateTimeFormat('zh-CN', { timeZone: 'UTC' });\n"
59                  + "    var options = region1.resolvedOptions();\n"
60                  + "    log(options.locale);\n"
61                  + "    log(options.calendar);\n"
62                  + "    log(options.numberingSystem);\n"
63                  + "    log(options.timeZone);\n"
64                  + "    log(options.hour12);\n"
65                  + "    log(options.weekday);\n"
66                  + "    log(options.era);\n"
67                  + "    log(options.year);\n"
68                  + "    log(options.month);\n"
69                  + "    log(options.day);\n"
70                  + "    log(options.hour);\n"
71                  + "    log(options.minute);\n"
72                  + "    log(options.second);\n"
73                  + "    log(options.timeZoneName);\n"
74                  + "  }\n"
75                  + "</script>\n"
76                  + "</head>\n"
77                  + "<body onload='test()'>\n"
78                  + "</body></html>";
79  
80          loadPageVerifyTitle2(html);
81      }
82  
83      /**
84       * @throws Exception if the test fails
85       */
86      @Test
87      @Alerts("[object Object]")
88      public void resolvedOptions() throws Exception {
89          final String html = DOCTYPE_HTML
90                  + "<html><head>\n"
91                  + "<script>\n"
92                  + LOG_TITLE_FUNCTION
93                  + "  function test() {\n"
94                  + "    var region1 = new Intl.DateTimeFormat('zh-CN', { timeZone: 'UTC' });\n"
95                  + "    var options = region1.resolvedOptions();\n"
96                  + "    log(options);\n"
97                  + "  }\n"
98                  + "</script>\n"
99                  + "</head>\n"
100                 + "<body onload='test()'>\n"
101                 + "</body></html>";
102 
103         loadPageVerifyTitle2(html);
104     }
105 
106     /**
107      * @throws Exception if the test fails
108      */
109     @Test
110     @Alerts({"true", "12/19/2013"})
111     @BuggyWebDriver(FF = {"true", "12/20/2013"}, FF_ESR = {"true", "12/20/2013"})
112     public void dateTimeFormat() throws Exception {
113         final String html = DOCTYPE_HTML
114                 + "<html><head>\n"
115                 + "<script>\n"
116                 + LOG_TITLE_FUNCTION
117                 + "  function test() {\n"
118                 + "    var dateFormat = Intl.DateTimeFormat('en');\n"
119                 + "    log(dateFormat instanceof Intl.DateTimeFormat);\n"
120 
121                 + "    var date = new Date(Date.UTC(2013, 11, 20, 3, 0, 0));\n"
122                 + "    log(dateFormat.format(date));\n"
123                 + "  }\n"
124                 + "</script>\n"
125                 + "</head>\n"
126                 + "<body onload='test()'>\n"
127                 + "</body></html>";
128 
129         loadPageVerifyTitle2(html);
130     }
131 
132     /**
133      * @throws Exception if the test fails
134      */
135     @Test
136     @Alerts({"true", "12/20/2013"})
137     public void dateTimeFormatGMT() throws Exception {
138         dateTimeFormat("GMT");
139     }
140 
141     /**
142      * @throws Exception if the test fails
143      */
144     @Test
145     @Alerts({"true", "12/20/2013"})
146     public void dateTimeFormatUTC() throws Exception {
147         dateTimeFormat("UTC");
148     }
149 
150     /**
151      * @throws Exception if the test fails
152      */
153     @Test
154     @Alerts({"true", "12/19/2013"})
155     @BuggyWebDriver(FF = {"true", "12/20/2013"}, FF_ESR = {"true", "12/20/2013"})
156     public void dateTimeFormatEST() throws Exception {
157         dateTimeFormat("EST");
158     }
159 
160     /**
161      * @throws Exception if the test fails
162      */
163     @Test
164     @Alerts({"true", "12/20/2013"})
165     public void dateTimeFormatBerlin() throws Exception {
166         dateTimeFormat("Europe/Berlin");
167     }
168 
169     /**
170      * @throws Exception if the test fails
171      */
172     @Test
173     @Alerts({"true", "12/19/2013"})
174     @BuggyWebDriver(FF = {"true", "12/20/2013"}, FF_ESR = {"true", "12/20/2013"})
175     public void dateTimeFormatNewYork() throws Exception {
176         dateTimeFormat("America/New_York");
177     }
178 
179     /**
180      * @throws Exception if the test fails
181      */
182     @Test
183     @Alerts({"true", "12/20/2013"})
184     public void dateTimeFormatTokyo() throws Exception {
185         dateTimeFormat("Asia/Tokyo");
186     }
187 
188     /**
189      * @throws Exception if the test fails
190      */
191     @Test
192     @Alerts({"true", "12/20/2013"})
193     public void dateTimeFormatJST() throws Exception {
194         dateTimeFormat("JST");
195     }
196 
197     private void dateTimeFormat(final String tz) throws Exception {
198         final String html = DOCTYPE_HTML
199                 + "<html><head>\n"
200                 + "<script>\n"
201                 + LOG_TITLE_FUNCTION
202                 + "  function test() {\n"
203                 + "    var dateFormat = Intl.DateTimeFormat('en');\n"
204                 + "    log(dateFormat instanceof Intl.DateTimeFormat);\n"
205 
206                 + "    var date = new Date(Date.UTC(2013, 11, 20, 3, 0, 0));\n"
207                 + "    log(dateFormat.format(date));\n"
208                 + "  }\n"
209                 + "</script>\n"
210                 + "</head>\n"
211                 + "<body onload='test()'>\n"
212                 + "</body></html>";
213 
214         shutDownAll();
215         try {
216             final BrowserVersion.BrowserVersionBuilder builder
217                 = new BrowserVersion.BrowserVersionBuilder(getBrowserVersion());
218             builder.setSystemTimezone(TimeZone.getTimeZone(tz));
219             setBrowserVersion(builder.build());
220 
221             loadPageVerifyTitle2(html);
222         }
223         finally {
224             shutDownAll();
225         }
226     }
227 
228     /**
229      * @throws Exception if the test fails
230      */
231     @Test
232     @Alerts("America/New_York")
233     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
234     public void timeZone() throws Exception {
235         final String html = DOCTYPE_HTML
236             + "<html><head><script>\n"
237             + LOG_TITLE_FUNCTION
238             + "function test() {\n"
239             + "  log(Intl.DateTimeFormat().resolvedOptions().timeZone);\n"
240             + "}\n"
241             + "</script></head><body onload='test()'>\n"
242             + "</body></html>";
243 
244         loadPageVerifyTitle2(html);
245     }
246 
247     /**
248      * @throws Exception if the test fails
249      */
250     @Test
251     @Alerts(DEFAULT = "+00:00",
252             FF = "GMT",
253             FF_ESR = "GMT")
254     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
255     @HtmlUnitNYI(CHROME = "GMT", EDGE = "GMT")
256     public void timeZoneGMT() throws Exception {
257         timeZone("GMT");
258     }
259 
260     /**
261      * @throws Exception if the test fails
262      */
263     @Test
264     @Alerts("UTC")
265     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
266     public void timeZoneUTC() throws Exception {
267         timeZone("UTC");
268     }
269 
270     /**
271      * @throws Exception if the test fails
272      */
273     @Test
274     @Alerts(DEFAULT = "America/Panama",
275             FF = "EST",
276             FF_ESR = "EST")
277     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
278     @HtmlUnitNYI(CHROME = "EST", EDGE = "EST")
279     public void timeZoneEST() throws Exception {
280         timeZone("EST");
281     }
282 
283     /**
284      * @throws Exception if the test fails
285      */
286     @Test
287     @Alerts("Europe/Berlin")
288     public void timeZoneBerlin() throws Exception {
289         timeZone("Europe/Berlin");
290     }
291 
292     /**
293      * @throws Exception if the test fails
294      */
295     @Test
296     @Alerts("America/New_York")
297     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
298     public void timeZoneNewYork() throws Exception {
299         timeZone("America/New_York");
300     }
301 
302     /**
303      * @throws Exception if the test fails
304      */
305     @Test
306     @Alerts("Asia/Tokyo")
307     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
308     public void timeZoneTokyo() throws Exception {
309         timeZone("Asia/Tokyo");
310     }
311 
312     /**
313      * @throws Exception if the test fails
314      */
315     @Test
316     @Alerts(DEFAULT = "Asia/Tokyo",
317             FF = "JST",
318             FF_ESR = "JST")
319     @BuggyWebDriver(FF = "Europe/Berlin", FF_ESR = "Europe/Berlin")
320     @HtmlUnitNYI(CHROME = "JST", EDGE = "JST")
321     public void timeZoneJST() throws Exception {
322         timeZone("JST");
323     }
324 
325     private void timeZone(final String tz) throws Exception {
326         final String html = DOCTYPE_HTML
327             + "<html><head><script>\n"
328             + LOG_TITLE_FUNCTION
329             + "function test() {\n"
330             + "  log(Intl.DateTimeFormat().resolvedOptions().timeZone);\n"
331             + "}\n"
332             + "</script></head><body onload='test()'>\n"
333             + "</body></html>";
334 
335         shutDownAll();
336         try {
337             final BrowserVersion.BrowserVersionBuilder builder
338                 = new BrowserVersion.BrowserVersionBuilder(getBrowserVersion());
339             builder.setSystemTimezone(TimeZone.getTimeZone(tz));
340             setBrowserVersion(builder.build());
341 
342             loadPageVerifyTitle2(html);
343         }
344         finally {
345             shutDownAll();
346         }
347     }
348 
349     /**
350      * @throws Exception if the test fails
351      */
352     @Test
353     @Alerts("en-US")
354     public void locale() throws Exception {
355         final String html = DOCTYPE_HTML
356             + "<html><head><script>\n"
357             + LOG_TITLE_FUNCTION
358             + "function test() {\n"
359             + "  log(Intl.DateTimeFormat().resolvedOptions().locale);\n"
360             + "}\n"
361             + "</script></head><body onload='test()'>\n"
362             + "</body></html>";
363 
364         loadPageVerifyTitle2(html);
365     }
366 
367     /**
368      * @throws Exception if the test fails
369      */
370     @Test
371     @Alerts(DEFAULT = "de",
372             EDGE = "de-DE")
373     @BuggyWebDriver(FF = "en-US", FF_ESR = "en-US")
374     @HtmlUnitNYI(CHROME = "de-DE",
375             FF = "de-DE",
376             FF_ESR = "de-DE")
377     public void localeGermany() throws Exception {
378         locale(Locale.GERMANY.toLanguageTag());
379     }
380 
381     /**
382      * @throws Exception if the test fails
383      */
384     @Test
385     @Alerts(DEFAULT = "de",
386             EDGE = "de-DE")
387     @BuggyWebDriver(FF = "en-US", FF_ESR = "en-US")
388     @HtmlUnitNYI(EDGE = "de")
389     public void localeGerman() throws Exception {
390         locale(Locale.GERMAN.toLanguageTag());
391     }
392 
393     /**
394      * @throws Exception if the test fails
395      */
396     @Test
397     @Alerts("fr")
398     @BuggyWebDriver(FF = "en-US", FF_ESR = "en-US")
399     public void localeFrench() throws Exception {
400         locale(Locale.FRENCH.toLanguageTag());
401     }
402 
403     /**
404      * @throws Exception if the test fails
405      */
406     @Test
407     @Alerts("en-GB")
408     @BuggyWebDriver(FF = "en-US", FF_ESR = "en-US")
409     @HtmlUnitNYI(CHROME = "en-CA",
410             EDGE = "en-CA",
411             FF = "en-CA",
412             FF_ESR = "en-CA")
413     public void localeCanada() throws Exception {
414         locale(Locale.CANADA.toLanguageTag());
415     }
416 
417     private void locale(final String language) throws Exception {
418         final String html = DOCTYPE_HTML
419             + "<html><head><script>\n"
420             + LOG_TITLE_FUNCTION
421             + "function test() {\n"
422             + "  log(Intl.DateTimeFormat().resolvedOptions().locale);\n"
423             + "}\n"
424             + "</script></head><body onload='test()'>\n"
425             + "</body></html>";
426 
427         shutDownAll();
428         try {
429             final BrowserVersion.BrowserVersionBuilder builder
430                 = new BrowserVersion.BrowserVersionBuilder(getBrowserVersion());
431             builder.setBrowserLanguage(language);
432             setBrowserVersion(builder.build());
433 
434             loadPageVerifyTitle2(html);
435         }
436         finally {
437             shutDownAll();
438         }
439     }
440 }