Skip to content

Commit 68a0c34

Browse files
authored
Fix for eui.ExpPanel/live input parse error and tests added (#239)
* Fix for eui.ExpPanel/live input parse error and tests added * Updated CHANGELOG and tests Contents file
1 parent 1bd4f97 commit 68a0c34

File tree

4 files changed

+129
-19
lines changed

4 files changed

+129
-19
lines changed

+eui/ExpPanel.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,19 @@
108108
% Outputs:
109109
% p (eui.ExpPanel) : handle to the panel object.
110110
%
111-
p = inputParser;
112-
addRequired(p, 'parent');
113-
addRequired(p, 'ref');
114-
addRequired(p, 'remoteRig');
115-
addRequired(p, 'paramsStruct');
111+
in = inputParser;
112+
addRequired(in, 'parent');
113+
addRequired(in, 'ref');
114+
addRequired(in, 'remoteRig');
115+
addRequired(in, 'paramsStruct');
116116
% Activate log
117-
addParameter(p, 'activateLog', true);
117+
addOptional(in, 'activateLog', true);
118118
% Resume experiment listening (experiment had alread started)
119-
addParameter(p, 'startedTime', []);
120-
parse(p, pos, t, Fs, varargin{:});
119+
addOptional(in, 'startedTime', []);
120+
in.parse(parent, ref, remoteRig, paramsStruct, varargin{:})
121121

122-
p = p.Results; % Final parameters
123-
if p.activateLog
122+
in = in.Results; % Final parameters
123+
if in.activateLog
124124
subject = dat.parseExpRef(ref); % Extract subject, date and seq from experiment ref
125125
try
126126
logEntry = dat.addLogEntry(... % Add new entry to log
@@ -162,7 +162,7 @@
162162
@() set(p.StopButtons, 'Enable', 'off')));
163163
p.Root.Title = sprintf('%s on ''%s''', p.Ref, remoteRig.Name); % Set experiment panel title
164164

165-
if ~isempty(p.startedTime)
165+
if ~isempty(in.startedTime)
166166
% If the experiment has all ready started, trigger all dependent
167167
% events.
168168
p.expStarted(remoteRig, srv.ExpEvent('started', ref, p.startedTime));

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Starting after Rigbox 2.2.0, this file contains a curated, chronologically order
2323
- added guide to creating custom ExpPanels `90294dd` 2019-12-18
2424
- correct behaviour when listening to already running experiments `32a2a17` 2019-12-18
2525
- added support for remote error ids in srv.StimulusControl `9d31eea` 2019-11-27
26+
- added tests for eui.ExpPanel `572463c` 2020-01-28
2627

2728
## [2.4.1]
2829

tests/Contents.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
% checkCoverage - Check the coverage of a given test
1111
%
1212
% AlyxPanel_test - Tests for eui.AlyxPanel
13+
% ExpPanel_test - Tests for eui.ExpPanel
1314
% ParamEditor_test - Tests for eui.ParamEditor
1415
% Parameters_test - Tests for exp.Parameters
1516
% calibrate_test - Tests for hw.calibrate

tests/ExpPanelTest.m

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
classdef (SharedTestFixtures={ % add 'fixtures' folder as test fixture
22
matlab.unittest.fixtures.PathFixture('fixtures'),...
3-
matlab.unittest.fixtures.PathFixture(['fixtures' filesep 'util'])})...
4-
ExpPanelTest < matlab.mock.TestCase
3+
matlab.unittest.fixtures.PathFixture(['fixtures' filesep 'expDefinitions'])})...
4+
ExpPanelTest < matlab.unittest.TestCase
55

66
properties (SetAccess = protected)
77
% The figure that contains the ExpPanel
88
Parent
99
% Handle for ExpPanel
1010
Panel eui.ExpPanel
11+
% Remote Rig object
12+
Remote srv.StimulusControl
13+
% A parameters structure
14+
Parameters
15+
% An experiment reference string
16+
Ref
1117
end
12-
18+
19+
properties (MethodSetupParameter)
20+
% Experiment type under test
21+
ExpType = {'Base', 'Signals'} % TODO Add tests for ChoiceWorld, etc.
22+
end
23+
1324
methods (TestClassSetup)
1425
function setup(testCase)
15-
% SETUP TODO Document
26+
% SETUP Set up test case
27+
% The following occurs during setup:
28+
% 1. Creating parent figure, turn off figure visability and delete
29+
% on taredown.
30+
% 2. Set test flag to true to avoid path in test assertion error.
31+
% 3. Applies repos fixture and create a test subject and expRef.
32+
% 4. Instantiates a StimulusControl object for event listeners.
1633

1734
% Hide figures and add teardown function to restore settings
1835
def = get(0,'DefaultFigureVisible');
@@ -21,24 +38,115 @@ function setup(testCase)
2138

2239
% Create figure for panel
2340
testCase.Parent = figure();
41+
testCase.addTeardown(@delete, testCase.Parent)
2442

