How do execute a GUI's callback functions from the command line
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I would like to incorporate unit tests as part of my GUI development. All the functions I use within my callbacks have tests, but of course nothing within the callback stops me from implementing my functions incorrectly, other than the MatLab throwing an error. For example,
function varargout = someAutoGenenratedGUIfile(varargin)
% The usual MatLab auto-generated stuff....
% End initialization code - DO NOT EDIT
...
function genericFunctionTag_Callback(hObject,~,handles)
output = functionThatReturnsOneOutput ( input );
end
...works fine for now.
But consider when I make a small change to the function:
...function before,
output = functionThatReturnsOneOutput ( input );
...function after,
output = functionThatReturnsOneOutput ( input1,input2 );
Now the old code in the GUI file,
...
function genericFunctionTag_Callback(hObject,~,handles)
output = functionThatReturnsOneOutput ( input );
throws an error because "functionThatReturnsOneOutput" expects two inputs instead of one.
Since opening the GUI and clicking everything one by one somewhat defeats the purpose of unit testing, whenever I make small changes to my functions/classes, I would like to open my GUI, run every callback function available (whilst passing some test data to it) and identify the changes needed to make sure the GUI works.
Any ideas?
0 comentarios
Respuestas (2)
Andy Campbell
el 9 de Abr. de 2014
Editada: Andy Campbell
el 9 de Abr. de 2014
Hello Tolulope,
What you ask for is something that is not currently available in MATLAB, and I have created an enhancement request for consideration for a future release.
In the meantime, you can access the callback directly and invoke it in a unit test. This is not a perfect solution because for one you still need to invoke the callback directly, and its possible that you may do so differently than MATLAB does. For example, if there was a scenario where the callback was invoked twice but you were not aware of that behavior and only tested calling the callback once you might not find a bug lurking in there. Also, certain side effects such as the figure's current point or other state may not be consistent with what happens during actual GUI interaction. However, that being said you might be able to still get some level of protection with this approach.
This can look like:
classdef MyGUITest < matlab.unittest.TestCase
methods(Test)
function testWindowButtonDownFcn(testCase)
% Setup
figHandle = createMyGUI;
testCase.addTeardown(@close, figHandle);
buttonDownFcn = get(figHandle, 'WindowButtonDownFcn');
% Exercise
% That calls the function with the handle to the object
% that generated the callback as well as the event data.
% If you don't use the event data you can just pass in a
% dummy value.
feval(buttonDownFcn, f, []);
% Verify
% verify correct result of buttondownfcn call
end
end
end
Hope that helps!
0 comentarios
Titus Edelhofer
el 9 de Abr. de 2014
Hi,
when you open the GUI in guide, open the property inspector for some uicontrol, you will see in the property "callback" the call that is executed when the user clicks the control.
You can call this code from command line as well. To get the handles do the following
handles = guidata(someAutoGenenratedGUIfile);
Then you can call the callback with something like
someAutoGenenratedGUIfile(handles.pushbutton1, [], handles);
Titus
2 comentarios
Letian Wang
el 24 de Abr. de 2017
Hi TiTus, can you specify what is the pushbutton1 in your statement? I am a bit confused.
Ragunanth Venkatesh
el 7 de Ag. de 2017
pushbutton1 is the tag name of the button. you can find it in the properties section of the pushbutton.
Ragu
Ver también
Categorías
Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!