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