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/public/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());
79 TEST(RendererDateTimePickerTest
, TestParserInvalidStringInputs
) {
81 // Random non parsable text
82 WebKit::WebDateTimeChooserParams params
;
83 params
.currentValue
= "<script injection";
84 params
.type
= WebKit::WebDateTimeInputTypeMonth
;
85 DateTimeFormatter
sut(params
);
86 EXPECT_EQ(0, sut
.GetYear());
87 EXPECT_EQ(0, sut
.GetMonth());
88 EXPECT_EQ(0, sut
.GetDay());
89 EXPECT_EQ(0, sut
.GetHour());
90 EXPECT_EQ(0, sut
.GetMinute());
91 EXPECT_EQ(0, sut
.GetSecond());
92 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE
, sut
.GetType());
95 params
.currentValue
= "week 23";
96 params
.type
= WebKit::WebDateTimeInputTypeWeek
; // Not implemented
97 DateTimeFormatter
sut2(params
);
98 EXPECT_EQ(0, sut2
.GetYear());
99 EXPECT_EQ(0, sut2
.GetMonth());
100 EXPECT_EQ(0, sut2
.GetDay());
101 EXPECT_EQ(0, sut2
.GetHour());
102 EXPECT_EQ(0, sut2
.GetMinute());
103 EXPECT_EQ(0, sut2
.GetSecond());
104 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE
, sut2
.GetType());
106 // type is a subset of pattern
107 params
.currentValue
= "2012-05-25";
108 params
.type
= WebKit::WebDateTimeInputTypeDateTimeLocal
;
109 DateTimeFormatter
sut3(params
);
110 EXPECT_EQ(0, sut3
.GetYear());
111 EXPECT_EQ(0, sut3
.GetMonth());
112 EXPECT_EQ(0, sut3
.GetDay());
113 EXPECT_EQ(0, sut3
.GetHour());
114 EXPECT_EQ(0, sut3
.GetMinute());
115 EXPECT_EQ(0, sut3
.GetSecond());
116 EXPECT_EQ(ui::TEXT_INPUT_TYPE_NONE
, sut3
.GetType());
118 // type is a superset of pattern
119 params
.currentValue
= "2013-05-21T12:15";
120 params
.type
= WebKit::WebDateTimeInputTypeMonth
;
121 DateTimeFormatter
sut4(params
);
122 EXPECT_EQ(2013, sut4
.GetYear());
123 EXPECT_EQ(4, sut4
.GetMonth());
124 EXPECT_EQ(1, sut4
.GetDay());
125 EXPECT_EQ(0, sut4
.GetHour());
126 EXPECT_EQ(0, sut4
.GetMinute());
127 EXPECT_EQ(0, sut4
.GetSecond());
128 EXPECT_EQ(ui::TEXT_INPUT_TYPE_MONTH
, sut4
.GetType());
132 TEST(RendererDateTimePickerTest
, TestParserValidDateInputs
) {
133 DateTimeFormatter
sut(ui::TEXT_INPUT_TYPE_MONTH
, 2012, 11, 1, 0, 0, 0, 0, 0);
134 EXPECT_EQ("2012-12", sut
.GetFormattedValue());
137 DateTimeFormatter
sut2(ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL
,
138 2013, 3, 23, 15, 47, 0, 0, 0);
139 EXPECT_EQ("2013-04-23T15:47", sut2
.GetFormattedValue());
141 // Disabling this case due to crbug.com/253231
142 //DateTimeFormatter sut3(ui::TEXT_INPUT_TYPE_WEEK, 0, 0, 0, 0, 0, 0, 2012, 2);
143 //EXPECT_EQ("2012-W02", sut3.GetFormattedValue());
146 TEST(RendererDateTimePickerTest
, TestParserInvalidDateInputs
) {
147 DateTimeFormatter
sut(ui::TEXT_INPUT_TYPE_WEEK
, 0, 0, 0, 0, 0, 0, 0, 0);
148 EXPECT_EQ("", sut
.GetFormattedValue());
150 DateTimeFormatter
sut2(ui::TEXT_INPUT_TYPE_NONE
, 2013, 3, 23, 0, 0, 0, 0, 0);
151 EXPECT_EQ("", sut2
.GetFormattedValue());
153 DateTimeFormatter
sut3(ui::TEXT_INPUT_TYPE_NONE
, 2013, 14, 32, 0, 0, 0, 0, 0);
154 EXPECT_EQ("", sut3
.GetFormattedValue());
156 DateTimeFormatter
sut4(ui::TEXT_INPUT_TYPE_DATE
, 0, 0, 0, 0, 0, 0, 0, 0);
157 EXPECT_EQ("", sut4
.GetFormattedValue());
159 DateTimeFormatter
sut5(ui::TEXT_INPUT_TYPE_TIME
, 0, 0, 0, 0, 0, 0, 0, 0);
160 EXPECT_EQ("", sut5
.GetFormattedValue());
162 DateTimeFormatter
sut6(ui::TEXT_INPUT_TYPE_PASSWORD
, 23, 0, 0, 0, 5, 0, 0, 0);
163 EXPECT_EQ("", sut6
.GetFormattedValue());
165 DateTimeFormatter
sut7(ui::TEXT_INPUT_TYPE_MAX
, 23, 0, 0, 0, 5, 0, 0, 0);
166 EXPECT_EQ("", sut7
.GetFormattedValue());
168 DateTimeFormatter
sut8(
169 static_cast<ui::TextInputType
>(10000), 23, 0, 0, 0, 5, 0, 0, 0);
170 EXPECT_EQ("", sut8
.GetFormattedValue());
172 } // namespace content