Skip to content

Commit 81c5f08

Browse files
committed
Add testButton test case for wt.FileSelector
1 parent e3e1230 commit 81c5f08

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

test/+wt/+test/FileSelector.m

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function testRootDirectoryAndHistory(testCase)
168168

169169
function testButtonLabel(testCase)
170170

171-
% Get the edit field
171+
% Get the button control
172172
buttonControl = testCase.Widget.ButtonControl;
173173

174174
% Set the value
@@ -185,13 +185,75 @@ function testButtonLabel(testCase)
185185

186186
end %function
187187

188-
% function testButton(testCase)
189-
%
190-
% % We can't test the button in this case because it triggers a
191-
% % modal dialog with no way to click on the dialog.
192-
%
193-
% end %function
188+
% Since this test-case unlocks the test figure it should be last in
189+
% line.
190+
function testButton(testCase)
191+
192+
% Get the button control
193+
buttonControl = testCase.Widget.ButtonControl;
194+
195+
% Ancestor figure
196+
fig = ancestor(buttonControl, "Figure");
197+
198+
% Make sure file dialog window is in-app by setting the
199+
% 'ShowInWebApps' value to true.
200+
201+
% Get active value to restore
202+
s = settings;
203+
curTempVal = s.matlab.ui.dialog.fileIO.ShowInWebApps.ActiveValue;
204+
205+
% Set temporary value of ShowInWebApps setting to true, so that file
206+
% selector dialog window is a component in the figure.
207+
s.matlab.ui.dialog.fileIO.ShowInWebApps.TemporaryValue = true;
208+
cleanup = onCleanup(@() localRevertShowInWebAppsSetting(s, curTempVal));
209+
210+
% While dialog window is open and blocked by waitfor, there is still
211+
% a possibility to execute code through the timer function.
212+
213+
% Set timer callback
214+
delay = 2; % seconds
215+
t = timer;
216+
t.StartDelay = delay; % starts after 2 seconds
217+
t.TimerFcn = @(s,e) localPressEscape(fig);
218+
start(t); % start the timer
219+
220+
% Now press the button
221+
tStart = tic;
222+
testCase.press(buttonControl);
223+
224+
% Wait for escape button to be pressed.
225+
tStop = toc(tStart);
226+
227+
% Time while MATLAB waits for an action should be larger than the
228+
% StartDelay. If not, MATLAB did not reach the waitfor status after
229+
% pressing the file-selection button.
230+
testCase.verifyGreaterThan(tStop, delay)
231+
232+
end %function
194233

195234
end %methods (Test)
196235

197-
end %classdef
236+
end %classdef
237+
238+
function localPressEscape(fig)
239+
240+
% Unlock the figure, otherwise escape will not work.
241+
matlab.uitest.unlock(fig);
242+
243+
% Bring focus to figure
244+
figure(fig)
245+
246+
% Press ESCAPE
247+
r = java.awt.Robot;
248+
r.keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
249+
pause(0.1);
250+
r.keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
251+
252+
end
253+
254+
function localRevertShowInWebAppsSetting(s, val)
255+
256+
% Revert setting on cleanup
257+
s.matlab.ui.dialog.fileIO.ShowInWebApps.TemporaryValue = val;
258+
259+
end

0 commit comments

Comments
 (0)