1+ classdef ParamEditorPerfTest < matlab .perftest .TestCase
2+
3+ properties
4+ % Figure visibility setting before running tests
5+ FigureVisibleDefault
6+ % ParamEditor instance
7+ ParamEditor
8+ % Figure handle for ParamEditor
9+ Figure
10+ % Handle to trial conditions UI Table
11+ Table
12+ % Test parameter structure
13+ Parameters
14+ end
15+
16+ methods (TestClassSetup )
17+ function setup(testCase )
18+ % Hide figures and add teardown function to restore settings
19+ testCase.FigureVisibleDefault = get(0 ,' DefaultFigureVisible' );
20+ set(0 ,' DefaultFigureVisible' ,' off' );
21+ testCase .addTeardown(@set , 0 , ' DefaultFigureVisible' , testCase .FigureVisibleDefault );
22+
23+ % Loads validation data
24+ % Graph data is a cell array where each element is the graph number
25+ % (1:3) and within each element is a cell of X- and Y- axis values
26+ % respecively
27+ testCase.Parameters = exp .choiceWorldParams ;
28+
29+ % Check paths file
30+ assert(endsWith(which(' dat.paths' ), fullfile(' tests' ,' +dat' ,' paths.m' )));
31+ % Create stand-alone panel
32+ testCase.ParamEditor = eui .ParamEditor ;
33+ testCase.Figure = gcf();
34+ testCase .addTeardown(@close , testCase .Figure );
35+ assert(isa(testCase .ParamEditor , ' eui.ParamEditor' ))
36+ % Find Condition Table
37+ testCase.Table = findobj(testCase .Figure , ' -property' , ' ColumnName' );
38+ assert(isa(testCase .Table , ' matlab.ui.control.Table' ), ...
39+ ' Failed to find handle to condition table' )
40+ end
41+
42+ end
43+
44+ methods (TestMethodSetup )
45+ function buildParams(testCase )
46+ % Re-build the parameters before each test so that changes in
47+ % previous test don't persist
48+ PE = testCase .ParamEditor ;
49+ pars = exp .Parameters(testCase .Parameters );
50+ PE .buildUI(pars );
51+ % Number of global parameters: find all text labels
52+ nGlobalLabels = numel(findobj(testCase .Figure , ' Style' , ' text' ));
53+ nGlobalInput = numel(findobj(testCase .Figure , ' Style' , ' checkbox' , ' -or' , ' Style' , ' edit' ));
54+ % Ensure all global params have UI input and label
55+ assert(nGlobalLabels == numel(PE .Parameters .GlobalNames ))
56+ assert(nGlobalInput == numel(PE .Parameters .GlobalNames ))
57+ % Ensure all conditional params have column in table
58+ assert(isequal(size(testCase .Table .Data ), ...
59+ [PE .Parameters .numTrialConditions , numel(PE .Parameters .TrialSpecificNames )]))
60+ end
61+ end
62+
63+ methods (Test )
64+ function test_newParamEditor(testCase )
65+ % Test instantiate new param editor from scratch
66+ pars = exp .Parameters(testCase .Parameters );
67+ testCase .startMeasuring();
68+ PE = eui .ParamEditor(pars );
69+ testCase .stopMeasuring();
70+ close(gcf )
71+ delete(PE )
72+ end
73+
74+ function test_buildUI(testCase )
75+ % Test clear and rebuild params
76+ pars = exp .Parameters(testCase .Parameters );
77+ testCase .startMeasuring();
78+ testCase .ParamEditor .buildUI(pars );
79+ testCase .stopMeasuring();
80+ end
81+
82+ function test_makeConditional(testCase )
83+ % Make some global params trial conditions. This test checks that
84+ % the UI elements are re-rendered after making a parameter
85+ % conditional, and that the underlying Parameters object is also
86+ % affected
87+ PE = testCase .ParamEditor ;
88+ % Number of global parameters: find all text labels
89+ gLabels = @()findobj(testCase .Figure , ' Style' , ' text' );
90+ gInputs = @()findobj(testCase .Figure , ' Style' , ' checkbox' , ' -or' , ' Style' , ' edit' );
91+ nGlobalLabels = numel(gLabels());
92+ nGlobalInputs = numel(gInputs());
93+ tableSz = size(testCase .Table .Data );
94+
95+ % Retrieve context menu function handle
96+ c = findobj(testCase .Figure , ' Text' , ' Make Conditional' );
97+ % Set the focused object to one of the parameter labels
98+ set(testCase .Figure , ' CurrentObject' , ...
99+ findobj(testCase .Figure , ' Tag' , ' rewardVolume' ))
100+
101+ % %% Make conditional %%%
102+ testCase .startMeasuring();
103+ c .MenuSelectedFcn()
104+ testCase .stopMeasuring();
105+
106+ % Verify change in UI elements
107+ testCase .verifyTrue(numel(gLabels()) == nGlobalLabels - 1 , ...
108+ ' Global parameter UI element not removed' )
109+ testCase .verifyTrue(numel(gInputs()) == nGlobalInputs - 1 , ...
110+ ' Global parameter UI element not removed' )
111+ testCase .verifyTrue(size(testCase .Table .Data ,2 ) == tableSz(2 )+1 , ...
112+ ' Incorrect condition table' )
113+ % Verify change in Parameters object for global
114+ testCase .assertTrue(numel(gLabels()) == numel(PE .Parameters .GlobalNames ))
115+ % Verify change in Parameters object for conditional
116+ testCase .assertTrue(isequal(size(testCase .Table .Data ), ...
117+ [PE .Parameters .numTrialConditions , numel(PE .Parameters .TrialSpecificNames )]))
118+ % Verify table values are correct
119+ testCase .verifyTrue(isequal(testCase .Table .Data(: ,1 ), repmat({' 3' }, ...
120+ size(testCase .Table .Data ,1 ), 1 )), ' Unexpected table values' )
121+ end
122+
123+ function test_newCondition(testCase )
124+ PE = testCase .ParamEditor ;
125+ tableRows = size(testCase .Table .Data , 1 );
126+
127+ % Make function handle param conditional to test default value
128+ % Set the focused object to one of the parameter labels
129+ set(testCase .Figure , ' CurrentObject' , ...
130+ findobj(testCase .Figure , ' Tag' , ' experimentFun' ))
131+ feval(pick(findobj(testCase .Figure , ' Text' , ' Make Conditional' ), ' MenuSelectedFcn' ))
132+
133+ % Retrieve function handle for new condition
134+ fn = pick(findobj(testCase .Figure , ' String' , ' New condition' ), ' Callback' );
135+ testCase .startMeasuring();
136+ fn()
137+ testCase .stopMeasuring();
138+
139+ % Verify change in table data
140+ testCase .verifyEqual(size(testCase .Table .Data , 1 ), tableRows + 1 , ...
141+ ' Unexpected number of trial conditions' )
142+
143+ % Verify change in Parameters object for conditional
144+ testCase .assertEqual(size(testCase .Table .Data ), ...
145+ [PE .Parameters .numTrialConditions , numel(PE .Parameters .TrialSpecificNames )])
146+ end
147+
148+ function test_deleteCondition(testCase )
149+ PE = testCase .ParamEditor ;
150+ tableRows = size(testCase .Table .Data , 1 );
151+ % Select some cells to delete
152+ event.Indices = [(1 : 5 )' ones(5 ,1 )];
153+ selection_fn = testCase .Table .CellSelectionCallback ;
154+ selection_fn([],event )
155+
156+ % Retrieve function handle for delete condition
157+ callback_fn = pick(findobj(testCase .Figure , ' String' , ' Delete condition' ), ' Callback' );
158+ testCase .startMeasuring();
159+ callback_fn()
160+ testCase .stopMeasuring();
161+
162+ % Verify change in table data
163+ testCase .verifyEqual(size(testCase .Table .Data , 1 ), tableRows - 5 , ...
164+ ' Unexpected number of trial conditions' )
165+
166+ % Verify change in Parameters object for conditional
167+ testCase .assertEqual(size(testCase .Table .Data ), ...
168+ [PE .Parameters .numTrialConditions , numel(PE .Parameters .TrialSpecificNames )])
169+ end
170+
171+ function test_globaliseParam(testCase )
172+ PE = testCase .ParamEditor ;
173+ tableCols = size(testCase .Table .Data , 2 );
174+ % Globalize one param
175+ event.Indices = [1 , 2 ];
176+ selection_fn = testCase .Table .CellSelectionCallback ;
177+ selection_fn([],event )
178+
179+ % Retrieve function handle for new condition
180+ callback_fn = pick(findobj(testCase .Figure , ' String' , ' Globalise parameter' ), ' Callback' );
181+ testCase .startMeasuring();
182+ callback_fn()
183+ testCase .stopMeasuring();
184+
185+ % Verify change in table data
186+ testCase .verifyEqual(size(testCase .Table .Data ,2 ), tableCols - 1 , ...
187+ ' Unexpected number of conditional parameters' )
188+ % Verify change in Parameters object for conditional
189+ testCase .verifyEqual(size(testCase .Table .Data ), ...
190+ [PE .Parameters .numTrialConditions , numel(PE .Parameters .TrialSpecificNames )])
191+ end
192+
193+ function test_paramEdits(testCase )
194+ % Test basic edits to Global UI controls and Condition table
195+ PE = testCase .ParamEditor ;
196+
197+ % Retreive all global parameters labels and input controls
198+ gLabels = findobj(testCase .Figure , ' Style' , ' text' );
199+ gInputs = findobj(testCase .Figure , ' Style' , ' checkbox' , ' -or' , ' Style' , ' edit' );
200+
201+ % Test editing global param, 'edit' UI
202+ idx = find(strcmp({gInputs .Style }, ' edit' ), 1 );
203+ % Change string
204+ gInputs(idx ).String = ' 666' ;
205+ % Trigger callback
206+ callback_fcn = gInputs(idx ).Callback;
207+ testCase .startMeasuring();
208+ callback_fcn(gInputs(idx ));
209+ testCase .stopMeasuring();
210+
211+ % Verify change in ui string
212+ testCase .verifyEqual(gInputs(idx ).String, ' 666' )
213+ % Verify change in label color
214+ testCase .verifyEqual(gLabels(idx ).ForegroundColor, [1 0 0 ], ...
215+ ' Unexpected label colour' )
216+ % Verify change in underlying param struct
217+ par = strcmpi(PE .Parameters .GlobalNames , strrep(gLabels(idx ).String, ' ' , ' ' ));
218+ testCase .verifyEqual(PE .Parameters .Struct.(PE.Parameters.GlobalNames{par }), 666 , ...
219+ ' UI edit failed to update parameters struct' )
220+
221+ % Test edits to conditions table
222+ callback_fcn = testCase .Table .CellEditCallback ;
223+ event.Indices = [1 , 1 ];
224+ event.NewData = ' 0,5' ;
225+ testCase .startMeasuring();
226+ callback_fcn(testCase .Table , event )
227+ testCase .stopMeasuring();
228+
229+ % Verify change to table value
230+ testCase .verifyEqual(testCase.Table.Data{1 ,1 }, ' 0, 5' , ...
231+ ' Unexpected table data' )
232+ % Verify change in underlying param struct
233+ value = PE .Parameters .Struct.(PE.Parameters.TrialSpecificNames{1 });
234+ testCase .verifyEqual(value(: ,1 ), [0 ;5], ...
235+ ' Table UI failed to update parameters struct' )
236+ end
237+
238+ end
239+
240+ end
0 commit comments