Skip to content

Commit b7d9418

Browse files
rjackeyrjackey
authored andcommitted
Add timezone and NaT support to DateTimePicker #14
1 parent 58b14c7 commit b7d9418

File tree

2 files changed

+390
-40
lines changed

2 files changed

+390
-40
lines changed

test/+wt/+test/DatetimeSelector.m

Lines changed: 218 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
classdef DatetimeSelector < wt.test.BaseWidgetTest
22
% Implements a unit test for a widget or component
33

4-
% Copyright 2021 The MathWorks, Inc.
4+
% Copyright 2021-2023 The MathWorks, Inc.
55

66

77

@@ -23,39 +23,45 @@ function setup(testCase)
2323
methods (Test)
2424

2525
function testValueProperty(testCase)
26+
27+
% Get the widget
28+
w = testCase.Widget;
2629

2730
% Toggle seconds and AM/PM
2831
testCase.verifySetProperty("ShowSeconds", matlab.lang.OnOffSwitchState.on);
2932
testCase.verifySetProperty("ShowAMPM", matlab.lang.OnOffSwitchState.off);
3033

3134
% Set the value
32-
dt = datetime("now");
35+
dt = datetime("now","TimeZone","local");
3336
testCase.verifySetProperty("Value", dt);
3437
drawnow
3538

3639
% Verify the value
37-
testCase.verifyEqual(testCase.Widget.DateControl.Value.Month, dt.Month);
38-
testCase.verifyEqual(testCase.Widget.DateControl.Value.Year, dt.Year);
39-
testCase.verifyEqual(testCase.Widget.DateControl.Value.Day, dt.Day);
40-
testCase.verifyEqual(testCase.Widget.HourControl.Value, dt.Hour);
41-
testCase.verifyEqual(testCase.Widget.MinuteControl.Value, dt.Minute);
42-
testCase.verifyEqual(testCase.Widget.SecondControl.Value, dt.Second);
40+
testCase.verifyEqual(w.DateControl.Value.Month, dt.Month);
41+
testCase.verifyEqual(w.DateControl.Value.Year, dt.Year);
42+
testCase.verifyEqual(w.DateControl.Value.Day, dt.Day);
43+
testCase.verifyEqual(w.HourControl.Value, dt.Hour);
44+
testCase.verifyEqual(w.MinuteControl.Value, dt.Minute);
45+
testCase.verifyEqual(w.SecondControl.Value, dt.Second);
4346

4447
end %function
4548

4649

4750
function testRollOver(testCase)
51+
52+
% Get the widget
53+
w = testCase.Widget;
4854

4955
% Toggle seconds and AM/PM
5056
testCase.verifySetProperty("ShowSeconds", matlab.lang.OnOffSwitchState.on);
5157
testCase.verifySetProperty("ShowAMPM", matlab.lang.OnOffSwitchState.on);
5258

5359
% Get the controls
54-
dateControl = testCase.Widget.DateControl;
55-
hourControl = testCase.Widget.HourControl;
56-
minuteControl = testCase.Widget.MinuteControl;
57-
secondControl = testCase.Widget.SecondControl;
58-
amPmControl = testCase.Widget.AmPmControl;
60+
dateControl = w.DateControl;
61+
hourControl = w.HourControl;
62+
minuteControl = w.MinuteControl;
63+
secondControl = w.SecondControl;
64+
amPmControl = w.AmPmControl;
5965

6066
% Use a value of today 12AM
6167
dt_0 = datetime("today");
@@ -74,7 +80,7 @@ function testRollOver(testCase)
7480

7581
% Roll down the hour by one to 11PM yesterday
7682
testCase.press(hourControl,"down");
77-
actVal = testCase.Widget.Value;
83+
actVal = w.Value;
7884
testCase.verifyEqual(actVal, dt_0 - hours(1));
7985
drawnow
8086
testCase.verifyEqual(dateControl.Value, dt_0 - days(1)); %datepicker strips the time
@@ -86,7 +92,7 @@ function testRollOver(testCase)
8692
% Roll down the hour up two to 1AM today
8793
testCase.press(hourControl,"up");
8894
testCase.press(hourControl,"up");
89-
actVal = testCase.Widget.Value;
95+
actVal = w.Value;
9096
testCase.verifyEqual(actVal, dt_0 + hours(1));
9197
drawnow
9298
testCase.verifyEqual(dateControl.Value, dt_0);
@@ -97,7 +103,7 @@ function testRollOver(testCase)
97103

