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;
16  
17  import org.htmlunit.WebDriverTestCase;
18  import org.htmlunit.junit.BrowserRunner;
19  import org.htmlunit.junit.annotation.Alerts;
20  import org.junit.Test;
21  import org.junit.runner.RunWith;
22  
23  /**
24   * Date is a native JavaScript object and therefore provided by Rhino but behavior should be
25   * different depending on the simulated browser.
26   *
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Frank Danek
30   * @author Ronald Brill
31   */
32  @RunWith(BrowserRunner.class)
33  public class NativeDateTest extends WebDriverTestCase {
34  
35      /**
36       * Test for bug <a href="http://sourceforge.net/support/tracker.php?aid=2615817">2615817</a>.
37       * @see <a href="http://www.w3schools.com/jsref/jsref_getYear.asp">this doc</a>
38       * @throws Exception if the test fails
39       */
40      @Test
41      @Alerts({"-13", "84", "109"})
42      public void getYear() throws Exception {
43          final String html = DOCTYPE_HTML
44              + "<html><head><script>\n"
45              + LOG_TITLE_FUNCTION
46              + "function doTest() {\n"
47              + "  log(new Date(1887, 2, 1).getYear());\n"
48              + "  log(new Date(1984, 2, 1).getYear());\n"
49              + "  log(new Date(2009, 2, 1).getYear());\n"
50              + "}\n"
51              + "</script></head><body onload='doTest()'>\n"
52              + "</body></html>";
53  
54          loadPageVerifyTitle2(html);
55      }
56  
57      /**
58       * Test for the methods with the same expectations for all browsers.
59       * @throws Exception if the test fails
60       */
61      @Test
62      @Alerts({"constructor: function", "getDate: function", "getDay: function", "getFullYear: function",
63               "getHours: function", "getMilliseconds: function", "getMinutes: function", "getMonth: function",
64               "getSeconds: function", "getTime: function", "getTimezoneOffset: function", "getUTCDate: function",
65               "getUTCDay: function", "getUTCFullYear: function", "getUTCHours: function", "getUTCMilliseconds: function",
66               "getUTCMinutes: function", "getUTCMonth: function", "getUTCSeconds: function", "getYear: function",
67               "now: undefined", "parse: undefined", "setDate: function", "setFullYear: function", "setHours: function",
68               "setMilliseconds: function", "setMinutes: function", "setMonth: function", "setSeconds: function",
69               "setTime: function", "setUTCDate: function", "setUTCFullYear: function", "setUTCHours: function",
70               "setUTCMilliseconds: function", "setUTCMinutes: function", "setUTCMonth: function",
71               "setUTCSeconds: function", "setYear: function", "toDateString: function",
72               "toLocaleDateString: function", "toLocaleString: function",
73               "toLocaleTimeString: function", "toString: function", "toTimeString: function",
74               "toUTCString: function", "valueOf: function", "UTC: undefined"})
75      public void methods_common() throws Exception {
76          final String[] methods = {
77              "constructor", "getDate", "getDay", "getFullYear", "getHours", "getMilliseconds",
78              "getMinutes", "getMonth", "getSeconds", "getTime", "getTimezoneOffset", "getUTCDate", "getUTCDay",
79              "getUTCFullYear", "getUTCHours", "getUTCMilliseconds", "getUTCMinutes", "getUTCMonth", "getUTCSeconds",
80              "getYear", "now", "parse", "setDate", "setFullYear", "setHours", "setMilliseconds", "setMinutes",
81              "setMonth", "setSeconds", "setTime", "setUTCDate", "setUTCFullYear", "setUTCHours",
82              "setUTCMilliseconds", "setUTCMinutes", "setUTCMonth", "setUTCSeconds", "setYear", "toDateString",
83              "toLocaleDateString", "toLocaleString", "toLocaleTimeString", "toString",
84              "toTimeString", "toUTCString", "valueOf", "UTC"};
85          final String html = createHTMLTestMethods("new Date()", methods);
86          loadPageVerifyTitle2(html);
87      }
88  
89      /**
90       * Test for the methods with different expectations depending on the browsers.
91       * @throws Exception if the test fails
92       */
93      @Test
94      @Alerts("toSource: undefined")
95      public void methods_toSource() throws Exception {
96          final String[] methods = {"toSource"};
97          final String html = createHTMLTestMethods("new Date()", methods);
98          loadPageVerifyTitle2(html);
99      }
100 
101     /**
102      * Test for the methods with different expectations depending on the browsers.
103      * @throws Exception if the test fails
104      */
105     @Test
106     @Alerts({"toISOString: function", "toJSON: function"})
107     public void methods_differences() throws Exception {
108         final String[] methods = {"toISOString", "toJSON"};
109         final String html = createHTMLTestMethods("new Date()", methods);
110         loadPageVerifyTitle2(html);
111     }
112 
113     static String createHTMLTestMethods(final String object, final String... methodNames) throws Exception {
114         final StringBuilder methodList = new StringBuilder();
115         for (final String methodName : methodNames) {
116             methodList.append(", '").append(methodName).append("'");
117         }
118         final String html = DOCTYPE_HTML
119             + "<html><head><script>\n"
120             + LOG_TITLE_FUNCTION
121             + "function doTest() {\n"
122             + "  var o = " + object + ";\n"
123             + "  var props = [" + methodList.substring(2) + "];\n"
124             + "  for (var i = 0; i < props.length; i++) {\n"
125             + "    var p = props[i];\n"
126             + "    log(p + ': ' + typeof(o[p]));\n"
127             + "  }\n"
128             + "}\n"
129             + "</script></head><body onload='doTest()'>\n"
130             + "</body></html>";
131 
132         return html;
133     }
134 
135     /**
136      * @throws Exception if the test fails
137      */
138     @Test
139     @Alerts({"2005-12-03T07:14:15.000Z", "2005-07-12T11:04:15.000Z",
140              "2005-07-03T15:14:05.000Z"})
141     public void toISOString() throws Exception {
142         final String html = DOCTYPE_HTML
143             + "<html><head><script>\n"
144             + LOG_TITLE_FUNCTION
145             + "function test() {\n"
146             + "  if (new Date().toISOString) {\n"
147             + "    log(new Date(Date.UTC(2005, 11, 3, 7, 14, 15)).toISOString());\n"
148             + "    log(new Date(Date.UTC(2005, 6, 12, 11, 4, 15)).toISOString());\n"
149             + "    log(new Date(Date.UTC(2005, 6, 3, 15, 14, 5)).toISOString());\n"
150             + "  }\n"
151             + "}\n"
152             + "</script></head><body onload='test()'>\n"
153             + "</body></html>";
154 
155         loadPageVerifyTitle2(html);
156     }
157 
158     /**
159      * @throws Exception if the test fails
160      */
161     @Test
162     @Alerts({"Sat, 03 Dec 2005 07:14:15 GMT", "Tue, 12 Jul 2005 11:04:15 GMT",
163              "Sun, 03 Jul 2005 15:14:05 GMT"})
164     public void toUTCString() throws Exception {
165         final String html = DOCTYPE_HTML
166             + "<html><head><script>\n"
167             + LOG_TITLE_FUNCTION
168             + "function test() {\n"
169             + "  log(new Date(Date.UTC(2005, 11, 3, 7, 14, 15)).toUTCString());\n"
170             + "  log(new Date(Date.UTC(2005, 6, 12, 11, 4, 15)).toUTCString());\n"
171             + "  log(new Date(Date.UTC(2005, 6, 3, 15, 14, 5)).toUTCString());\n"
172             + "}\n"
173             + "</script></head><body onload='test()'>\n"
174             + "</body></html>";
175 
176         loadPageVerifyTitle2(html);
177     }
178 
179     /**
180      * @throws Exception if the test fails
181      */
182     @Test
183     @Alerts({"Sat, 03 Dec 2005 07:14:15 GMT", "Tue, 12 Jul 2005 11:04:15 GMT",
184              "Sun, 03 Jul 2005 15:14:05 GMT"})
185     public void toGMTString() throws Exception {
186         final String html = DOCTYPE_HTML
187             + "<html><head><script>\n"
188             + LOG_TITLE_FUNCTION
189             + "function test() {\n"
190             + "  log(new Date(Date.UTC(2005, 11, 3, 7, 14, 15)).toGMTString());\n"
191             + "  log(new Date(Date.UTC(2005, 6, 12, 11, 4, 15)).toGMTString());\n"
192             + "  log(new Date(Date.UTC(2005, 6, 3, 15, 14, 5)).toGMTString());\n"
193             + "}\n"
194             + "</script></head><body onload='test()'>\n"
195             + "</body></html>";
196 
197         loadPageVerifyTitle2(html);
198     }
199 
200     /**
201      * @throws Exception if the test fails
202      */
203     @Test
204     public void enumerable() throws Exception {
205         final String html = DOCTYPE_HTML
206             + "<html><head><script>\n"
207             + "function test() {\n"
208             + "  var date = new Date(2000, 0, 1);\n"
209             + "  for (var x in date) {\n"
210             + "    log(x);\n"
211             + "  }\n"
212             + "}\n"
213             + "</script></head><body onload='test()'>\n"
214             + "</body></html>";
215 
216         loadPageVerifyTitle2(html);
217     }
218 }