-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathDateSlider.m
More file actions
408 lines (309 loc) · 16.1 KB
/
Copy pathDateSlider.m
File metadata and controls
408 lines (309 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
classdef DateSlider < wt.test.BaseWidgetTest
% Implements a unit test for a widget or component
%% Test Method Setup
methods (TestMethodSetup)
function setup(testCase)
% Call superclass method
testCase.setup@wt.test.BaseWidgetTest();
% Setup widget
fcn = @()wt.DateSlider(testCase.Grid);
testCase.Widget = verifyWarningFree(testCase,fcn);
% Set callback
testCase.Widget.ValueChangedFcn = @(s,e)onCallbackTriggered(testCase,e);
% Ensure it renders
drawnow
end %function
end %methods
%% Unit Tests
methods (Test)
function testValuePropertyAndBoundaries(testCase)
% Set the value
newValue = datetime("01-Jan-2020");
testCase.verifySetProperty("Value", newValue);
testCase.verifyControlValue(newValue);
% Set the value
newValue = datetime("03-Jan-2020");
testCase.verifySetProperty("Value", newValue);
testCase.verifyControlValue(newValue);
% Configure the control
newLimits = datetime("today") + [-days(20) days(15)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifyControlValue(newLimits(1));
% Set the value within limits
newValue = datetime("today") + days(15);
testCase.verifySetProperty("Value", newValue);
testCase.verifyControlValue(newValue);
% Change the limits out of bounds
newLimits = datetime("today") + [days(20) days(30)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifyControlValue(newLimits(1));
% Set an invalid value
expValue = datetime("today") + days(20);
invldValue = datetime("today") + days(31);
testCase.verifySetPropertyError("Value", invldValue, 'MATLAB:ui:DatePicker:valueNotValid');
testCase.verifyControlValue(expValue);
% Set an invalid value
expValue = datetime("today") + days(20);
invldValue = datetime("today") + days(15);
testCase.verifySetPropertyError("Value", invldValue, 'MATLAB:ui:DatePicker:valueNotValid');
testCase.verifyControlValue(expValue);
% Configure a simple range
newLimits = datetime("today") + [days(0) days(10)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
% Expect index 1 -> min date, index 11 -> max date
testCase.verifySetProperty("ValueIndex", 1);
testCase.verifyControlValue(newLimits(1));
testCase.verifySetProperty("ValueIndex", 11);
testCase.verifyControlValue(newLimits(2));
% Out-of-bounds should error
testCase.verifySetPropertyError("ValueIndex", -1, 'MATLAB:validators:mustBeInRange');
testCase.verifySetPropertyError("ValueIndex", 12, 'MATLAB:validators:mustBeInRange');
end %function
function testDatepicker(testCase)
% Configure the control
newLimits = datetime("today") + [-days(20) days(15)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifyControlValue(newLimits(1));
% Enable the datepicker to be able to type
testCase.Widget.Datepicker.Editable = true;
% Pick a date using datepicker
newValue = datetime("today") + days(15);
testCase.verifyTypeAction(testCase.Widget.Datepicker, newValue, "Value")
testCase.verifyEqual(testCase.Widget.Value, newValue)
testCase.verifyCallbackCount(1);
newValue = datetime("today") + days(10);
testCase.verifyTypeAction(testCase.Widget.Datepicker, newValue, "Value")
testCase.verifyEqual(testCase.Widget.Value, newValue)
testCase.verifyCallbackCount(2);
% Pick an out-of-range date using datepicker
invldValue = datetime("today") + days(20);
testCase.verifyTypeAction(testCase.Widget.Datepicker, invldValue, "Value", newValue)
testCase.verifyEqual(testCase.Widget.Value, newValue)
testCase.verifyCallbackCount(2);
% Change date using value property
newValue = datetime("today") - days(20);
testCase.verifySetProperty("Value", newValue)
testCase.verifyEqual(testCase.Widget.Datepicker.Value, newValue)
testCase.verifyCallbackCount(2);
end
function testDatepickerButtons(testCase)
% Configure the control
newLimits = datetime("today") + [days(0) days(10)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifyControlValue(newLimits(1));
% Verify buttons enabled state
testCase.verifyButtonsEnabled([true false])
% Push down is disabled
testCase.verifyButtonPushAction("down", newLimits(1))
testCase.verifyCallbackCount(0);
% Push the button up once
testCase.verifyButtonPushAction("up", newLimits(1) + days(1))
testCase.verifyButtonsEnabled([true true])
testCase.verifyCallbackCount(1);
% Change step size and push down
testCase.Widget.Step = caldays(4);
testCase.verifyButtonPushAction("down", newLimits(1))
testCase.verifyButtonsEnabled([true false])
testCase.verifyCallbackCount(2);
% Push up
testCase.verifyButtonPushAction("up", newLimits(1) + days(4))
testCase.verifyButtonPushAction("up", newLimits(1) + days(8))
testCase.verifyButtonPushAction("up", newLimits(2))
testCase.verifyCallbackCount(5);
% Push up disabled at this point
testCase.verifyButtonsEnabled([false true])
testCase.verifyButtonPushAction("up", newLimits(2))
testCase.verifyCallbackCount(5);
end
function testValueChangingEventOnDrag(testCase)
% Attach ValueChanging callback to count intermediate events
% Add ValueChanging callback function only for this test
testCase.Widget.ValueChangingFcn = @(s,e)onCallbackTriggered(testCase,e);
% Configure range
newLimits = datetime("today") + [days(0) days(50)];
testCase.verifySetProperty("Limits", newLimits);
% Drag the slider; ValueChanging should fire at least once
sliderControl = testCase.Widget.Slider;
startValue = 5; newValue = 25;
testCase.drag(sliderControl, startValue, newValue);
% We don't assert exact count (depends on platform), just > 1
testCase.verifyTrue(testCase.CallbackCount > 1);
end %function
function testStepWithCalendarMonths(testCase)
% Configure range across months
base = datetime(2020,1,1);
newLimits = base + [days(0) calmonths(2)]; % Jan 1 .. Mar 1 (approx)
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifySetProperty("Value", newLimits(1));
testCase.verifyControlValue(newLimits(1));
% Set step to one calendar month, start at min
testCase.verifySetProperty("Step", calmonths(1));
% Push 'up' twice: Jan->Feb->Mar (clamped at max)
testCase.verifyButtonPushAction("up", base + calmonths(1));
testCase.verifyButtonPushAction("up", newLimits(2));
% Push 'down' twice: back to Feb->Jan
testCase.verifyButtonPushAction("down", base + calmonths(1));
testCase.verifyButtonPushAction("down", newLimits(1));
end %function
function testButtonsEnabledMidRange(testCase)
% In mid-range both buttons must be enabled
newLimits = datetime("today") + [days(0) days(10)];
testCase.verifySetProperty("Limits", newLimits);
mid = newLimits(1) + days(5);
testCase.verifySetProperty("Value", mid);
testCase.verifyButtonsEnabled([true true]);
end %function
function testLimitsNormalizationToStartOfDay(testCase)
% Provide limits with non-midnight time components
d1 = dateshift(datetime("today"), 'start', 'day') + hours(10);
d2 = d1 + days(3) + hours(12);
testCase.verifySetProperty("Limits", [d1 d2], [d1 d2 ] - timeofday([d1 d2]));
end %function
function testTickLabels(testCase)
% Change the limits
expValue = datetime("today") + days(5);
newLimits = datetime("today") + [days(5) days(10)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifyControlValue(newLimits(1));
% Check tick labels
expLabels = string(datetime("today") + days(5:10));
actualLabels = testCase.Widget.Slider.MajorTickLabels;
actualLabels = convertCharsToStrings(actualLabels(1:min(6, numel(actualLabels))));
testCase.verifyEqual(actualLabels, expLabels)
% Change the limits
newLimits = datetime("today") + [days(0) days(10)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
testCase.verifyControlValue(expValue);
% Change datepicker size so that slider barely fits
testCase.Widget.DatepickerSize = 240;
% Check widgets exist
expLabels = string(datetime("today") + days([0 5 10]));
actualLabels = testCase.Widget.Slider.MajorTickLabels;
actualLabels = convertCharsToStrings(actualLabels(1:min(3, numel(actualLabels))));
testCase.verifyEqual(actualLabels, expLabels)
end %function
function testSlider(testCase)
% Configure the control
newLimits = datetime("today") + [days(0) days(100)];
testCase.verifySetProperty("Limits", newLimits);
testCase.verifyControlLimits(newLimits);
% Get the control
sliderControl = testCase.Widget.Slider;
% Click the slider
newValue = 75;
expValue = datetime("today") + days(newValue - 1);
testCase.choose(sliderControl,newValue);
testCase.verifyControlValue(expValue);
testCase.verifyEqual(testCase.Widget.Value, expValue);
% Verify callback triggered
testCase.verifyEqual(testCase.CallbackCount, 1)
% Drag the slider
startValue = 1;
newValue = 32;
expValue = datetime("today") + days(newValue - 1);
testCase.drag(sliderControl,startValue,newValue);
testCase.verifyControlValue(expValue);
testCase.verifyEqual(testCase.Widget.Value, expValue);
% Verify callback triggered
testCase.verifyEqual(testCase.CallbackCount, 2)
% Drag the slider to a fraction (with rounding turned on)
startValue = 32;
newValue = 13.8;
expValue = datetime("today") + days(round(newValue - 1));
testCase.drag(sliderControl,startValue,newValue);
testCase.verifyControlValue(expValue);
testCase.verifyEqual(testCase.Widget.Value, expValue);
% Verify callback triggered
testCase.verifyEqual(testCase.CallbackCount, 3)
end %function
function testOrientationVerticalLayout(testCase)
% Switch to vertical orientation
testCase.verifySetProperty("Orientation", wt.enum.HorizontalVerticalState.vertical);
% Slider orientation must change
testCase.verifyEquality(testCase.Widget.Slider.Orientation, "vertical");
% Layout positions should match update() vertical branch
drawnow
testCase.verifyEqual(testCase.Widget.Datepicker.Layout.Row, 2);
testCase.verifyEqual(testCase.Widget.Datepicker.Layout.Column, 1);
testCase.verifyEqual(testCase.Widget.GridButtons.Layout.Row, 2);
testCase.verifyEqual(testCase.Widget.GridButtons.Layout.Column, 2);
% DatepickerSize affects height in vertical orientation
testCase.verifySetProperty("DatepickerSize", 180);
drawnow
% Grid.RowHeight should include DatepickerSize as second row
testCase.verifyEqual(testCase.Widget.Grid.RowHeight{2}, 180);
end %function
function testDisplayFormatPropagationAndTicks(testCase)
% Change the display format
fmt = "uuuu-MM-dd";
testCase.Widget.DisplayFormat = fmt;
drawnow
% Verify propagation to datepicker and limits
testCase.verifyEquality(testCase.Widget.Datepicker.DisplayFormat, fmt);
testCase.verifyEquality(testCase.Widget.Limits.Format, fmt);
% Ticks/labels should be updated (non-empty categorical labels)
drawnow
labels = testCase.Widget.Slider.MajorTickLabels;
testCase.verifyTrue(~isempty(labels));
end %function
end %methods (Test)
%% Helper methods
methods (Access = private)
function verifyControlValue(testCase, dateValue, absTol)
% Verifies the control fields have the specified value
arguments
testCase
dateValue (1,1) datetime
absTol (1,1) double = 0
end
drawnow
numValue = days(dateValue - testCase.Widget.Limits(1)) + 1;
testCase.verifyEqual(testCase.Widget.Slider.Value, numValue, 'AbsTol', absTol);
testCase.verifyEqual(testCase.Widget.Datepicker.Value, dateValue, 'AbsTol', absTol);
end %function
function verifyControlLimits(testCase, dateLimits, absTol)
% Verifies the control fields have the specified limit
arguments
testCase
dateLimits (1,2) datetime
absTol (1,1) double = 0
end
drawnow
numLimits = [0 days(dateLimits(2) - dateLimits(1))] + 1;
testCase.verifyEqual(testCase.Widget.Slider.Limits, numLimits, 'AbsTol', absTol);
testCase.verifyEqual(testCase.Widget.Datepicker.Limits, dateLimits, 'AbsTol', absTol);
end %function
function verifyButtonsEnabled(testCase, enabledStatus)
% Verifies the control buttons have the specified enable status
arguments
testCase
enabledStatus (1,2) matlab.lang.OnOffSwitchState = [true true]
end
testCase.verifyEqual([testCase.Widget.Buttons.Enable], enabledStatus);
end %function
function verifyButtonPushAction(testCase, direction, expValue)
arguments
testCase
direction (1,1) string {mustBeMember(direction, ["up", "down"])}
expValue (1,1) datetime
end
if direction == "up"
buttonIdx = 1;
else
buttonIdx = 2;
end
% Type the new value into the control
testCase.press(testCase.Widget.Buttons(buttonIdx));
% Verify new property value
testCase.verifyControlValue(expValue);
end %function
end %methods
end %classdef