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