How do execute a GUI's callback functions from the command line
조회 수: 14 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Andy Campbell
2014년 4월 9일
편집: Andy Campbell
2014년 4월 9일
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
Titus Edelhofer
2014년 4월 9일
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
Letian Wang
2017년 4월 24일
Hi TiTus, can you specify what is the pushbutton1 in your statement? I am a bit confused.
Ragunanth Venkatesh
2017년 8월 7일
pushbutton1 is the tag name of the button. you can find it in the properties section of the pushbutton.
Ragu
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!