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..c3d1428ef305dbce95987d6b6547385b11a8e4fb |
| --- /dev/null |
| +++ b/visual_studio/NativeClientVSAddIn/UnitTests/TestUtilities.cs |
| @@ -0,0 +1,178 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +using System; |
| +using System.Collections.Generic; |
| +using System.Linq; |
| +using System.Management; |
| +using System.Text; |
| + |
| +using EnvDTE; |
|
elijahtaylor1
2012/07/11 20:56:18
ABC order
tysand
2012/07/12 23:56:15
Done.
|
| +using EnvDTE80; |
| + |
| +namespace UnitTests |
| +{ |
| + public static 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; |
| + if (vs == null) |
| + { |
| + throw new Exception("Visual Studio failed to start"); |
| + } |
| + 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) |
| + { |
| + if (dte != null) |
| + { |
| + 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 Strings in its command line arguments</returns> |
| + public static bool DoesProcessExist(String processName, params String[] commandLineIdentifiers) |
| + { |
| + 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()) |
| + { |
| + 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 |
| + if (commandLineIdentifiers.All(i => commandLine.Contains(i))) |
| + { |
| + return true; |
| + } |
| + } |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + /// <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 == null) |
| + { |
| + throw new Exception("Failed to get solution context"); |
| + } |
| + |
| + if (context.PlatformName == platformName && context.ConfigurationName == configurationName) |
| + { |
| + config.Activate(); |
| + return; |
| + } |
| + } |
| + throw new Exception(String.Format("Matching configuration not found for {0}: {1}|{2}", |
| + projectUniqueName, platformName, configurationName)); |
| + } |
| + |
| + /// <summary> |
| + /// Extends the String class to allow checking if a string contains another string |
| + /// with case-insensitivity |
| + /// </summary> |
| + /// <param name="source">Base string</param> |
| + /// <param name="toCheck">String to check if contained within base string</param> |
| + /// <param name="comparison">Comparison type</param> |
| + /// <returns>True if toCheck is contained in source</returns> |
| + public static bool Contains(this String source, String toCheck, StringComparison comparison) |
| + { |
| + return source.IndexOf(toCheck, comparison) != -1; |
| + } |
| + } |
| +} |