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