How to: Provide Custom Context-Sensitive Commands to Your Test Type
You might want to provide your own shortcut menu commands when a test of your custom test type is selected in one of the Test windows. You might also want to override the behavior of certain existing shortcut menu commands. The steps are as follows:
Define one or more new menu commands
Update your TUIP class to implement ICommandProvider
If you know the IDs of the groups where you want to insert commands, your package can add them to any of the Visual Studio 2008 menus or toolbars. The framework just helps make sure that your command appears only if a test of your test type is selected, and helps you wire up the command with its status and command handler.
Define New Menu Commands
To define new menu commands
To provide your own shortcut menu commands, you first have to define them. Menu commands are defined by using XML Command Table (.vsct) files. For more information, see XML-Based Command Table Configuration (.vsct) Files.
You must specify the parent GUID:ID of each new command. This GUID:ID pair determines which window’s shortcut menu the command will appear in.
The command's GUID identifes your command set, the group to which the command belongs. The ID is a number that uniquely represents the command within your command set, the menu group.
Use the following constant values:
Parent command-set GUID: B85579AA-8BE0-4c4f-A850-90902B317571
Command IDs: For the shortcut menus of the Test View and Test Manager windows, use the parent command ID0x1306. For the shortcut menu of the Test Result window, use the parent command ID 0x1301.
The CommandID method is called by the framework to discover the set of commands to display when a particular test or test result is right-clicked. You should return an array of CommandIDs for your test type specific commands, for the specific shortcut menu.
Examples
1. TestView's context menu id =
New System.ComponentModel.Design.CommandID(CommandSetGuid, &H1306)
new System.ComponentModel.Design.CommandID(CommandSetGuid, 0x1306)
2. TestManager shares its context menu with TestView. Therefore, it has the same CommandID.
3. TestResult's context menu id =
New System.ComponentModel.Design.CommandID(CommandSetGuid, &H1301)
new System.ComponentModel.Design.CommandID(CommandSetGuid , 0x1301)
where CommandSetGuid =
New Guid("{B85579AA-8BE0-4c4f-A850-90902B317571}")
new Guid("{B85579AA-8BE0-4c4f-A850-90902B317571}");
Implement ICommandProvider
If you have written a TIP and you wan to provide shortcut-menu commands for the test type, you must implement the ICommandProvider interface.
To implement ICommandProvider
Your TUIP class must implement ICommandProvider to provide your test type specific commands and, if necessary, to override behavior of existing shared commands.
Public Interface ICommandProvider Function GetTypeSpecificCommands(ByVal contextMenuId As CommandID) As CommandID() Function QueryStatus(ByVal contextMenuId As CommandID, ByVal commandId As CommandID, ByVal items As IList) AsCommandStatus Function GetCommandHandler(ByVal contextMenuId As CommandID, ByVal commandId As CommandID) As CommandEventHandler End Interface
public interface ICommandProvider { CommandID[] GetTypeSpecificCommands(CommandID contextMenuId); CommandStatus QueryStatus(CommandID contextMenuId, CommandID commandId, IList items); CommandEventHandler GetCommandHandler(CommandID contextMenuId, CommandID commandId); }
The following sections provide more detail about how to implementICommandProvider:
Using QueryStatus
Function QueryStatus(ByVal contextMenuId As CommandID,ByVal commandId As CommandID ,ByVal items As IList) As CommandStatus
CommandStatus QueryStatus(CommandID contextMenuId, CommandID commandId, IList items);
This resembles the VSIP QueryStatus method. Return the status of the particular command in the shortcut menu in the form of a CommandStatus object. You can create a new CommandStatus object by calling its constructor:
Public Sub New(ByVal visible As Boolean, ByVal enabled As Boolean)
public CommandStatus(bool visible, bool enabled)
Using GetCommandHandler
Private Function GetCommandHandler(ByVal contextMenuId As CommandID, ByVal commandId As CommandID) As CommandEventHandler
CommandEventHandler GetCommandHandler(CommandID contextMenuId, CommandID commandId);
Return the command handler for the specific command in the shortcut menu. CommandEventHandler is defined as:
Public Delegate Function CommandEventHandler(ByVal sender As Object, ByVal e As CommandEventArgs) As CommandResult
public delegate CommandResult CommandEventHandler(object sender, CommandEventArgs e);
Shared Commands
To encourage consistency across test types, the framework uses shared commands. These are commands for which all test types have to provide an implementation. Currently, the only shared command is the delete command. The delete command is available in the Test View and Test Manager shortcut menus. Its purpose is to delete the selected test or tests.
A TUIP must provide an implementation for the delete command because only the test type knows how to delete a test. Therefore, your QueryStatus implementation should handle the case when these conditions exist:
contextMenuId equals the shortcut menu for either the Test View window or the Test Manager window
commandId equals that of the delete command, returning the desired CommandStatus.
Similarly, your GetCommandHandler should return your handler for the delete command in that case.
The Command Id for the delete command is shown here:
Dim TempCommandID As System.ComponentModel.Design.CommandID = New System.ComponentModel.Design.CommandID(CommandSetGuid, &H3306)
[C#] new System.ComponentModel.Design.CommandID(CommandSetGuid, 0x3306)
Example: Delete Command
The TIP writer could provide a handler for the delete command in the Test View window as follows:
'' The following IDs were included with VSTS 2005: Private Shared ReadOnly s_testToolsCmdSetGuid As Guid = New Guid("{B85579AA-8BE0-4c4f-A850-90902B317571}") Private Shared ReadOnly s_testViewContextCmdId As CommandID = New CommandID(s_testToolsCmdSetGuid, &H1306) Private Shared ReadOnly s_deleteCmdId As CommandID = New CommandID(s_testToolsCmdSetGuid, &H3306) '' <summary> '' Gets the handler for a specific command ID & Context menu. This is '' used to invoke the command. '' </summary> '' <param name="contextMenuId">Context Menu which this command is '' for</param> '' <param name="commandId">The command for which a handler is '' required</param> '' <returns>The CommandEventHandler to be invoked</returns> Public Function GetCommandHandler(ByVal contextMenuId As CommandID, ByVal commandId As CommandID) As CommandEventHandler If contextMenuId.Equals(s_testContextCmdId) Then If commandId.Equals(s_deleteCmdId) Then Return New CommandEventHandler(AddressOf OnDeleteCommand) End If End If Return Nothing End Function '' <summary> '' Delete selected tests. '' </summary> Private Function OnDeleteCommand(ByVal sender As Object, ByVal e As CommandEventArgs) As CommandResult ... End Function
// The following IDs were included with VSTS 2005: private static readonly Guid s_testToolsCmdSetGuid = new Guid("{B85579AA-8BE0-4c4f-A850-90902B317571}"); private static readonly CommandID s_testViewContextCmdId = new CommandID(s_testToolsCmdSetGuid, 0x1306); private static readonly CommandID s_deleteCmdId = new CommandID(s_testToolsCmdSetGuid, 0x3306); /// <summary> /// Gets the handler for a specific command ID & Context menu. This is /// used to invoke the command. /// </summary> /// <param name="contextMenuId">Context Menu which this command is /// for</param> /// <param name="commandId">The command for which a handler is /// required</param> /// <returns>The CommandEventHandler to be invoked</returns> public CommandEventHandler GetCommandHandler(CommandID contextMenuId, CommandID commandId) { if (contextMenuId.Equals(s_testContextCmdId)) { if (commandId.Equals(s_deleteCmdId)) { return new CommandEventHandler(OnDeleteCommand); } } return null; } /// <summary> /// Delete selected tests. /// </summary> private CommandResult OnDeleteCommand(object sender, CommandEventArgs e) { ... }
See Also
Tasks
How to: Implement a Basic Test Type