98104
% Roll the seconds back by one
99105
testCase.press(secondControl,"down");
100-
actVal = testCase.Widget.Value;
106+
actVal = w.Value;
101107
testCase.verifyEqual(actVal, dt_0 + hours(1) - seconds(1));
102108
drawnow
103109
testCase.verifyEqual(dateControl.Value, dt_0);
@@ -123,7 +129,7 @@ function testRollOver(testCase)
123129

124130
% Roll the minutes back by one
125131
testCase.press(minuteControl,"down");
126-
actVal = testCase.Widget.Value;
132+
actVal = w.Value;
127133
testCase.verifyEqual(actVal, dt_12 - minutes(1));
128134
drawnow
129135
testCase.verifyEqual(dateControl.Value, dt_0);
@@ -133,7 +139,201 @@ function testRollOver(testCase)
133139
testCase.verifyEqual(amPmControl.Value, 'AM');
134140

135141
end %function
142+
143+
144+
function testTimeZoneName(testCase)
145+
146+
% Get the widget
147+
w = testCase.Widget;
148+
149+
% Set to 24-hour format to easily verify hours displayed
150+
%testCase.verifySetProperty("ShowAMPM", matlab.lang.OnOffSwitchState.off);
151+
152+
% Toggle timezone field
153+
testCase.verifySetProperty("ShowTimeZone", matlab.lang.OnOffSwitchState.on);
154+
155+
% Set the value using local time
156+
dt = datetime("now","TimeZone","local");
157+
testCase.verifySetProperty("Value", dt);
158+
testCase.verifyTimeZoneDisplay();
159+
160+
161+
% Change the time zone programmatically
162+
w.Value.TimeZone = "Europe/Amsterdam";
163+
testCase.verifyTimeZoneDisplay();
164+
165+
166+
% Change the time zone interactively
167+
expValue = "America/Los_Angeles";
168+
selIdx = find( contains(w.TimeZoneControl.Items, expValue), 1);
169+
selValue = w.TimeZoneControl.Items{selIdx};
170+
testCase.choose(w.TimeZoneControl, selValue)
171+
testCase.verifyTimeZoneDisplay();
172+
173+
% Verify the Value field was updated
174+
actValue = string(w.Value.TimeZone);
175+
testCase.verifyMatches(actValue, expValue)
176+
177+
end %function
178+
179+
180+
function testTimeZoneOffsetOnly(testCase)
181+
182+
% Get the widget
183+
w = testCase.Widget;
184+
185+
% Set to 24-hour format to easily verify hours displayed
186+
%testCase.verifySetProperty("ShowAMPM", matlab.lang.OnOffSwitchState.off);
187+
188+
% Toggle timezone field
189+
testCase.verifySetProperty("ShowTimeZone", matlab.lang.OnOffSwitchState.on);
190+
191+
% Set the value using local time
192+
dt = datetime("now","TimeZone","-03:00");
193+
testCase.verifySetProperty("Value", dt);
194+
testCase.verifyTimeZoneDisplay();
195+
196+
% Change the time zone programmatically
197+
w.Value.TimeZone = "+13:00";
198+
testCase.verifyTimeZoneDisplay();
199+
200+
201+
% Change the time zone programmatically
202+
w.Value.TimeZone = "+00:00";
203+
testCase.verifyTimeZoneDisplay();
204+
205+
206+
% Change the time zone interactively
207+
expValue = "-05:00";
208+
testCase.choose(w.TimeZoneControl, expValue)
209+
testCase.verifyTimeZoneDisplay();
210+
211+
% Verify the Value field was updated
212+
actValue = string(w.Value.TimeZone);
213+
testCase.verifyMatches(actValue, expValue)
214+
215+
end %function
216+
217+
218+
function testTimeZoneSwitchingFormat(testCase)
219+
220+
% Get the widget
221+
w = testCase.Widget;
222+
223+
% Set to 24-hour format to easily verify hours displayed
224+
%testCase.verifySetProperty("ShowAMPM", matlab.lang.OnOffSwitchState.off);
225+
226+
% Toggle timezone field
227+
testCase.verifySetProperty("ShowTimeZone", matlab.lang.OnOffSwitchState.on);
228+
229+
% Set the value using local time
230+
dt = datetime("now","TimeZone","local");
231+
testCase.verifySetProperty("Value", dt);
232+
testCase.verifyTimeZoneDisplay();
233+
234+
w.Value.TimeZone = "+12:45";
235+
testCase.verifyTimeZoneDisplay();
236+
237+
w.Value.TimeZone = "America/Los_Angeles";
238+
testCase.verifyTimeZoneDisplay();
239+
240+
w.Value.TimeZone = "-09:30";
241+
testCase.verifyTimeZoneDisplay();
242+
243+
w.Value.TimeZone = "";
244+
testCase.verifyTimeZoneDisplay();
245+
246+
w.Value.TimeZone = "+00:00";
247+
testCase.verifyTimeZoneDisplay();
248+
249+
w.Value.TimeZone = "local";
250+
testCase.verifyTimeZoneDisplay();
251+
252+
end %function
253+
254+
255+
function testNaTValue(testCase)
256+
257+
% Get the widget
258+
w = testCase.Widget;
259+
260+
% Toggle settings
261+
testCase.verifySetProperty("ShowSeconds", matlab.lang.OnOffSwitchState.on);
262+
testCase.verifySetProperty("ShowAMPM", matlab.lang.OnOffSwitchState.on);
263+
testCase.verifySetProperty("ShowTimeZone", matlab.lang.OnOffSwitchState.on);
264+
265+
266+
% Set the value to NaT
267+
dt = NaT;
268+
testCase.verifySetProperty("Value", dt);
269+
270+
271+
% Verify the expected values for NaT
272+
testCase.verifyEqual(w.DateControl.Value, NaT);
273+
testCase.verifyEqual(w.HourControl.Value, 12);
274+
testCase.verifyEqual(w.MinuteControl.Value, 0);
275+
testCase.verifyEqual(w.SecondControl.Value, 0);
276+
testCase.verifyEqual(w.AmPmControl.Value, 'AM');
277+
testCase.verifyEqual(w.TimeZoneControl.Value, "");
278+
279+
280+
% Select a date interactively to remove the NaT
281+
newDate = datetime("11/21/2022");
282+
testCase.type(w.DateControl, newDate)
283+
284+
% Verify the expected values
285+
testCase.verifyTimeZoneDisplay();
286+
testCase.verifyEqual(w.DateControl.Value.Month, w.Value.Month);
287+
testCase.verifyEqual(w.DateControl.Value.Year, w.Value.Year);
288+
testCase.verifyEqual(w.DateControl.Value.Day, w.Value.Day);
289+
testCase.verifyEqual(w.HourControl.Value, 12);
290+
testCase.verifyEqual(w.MinuteControl.Value, 0);
291+
testCase.verifyEqual(w.SecondControl.Value, 0);
292+
testCase.verifyEqual(w.AmPmControl.Value, 'AM');
293+
294+
295+
% Set the value to NaT
296+
dt = NaT;
297+
testCase.verifySetProperty("Value", dt);
298+
299+
300+
% Set the time interactively to remove the NaT
301+
testCase.press(w.HourControl, "up")
302+
303+
% Verify the expected values
304+
testCase.verifyTimeZoneDisplay();
305+
testCase.verifyEqual(w.HourControl.Value, 1);
306+
testCase.verifyEqual(w.MinuteControl.Value, 0);
307+
testCase.verifyEqual(w.SecondControl.Value, 0);
308+
testCase.verifyEqual(w.AmPmControl.Value, 'AM');
309+
310+
end %function
136311

137312
end %methods (Test)
138313

139-
end %classdef
314+
%% Helper Methods
315+
methods
316+
317+
function verifyTimeZoneDisplay(testCase, expVal)
318+
% Verify the time zone display matches correctly the Value field
319+
320+
arguments
321+
testCase
322+
expVal (1,1) string = testCase.Widget.Value.TimeZone
323+
end
324+
325+
% Give a moment for any display updates
326+
pause(0.01);
327+
drawnow
328+
329+
% Verify correct timezone is selected
330+
actVal = string(testCase.Widget.TimeZoneControl.Value);
331+
testCase.verifyEqual(actVal, expVal);
332+
333+
end %function
334+
335+
end %methods
336+
337+
338+
end %classdef
339+

0 commit comments

Comments
 (0)