%% Indicate where to find screen capture functions
addpath([matlabroot,'\doc\tools\bin'])
addpath([matlabroot,'\test\tools\slrtw\dastudio'])
addpath([matlabroot,'\test\tools\matlab\qehgtools'])

%% Save the EM working example path, where the PNG is
EMPath = pwd;


%% Set up the Test Sequence, programmatically

% Create a Test Harness Containing a Test Sequence Block
%
% 1. Load the model.
model = 'sltestCruiseChart';
load_system(fullfile(matlabroot,'examples','simulinktest','sltestCruiseChart'))


% 2. Create the test harness.
sltest.harness.create(model,'Name','Harness1',...
    'Source','Test Sequence')
sltest.harness.load(model,'Harness1');
set_param('Harness1','StopTime','15');

% Author the Test Sequence
%
% 1. Add a local variable |endTest|, which you use to transition between
% test steps, and set the data type to |boolean|.
sltest.testsequence.addSymbol('Harness1/Test Sequence','endTest',...
    'Data','Local');

sltest.testsequence.editSymbol('Harness1/Test Sequence','endTest',...
    'DataType','boolean');


% 2. Change the name of the step |Run| to |Initialize1|.
sltest.testsequence.editStep('Harness1/Test Sequence','Run',...
    'Name','Initialize1');


% 3. Add a step |BrakeTest| to test that the cruise control disengages when
% the brake is applied. Also add substeps defining the test scenario
% actions and verification.
sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
    'BrakeTest','Initialize1','Action','endTest = false;')
    
    % Add a transition from |Initialize1| to |BrakeTest|.
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'Initialize1','true','BrakeTest')

    % This sub-step enables the cruise control and sets the speed.
    % |SetValuesActions| is the actions for BrakeTest.SetValues.
    setValuesActions = sprintf('CruiseOnOff = true;\nSpeed = 50;');
    sltest.testsequence.addStep('Harness1/Test Sequence',...
        'BrakeTest.SetValues','Action',setValuesActions)
    
    % This sub-step engages the cruise control.
    setCCActions = sprintf('CoastSetSw = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'BrakeTest.Engage','BrakeTest.SetValues','Action',setCCActions)
    
    % This step applies the brake.
    brakeActions = sprintf('CoastSetSw = false;\nBrake = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'BrakeTest.Brake','BrakeTest.Engage','Action',brakeActions)
    
    % This step verifies that the cruise control is off.
    brakeVerifyActions = sprintf('verify(engaged == false)\nendTest = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'BrakeTest.Verify','BrakeTest.Brake','Action',brakeVerifyActions)
    
    % Add transitions between steps.
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'BrakeTest.SetValues','true','BrakeTest.Engage')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'BrakeTest.Engage','after(2,sec)','BrakeTest.Brake')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'BrakeTest.Brake','true','BrakeTest.Verify')


% 4. Add a step |Initialize2| to initialize component inputs again, and add
% a transition from |BrakeTest| to |Initialize2|.
init2Actions = sprintf(['CruiseOnOff = false;\n'...
    'Brake = false;\n'...
    'Speed = 0;\n'...
    'CoastSetSw = false;\n'...
    'AccelResSw = false;']);
sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
    'Initialize2','BrakeTest','Action',init2Actions)
sltest.testsequence.addTransition('Harness1/Test Sequence',...
    'BrakeTest','endTest == true','Initialize2')


% 5. Add a step |LimitTest| to test cruise control disengagement when the
% vehicle speed exceeds the high limit. Also, add a transition from the
% |Initialize2| step, and add sub-steps to define the scenario actions and
% verification.
sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
    'LimitTest','Initialize2')
sltest.testsequence.addTransition('Harness1/Test Sequence',...
    'Initialize2','true','LimitTest')

    % Add a step to enable cruise control and set the speed.
    setValuesActions2 = sprintf('CruiseOnOff = true;\nSpeed = 60;');
    sltest.testsequence.addStep('Harness1/Test Sequence',...
        'LimitTest.SetValues','Action',setValuesActions2)
    
    % Add a step to engage the cruise control.
    setCCActions = sprintf('CoastSetSw = true;');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'LimitTest.Engage','LimitTest.SetValues','Action',setCCActions)

    % Add a step to ramp the vehicle speed.
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'LimitTest.RampUp','LimitTest.Engage','Action','Speed = Speed + ramp(5*et);')
    
    % Add a step to verify that the cruise control is off.
    highLimVerifyActions = sprintf('verify(engaged == false)');
    sltest.testsequence.addStepAfter('Harness1/Test Sequence',...
        'LimitTest.VerifyHigh','LimitTest.RampUp','Action',highLimVerifyActions)
    
    % Add transitions between steps. The speed ramp transitions when the
    % vehicle speed exceeds 90.
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'LimitTest.SetValues','true','LimitTest.Engage')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'LimitTest.Engage','true','LimitTest.RampUp')
    sltest.testsequence.addTransition('Harness1/Test Sequence',...
        'LimitTest.RampUp','Speed > 90','LimitTest.VerifyHigh')

% Open the test harness to view the test sequence.
sltest.harness.open(model,'Harness1');


%% Replace the image test_sequence.png

% Get the screen capture
cd(EMPath)   % Go to the path to the working example
open_system('Harness1/Test Sequence')
disp('Now size the window')
pause(30)
snapAnyWindow('test_sequence.png')

%%
disp('Finished taking screen captures')
