Chromium Code Reviews| Index: visual_studio/NativeClientVSAddIn/UnitTests/TestUtilities.cs |
| diff --git a/visual_studio/NativeClientVSAddIn/UnitTests/TestUtilities.cs b/visual_studio/NativeClientVSAddIn/UnitTests/TestUtilities.cs |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4c617c3da6a8262d5299b60ed73cd1ef23b8c00a |
| --- /dev/null |
| +++ b/visual_studio/NativeClientVSAddIn/UnitTests/TestUtilities.cs |
| @@ -0,0 +1,151 @@ |
| +using System; |
| +using System.Collections.Generic; |
| +using System.Linq; |
| +using System.Management; |
| +using System.Text; |
| + |
| +using EnvDTE; |
| +using EnvDTE80; |
| + |
| +namespace UnitTests |
| +{ |
| + class TestUtilities |
| + { |
| + /// <summary> |
| + /// Utility to start an instance of Visual Studio and get it's DTE |
| + /// </summary> |
| + /// <returns>DTE of the started instance</returns> |
| + public static DTE2 StartVisualStudioInstance() |
| + { |
| + // Set up filter to handle threading events and automatically retry calls |
| + // to dte which fail because dte is busy |
| + ComMessageFilter.Register(); |
| + |
| + Type vsType = Type.GetTypeFromProgID("VisualStudio.DTE.10.0"); |
| + DTE2 vs = Activator.CreateInstance(vsType) as DTE2; |
|
Petr Hosek
2012/07/10 05:37:21
You should check for vs being null here.
tysand
2012/07/11 05:23:46
Done.
|
| + vs.MainWindow.Visible = true; |
| + return vs; |
| + } |
| + |
| + |
| + /// <summary> |
| + /// Properly cleans up after StartVisualStudioInstance() |
| + /// </summary> |
| + /// <param name="dte">Dte instance returned by StartVisualStudioInstance</param> |
| + public static void CleanUpVisualStudioInstance(DTE2 dte) |
| + { |
| + dte.Quit(); |
| + |
| + // Stop the message filter |
| + ComMessageFilter.Revoke(); |
| + } |
| + |
| + /// <summary> |
| + /// Returns the text contained in the output window pane |
| + /// </summary> |
| + /// <param name="pane">Pane to get text from</param> |
| + /// <returns>Text in the window</returns> |
| + public static string GetPaneText(OutputWindowPane pane) |
| + { |
| + TextSelection selection = pane.TextDocument.Selection; |
| + selection.StartOfDocument(false); |
| + selection.EndOfDocument(true); |
| + return selection.Text; |
| + } |
| + |
| + /// <summary> |
| + /// Starts a python process that just sleeps waiting to be killed |
| + /// Can be used with DoesProcessExist to verify that a process started/exited |
| + /// </summary> |
| + /// <param name="identifierString">A unique string to identify the process via command line</param> |
| + /// <param name="timeout">Time in seconds to wait before process exits on its own</param> |
| + /// <returns>The process object that was started</returns> |
| + public static System.Diagnostics.Process StartProcessForKilling(String identifierString, int timeout) |
| + { |
| + string args = String.Format( |
| + "-c \"import time; time.sleep({0}); print '{1}'\"", |
| + timeout, identifierString); |
| + System.Diagnostics.Process proc = new System.Diagnostics.Process(); |
| + proc.StartInfo.CreateNoWindow = true; |
| + proc.StartInfo.UseShellExecute = false; |
| + proc.StartInfo.FileName = "python.exe"; |
| + proc.StartInfo.Arguments = args; |
| + proc.Start(); |
| + return proc; |
| + } |
| + |
| + /// <summary> |
| + /// Returns true if there is a running process that has command line arguments |
| + /// containing the given strings. Search is case-insensitive |
| + /// </summary> |
| + /// <param name="commandLineIdentifiers">Strings to check for</param> |
| + /// <param name="processName">Name of the process executable</param> |
| + /// <returns>True if some process has the string in its command line arguments</returns> |
| + public static bool DoesProcessExist(String[] commandLineIdentifiers, String processName) |
|
Petr Hosek
2012/07/10 05:37:21
You could possible switch the order of arguments a
tysand
2012/07/11 05:23:46
Done.
|
| + { |
| + List<string> results = new List<string>(); |
| + string query = String.Format("select CommandLine from Win32_Process where Name='{0}'", processName); |
| + using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query)) |
| + { |
| + using (ManagementObjectCollection result = searcher.Get()) |
|
Petr Hosek
2012/07/10 05:37:21
You can combine the two using statements together.
tysand
2012/07/11 05:23:46
Can't since they are of different types.
On 2012/0
|
| + { |
| + foreach (ManagementObject process in result) |
| + { |
| + string commandLine = process["CommandLine"] as string; |
| + if (String.IsNullOrEmpty(commandLine)) |
| + break; |
| + |
| + // Check if the command line contains each of the required identifiers |
| + bool found = true; |
| + foreach (String identifier in commandLineIdentifiers) |
| + if (commandLine.IndexOf(identifier, StringComparison.InvariantCultureIgnoreCase) == -1) |
| + found = false; |
| + if (found) |
| + return true; |
|
Petr Hosek
2012/07/10 05:37:21
You can use the LINQ here `return commandLineIdent
tysand
2012/07/11 05:23:46
Made this return true if All identifiers were foun
|
| + } |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + /// <summary> |
| + /// Overload taking a string instead of string array |
| + /// </summary> |
| + public static bool DoesProcessExist(String commandLineIdentifier, String processName) |
| + { |
| + String[] arr = { commandLineIdentifier }; |
| + return DoesProcessExist(arr, processName); |
| + } |
| + |
| + /// <summary> |
| + /// Sets the active configuration for the solution by specifying the configuration name |
| + /// and platform name. A solution configuration containing a project configuration for |
| + /// the specified project which has the config and platform names specified is selected. |
| + /// </summary> |
| + /// <param name="configurationName">Ex: "Debug" or "Release"</param> |
| + /// <param name="platformName">Ex: "Win32" or "NaCl" or "PPAPI"</param> |
| + public static void SetSolutionConfiguration(DTE2 dte, String projectUniqueName, String configurationName, String platformName) |
| + { |
| + foreach (EnvDTE.SolutionConfiguration config in dte.Solution.SolutionBuild.SolutionConfigurations) |
| + { |
| + EnvDTE.SolutionContext context = null; |
| + try |
| + { |
| + context = config.SolutionContexts.Item(projectUniqueName); |
| + } |
| + catch (ArgumentException) |
| + { |
| + throw new Exception(String.Format("Project unique name not found in solution: {0}", projectUniqueName)); |
| + } |
| + |
| + if (context.PlatformName == platformName && context.ConfigurationName == configurationName) |
|
Petr Hosek
2012/07/10 05:37:21
You should probably check here that the context is
tysand
2012/07/11 05:23:46
Done.
|
| + { |
| + config.Activate(); |
| + return; |
| + } |
| + } |
| + throw new Exception(String.Format("Matching configuration not found for {0}: {1}|{2}", |
| + projectUniqueName, platformName, configurationName)); |
| + } |
| + } |
| +} |