Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 namespace UnitTests | |
| 6 { | |
| 7 using System; | |
| 8 using System.Collections.Generic; | |
| 9 using System.Linq; | |
| 10 using System.Management; | |
| 11 | |
| 12 using EnvDTE; | |
| 13 using EnvDTE80; | |
| 14 | |
| 15 /// <summary> | |
| 16 /// This class contains utilities for running tests. | |
| 17 /// </summary> | |
| 18 public static class TestUtilities | |
| 19 { | |
| 20 /// <summary> | |
| 21 /// This starts an instance of Visual Studio and get it's DTE object. | |
|
binji
2012/07/20 00:24:53
s/it's/its/
tysand
2012/08/07 23:01:54
Done.
| |
| 22 /// </summary> | |
| 23 /// <returns>DTE of the started instance.</returns> | |
| 24 public static DTE2 StartVisualStudioInstance() | |
| 25 { | |
| 26 // Set up filter to handle threading events and automatically retry calls | |
| 27 // to dte which fail because dte is busy. | |
| 28 ComMessageFilter.Register(); | |
| 29 | |
| 30 Type visualStudioType = Type.GetTypeFromProgID("VisualStudio.DTE.10.0"); | |
| 31 DTE2 visualStudio = Activator.CreateInstance(visualStudioType) as DTE2; | |
| 32 if (visualStudio == null) | |
| 33 { | |
| 34 throw new Exception("Visual Studio failed to start"); | |
| 35 } | |
| 36 | |
| 37 visualStudio.MainWindow.Visible = true; | |
| 38 return visualStudio; | |
| 39 } | |
| 40 | |
| 41 /// <summary> | |
| 42 /// This properly cleans up after StartVisualStudioInstance(). | |
| 43 /// </summary> | |
| 44 /// <param name="dte">Dte instance returned by StartVisualStudioInstance().< /param> | |
| 45 public static void CleanUpVisualStudioInstance(DTE2 dte) | |
| 46 { | |
| 47 if (dte != null) | |
| 48 { | |
| 49 if (dte.Solution != null) | |
| 50 { | |
| 51 dte.Solution.Close(); | |
| 52 } | |
| 53 | |
| 54 dte.Quit(); | |
| 55 } | |
| 56 | |
| 57 // Stop the message filter. | |
| 58 ComMessageFilter.Revoke(); | |
| 59 } | |
| 60 | |
| 61 /// <summary> | |
| 62 /// This returns the text contained in the given output window pane. | |
| 63 /// </summary> | |
| 64 /// <param name="pane">Pane to get text from.</param> | |
| 65 /// <returns>Text in the window.</returns> | |
| 66 public static string GetPaneText(OutputWindowPane pane) | |
| 67 { | |
| 68 TextSelection selection = pane.TextDocument.Selection; | |
| 69 selection.StartOfDocument(false); | |
| 70 selection.EndOfDocument(true); | |
| 71 return selection.Text; | |
| 72 } | |
| 73 | |
| 74 /// <summary> | |
| 75 /// This starts a python process that just sleeps waiting to be killed. | |
| 76 /// It can be used with DoesProcessExist() to verify that a process started/ exited. | |
| 77 /// </summary> | |
| 78 /// <param name="identifierString"> | |
| 79 /// A unique string to identify the process via its command line arguments. | |
| 80 /// </param> | |
| 81 /// <param name="timeout">Time in seconds to wait before process exits on it s own.</param> | |
| 82 /// <returns>The process object that was started.</returns> | |
| 83 public static System.Diagnostics.Process StartProcessForKilling( | |
| 84 string identifierString, int timeout) | |
| 85 { | |
| 86 string args = string.Format( | |
| 87 "-c \"import time; time.sleep({0}); print '{1}'\"", | |
| 88 timeout, | |
| 89 identifierString); | |
| 90 System.Diagnostics.Process proc = new System.Diagnostics.Process(); | |
| 91 proc.StartInfo.CreateNoWindow = true; | |
| 92 proc.StartInfo.UseShellExecute = false; | |
| 93 proc.StartInfo.FileName = "python.exe"; | |
| 94 proc.StartInfo.Arguments = args; | |
| 95 proc.Start(); | |
| 96 return proc; | |
| 97 } | |
| 98 | |
| 99 /// <summary> | |
| 100 /// This returns true if there is a running process that has command line ar guments | |
| 101 /// containing the given Strings. The search is case-insensitive. | |
| 102 /// </summary> | |
| 103 /// <param name="processName">Name of the process executable.</param> | |
| 104 /// <param name="commandLineIdentifiers">Strings to check for.</param> | |
| 105 /// <returns>True if some process has the Strings in its command line argume nts.</returns> | |
| 106 public static bool DoesProcessExist(string processName, params string[] comm andLineIdentifiers) | |
| 107 { | |
| 108 List<string> results = new List<string>(); | |
| 109 string query = | |
| 110 string.Format("select CommandLine from Win32_Process where Name='{0}'" , processName); | |
| 111 using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(qu ery)) | |
| 112 { | |
| 113 using (ManagementObjectCollection result = searcher.Get()) | |
| 114 { | |
| 115 foreach (ManagementObject process in result) | |
| 116 { | |
| 117 string commandLine = process["CommandLine"] as string; | |
| 118 if (string.IsNullOrEmpty(commandLine)) | |
| 119 { | |
| 120 break; | |
| 121 } | |
| 122 | |
| 123 // Check if the command line contains each of the required identifie rs. | |
| 124 if (commandLineIdentifiers.All(i => commandLine.Contains(i))) | |
| 125 { | |
| 126 return true; | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 return false; | |
| 133 } | |
| 134 | |
| 135 /// <summary> | |
| 136 /// Sets the active configuration for the solution by specifying the configu ration name | |
| 137 /// and platform name. A solution configuration containing a project configu ration that has | |
| 138 /// the config and platform names specified for the specified project is sel ected. | |
| 139 /// </summary> | |
| 140 /// <param name="dte">The main visual studio object.</param> | |
| 141 /// <param name="projectUniqueName">UniqueName of the project to match.</par am> | |
| 142 /// <param name="configurationName">Ex: "Debug" or "Release".</param> | |
| 143 /// <param name="platformName">Ex: "Win32" or "NaCl" or "PPAPI".</param> | |
| 144 public static void SetSolutionConfiguration( | |
| 145 DTE2 dte, | |
| 146 string projectUniqueName, | |
| 147 string configurationName, | |
| 148 string platformName) | |
| 149 { | |
| 150 foreach (EnvDTE.SolutionConfiguration config in | |
| 151 dte.Solution.SolutionBuild.SolutionConfigurations) | |
| 152 { | |
| 153 EnvDTE.SolutionContext context = null; | |
| 154 try | |
| 155 { | |
| 156 context = config.SolutionContexts.Item(projectUniqueName); | |
| 157 } | |
| 158 catch (ArgumentException) | |
| 159 { | |
| 160 throw new Exception( | |
| 161 string.Format("Project unique name not found in solution: {0}", pr ojectUniqueName)); | |
| 162 } | |
| 163 | |
| 164 if (context == null) | |
| 165 { | |
| 166 throw new Exception("Failed to get solution context"); | |
| 167 } | |
| 168 | |
| 169 if (context.PlatformName == platformName && context.ConfigurationName == configurationName) | |
| 170 { | |
| 171 config.Activate(); | |
| 172 return; | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 throw new Exception(string.Format( | |
| 177 "Matching configuration not found for {0}: {1}|{2}", | |
| 178 projectUniqueName, | |
| 179 platformName, | |
| 180 configurationName)); | |
| 181 } | |
| 182 | |
| 183 /// <summary> | |
| 184 /// Extends the string class to allow checking if a string contains another string | |
| 185 /// allowing a comparison type (such as case-insensitivity). | |
| 186 /// </summary> | |
| 187 /// <param name="source">Base string to search.</param> | |
| 188 /// <param name="toCheck">String to check if contained within base string.</ param> | |
| 189 /// <param name="comparison">Comparison type.</param> | |
| 190 /// <returns>True if toCheck is contained in source.</returns> | |
| 191 public static bool Contains(this string source, string toCheck, StringCompar ison comparison) | |
| 192 { | |
| 193 return source.IndexOf(toCheck, comparison) != -1; | |
| 194 } | |
| 195 } | |
| 196 } | |
| OLD | NEW |