A
download MyHostAdapterRunConfigControl.cs
Language: C#
License: MSVSSDK
LOC: 188
Project Info
C# Example.MyTestTypeSample
Server: Visual Studio SDK
Type: filesystem
...xtensibility\MyTest\MyTest\
   assembly.cs
   MyHostAdapter.cs
   ...pterRunConfigControl.cs
   ...nfigControl.Designer.cs
   ...erRunConfigControl.resx
   MyTest.cs
   MyTest.csproj
   MyTestAdapter.cs
   MyTestAssertHelper.cs
   MyTestEditorControl.cs
   ...itorControl.Designer.cs
   MyTestEditorControl.resx
   MyTestEditorFactory.cs
   MyTestPackage.cs
   MyTestResourceIds.cs
   MyTestResult.cs
   MyTestResultViewControl.cs
   ...ViewControl.designer.cs
   ...tResultViewControl.resx
   MyTestResultViewWindow.cs
   MyTestRunConfigControl.cs
   ...nfigControl.Designer.cs
   ...stRunConfigControl.resx
   MyTestTip.cs
   MyTestTuip.cs
   MyTestWizard.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/***************************************************************************
 
Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualStudio.TestTools.Common;
using Microsoft.VisualStudio.TestTools.Vsip;

namespace Microsoft.VisualStudio.TestTools.Samples
{
    /// <summary>
    /// UI control for my host adapter configuartion. Hosted inside test run config editor.
    /// It contains a data grid view where you could define environment variables.
    /// </summary>
    public partial class MyHostAdapterRunConfigControl : UserControl, IRunConfigurationCustomHostEditor
    {
        #region Private Data

        private MyHostRunConfigData m_config = new MyHostRunConfigData();
        private bool m_isLoading = false;

        #endregion

        #region Constructor

        public MyHostAdapterRunConfigControl()
        {
            InitializeComponent();
            m_envVarDataGridView.CellValueChanged += new DataGridViewCellEventHandler(DataGridView_CellValueChanged);
            m_envVarDataGridView.RowsRemoved += new DataGridViewRowsRemovedEventHandler(DataGridView_RowsRemoved);
            m_envVarDataGridView.RowValidating += new DataGridViewCellCancelEventHandler(DataGridView_RowValidating);
            m_envVarDataGridView.RowValidated += new DataGridViewCellEventHandler(DataGridView_RowValidated);
        }

        #endregion

        #region Event Handlers

        void DataGridView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            UpdateConfig();
        }

        void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            UpdateConfig();
        }


        /// <summary>
        /// Validate the row. A row is invalid if there is an empty variable name, or duplicate variable names.
        /// </summary>
        void DataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            // Save existing names to find out duplicates
            StringDictionary names = new StringDictionary();
            e.Cancel = false;
            foreach (DataGridViewRow row in m_envVarDataGridView.Rows)
            {
                if (IsEmptyCell(row.Cells[0]) && !IsEmptyCell(row.Cells[1]))
                {
                    e.Cancel = true;
                    row.ErrorText = "Empty name.";
                    return;
                }
                else if (!IsEmptyCell(row.Cells[0]))
                {
                    string name = row.Cells[0].Value.ToString();
                    if (names.ContainsKey(name))
                    {
                        e.Cancel = true;
                        row.ErrorText = "Duplicate name.";
                        return;
                    }
                    else
                    {
                        names.Add(name, string.Empty);
                    }
                }
            }
        }


        /// <summary>
        /// Clear the error text after validation is done.
        /// </summary>
        void DataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            // Clear the error text
            m_envVarDataGridView.Rows[e.RowIndex].ErrorText = null;
        }

        #endregion

        #region Private Methods


        /// <summary>
        /// Help method. A cell is empty if either the value is null, or an emtpy string.
        /// </summary>
        private bool IsEmptyCell(DataGridViewCell cell)
        {
            return cell.Value == null || string.IsNullOrEmpty(cell.Value.ToString());
        }


        /// <summary>
        /// Update the config object based on user's input.
        /// </summary>
        private void UpdateConfig()
        {
            if (!m_isLoading)
            {
                // Notify the editor that data get modified.
                if (null != DataGetDirty)
                {
                    DataGetDirty(this, EventArgs.Empty);
                }
                m_config.EnvironmentVariables.Clear();
                foreach (DataGridViewRow row in m_envVarDataGridView.Rows)
                {
                    // If the cell is not empty, and not duplicate, we add it to the dictionary
                    if (!IsEmptyCell(row.Cells[0]) && !m_config.EnvironmentVariables.ContainsKey(row.Cells[0].Value.ToString()))
                    {
                        m_config.EnvironmentVariables.Add(row.Cells[0].Value.ToString(),
                            (null == row.Cells[1].Value) ? string.Empty : row.Cells[1].Value.ToString());
                    }
                }
            }
        }

        #endregion

        #region IRunConfigurationEditor

        /// <summary>
        /// Initialize the editor to a default state based on given test run.
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="run">Obselete. Always null.</param>
        void IRunConfigurationEditor.Initialize(System.IServiceProvider serviceProvider, TestRun run)
        {
            // Nothing to do here.
        }

        /// <summary>
        /// Fire this event when data are modified in this editor.
        /// </summary>
        public event EventHandler DataGetDirty;


        /// <summary>
        /// Handle the event that core (non-host and not-test-specific) run config data are modified outside this editor.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="dirtyEventArgs">contains run config object that is changed outside</param>
        void IRunConfigurationEditor.OnCommonDataDirty(object sender, CommonRunConfigurationDirtyEventArgs dirtyEventArgs)
        {
            // My test config does not depend on other data contained in the run config.
        }


        /// <summary>
        /// Desciption about this editor is displayed in the help panel of main run config editor.
        /// </summary>
        string IRunConfigurationEditor.Description
        {
            get
            {
                return "This host adapter only supports unit test and my test. It sets environment variables that affect your unit test execution. It also affects how my test decides the test result. A \"my test\" passes when the process exit code is equal to value of environment variable PassedCode.";
            }
        }


        /// <summary>
        /// The keyword that is hooked up with the help topic.
        /// </summary>
        string IRunConfigurationEditor.HelpKeyword
        {
            get
            {
                return null;
            }
        }


        /// Verify the data in the editor. Prompt the user when neccessary.
        /// </summary>
        /// <returns>true if the data are correct and don't need correction; otherwise, false.</returns>
        bool IRunConfigurationEditor.VerifyData()
        {
            // DataGridView already does validation.
            return true;
        }


        #endregion

        #region IRunConfigurationCustomHostEditor


        /// <summary>
        /// The host adapter type that this editor is used for.
        /// </summary>
        string IRunConfigurationCustomHostEditor.HostType
        {
            get
            {
                return MyHostAdapter.Name;
            }
        }


        /// <summary>
        /// Called by the main editor to load the data into UI.
        /// </summary>
        /// <param name="data">host specific data</param>
        void IRunConfigurationCustomHostEditor.SetData(IHostSpecificRunConfigurationData data)
        {
            // We need to turn off the event handlers when loading data into UI.
            m_isLoading = true;
            try
            {
                m_config = (MyHostRunConfigData)data;
                m_envVarDataGridView.Rows.Clear();
                foreach (string name in m_config.EnvironmentVariables.Keys)
                {
                    DataGridViewRow row = new DataGridViewRow();
                    DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
                    cell1.Value = name;
                    DataGridViewTextBoxCell cell2 = new DataGridViewTextBoxCell();
                    cell2.Value = m_config.EnvironmentVariables[name];
                    row.Cells.Add(cell1);
                    row.Cells.Add(cell2);
                    m_envVarDataGridView.Rows.Add(row);
                }
            }
            finally
            {
                m_isLoading = false;
            }
        }


        /// <summary>
        /// Main editor is asking for the current host specific data.
        /// </summary>
        /// <returns></returns>
        IHostSpecificRunConfigurationData IRunConfigurationCustomHostEditor.GetData()
        {
            return m_config;
        }

        #endregion
    }


    /// <summary>
    /// Test run level configuration information of my host adapter.
    /// The information is accessible to my host adapter.
    /// Must be Serializable.
    /// </summary>
    [Serializable]
    class MyHostRunConfigData : IHostSpecificRunConfigurationData
    {
        #region Private Data

        private StringDictionary m_envVarDictionary = new StringDictionary();

        #endregion

        #region Properties

        /// <summary>
        /// Environment variables that will be set by the host adapter.
        /// </summary>
        public StringDictionary EnvironmentVariables
        {
            get
            {
                return m_envVarDictionary;
            }
        }

        #endregion

        #region ICloneable

        object ICloneable.Clone()
        {
            MyHostRunConfigData copy = new MyHostRunConfigData();
            copy.EnvironmentVariables.Clear();
            foreach (string name in this.EnvironmentVariables.Keys)
            {
                copy.EnvironmentVariables.Add(name, this.EnvironmentVariables[name]);
            }
            return copy;
        }

        #endregion
    }
}

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us