classdef (Hidden = true) CodeCoverageHelper
%CODECOVERAGEHELPER
%
%   See also 
%
%   This is an undocumented class. Its methods and properties are likely to
%   change without warning from one release to the next.

%   Copyright 2010-2016 The MathWorks, Inc.

    methods (Access=public, Static=true)
 
        function [toolNames, toolClasses, toolCompanies] = getTools(hideSlCov)

            cm = DAStudio.CustomizationManager;
            if isprop(cm, 'CodeCoverageTools')
                hTmp = cm.CodeCoverageTools;
                [toolNames, toolClasses, toolCompanies] = hTmp.get();
                if (nargin >= 1) && hideSlCov
                    idx = strcmp(toolNames, SlCov.getCoverageToolName());
                    toolNames(idx) = [];
                    toolClasses(idx) = [];
                    toolCompanies(idx) = [];
                end
            else
                toolNames = {};
                toolClasses = {};
                toolCompanies = {};
            end
            
        end
        
        function toolDisplayNames = getDisplayNamesForTools(cs, toolNames)
        % Get the display names associated with the specified list of  
        % coverage tools (given the specified configset).

        % Display names are all the same as the tool names except for the
        % "None" option.  This will be displayed differently based on 
        % visibility of Coverage pane (SLV&V) for this configset.
            toolDisplayNames = toolNames;
            toolNoneIdx = strcmp(toolDisplayNames, 'None');
            if configset.internal.customwidget.isSlCovVisible(cs)
                toolDisplayNames{toolNoneIdx} = configset.internal.getMessage('ERTDialogCodeCoverageToolNoneSimcoverage');
            else
                toolDisplayNames{toolNoneIdx} = configset.internal.getMessage('ERTDialogCodeCoverageToolNone');
            end
        end

        function covSettings = getSettingsForClass(cs, toolName, toolClass)
            
            covHook = coder.coverage.BuildHook.getBuildHookForClass(cs, toolClass);
            if ~isempty(covHook)
                covSettings = coder.coverage.CodeCoverageHelper.getCovSettingsFromHook(covHook);
            else
                % Start with default settings
                covSettings = coder.coverage.CodeCoverageSettings('');
            end
            
            covSettings.CoverageTool = toolName;
            % Enable coverage for top model and referenced models
            if ~isempty(toolClass)
                covSettings.TopModelCoverage = 'on';
                covSettings.ReferencedModelCoverage = 'on';
            end
            
        end
        
        
        function settings = getCovSettingsFromHook(hook)

            % First check that we have a valid code coverage hook
            [topModel, refModels] = ...
                coder.coverage.CodeCoverageHelper.validateCodeCoverageHook(hook);
        
            % Default values
            settings = coder.coverage.CodeCoverageSettings('');
            
            [toolNames, toolClasses] = coder.coverage.CodeCoverageHelper.getTools();

            toolIdx = strcmp(toolClasses, hook.className);
            
            if sum(toolIdx)==1
                    settings.CoverageTool =  toolNames{toolIdx};
                    settings.TopModelCoverage = topModel;
                    settings.ReferencedModelCoverage = refModels;
            end
            
        end
    
    end
    
            
    methods (Access=private, Static=true)
        
        function [topModel, refModels] = validateCodeCoverageHook(hook)
            
            hookValid=true;
            topModel = 'off';
            
            if ~isempty(hook.argsAllComponents ) || ...
                    ~isempty(hook.excludedModels) || ...
                    isempty(hook.includeReferencedModels) || ...
                    isempty(hook.includeTopModel)
                hookValid=false;
            end

            if hookValid
                argsTopModelOnly=hook.argsTopModelOnly;
                if ~iscell(argsTopModelOnly) || ...
                        ~(length(argsTopModelOnly)==2) || ...
                        ~strcmp(argsTopModelOnly{1},'CoverageForTopModel')
                    hookValid=false;
                else
                    topModel=argsTopModelOnly{2};
                end
                if ~any(strcmp(topModel,{'on','off'}))
                    hookValid=false;
                end
            end
                
            if hookValid
                refModels=hook.includeReferencedModels;
                if ~any(strcmp(refModels,{'on','off'}))
                    hookValid=false;
                end
            end

            if ~hookValid
                DAStudio.error('CoderCoverage:AllTools:InvalidBuildHook');
            end
            
        end
    
    end
    
    
end
