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.Reflection; |
| 9 |
| 10 using EnvDTE80; |
| 11 using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 12 |
| 13 using NativeClientVSAddIn; |
| 14 |
| 15 /// <summary> |
| 16 /// This is a test class for PluginDebuggerVSTest and is intended |
| 17 /// to contain all PluginDebuggerVS Unit Tests |
| 18 /// </summary> |
| 19 [TestClass] |
| 20 public class PluginDebuggerVSTest |
| 21 { |
| 22 /// <summary> |
| 23 /// The main visual studio object. |
| 24 /// </summary> |
| 25 private DTE2 dte_; |
| 26 |
| 27 /// <summary> |
| 28 /// Holds the default properties from property pages to use during tests. |
| 29 /// </summary> |
| 30 private MockPropertyManager properties_; |
| 31 |
| 32 /// <summary> |
| 33 /// Gets or sets the test context which provides information about, |
| 34 /// and functionality for the current test run. |
| 35 /// </summary> |
| 36 public TestContext TestContext { get; set; } |
| 37 |
| 38 /// <summary> |
| 39 /// This is run before each test to create test resources. |
| 40 /// </summary> |
| 41 [TestInitialize] |
| 42 public void TestSetup() |
| 43 { |
| 44 dte_ = TestUtilities.StartVisualStudioInstance(); |
| 45 try |
| 46 { |
| 47 TestUtilities.AssertAddinLoaded(dte_, NativeClientVSAddIn.Strings.AddInN
ame); |
| 48 } |
| 49 catch |
| 50 { |
| 51 TestUtilities.CleanUpVisualStudioInstance(dte_); |
| 52 throw; |
| 53 } |
| 54 |
| 55 // Set up mock property manager to return the desired property values. |
| 56 properties_ = new MockPropertyManager( |
| 57 PropertyManager.ProjectPlatformType.Pepper, |
| 58 delegate(string page, string name) |
| 59 { |
| 60 switch (page) |
| 61 { |
| 62 case "ConfigurationGeneral": |
| 63 switch (name) |
| 64 { |
| 65 case "VSNaClSDKRoot": return System.Environment.GetEnvironmentVa
riable( |
| 66 NativeClientVSAddIn.Strings.SDKPathEnvironmentVariable); |
| 67 } |
| 68 |
| 69 break; |
| 70 case "Property": |
| 71 switch (name) |
| 72 { |
| 73 case "ProjectDirectory": return TestContext.DeploymentDirectory; |
| 74 case "PluginAssembly": return @"fake\Assembly\String"; |
| 75 } |
| 76 |
| 77 break; |
| 78 } |
| 79 |
| 80 return null; |
| 81 }, |
| 82 null); |
| 83 } |
| 84 |
| 85 /// <summary> |
| 86 /// This is run after each test to clean up things created in TestSetup(). |
| 87 /// </summary> |
| 88 [TestCleanup] |
| 89 public void TestCleanup() |
| 90 { |
| 91 TestUtilities.CleanUpVisualStudioInstance(dte_); |
| 92 } |
| 93 |
| 94 /// <summary> |
| 95 /// A test for the constructor. |
| 96 /// </summary> |
| 97 [TestMethod] |
| 98 public void PluginDebuggerVSConstructorTest() |
| 99 { |
| 100 // Check that a null dte fails. |
| 101 try |
| 102 { |
| 103 PluginDebuggerBase nullDte = new PluginDebuggerVS(null, properties_); |
| 104 Assert.Fail("Using null DTE instance did not throw exception"); |
| 105 } |
| 106 catch (ArgumentNullException) |
| 107 { |
| 108 // This is expected for a correct implementation. |
| 109 } |
| 110 catch |
| 111 { |
| 112 Assert.Fail("Using null DTE instance threw something other than Argument
NullException"); |
| 113 } |
| 114 |
| 115 // Check that a null PropertyManager fails. |
| 116 try |
| 117 { |
| 118 PluginDebuggerBase nullDte = new PluginDebuggerVS(dte_, null); |
| 119 Assert.Fail("Using null property manager did not throw exception"); |
| 120 } |
| 121 catch (ArgumentNullException) |
| 122 { |
| 123 // This is expected for a correct implementation. |
| 124 } |
| 125 catch |
| 126 { |
| 127 Assert.Fail("Using null property manager threw something other than Argu
mentNullException"); |
| 128 } |
| 129 } |
| 130 |
| 131 /// <summary> |
| 132 /// A test for FindAndAttachToPlugin. |
| 133 /// </summary> |
| 134 [TestMethod] |
| 135 [DeploymentItem("NativeClientVSAddIn.dll")] |
| 136 public void FindAndAttachToPepperPluginTest() |
| 137 { |
| 138 MockProcessSearcher processResults = new MockProcessSearcher(); |
| 139 |
| 140 using (PluginDebuggerVS target = new PluginDebuggerVS(dte_, properties_)) |
| 141 { |
| 142 PluginDebuggerBase_Accessor targetBase = new PluginDebuggerBase_Accessor
( |
| 143 new PrivateObject(target, new PrivateType(typeof(PluginDebuggerBase)
))); |
| 144 targetBase.debuggedChromeMainProcess_ = System.Diagnostics.Process.GetCu
rrentProcess(); |
| 145 uint currentProcId = (uint)targetBase.debuggedChromeMainProcess_.Id; |
| 146 |
| 147 string pluginLoadFlag = string.Format( |
| 148 Strings.PepperProcessPluginFlagFormat, properties_.PluginAssembly); |
| 149 string pepperCommandLine = string.Concat( |
| 150 pluginLoadFlag, " ", Strings.ChromeRendererFlag); |
| 151 |
| 152 // Fake the list of processes on the system. |
| 153 processResults.ProcessList.Add( |
| 154 new ProcessInfo( |
| 155 currentProcId, |
| 156 currentProcId, |
| 157 string.Empty, |
| 158 Strings.NaClDebugFlag, |
| 159 Strings.ChromeProcessName)); |
| 160 processResults.ProcessList.Add( |
| 161 new ProcessInfo(1, currentProcId, string.Empty, string.Empty, "MyPar
entProcess")); |
| 162 processResults.ProcessList.Add( |
| 163 new ProcessInfo(10, 1, string.Empty, pepperCommandLine, Strings.Chro
meProcessName)); |
| 164 |
| 165 // This is missing some relevant command line args, and should not be at
tached to. |
| 166 processResults.ProcessList.Add( |
| 167 new ProcessInfo(12, 1, string.Empty, pluginLoadFlag, Strings.ChromeP
rocessName)); |
| 168 |
| 169 // This doesn't have this process as its parent, and should not be attac
hed to. |
| 170 processResults.ProcessList.Add( |
| 171 new ProcessInfo(14, 14, string.Empty, pepperCommandLine, Strings.Chr
omeProcessName)); |
| 172 |
| 173 // Set the private value to the mock object (can't use accessor since no
valid cast). |
| 174 FieldInfo processSearcherField = typeof(PluginDebuggerBase).GetField( |
| 175 "processSearcher_", |
| 176 BindingFlags.NonPublic | BindingFlags.Instance); |
| 177 processSearcherField.SetValue(targetBase.Target, processResults); |
| 178 |
| 179 // Test that the correct processes are attached to. |
| 180 bool goodPepper = false; |
| 181 var handler = new EventHandler<NativeClientVSAddIn.PluginDebuggerBase.Pl
uginFoundEventArgs>( |
| 182 delegate(object unused, NativeClientVSAddIn.PluginDebuggerBase.PluginF
oundEventArgs args) |
| 183 { |
| 184 switch (args.ProcessID) |
| 185 { |
| 186 case 10: |
| 187 if (goodPepper) |
| 188 { |
| 189 Assert.Fail("Should not attach twice to same pepper process"); |
| 190 } |
| 191 |
| 192 goodPepper = true; |
| 193 break; |
| 194 case 12: |
| 195 Assert.Fail("Should not attach to pepper process with bad args")
; |
| 196 break; |
| 197 case 14: |
| 198 Assert.Fail("Should not attach to pepper process that is not " |
| 199 + "descendant of Visual Studio"); |
| 200 break; |
| 201 default: |
| 202 Assert.Fail("Should not attach to non-pepper/non-nacl process"); |
| 203 break; |
| 204 } |
| 205 }); |
| 206 |
| 207 target.PluginFoundEvent += handler; |
| 208 target.FindAndAttachToPlugin(null, null); |
| 209 target.PluginFoundEvent -= handler; |
| 210 |
| 211 Assert.IsTrue(goodPepper, "Failed to attach to pepper process"); |
| 212 } |
| 213 } |
| 214 |
| 215 /// <summary> |
| 216 /// Checks that VS properly attaches debugger. |
| 217 /// </summary> |
| 218 [TestMethod] |
| 219 [DeploymentItem("NativeClientVSAddIn.dll")] |
| 220 public void AttachVSDebuggerTest() |
| 221 { |
| 222 using (System.Diagnostics.Process dummyProc = TestUtilities.StartProcessFo
rKilling( |
| 223 "DummyProc", 20)) |
| 224 { |
| 225 try |
| 226 { |
| 227 PluginDebuggerVS_Accessor target = new PluginDebuggerVS_Accessor(dte_,
properties_); |
| 228 |
| 229 var pluginFoundArgs = new NativeClientVSAddIn.PluginDebuggerBase.Plugi
nFoundEventArgs( |
| 230 (uint)dummyProc.Id); |
| 231 target.Attach(null, pluginFoundArgs); |
| 232 |
| 233 bool isBeingDebugged = false; |
| 234 foreach (EnvDTE.Process proc in dte_.Debugger.DebuggedProcesses) |
| 235 { |
| 236 if (proc.ProcessID == dummyProc.Id) |
| 237 { |
| 238 isBeingDebugged = true; |
| 239 } |
| 240 } |
| 241 |
| 242 Assert.IsTrue(isBeingDebugged, "Visual Studio debugger did not attach"
); |
| 243 } |
| 244 finally |
| 245 { |
| 246 if (dummyProc != null && !dummyProc.HasExited) |
| 247 { |
| 248 dummyProc.Kill(); |
| 249 dummyProc.Dispose(); |
| 250 } |
| 251 } |
| 252 } |
| 253 } |
| 254 |
| 255 /// <summary> |
| 256 /// A test for IsPluginProcess. |
| 257 /// </summary> |
| 258 [TestMethod] |
| 259 [DeploymentItem("NativeClientVSAddIn.dll")] |
| 260 public void IsPepperPluginProcessTest() |
| 261 { |
| 262 PluginDebuggerVS_Accessor target = new PluginDebuggerVS_Accessor(dte_, pro
perties_); |
| 263 |
| 264 string identifierFlagTarget = |
| 265 string.Format(Strings.PepperProcessPluginFlagFormat, target.pluginAsse
mbly_); |
| 266 string goodFlags = string.Concat(Strings.ChromeRendererFlag, ' ', identifi
erFlagTarget); |
| 267 |
| 268 ProcessInfo badProc1 = new ProcessInfo( |
| 269 1, 1, string.Empty, goodFlags, Strings.NaClProcessName); |
| 270 ProcessInfo badProc2 = new ProcessInfo( |
| 271 1, 1, string.Empty, Strings.NaClLoaderFlag, Strings.ChromeProcessName)
; |
| 272 ProcessInfo badProc3 = new ProcessInfo( |
| 273 1, 1, string.Empty, Strings.ChromeRendererFlag, Strings.ChromeProcessN
ame); |
| 274 ProcessInfo badProc4 = new ProcessInfo( |
| 275 1, 1, string.Empty, identifierFlagTarget, Strings.ChromeProcessName); |
| 276 ProcessInfo goodProc = new ProcessInfo( |
| 277 1, 1, string.Empty, goodFlags, Strings.ChromeProcessName); |
| 278 |
| 279 Assert.IsTrue(target.IsPluginProcess(goodProc, string.Empty)); |
| 280 Assert.IsFalse(target.IsPluginProcess(badProc1, string.Empty)); |
| 281 Assert.IsFalse(target.IsPluginProcess(badProc2, string.Empty)); |
| 282 Assert.IsFalse(target.IsPluginProcess(badProc3, string.Empty)); |
| 283 Assert.IsFalse(target.IsPluginProcess(badProc4, string.Empty)); |
| 284 } |
| 285 } |
| 286 } |
OLD | NEW |