Repository navigation
Expand file tree
/
Copy pathTestProgressBarWindow.m
More file actions
641 lines (573 loc) · 36.2 KB
/
Copy pathTestProgressBarWindow.m
File metadata and controls
641 lines (573 loc) · 36.2 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
% TestProgressBarWindow.m
classdef TestProgressBarWindow < matlab.unittest.TestCase
% TestProgressBarWindow Unittests for ProgressBarWindow class.
%
% Run with: results = runtests('TestProgressBarWindow');
%
% Ensure ProgressBarWindow.m (and its package +ndi) is on the path.
methods (TestClassSetup)
function cleanUpBeforeAllTests(testCase)
% This function runs once before any test in this class.
% It ensures no progress bars from previous runs interfere.
% These tests assert on the figure and its child components, so
% they must run in the windowed path. Pin the rendering decision
% rather than let it be detected: the CI test process itself runs
% under `-batch`, where ndi.gui.component.ProgressBarWindow now
% chooses silent mode by default (issue #941) and there would be
% no figure to assert on.
previousSilentDefault = ndi.gui.component.ProgressBarWindow.silentModeDefault();
testCase.addTeardown(@() ...
ndi.gui.component.ProgressBarWindow.silentModeDefault(previousSilentDefault));
ndi.gui.component.ProgressBarWindow.silentModeDefault(false);
testCase.log(1, 'Closing all pre-existing progress bar windows...');
figs = findall(groot, 'Type', 'figure', 'Tag', 'progressbar');
delete(figs);
% Also ensure no navigator is open, so these tests exercise the
% standalone-window path (a navigator would cause docking).
delete(findall(groot, 'Type', 'figure', 'Tag', 'ndiNavigator'));
end
end
methods (TestMethodTeardown)
function closeAllProgressBars(testCase)
% Close any progress bar figures created during tests
figs = findall(groot, 'Type', 'figure', 'Tag', 'progressbar');
delete(figs);
delete(findall(groot, 'Type', 'figure', 'Tag', 'ndiNavigator'));
end
end
methods (Test)
% Constructor Tests
function testConstructorDefault(testCase)
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
testCase.verifyClass(app.ProgressFigure, 'matlab.ui.Figure');
testCase.verifyEmpty(app.ProgressFigure.Name, 'Default title should be empty.');
testCase.verifyEmpty(app.ProgressBars, 'ProgressBars should be initialized empty.');
end
function testConstructorWithTitle(testCase)
title = 'My Test Progress';
app = ndi.gui.component.ProgressBarWindow(title, 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
testCase.verifyEqual(app.ProgressFigure.Name, title, 'Title not set correctly.');
end
function testConstructorOverwriteFalseReturnsExisting(testCase)
title = 'Overwrite Test False';
app1 = ndi.gui.component.ProgressBarWindow(title, 'Visible', 'off');
testCase.addTeardown(@delete, app1.ProgressFigure);
guidata(app1.ProgressFigure, app1); % Save app instance
drawnow;
app2 = ndi.gui.component.ProgressBarWindow(title, 'Overwrite', false);
testCase.verifySameHandle(app2.ProgressFigure, app1.ProgressFigure, ...
'Should return handle to existing figure if Overwrite is false.');
testCase.verifySameHandle(app2, app1, 'Should return the same app object.');
end
function testConstructorOverwriteFalseCreatesNewIfDifferentApp(testCase)
import matlab.unittest.fixtures.SuppressedWarningsFixture
testCase.applyFixture(SuppressedWarningsFixture('ProgressBarWindow:ExistingFigureNotApp'))
title = 'Overwrite Test False Different App';
fig = uifigure('Name',title,'Tag','progressbar'); % A plain figure, not our app
testCase.addTeardown(@delete, fig);
guidata(fig, struct('dummy',true)); % Put some other guidata
drawnow;
app = ndi.gui.component.ProgressBarWindow(title, 'Overwrite', false);
testCase.addTeardown(@delete, app.ProgressFigure);
testCase.verifyNotSameHandle(app.ProgressFigure, fig, ...
'Should create a new figure if existing is not a ProgressBarWindow.');
testCase.verifyFalse(ishandle(fig), 'Non-app figure should have been deleted.');
end
function testConstructorOverwriteTrue(testCase)
title = 'Overwrite Test True';
app1 = ndi.gui.component.ProgressBarWindow(title, 'Visible', 'off');
fig1Handle = app1.ProgressFigure;
guidata(app1.ProgressFigure, app1); % Save app instance
testCase.addTeardown(@delete, fig1Handle); % Will be deleted by app2 creation
drawnow;
app2 = ndi.gui.component.ProgressBarWindow(title, 'Overwrite', true, 'Visible', 'off');
testCase.addTeardown(@delete, app2.ProgressFigure);
testCase.verifyNotSameHandle(app2.ProgressFigure, fig1Handle, ...
'Should create a new figure if Overwrite is true.');
testCase.verifyFalse(ishandle(fig1Handle), 'Old figure should be deleted.');
end
function testConstructorGrabMostRecent(testCase)
% Tests the 'GrabMostRecent' constructor option.
% Create a first, older window with a different title
app1 = ndi.gui.component.ProgressBarWindow('Old Window', 'Visible', 'off');
testCase.addTeardown(@delete, app1.ProgressFigure);
guidata(app1.ProgressFigure, app1);
pause(0.1); % Ensure timestamp is different
% Create a second, newer window
app2 = ndi.gui.component.ProgressBarWindow('New Window', 'Visible', 'off');
testCase.addTeardown(@delete, app2.ProgressFigure);
guidata(app2.ProgressFigure, app2);
drawnow;
% Now, construct with a non-matching title but with GrabMostRecent
app3 = ndi.gui.component.ProgressBarWindow('NonExistent Title', 'GrabMostRecent', true, 'Visible', 'off');
testCase.verifySameHandle(app3.ProgressFigure, app2.ProgressFigure, ...
'Should have grabbed the most recent progress bar window.');
end
% AddBar Tests
function testAddOneBar(testCase)
app = ndi.gui.component.ProgressBarWindow('Add Bar Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Label', 'Task 1', 'Tag', 'T1');
testCase.verifyNumElements(app.ProgressBars, 1, 'Should have one progress bar.');
testCase.verifyEqual(app.ProgressBars(1).Tag, 'T1');
testCase.verifyEqual(app.ProgressBars(1).Label.Text, 'Task 1');
testCase.verifyEqual(app.ProgressBars(1).Progress, 0);
testCase.verifyEqual(app.ProgressBars(1).State, 'Open');
testCase.verifyNotEmpty(app.ProgressBars(1).Axes, 'Axes handle should not be empty.');
testCase.verifyNotEmpty(app.ProgressBars(1).Patch, 'Patch handle should not be empty.');
end
function testBarFillsItsRow(testCase)
% The bar's axes sit in a borderless panel that the grid lays
% out, the axes' INNER box BarMargin px inside it. In a grid cell
% a uiaxes keeps room for hidden tick labels; on R2026a that
% left a 2.7 px bar in a 28 px row.
app = ndi.gui.component.ProgressBarWindow('Fill Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Label', 'Task 1', 'Tag', 'T1');
app.addBar('Label', 'Task 2', 'Tag', 'T2');
ax = app.ProgressBars(2).Axes;
testCase.verifyClass(ax.Parent, 'matlab.ui.container.Panel');
testCase.verifyEqual(ax.Parent.Layout.Row, 4, 'the panel takes the bar row');
testCase.verifyEqual(ax.PositionConstraint, 'innerposition');
testCase.verifyEqual(ax.Units, 'pixels');
testCase.verifyNotEmpty(ax.Parent.SizeChangedFcn, 'the bar follows its panel''s size');
testCase.verifyEqual(char(ax.Parent.AutoResizeChildren), 'off', ...
'SizeChangedFcn does not run while AutoResizeChildren is on');
% BarMargin px inside the panel on every side
m = ndi.gui.component.ProgressBarWindow.BarMargin;
drawnow
ndi.gui.component.ProgressBarWindow.fitBarAxes(ax, ax.Parent);
pos = getpixelposition(ax.Parent); % the grid sets the panel's size
testCase.verifyEqual(ax.InnerPosition, ...
[1+m, 1+m, max(pos(3)-2*m, 1), max(pos(4)-2*m, 1)], 'AbsTol', 1e-6);
% removing the first bar moves the second panel up and deletes
% the first bar's panel along with its axes
firstPanel = app.ProgressBars(1).Axes.Parent;
app.updateBar('T1', 1);
app.removeBar('T1');
testCase.verifyFalse(isvalid(firstPanel), 'the removed bar''s panel is deleted');
testCase.verifyEqual(ax.Parent.Layout.Row, 2, 'the remaining bar''s panel moves up');
end
function testAddMultipleBars(testCase)
app = ndi.gui.component.ProgressBarWindow('Multi Bar Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Label', 'Task A', 'Tag', 'TA');
app.addBar('Label', 'Task B', 'Tag', 'TB');
testCase.verifyNumElements(app.ProgressBars, 2, 'Should have two progress bars.');
testCase.verifyEqual(app.ProgressBars(1).Tag, 'TA');
testCase.verifyEqual(app.ProgressBars(2).Tag, 'TB');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 4, 'Grid should have 4 rows.');
end
function testAddBarDuplicateTagResets(testCase)
app = ndi.gui.component.ProgressBarWindow('Duplicate Tag Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'DupTag');
app.updateBar('DupTag', 0.5); % Set some progress
testCase.verifyWarning(@() app.addBar('Tag', 'DupTag'), 'ProgressBarWindow:DuplicateTag');
testCase.verifyNumElements(app.ProgressBars, 1, ...
'Number of bars should not change when duplicate tag is added.');
testCase.verifyEqual(app.ProgressBars(1).Progress, 0, 'Progress should reset on duplicate tag.');
end
function testAddBarDuplicateTagWhenClosed(testCase)
app = ndi.gui.component.ProgressBarWindow('Duplicate Closed Tag Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'DupTagClosed');
app.ProgressBars(1).State = 'Closed'; % Manually mark as closed
app.addBar('Tag', 'DupTagClosed', 'Label', 'New Bar Same Tag'); % Should add a new bar
testCase.verifyNumElements(app.ProgressBars, 1, ... % Assuming the closed one is removed from active list internally
'Should have 1 bar if old one was closed and removed from struct.');
% If the old one is simply overwritten by the new one due to removal then re-add:
activeBars = app.ProgressBars(~strcmpi({app.ProgressBars.State}, 'Closed'));
testCase.verifyNumElements(activeBars, 1);
testCase.verifyEqual(activeBars(1).Label.Text, 'New Bar Same Tag');
end
function testAddBarDefaultColor(testCase)
app = ndi.gui.component.ProgressBarWindow('Default Color Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'ColorTest');
patchColor = app.ProgressBars(1).Patch.FaceColor;
testCase.verifyFalse(all(patchColor == [1 1 1]), 'Default color should not be white.');
testCase.verifyTrue(sum(patchColor) >= 1.5 && sum(patchColor) <= 2.8, ...
'Random color sum is out of expected bounds.');
end
function testAddBarProvidedColor(testCase)
app = ndi.gui.component.ProgressBarWindow('Provided Color Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
testColor = [0.1 0.2 0.3];
app.addBar('Tag', 'ColorTestProvided', 'Color', testColor);
patchColor = app.ProgressBars(1).Patch.FaceColor;
testCase.verifyEqual(patchColor, testColor, 'Provided color not used.');
end
% UpdateBar Tests
function testUpdateBarProgress(testCase)
app = ndi.gui.component.ProgressBarWindow('Update Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TUpdate');
app.updateBar('TUpdate', 0.5);
testCase.verifyEqual(app.ProgressBars(1).Progress, 0.5, 'Progress not updated.');
testCase.verifyEqual(app.ProgressBars(1).Percent.Text, '50%', 'Percent text not updated.');
testCase.verifyEqual(app.ProgressBars(1).Patch.XData, [0; 0.5; 0.5; 0], 'Patch XData not updated correctly.');
testCase.verifyEqual(app.ProgressBars(1).Patch.YData, [0; 0; 1; 1], 'Patch YData should be for full height.');
end
function testUpdateBarToComplete(testCase)
app = ndi.gui.component.ProgressBarWindow('Complete Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TComplete');
app.updateBar('TComplete', 1);
testCase.verifyEqual(app.ProgressBars(1).Progress, 1);
testCase.verifyEqual(app.ProgressBars(1).Percent.Text, '100%');
testCase.verifyEqual(app.ProgressBars(1).State, 'Complete', 'Status should be Complete.');
testCase.verifyEqual(app.ProgressBars(1).Timer.Text, 'Complete', 'Timer text should be Complete.');
testCase.verifyButtonIconIsSuccess(app.ProgressBars(1).Button)
end
function testUpdateBarNonExistent(testCase)
app = ndi.gui.component.ProgressBarWindow('NonExistent Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
testCase.verifyWarning(@() app.updateBar('TNonExistent', 0.5), 'ProgressBarWindow:NoBarsExist');
testCase.verifyEmpty(app.ProgressBars, 'No bar should be created on update of non-existent.');
end
function testUpdateBarAutoCloseMultipleCorrectly(testCase)
app = ndi.gui.component.ProgressBarWindow('Auto Close Multi', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'AC1', 'Auto', true);
app.addBar('Tag', 'AC2', 'Auto', true);
app.addBar('Tag', 'NoAC', 'Auto', false);
app.addBar('Tag', 'AC3', 'Auto', true);
% Mark AC1 and AC3 as complete by updating them
app.updateBar('AC1', 1); % This update should also trigger their auto-removal
app.updateBar('AC3', 1); % This update should also trigger their auto-removal
% Update a non-auto-close bar to ensure the auto-close for others has processed
app.updateBar('NoAC', 0.5);
drawnow;
testCase.verifyEqual(app.getState('AC1'), 'Closed');
testCase.verifyEqual(app.getState('AC3'), 'Closed');
testCase.verifyEqual(app.getState('AC2'), 'Open');
testCase.verifyEqual(app.getState('NoAC'), 'Open');
% Count bars that are NOT closed
openOrActiveCount = sum(~strcmpi({app.ProgressBars.State}, 'Closed'));
testCase.verifyEqual(openOrActiveCount, 2, 'Only AC2 and NoAC should remain effectively open/active in the struct logic.');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 2 * 2, 'Grid should have rows for 2 bars.');
end
% RemoveBar Tests
function testRemoveBar(testCase)
app = ndi.gui.component.ProgressBarWindow('Remove Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TRemove', 'Label', 'Item to Remove');
app.addBar('Tag', 'TStay', 'Label', 'Item to Stay');
initialRowCount = numel(app.ProgressGrid.RowHeight); % Should be 4
app.updateBar('TRemove', 1); % Mark as complete to avoid error on removal
app.removeBar('TRemove');
testCase.verifyNumElements(app.ProgressBars, 2, 'The deleted bar should still remain in progress bars.');
% Verify properties of remaining bar
testCase.verifyEqual(app.ProgressBars(2).Tag, 'TStay', 'Remaining bar is incorrect.');
testCase.verifyEqual(app.ProgressBars(2).State, 'Open', 'Remaining bar state incorrect.');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, initialRowCount - 2, 'Grid rows not removed correctly.');
testCase.verifyEqual(app.ProgressBars(2).Label.Layout.Row, 1);
testCase.verifyEqual(app.ProgressBars(2).Axes.Parent.Layout.Row, 2); % the bar's panel
% Verify properties of removed bar
testCase.verifyEqual(app.ProgressBars(1).Tag, 'TRemove', 'Removed bar is incorrect.');
testCase.verifyEqual(app.ProgressBars(1).State, 'Closed', 'Removed bar state incorrect.');
testCase.verifyFalse(isvalid(app.ProgressBars(1).Patch),'Removed bar patch was not deleted.')
end
function testRemoveBarIncompleteWarning(testCase)
app = ndi.gui.component.ProgressBarWindow('Remove Incomplete Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TIncomplete');
testCase.verifyWarning(@() app.removeBar('TIncomplete'), 'ProgressBarWindow:BarRemoved');
end
function testRemoveBarIncompleteDifferentStates(testCase)
% Test 'Button' state leading to UserTermination error
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TButtonTerm');
app.ProgressBars(1).State = 'Button'; % Simulate state set by handleButtonPress
testCase.verifyError(@() app.removeBar('TButtonTerm'), 'ProgressBarWindow:UserTermination');
delete(findall(groot, 'Type', 'figure', 'Tag', 'progressbar')); % Cleanup
% Test 'Timeout' state leading to AutoCloseOnTimeout error
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TTimeoutTerm');
app.ProgressBars(1).State = 'Timeout';
testCase.verifyWarning(@() app.removeBar('TTimeoutTerm'), 'ProgressBarWindow:AutoCloseOnTimeout');
delete(findall(groot, 'Type', 'figure', 'Tag', 'progressbar'));
% Test 'Open' state leading to BarRemoved warning
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TOpenTerm');
app.ProgressBars(1).State = 'Open'; % Default state if not completed
testCase.verifyWarning(@() app.removeBar('TOpenTerm'), 'ProgressBarWindow:BarRemoved');
end
function testRemoveBarDifferentIncompleteStates(testCase)
% Tests the specific error/warning logic in removeBar for
% incomplete tasks. This replaces testRemoveBarIncompleteWarning.
% Test 'Button' state leading to UserTermination error
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TButtonTerm');
app.ProgressBars(1).State = 'Button'; % Simulate state set by handleButtonPress
testCase.verifyError(@() app.removeBar('TButtonTerm'), 'ProgressBarWindow:UserTermination');
delete(findall(groot, 'Type', 'figure', 'Tag', 'progressbar'));
% Test 'Timeout' state leading to AutoCloseOnTimeout warning
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TTimeoutTerm');
app.ProgressBars(1).State = 'Timeout';
testCase.verifyWarning(@() app.removeBar('TTimeoutTerm'), 'ProgressBarWindow:AutoCloseOnTimeout');
delete(findall(groot, 'Type', 'figure', 'Tag', 'progressbar'));
% Test 'Open' state leading to BarRemoved warning
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TOpenTerm');
app.ProgressBars(1).State = 'Open';
testCase.verifyWarning(@() app.removeBar('TOpenTerm'), 'ProgressBarWindow:BarRemoved');
end
% Button Press Test
function testHandleButtonPressRemovesBar(testCase)
app = ndi.gui.component.ProgressBarWindow('Button Press Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TButton1');
app.addBar('Tag', 'TButton2');
figHandle = app.ProgressFigure; % Store the figure handle to check it's closed
app.updateBar('TButton1', 1); % Complete it
buttonHandle = app.ProgressBars(1).Button;
buttonHandle.ButtonPushedFcn(buttonHandle, []); % Programmatic press
drawnow;
testCase.verifyNumElements(app.ProgressBars,2, 'Bar metadata should remain after button press.');
testCase.verifyNumElements(app.ProgressGrid.RowHeight,2, 'There should be 2 grid rows.');
testCase.verifyFalse(isvalid(app.ProgressBars(1).Patch),'Bar patch should be deleted after button press.')
app.updateBar('TButton2', 1); % Complete it
buttonHandle = app.ProgressBars(2).Button;
buttonHandle.ButtonPushedFcn(buttonHandle, []); % Programmatic press
drawnow;
testCase.verifyFalse(isvalid(app), ...
'The app handle should be invalid after the last bar is removed with AutoDelete=true.');
testCase.verifyFalse(ishandle(figHandle), ...
'The figure handle should be invalid after the last bar is removed with AutoDelete=true.');
end
% SetFigureTitle Test
function testSetFigureTitle(testCase)
app = ndi.gui.component.ProgressBarWindow('Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
newTitle = 'New Progress Title';
app.setFigureTitle(newTitle);
testCase.verifyEqual(app.ProgressFigure.Name, newTitle);
end
% Status Checks
function testCheckComplete(testCase)
app = ndi.gui.component.ProgressBarWindow('Check Complete Method', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TCheckComplete');
app.ProgressBars(1).Progress = 1; % Manually set progress
app.checkComplete(); % Call the method directly
testCase.verifyEqual(app.ProgressBars(1).State, 'Complete');
testCase.verifyEqual(app.ProgressBars(1).Timer.Text, 'Complete');
testCase.verifyButtonIconIsSuccess(app.ProgressBars(1).Button)
end
function testCheckTimeout(testCase)
app = ndi.gui.component.ProgressBarWindow('Check Timeout Method', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
originalTimeout = app.Timeout;
app.setTimeout(duration(0,0,0.1)); % 0.1 seconds
testCase.addTeardown(@() setfield(app,'Timeout',originalTimeout));
app.addBar('Tag', 'TCheckTimeout');
app.ProgressBars(1).Clock{2} = datetime('now') - duration(0,0,0.2); % Simulate last update was 0.2s ago
app.ProgressBars(1).Progress = 0.5; % Not complete
app.checkTimeout();
testCase.verifyEqual(app.ProgressBars(1).State, 'Timeout');
testCase.verifyButtonIconIsError(app.ProgressBars(1).Button)
end
function testGetState(testCase)
app = ndi.gui.component.ProgressBarWindow('Get State Test', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag', 'TGetState');
state = app.getState('TGetState');
testCase.verifyEqual(state, 'Open');
app.ProgressBars(1).State = 'Complete'; % Manually change
state = app.getState('TGetState');
testCase.verifyEqual(state, 'Complete');
% Test non-existent
stateNonExistent = testCase.verifyWarning(...
@() app.getState('TNonExistent'), ...
'ProgressBarWindow:InvalidBarTag');
testCase.verifyEmpty(stateNonExistent);
end
function testAutoCloseOnComplete(testCase)
app = ndi.gui.component.ProgressBarWindow('Auto Close Complete', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
app.addBar('Tag','TStay');
app.addBar('Tag', 'TAutoClose', 'Auto', true);
app.updateBar('TAutoClose', 1); % This will trigger checkComplete and auto-close
drawnow;
testCase.verifyNumElements(app.ProgressBars, 2, 'The auto-closed bar should still remain in progress bars.');
testCase.verifyEqual(app.ProgressBars(2).Tag, 'TAutoClose', 'Auto-closed bar tag is incorrect.');
testCase.verifyEqual(app.ProgressBars(2).State, 'Closed', 'Auto-closed bar state incorrect.');
testCase.verifyFalse(isvalid(app.ProgressBars(2).Patch),'Auto-closed bar patch was not deleted.')
end
function testAutoCloseOnTimeout(testCase)
app = ndi.gui.component.ProgressBarWindow('Auto Close Timeout', 'Visible', 'off');
testCase.addTeardown(@delete, app.ProgressFigure);
originalTimeout = app.Timeout;
app.setTimeout(duration(0,0,0.1)); % 0.1 seconds
% Guard the restore teardown against AutoDelete: this test's
% updateBar call auto-closes both bars, and with AutoDelete
% on (the constructor default) the app is then deleted.
% Touching a deleted AppBase handle raises
% MATLAB:class:InvalidHandle, which would fail the test
% even though the assertion above succeeded.
testCase.addTeardown(@() restoreTimeoutIfAlive(app, originalTimeout));
app.addBar('Tag', 'TAutoTimeout', 'Auto', true);
app.addBar('Tag', 'TUpdated', 'Auto', true);
pause(0.2); % Wait for timeout
testCase.verifyWarning(@() app.updateBar('TUpdated', 0.2), 'ProgressBarWindow:AutoCloseOnTimeout');
end
function testAutoDeleteFigureOnLastBarRemoved(testCase)
% Tests that the figure closes when the last bar is removed
% and AutoDelete is true.
app = ndi.gui.component.ProgressBarWindow('AutoDelete Test', 'AutoDelete', true, 'Visible', 'off');
figHandle = app.ProgressFigure; % Grab handle before it's deleted
app.addBar('Tag', 'T1');
app.addBar('Tag', 'T2');
app.updateBar('T1', 1); % Mark as complete
app.removeBar('T1'); % Remove first bar
testCase.verifyTrue(ishandle(figHandle), 'Figure should not be deleted while one bar remains.');
app.updateBar('T2', 1); % Mark as complete
app.removeBar('T2'); % Remove final bar
drawnow; % Allow delete operation to process
testCase.verifyFalse(ishandle(figHandle), 'Figure should be deleted when the last bar is removed.');
end
function testNoAutoDeleteFigure(testCase)
% Tests that the figure remains open when AutoDelete is false.
app = ndi.gui.component.ProgressBarWindow('No AutoDelete Test', 'AutoDelete', false, 'Visible', 'off');
figHandle = app.ProgressFigure;
testCase.addTeardown(@delete, figHandle); % Ensure cleanup
app.addBar('Tag', 'T1');
app.updateBar('T1', 1);
app.removeBar('T1');
drawnow;
testCase.verifyTrue(ishandle(figHandle), 'Figure should NOT be deleted when AutoDelete is false.');
end
function testNestedProgressTasks(testCase)
% Tests handling of nested tasks where one starts and finishes
% during the execution of another.
app = ndi.gui.component.ProgressBarWindow('Nested Tasks Test', 'AutoDelete', true, 'Visible', 'off');
figHandle = app.ProgressFigure; % For final deletion check
% Teardown to clean up if test fails before the end
testCase.addTeardown(@() delete(figHandle(isvalid(figHandle))));
% 1. Start Task A
app.addBar('Tag', 'TaskA', 'Label', 'Outer Task A');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 2, 'Grid should have 2 rows for Task A.');
testCase.verifyEqual(app.getState('TaskA'), 'Open');
% 2. Update Task A partway
app.updateBar('TaskA', 0.5);
drawnow;
testCase.verifyEqual(app.ProgressBars(1).Progress, 0.5, 'Task A progress should be 50%.');
% 3. Start nested Task B
app.addBar('Tag', 'TaskB', 'Label', 'Inner Task B');
drawnow;
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 4, 'Grid should expand to 4 rows for two tasks.');
testCase.verifyEqual(app.getState('TaskB'), 'Open');
% Verify Task A is unaffected
testCase.verifyEqual(app.ProgressBars(1).Progress, 0.5, 'Task A progress should remain 50%.');
% 4. Update and Complete Task B
app.updateBar('TaskB', 1.0);
drawnow;
testCase.verifyEqual(app.getState('TaskB'), 'Complete', 'Task B should be marked as complete.');
% 5. Remove Task B
app.removeBar('TaskB');
drawnow;
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 2, 'Grid should shrink back to 2 rows.');
testCase.verifyEqual(app.getState('TaskB'), 'Closed', 'Task B state should be Closed after removal.');
% Verify Task A is still where we left it
testCase.verifyEqual(app.getState('TaskA'), 'Open', 'Task A should still be open.');
testCase.verifyEqual(app.ProgressBars(1).Progress, 0.5, 'Task A progress should still be 50%.');
% 6. Continue and Complete Task A
app.updateBar('TaskA', 1.0);
drawnow;
testCase.verifyEqual(app.getState('TaskA'), 'Complete', 'Task A should now be complete.');
% 7. Remove Task A (which should close the window)
app.removeBar('TaskA');
drawnow;
% Check that the figure window was deleted automatically
testCase.verifyFalse(isvalid(figHandle), 'Figure should be auto-deleted after the last bar is removed.');
end
function testDeeplyNestedProgressTasks(testCase)
% Tests a three-level nested scenario: A starts, then B starts,
% then C starts, C finishes, B finishes, A finishes.
app = ndi.gui.component.ProgressBarWindow('Deeply Nested Test', 'AutoDelete', true, 'Visible', 'off');
figHandle = app.ProgressFigure;
testCase.addTeardown(@() delete(figHandle(isvalid(figHandle))));
% 1. Start Task A and progress partway
app.addBar('Tag', 'TaskA', 'Label', 'Top-Level Task A');
app.updateBar('TaskA', 0.33);
drawnow;
idxA = strcmpi({app.ProgressBars.Tag}, 'TaskA');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 2, 'Grid should have 2 rows for Task A.');
testCase.verifyEqual(app.ProgressBars(idxA).Progress, 0.33, 'Task A initial progress incorrect.');
% 2. Start Task B and progress partway
app.addBar('Tag', 'TaskB', 'Label', 'Mid-Level Task B');
app.updateBar('TaskB', 0.5);
drawnow;
idxB = strcmpi({app.ProgressBars.Tag}, 'TaskB');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 4, 'Grid should have 4 rows for A and B.');
testCase.verifyEqual(app.ProgressBars(idxB).Progress, 0.5, 'Task B initial progress incorrect.');
testCase.verifyEqual(app.ProgressBars(idxA).Progress, 0.33, 'Task A progress should be unchanged.');
% 3. Start Task C
app.addBar('Tag', 'TaskC', 'Label', 'Inner-Most Task C');
drawnow;
idxC = strcmpi({app.ProgressBars.Tag}, 'TaskC');
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 6, 'Grid should have 6 rows for A, B, and C.');
testCase.verifyEqual(app.ProgressBars(idxC).Progress, 0, 'Task C should start at 0.');
testCase.verifyEqual(app.ProgressBars(idxB).Progress, 0.5, 'Task B progress should be unchanged.');
% 4. Complete and remove Task C
app.updateBar('TaskC', 1.0);
drawnow;
testCase.verifyEqual(app.getState('TaskC'), 'Complete', 'Task C should be complete.');
app.removeBar('TaskC');
drawnow;
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 4, 'Grid should shrink to 4 rows after C is removed.');
testCase.verifyEqual(app.getState('TaskC'), 'Closed', 'Task C should be marked as closed.');
% 5. Resume, complete, and remove Task B
testCase.verifyEqual(app.ProgressBars(idxB).Progress, 0.5, 'Task B progress should have been preserved.');
app.updateBar('TaskB', 1.0);
drawnow;
testCase.verifyEqual(app.getState('TaskB'), 'Complete', 'Task B should be complete.');
app.removeBar('TaskB');
drawnow;
testCase.verifyNumElements(app.ProgressGrid.RowHeight, 2, 'Grid should shrink to 2 rows after B is removed.');
testCase.verifyEqual(app.getState('TaskB'), 'Closed', 'Task B should be marked as closed.');
% 6. Resume, complete, and remove Task A
testCase.verifyEqual(app.ProgressBars(idxA).Progress, 0.33, 'Task A progress should have been preserved.');
app.updateBar('TaskA', 1.0);
drawnow;
testCase.verifyEqual(app.getState('TaskA'), 'Complete', 'Task A should be complete.');
app.removeBar('TaskA');
drawnow;
% 7. Verify window is closed
testCase.verifyFalse(isvalid(figHandle), 'Figure should be auto-deleted after the final bar is removed.');
end
end
methods (Access = private)
function verifyButtonIconIsSuccess(testCase, buttonHandle)
% Backwards compatibility: Adding icons by name was introduced in MATLAB R2022b
if exist('isMATLABReleaseOlderThan', 'file') && ~isMATLABReleaseOlderThan('R2022b')
testCase.verifyEqual(buttonHandle.Icon, 'success', 'Button icon should be success.');
end
end
function verifyButtonIconIsError(testCase, buttonHandle)
% Backwards compatibility: Adding icons by name was introduced in MATLAB R2022b
if exist('isMATLABReleaseOlderThan', 'file') && ~isMATLABReleaseOlderThan('R2022b')
testCase.verifyEqual(buttonHandle.Icon, 'error', 'Button icon should be error.');
end
end
end
end
function restoreTimeoutIfAlive(app, originalTimeout)
% Teardown helper: restore the app's Timeout only if the app is
% still a live handle. A test that drives AutoDelete to tear the
% window down cannot then reach through app to write a property.
if isvalid(app)
app.Timeout = originalTimeout;
end
end