2543
% Set INTEST flag to true
2644
setTestFlag(true);
2745
testCase.addTeardown(@setTestFlag, false)
46+
47+
% Ensure we're using the correct test paths and add teardowns to
48+
% remove any folders we create
49+
testCase.applyFixture(ReposFixture)
50+
51+
% Now create a single subject folder for testing the log
52+
subject = 'test';
53+
mainRepo = dat.reposPath('main', 'master');
54+
assert(mkdir(fullfile(mainRepo, subject)), ...
55+
'Failed to create subject folder')
56+
testCase.Ref = dat.constructExpRef(subject, now, 1);
57+
58+
% Set up a StimulusControl object for simulating rig events
59+
testCase.Remote = srv.StimulusControl.create('testRig');
2860
end
2961
end
3062

3163
methods (TestMethodSetup)
32-
function setupPanel(testCase)
33-
% testCase.ExpPanel = eui.ExpPanel.live();
64+
function setupParams(testCase, ExpType)
65+
% SETUPPARAMS Set up parameters struct
66+
% Create a parameters structure depending of the ExpType.
67+
68+
switch lower(ExpType)
69+
case 'signals'
70+
% A Signals experiment without the custom ExpPanel. Instantiates
71+
% the eui.SignalsExpPanel class
72+
testCase.Parameters = struct('type', 'custom', 'defFunction', @nop);
73+
case 'choiceworld'
74+
% ChoiceWorld experiment params. Instantiates the
75+
% eui.ChoiceExpPanel class
76+
testCase.Parameters = exp.choiceWorldParams;
77+
case 'custom'
78+
% Signals experiment params with the expPanelFun parameter.
79+
% Calls the function defined in that parameter
80+
testCase.Parameters = exp.inferParameters(@advancedChoiceWorld);
81+
case 'base'
82+
% Instantiates the eui.ExpPanel base class
83+
testCase.Parameters = struct(...
84+
'experimentFun', @(pars, rig) nop, ...
85+
'type', 'unknown');
86+
case 'barmapping'
87+
% Instantiates the eui.MappingExpPanel class
88+
testCase.Parameters = exp.barMappingParams;
89+
otherwise
90+
testCase.Parameters = [];
91+
end
92+
end
93+
end
94+
95+
methods (TestMethodTeardown)
96+
function clearFigure(testCase)
97+
% Completely reset the figure on taredown
98+
testCase.Parent = clf(testCase.Parent, 'reset');
3499
end
35100
end
36101

37102
methods (Test)
38-
function test_panel(testCase)
39-
% TODO Write tests for ExpPanel
103+
function test_live(testCase)
104+
% Test the live constructor method for various experiment types. The
105+
% following things are tested:
106+
% 1. Default update labels
107+
% 2. ActivateLog parameter functionality
108+
% 3. Comments box context menu functionality
109+
% 4. TODO Test comments changed callback
110+
% 5. TODO Check params button function
111+
inputs = {
112+
testCase.Parent;
113+
testCase.Ref;
114+
testCase.Remote;
115+
testCase.Parameters};
116+
testCase.Panel = eui.ExpPanel.live(inputs{:}, 'ActivateLog', false);
117+
118+
testCase.fatalAssertTrue(isvalid(testCase.Panel))
119+
% Test the default labels have been created
120+
% Find all labels
121+
labels = findall(testCase.Parent, 'Style', 'text');
122+
expected = {'0', '-:--', 'Pending', 'Trial count', 'Elapsed', 'Status'};
123+
testCase.verifyEqual({labels.String}, expected, 'Default labels incorrect')
124+
comments = findall(testCase.Parent, 'Style', 'edit');
125+
testCase.assertEmpty(comments, 'Unexpected comments box');
126+
127+
% Test build with log activated
128+
delete(testCase.Panel) % Delete previous panel
129+
testCase.Panel = eui.ExpPanel.live(inputs{:}, 'ActivateLog', true);
130+
% Check Comments label exists
131+
labels = findall(testCase.Parent, 'Style', 'text');
132+
commentsLabel = labels(strcmp({labels.String}, 'Comments'));
133+
testCase.assertNotEmpty(commentsLabel)
134+
% Check comments box exits
135+
comments = findall(testCase.Parent, 'Style', 'edit');
136+
testCase.assertNotEmpty(comments, 'Failed to create comments box');
137+
% Test comments box hiding
138+
testCase.assertTrue(strcmp(comments.Visible, 'on'))
139+
menuOption = commentsLabel.UIContextMenu.Children(1);
140+
menuOption.MenuSelectedFcn(menuOption) % Trigger menu callback
141+
testCase.assertTrue(strcmp(comments.Visible, 'off'), 'Failed to hide comments')
142+
menuOption.MenuSelectedFcn(menuOption) % Trigger menu callback
143+
testCase.assertTrue(strcmp(comments.Visible, 'on'), 'Failed to show comments')
40144
end
41145

146+
% function test_starttime(testCase)
147+
% % TODO Test Start time input as input (i.e. for reconnect)
148+
% end
149+
42150
end
43151

44152
end

0 commit comments

Comments
 (0)