-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathFileSelector.m
More file actions
263 lines (178 loc) · 9.04 KB
/
Copy pathFileSelector.m
File metadata and controls
263 lines (178 loc) · 9.04 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
classdef FileSelector < wt.test.BaseWidgetTest
% Implements a unit test for a widget or component
% Copyright 2020-2025 The MathWorks Inc.
%% Test Method Setup
methods (TestMethodSetup)
function setup(testCase)
fcn = @()wt.FileSelector(testCase.Grid);
testCase.Widget = verifyWarningFree(testCase,fcn);
drawnow
end %function
end %methods
%% Unit Tests
methods (Test)
function testValueProperty(testCase)
% Get the edit field
editControl = testCase.Widget.EditControl;
% Set the value
newValue = fullfile("C:","Users");
testCase.verifySetProperty("Value", newValue);
drawnow
testCase.verifyEqual(string(editControl.Value), newValue);
% Set the value
newValue = fullfile("C","Temp","filename.mat");
testCase.verifySetProperty("Value", newValue);
drawnow
testCase.verifyEqual(string(editControl.Value), newValue);
end %function
function testFolderEditField(testCase)
% Get the edit field
editControl = testCase.Widget.EditControl;
% Set the type
testCase.verifySetProperty("SelectionType", "folder");
% Type a valid value
newValue = string(matlabroot);
testCase.verifyTypeAction(editControl, newValue, "Value");
% Verify the warn image does not show
testCase.verifyFalse(logical(testCase.Widget.WarnImage.Visible));
% Verify the ValueIsValidPath value
testCase.verifyTrue(testCase.Widget.ValueIsValidPath)
% Type an invalid value
newValue = "some bad folder path";
testCase.verifyTypeAction(editControl, newValue, "Value");
% Verify the ValueIsValidPath value
testCase.verifyFalse(testCase.Widget.ValueIsValidPath)
% Verify the warn image shows
testCase.verifyVisible(testCase.Widget.WarnImage)
%testCase.verifyTrue(logical(testCase.Widget.WarnImage.Visible));
end %function
function testFileEditField(testCase)
% Get the edit field
editControl = testCase.Widget.EditControl;
% Set the type
testCase.verifySetProperty("SelectionType", "file");
% Type a valid value
newValue = fullfile(matlabroot,"VersionInfo.xml");
testCase.verifyTypeAction(editControl, newValue, "Value");
% Verify the ValueIsValidPath value
testCase.verifyTrue(testCase.Widget.ValueIsValidPath)
% Verify the warn image does not show
testCase.verifyFalse(logical(testCase.Widget.WarnImage.Visible));
% Type an invalid value
newValue = "some bad file path";
testCase.verifyTypeAction(editControl, newValue, "Value");
% Verify the ValueIsValidPath value
testCase.verifyFalse(testCase.Widget.ValueIsValidPath)
% Verify the warn image shows
testCase.verifyVisible(testCase.Widget.WarnImage)
%testCase.verifyTrue(logical(testCase.Widget.WarnImage.Visible));
end %function
function testRootDirectoryAndHistory(testCase)
% Get the dropdown field
dropdownControl = testCase.Widget.DropdownControl;
% Configure the widget
testCase.verifySetProperty("SelectionType", "folder");
testCase.verifySetProperty("ShowHistory", true);
testCase.verifySetProperty("History", string.empty(0,1));
% Use MATLAB's root
rootDir = string(matlabroot);
% Set the root
testCase.verifySetProperty("RootDirectory", rootDir);
% Type a valid value
newValue1 = fullfile("bin");
testCase.verifyTypeAction(dropdownControl, newValue1, "Value");
testCase.verifyEqual(testCase.Widget.Value, newValue1);
% Verify the ValueIsValidPath value
testCase.verifyTrue(testCase.Widget.ValueIsValidPath)
% Verify the warn image does not show
testCase.verifyFalse(logical(testCase.Widget.WarnImage.Visible));
% Verify the full path
fullPath = fullfile(rootDir,newValue1);
testCase.verifyEqual(testCase.Widget.FullPath, fullPath);
% Switch to folder mode
testCase.verifySetProperty("SelectionType", "folder");
% Set a valid value
newValue2 = fullfile("toolbox","matlab");
testCase.verifySetProperty("Value", newValue2);
drawnow
testCase.verifyEqual(string(dropdownControl.Value), newValue2);
% Verify the ValueIsValidPath value
testCase.verifyTrue(testCase.Widget.ValueIsValidPath)
% Verify the warn image does not show
testCase.verifyFalse(logical(testCase.Widget.WarnImage.Visible));
% Verify the full path
fullPath = fullfile(rootDir,newValue2);
testCase.verifyEqual(testCase.Widget.FullPath, fullPath);
% Verify the history
expValue = [newValue2; newValue1];
testCase.verifyEqual(testCase.Widget.History, expValue);
testCase.verifyEqual(string(dropdownControl.Items(:)), expValue);
end %function
function testButtonLabel(testCase)
% Get the button control
buttonControl = testCase.Widget.ButtonControl;
% Set the value
newValue = "Select File";
testCase.verifySetProperty("ButtonLabel", newValue);
drawnow
testCase.verifyEqual(string(buttonControl.Text), newValue);
% Set the value
newValue = "Browse";
testCase.verifySetProperty("ButtonLabel", newValue);
drawnow
testCase.verifyEqual(string(buttonControl.Text), newValue);
end %function
% Since this test-case unlocks the test figure it should be last in
% line.
function testButton(testCase)
% Running in desktop mode?
testCase.assumeEqual(exist('desktop', 'file'), 6, 'Cannot find function ''desktop.m''.')
testCase.assumeTrue(desktop('-inuse'), 'MATLAB must run in desktop mode in order to complete current test.')
% Get the button control
buttonControl = testCase.Widget.ButtonControl;
% Ancestor figure
fig = ancestor(buttonControl, "Figure");
% Make sure file dialog window is in-app by setting the
% 'ShowInWebApps' value to true.
% Get active value to restore
s = settings;
curTempVal = s.matlab.ui.dialog.fileIO.ShowInWebApps.ActiveValue;
% Set temporary value of ShowInWebApps setting to true, so that file
% selector dialog window is a component in the figure.
s.matlab.ui.dialog.fileIO.ShowInWebApps.TemporaryValue = true;
cleanup = onCleanup(@() localRevertShowInWebAppsSetting(s, curTempVal));
% While dialog window is open and blocked by waitfor, there is still
% a possibility to execute code through the timer function.
% Set timer callback
delay = 2; % seconds
t = timer;
t.StartDelay = delay; % starts after 2 seconds
t.TimerFcn = @(s,e) localPressEscape(fig);
start(t); % start the timer
% Now press the button
tStart = tic;
testCase.press(buttonControl);
% Wait for escape button to be pressed.
tStop = toc(tStart);
% Time while MATLAB waits for an action should be larger than the
% StartDelay. If not, MATLAB did not reach the waitfor status after
% pressing the file-selection button.
testCase.verifyGreaterThan(tStop, delay)
end %function
end %methods (Test)
end %classdef
function localPressEscape(fig)
% Unlock the figure, otherwise escape will not work.
matlab.uitest.unlock(fig);
% Bring focus to figure
figure(fig)
% Press ESCAPE
r = java.awt.Robot;
r.keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
pause(0.1);
r.keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
end
function localRevertShowInWebAppsSetting(s, val)
% Revert setting on cleanup
s.matlab.ui.dialog.fileIO.ShowInWebApps.TemporaryValue = val;
end