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.BrowserRunner;
19 import org.htmlunit.junit.annotation.Alerts;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22
23
24
25
26
27
28
29
30
31
32 @RunWith(BrowserRunner.class)
33 public class NativeDateTest extends WebDriverTestCase {
34
35
36
37
38
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
59
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
91
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
103
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
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
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
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
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 }