Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

How to: Create a Custom Editor for Data for Your Diagnostic Data Adapter

When you create a diagnostic data adapter, you might want to enable the user to configure specific data when your custom diagnostic data adapter is selected for their test settings. For example, you can select the configuration data that specifies which registry keys to extract, what level of network load to simulate, or in which directory to find temporary files or work files to attach.

You must use a configuration file to set up initial values for your diagnostic data adapter. You can provide a custom editor to enable the user to modify the configuration data.

To create your own editor, you will create a user control that implements IDataCollectorConfigurationEditor.

Your diagnostic data adapter can use a DataCollectorConfigurationEditorAttribute to specify the editor class to use for editing diagnostic data configuration settings.

You also specify the default configuration data that you want to use. See Sample Project for Creating a Diagnostic Data Adapter for sample default configuration.

Use the following procedure to create a custom editor to update data for your test settings when your custom data diagnostic adapter is used.

NoteNote

To create a custom editor, you must first create your diagnostic data adapter that has the DataCollectorConfigurationEditorAttribute applied to the class. You can use the optional HelpUri property in that attribute to specify the help content source for your editor. For more information about how to create your diagnostic data adapter, see How to: Create a Diagnostic Data Adapter.

For a complete example diagnostic data adapter project, including a custom configuration editor, see Sample Project for Creating a Diagnostic Data Adapter.

To create a custom editor for your diagnostic data adapter

  1. Create a user control in the project for your data diagnostic adapter:

    1. Right-click the code project that contains your diagnostic data adapter class, point to Add and then point to User Control.

    2. For this example, add a label to the form with this text: Data File Name: and a text box named FileTextBox that will enable the user to enter the necessary data.

    NoteNote

    Only Windows Forms user controls are currently supported.

  2. Add these lines to the declaration section:

    using System.Xml;
    using Microsoft.VisualStudio.TestTools.Common;
    using Microsoft.VisualStudio.TestTools.Execution;
    
  3. Make this user control into a custom editor.

    1. Right-click the user control in your code project and point to View code.

    2. Set the class to implement the editor interface IDataCollectorConfigurationEditor as follows:

       public partial class MyDataConfigEditor :
            UserControl, IDataCollectorConfigurationEditor
    
    1. Right-click IDataCollectorConfigurationEditor in the code, and select the Implement Interface command. The methods that you must implement for this interface are added to your class.

    2. Add the DataCollectorConfigurationEditorAttribute to your user control for your editor to identify it as a diagnostic data adapter editor, replacing Company, Product, and Version with the appropriate information for your diagnostic data adapter:

      [DataCollectorConfigurationEditorTypeUri(
          "configurationeditor://MyCompany/MyConfigEditor/1.0")]
      
  4. Add two private variables as follows:

    private DataCollectorSettings collectorSettings;
    private IServiceProvider ServiceProvider { get; set; }
    
  5. Add code to initialize your editor for your diagnostic data adapter. You can add default values to the fields in your user control by using data that is in the settings variable. This is the data that is in the <DefaultConfiguration> element in the XML configuration file for your adapter.

    public void Initialize(
        IServiceProvider svcProvider,
        DataCollectorSettings settings)
    {
        ServiceProvider = svcProvider;
        collectorSettings = settings;
    
        // Display the default file name as listed in the settings file.
        this.SuspendLayout();
        this.FileTextBox.Text = getText(collectorSettings.Configuration);
        this.ResumeLayout();
    }
    
  6. Add code to save the data from your controls in your editor back into the XML format required by the diagnostic data adapter API as follows:

    public DataCollectorSettings SaveData()
    {
        collectorSettings.Configuration.InnerXml =
            String.Format(
    @"<MyCollectorName
        xmlns=""http://MyCompany/schemas/MyDiagnosticDataCollector/1.0"">
      <File FullPath=""{0}"" />
    </MyCollectorName>",
        FileTextBox.Text);
        return collectorSettings;
    }
    
  7. If it is important to you, add code to verify the data is correct in the VerifyData method, or you can have the method return true.

    public bool VerifyData()
    {
        // Not currently verifying data
        return true;
    }
    
  8. (Optional) You can add code to reset the data to the initial settings that are provided in the XML configuration file in the ResetToAgentDefaults() method which uses the private getText() method.

    // Reset to default value from XML configuration
    // using a custom getText() method
    public void ResetToAgentDefaults()
    {
        this.FileTextBox.Text = getText(collectorSettings.DefaultConfiguration);
    }
    
    // Local method to read the configuration settings
    private string getText(XmlElement element)
    {
        // Setup namespace manager with our namespace
        XmlNamespaceManager nsmgr =
            new XmlNamespaceManager(
                element.OwnerDocument.NameTable);
    
        // Find all the "File" elements under our configuration
        XmlNodeList files = element.SelectNodes("//ns:MyCollectorName/ns:File", nsmgr);
    
        string result = String.Empty;
        if (files.Count > 0)
        {
            XmlAttribute pathAttribute = files[0].Attributes["FullPath"];
            if (pathAttribute != null &&
                !String.IsNullOrEmpty(pathAttribute.Value))
            {
                result = pathAttribute.Value;
            }
        }
    
        return result;
    }
    
  9. Build your solution. Copy the data diagnostic adapter assembly and the XML configuration file (<diagnostic data adapter name>.dll.config) to the following location based on your installation directory: Program Files\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\DataCollectors.

    NoteNote

    Although the configuration editor can be in a project and an assembly that is different from the diagnostic data adapter, they can also be in the same assembly.

  10. To use your diagnostic data adapter in testing, you must select from the list of existing test settings or create a new one from Microsoft Test Manager or Microsoft Visual Studio 2010 and then select your diagnostic data adapter.

    The adapter is displayed on the Data and Diagnostics tab of your test settings with the friendly name that you assigned to the class.

  11. To configure your diagnostic data adapter for your test settings, click Configure next to the adapter name.

    Your custom editor is now displayed.

  12. Edit the fields in your custom editor as required, and then click Save.

  13. If you are running your tests from Microsoft Test Manager, you can assign these test settings to your test plan before you run your tests, or use the Run with Options command to assign test settings and override test settings. For more information about test settings, see Setting Up Machines and Collecting Diagnostic Information Using Test Settings.

  14. Before you can use your new configuration editor with a diagnostic data adapter, you must apply the DataCollectorConfigurationEditorAttribute to each diagnostic data adapter class that you want to use the editor, and recompile and reinstall them on the client computer. For more information about how to install diagnostic data adapters and configuration editors, see How to: Install a Custom Diagnostic Data Adapter.

  15. Run your tests using the test settings with your diagnostic data adapter selected.

    The data file that you specified in your editor is attached to your test results.

For more information about how to configure your test settings to use an environment when you run your tests, see Create Test Settings for Manual Tests or Create Test Settings for Automated Tests as Part of a Test Plan.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.