1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "content/renderer/date_time_formatter.h"
8 #include "content/renderer/renderer_date_time_picker.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/web/WebDateTimeChooserParams.h"
11 #include "third_party/icu/source/common/unicode/unistr.h"
12 #include "ui/base/ime/text_input_type.h"
16 class RendererDateTimePickerTest
{
19 TEST(RendererDateTimePickerTest
, TestParserValidStringInputs
) {
20 WebKit::WebDateTimeChooserParams params
;
21 params
.currentValue
= "2010-07";
22 params
.type
= WebKit::WebDateTimeInputTypeMonth
;
23 DateTimeFormatter
sut(params
);
24 EXPECT_EQ(2010, sut
.GetYear());
26 // Month field is 0 based
27 EXPECT_EQ(6, sut
.GetMonth());
29 // Month input defaults to the first day of the month (1 based)
30 EXPECT_EQ(1, sut
.GetDay());
31 EXPECT_EQ(0, sut
.GetHour());
32 EXPECT_EQ(0, sut
.GetMinute());
33 EXPECT_EQ(0, sut
.GetSecond());
34 EXPECT_EQ(2010, sut
.GetWeekYear());
35 EXPECT_EQ(26, sut
.GetWeek());
36 EXPECT_EQ(ui::TEXT_INPUT_TYPE_MONTH
, sut
.GetType());
38 params
.currentValue
= "2012-05-25";
39 params
.type
= WebKit::WebDateTimeInputTypeDate
;
40 DateTimeFormatter
sut2(params
);
41 EXPECT_EQ(2012, sut2
.GetYear());
42 EXPECT_EQ(4, sut2
.GetMonth());
43 EXPECT_EQ(25, sut2
.GetDay());
44 EXPECT_EQ(0, sut2
.GetHour());
45 EXPECT_EQ(0, sut2
.GetMinute());
46 EXPECT_EQ(0, sut2
.GetSecond());
47 EXPECT_EQ(2012, sut2
.GetWeekYear());
48 EXPECT_EQ(21, sut2
.GetWeek());
49 EXPECT_EQ(ui::TEXT_INPUT_TYPE_DATE
, sut2
.GetType());
51 params
.currentValue
= "2013-05-21T12:15";
52 params
.type
= WebKit::WebDateTimeInputTypeDateTimeLocal
;
53 DateTimeFormatter
sut3(params
);
54 EXPECT_EQ(2013, sut3
.GetYear());
55 EXPECT_EQ(4, sut3
.GetMonth());
56 EXPECT_EQ(21, sut3
.GetDay());
57 EXPECT_EQ(12, sut3
.GetHour());
58 EXPECT_EQ(15, sut3
.GetMinute());
59 EXPECT_EQ(0, sut3
.GetSecond());
60 EXPECT_EQ(2013, sut3
.GetWeekYear());
61 EXPECT_EQ(21, sut3
.GetWeek());
62 EXPECT_EQ(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL
, sut3
.GetType());
64 params
.currentValue
= "2013-W15";
65 params
.type
= WebKit::WebDateTimeInputTypeWeek
;
66 DateTimeFormatter
sut4(params
);
67 EXPECT_EQ(2013, sut4
.GetYear());
68 EXPECT_EQ(3, sut4
.GetMonth());
69 EXPECT_EQ(7, sut4
.GetDay());
70 EXPECT_EQ(0, sut4
.GetHour());
71 EXPECT_EQ(0, sut4
.GetMinute());
72 EXPECT_EQ(0, sut4
.GetSecond());
73 EXPECT_EQ(2013, sut4
.GetWeekYear());
74 EXPECT_EQ(15, sut4
.GetWeek());
75 EXPECT_EQ(ui::TEXT_INPUT_TYPE_WEEK
, sut4
.GetType());
77 params
.currentValue
= "12:15";
78 params
.type
= WebKit::WebDateTimeInputTypeTime
;
79 DateTimeFormatter
sut5(params
);
80 EXPECT_EQ(12, sut5
.GetHour());
81 EXPECT_EQ(15, sut5
.GetMinute());
82 EXPECT_EQ(0, sut5
.GetSecond());
83 EXPECT_EQ(0, sut5
.GetMilli());
84 EXPECT_EQ(ui::TEXT_INPUT_TYPE_TIME
, sut5
.GetType());
86 params
.currentValue
= "12:15:02";
87 params
.type
= WebKit::WebDateTimeInputTypeTime
;
88 DateTimeFormatter
sut6(params
);
89 EXPECT_EQ(12, sut6
.GetHour());
90 EXPECT_EQ(15, sut6
.GetMinute());
91 EXPECT_EQ(02, sut6
.GetSecond());
92 EXPECT_EQ(0, sut6
.GetMilli());
93 EXPECT_EQ(ui::TEXT_INPUT_TYPE_TIME
, sut6
.GetType());
95 params
.currentValue
= "12:15:02.1";
96 params
.type
= WebKit::WebDateTimeInputTypeTime
;
97 DateTimeFormatter
sut7(params
);
98 EXPECT_EQ(12, sut7
.GetHour());
99 EXPECT_EQ(15, sut7
.GetMinute());
100 EXPECT_EQ(02, sut7
.GetSecond());
101 EXPECT_EQ(100, sut7
.GetMilli());
102 EXPECT_EQ(ui::TEXT_INPUT_TYPE_TIME
, sut7
.GetType());
104 params
.currentValue
= "12:15:02.123";
105 params
.type
= WebKit::WebDateTimeInputTypeTime
;
106 DateTimeFormatter
sut8(params
);
107 EXPECT_EQ(12, sut8
.GetHour());
108 EXPECT_EQ(15, sut8
.GetMinute());
109 EXPECT_EQ(02, sut8
.GetSecond());
110 EXPECT_EQ(123, sut8
.GetMilli());
111 EXPECT_EQ(ui::TEXT_INPUT_TYPE_TIME
, sut8
.GetType());
115 TEST(RendererDateTimePickerTest
, TestParserInvalidStringInputs
) {
117 // Random non parsable text
118 WebKit::WebDateTimeChooserParams params
;
119 params
.currentValue
= "<script injection";
120 params
.type
= WebKit::WebDateTimeInputTypeMonth
;
121 DateTimeFormatter
sut(params
);
122 EXPECT_EQ(0, sut
.GetYear());
123 EXPECT_EQ(0, sut
.GetMonth());
124 EXPECT_EQ(0, sut
.GetDay());
125 EXPECT_EQ(0, sut
.GetHour());
126 EXPECT_EQ(0, sut
.GetMinute());
127 EXPECT_EQ(0, sut
.GetSecond());
128 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE
, sut
.GetType());
130 // unimplemented type
131 params
.currentValue
= "week 23";
132 params
.type
= WebKit::WebDateTimeInputTypeWeek
; // Not implemented
133 DateTimeFormatter
sut2(params
);
134 EXPECT_EQ(0, sut2
.GetYear());
135 EXPECT_EQ(0, sut2
.GetMonth());
136 EXPECT_EQ(0, sut2
.GetDay());
137 EXPECT_EQ(0, sut2
.GetHour());
138 EXPECT_EQ(0, sut2
.GetMinute());
139 EXPECT_EQ(0, sut2
.GetSecond());
140 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE
, sut2
.GetType());
142 // type is a subset of pattern
143 params
.currentValue
= "2012-05-25";
144 params
.type
= WebKit::WebDateTimeInputTypeDateTimeLocal
;
145 DateTimeFormatter
sut3(params
);
146 EXPECT_EQ(0, sut3
.GetYear());
147 EXPECT_EQ(0, sut3
.GetMonth());
148 EXPECT_EQ(0, sut3
.GetDay());
149 EXPECT_EQ(0, sut3
.GetHour());
150 EXPECT_EQ(0, sut3
.GetMinute());
151 EXPECT_EQ(0, sut3
.GetSecond());
152 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE
, sut3
.GetType());
154 // type is a superset of pattern
155 params
.currentValue
= "2013-05-21T12:15";
156 params
.type
= WebKit::WebDateTimeInputTypeMonth
;
157 DateTimeFormatter
sut4(params
);
158 EXPECT_EQ(2013, sut4
.GetYear());
159 EXPECT_EQ(4, sut4
.GetMonth());
160 EXPECT_EQ(1, sut4
.GetDay());
161 EXPECT_EQ(0, sut4
.GetHour());
162 EXPECT_EQ(0, sut4
.GetMinute());
163 EXPECT_EQ(0, sut4
.GetSecond());
164 EXPECT_EQ(ui::TEXT_INPUT_TYPE_MONTH
, sut4
.GetType());
168 TEST(RendererDateTimePickerTest
, TestParserValidDateInputs
) {
169 DateTimeFormatter
sut(
170 ui::TEXT_INPUT_TYPE_MONTH
, 2012, 11, 1, 0, 0, 0, 0, 0, 0);
171 EXPECT_EQ("2012-12", sut
.GetFormattedValue());
173 DateTimeFormatter
sut2(
174 ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL
, 2013, 3, 23, 15, 47, 0, 0, 0, 0);
175 EXPECT_EQ("2013-04-23T15:47", sut2
.GetFormattedValue());
177 DateTimeFormatter
sut3(
178 ui::TEXT_INPUT_TYPE_WEEK
, 0, 0, 0, 0, 0, 0, 0, 2012, 2);
179 EXPECT_EQ("2012-W02", sut3
.GetFormattedValue());
182 TEST(RendererDateTimePickerTest
, TestParserValidTimeInputs
) {
183 DateTimeFormatter
sut(
184 ui::TEXT_INPUT_TYPE_TIME
, 0, 0, 0, 12, 15, 0, 0, 0, 0);
185 EXPECT_EQ("12:15", sut
.GetFormattedValue());
187 DateTimeFormatter
sut2(
188 ui::TEXT_INPUT_TYPE_TIME
, 0, 0, 0, 12, 15, 02, 0, 0, 0);
189 EXPECT_EQ("12:15:02", sut2
.GetFormattedValue());
191 DateTimeFormatter
sut3(
192 ui::TEXT_INPUT_TYPE_TIME
, 0, 0, 0, 12, 15, 02, 123, 0, 0);
193 EXPECT_EQ("12:15:02.123", sut3
.GetFormattedValue());
196 TEST(RendererDateTimePickerTest
, TestParserInvalidDateInputs
) {
197 DateTimeFormatter
sut(ui::TEXT_INPUT_TYPE_WEEK
, 0, 0, 0, 0, 0, 0, 0, 0, 0);
198 EXPECT_EQ("", sut
.GetFormattedValue());
200 DateTimeFormatter
sut2(
201 ui::TEXT_INPUT_TYPE_NONE
, 2013, 3, 23, 0, 0, 0, 0, 0, 0);
202 EXPECT_EQ("", sut2
.GetFormattedValue());
204 DateTimeFormatter
sut3(
205 ui::TEXT_INPUT_TYPE_NONE
, 2013, 14, 32, 0, 0, 0, 0, 0, 0);
206 EXPECT_EQ("", sut3
.GetFormattedValue());
208 DateTimeFormatter
sut4(ui::TEXT_INPUT_TYPE_DATE
, 0, 0, 0, 0, 0, 0, 0, 0, 0);
209 EXPECT_EQ("", sut4
.GetFormattedValue());
211 DateTimeFormatter
sut5(ui::TEXT_INPUT_TYPE_TIME
, 0, 0, 0, 0, 0, 0, 0, 0, 0);
212 EXPECT_EQ("", sut5
.GetFormattedValue());
214 DateTimeFormatter
sut6(
215 ui::TEXT_INPUT_TYPE_PASSWORD
, 23, 0, 0, 0, 5, 0, 0, 0, 0);
216 EXPECT_EQ("", sut6
.GetFormattedValue());
218 DateTimeFormatter
sut7(ui::TEXT_INPUT_TYPE_MAX
, 23, 0, 0, 0, 5, 0, 0, 0, 0);
219 EXPECT_EQ("", sut7
.GetFormattedValue());
221 DateTimeFormatter
sut8(
222 static_cast<ui::TextInputType
>(10000), 23, 0, 0, 0, 5, 0, 0, 0, 0);
223 EXPECT_EQ("", sut8
.GetFormattedValue());
225 } // namespace content