OLD | NEW |
| (Empty) |
1 using System; | |
2 using System.IO; | |
3 using System.Text; | |
4 using System.Reflection; | |
5 using System.Diagnostics; | |
6 using System.Collections; | |
7 using System.Collections.Generic; | |
8 using System.ComponentModel.Design; | |
9 using System.Runtime.InteropServices; | |
10 using Microsoft.VisualStudio.Shell.Interop; | |
11 using Microsoft.VisualStudio.Shell; | |
12 using EnvDTE; | |
13 using EnvDTE80; | |
14 using Microsoft.Win32; | |
15 using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider; | |
16 using Microsoft.VisualStudio.TestTools.UnitTesting; | |
17 using Microsoft.VSSDK.Tools.VsIdeTesting; | |
18 using Microsoft.VisualStudio; | |
19 | |
20 namespace Microsoft.VsSDK.IntegrationTestLibrary | |
21 { | |
22 /// <summary> | |
23 /// </summary> | |
24 public class TestUtils | |
25 { | |
26 | |
27 #region Methods: Handling embedded resources | |
28 /// <summary> | |
29 /// Gets the embedded file identified by the resource name, and converts
the | |
30 /// file into a string. | |
31 /// </summary> | |
32 /// <param name="resourceName">In VS, is DefaultNamespace.FileName?</par
am> | |
33 /// <returns></returns> | |
34 public static string GetEmbeddedStringResource(Assembly assembly, string
resourceName) | |
35 { | |
36 string result = null; | |
37 | |
38 // Use the .NET procedure for loading a file embedded in the assembl
y | |
39 Stream stream = assembly.GetManifestResourceStream(resourceName); | |
40 if (stream != null) | |
41 { | |
42 // Convert bytes to string | |
43 byte[] fileContentsAsBytes = new byte[stream.Length]; | |
44 stream.Read(fileContentsAsBytes, 0, (int)stream.Length); | |
45 result = Encoding.Default.GetString(fileContentsAsBytes); | |
46 } | |
47 else | |
48 { | |
49 // Embedded resource not found - list available resources | |
50 Debug.WriteLine("Unable to find the embedded resource file '" +
resourceName + "'."); | |
51 Debug.WriteLine(" Available resources:"); | |
52 foreach (string aResourceName in assembly.GetManifestResourceNam
es()) | |
53 { | |
54 Debug.WriteLine(" " + aResourceName); | |
55 } | |
56 } | |
57 | |
58 return result; | |
59 } | |
60 /// <summary> | |
61 /// | |
62 /// </summary> | |
63 /// <param name="embeddedResourceName"></param> | |
64 /// <param name="baseFileName"></param> | |
65 /// <param name="fileExtension"></param> | |
66 /// <returns></returns> | |
67 public static void WriteEmbeddedResourceToFile(Assembly assembly, string
embeddedResourceName, string fileName) | |
68 { | |
69 // Get file contents | |
70 string fileContents = GetEmbeddedStringResource(assembly, embeddedRe
sourceName); | |
71 if (fileContents == null) | |
72 throw new ApplicationException("Failed to get embedded resource
'" + embeddedResourceName + "' from assembly '" + assembly.FullName); | |
73 | |
74 // Write to file | |
75 StreamWriter sw = new StreamWriter(fileName); | |
76 sw.Write(fileContents); | |
77 sw.Close(); | |
78 } | |
79 | |
80 /// <summary> | |
81 /// Writes an embedded resource to a file. | |
82 /// </summary> | |
83 /// <param name="assembly">The name of the assembly that the embedded re
source is defined.</param> | |
84 /// <param name="embeddedResourceName">The name of the embedded resource
.</param> | |
85 /// <param name="fileName">The file to write the embedded resource's con
tent.</param> | |
86 public static void WriteEmbeddedResourceToBinaryFile(Assembly assembly,
string embeddedResourceName, string fileName) | |
87 { | |
88 // Get file contents | |
89 Stream stream = assembly.GetManifestResourceStream(embeddedResourceN
ame); | |
90 if (stream == null) | |
91 throw new InvalidOperationException("Failed to get embedded reso
urce '" + embeddedResourceName + "' from assembly '" + assembly.FullName); | |
92 | |
93 // Write to file | |
94 BinaryWriter sw = null; | |
95 FileStream fs = null; | |
96 try | |
97 { | |
98 byte[] fileContentsAsBytes = new byte[stream.Length]; | |
99 stream.Read(fileContentsAsBytes, 0, (int)stream.Length); | |
100 | |
101 FileMode mode = FileMode.CreateNew; | |
102 if (File.Exists(fileName)) | |
103 { | |
104 mode = FileMode.Truncate; | |
105 } | |
106 | |
107 fs = new FileStream(fileName, mode); | |
108 | |
109 sw = new BinaryWriter(fs); | |
110 sw.Write(fileContentsAsBytes); | |
111 } | |
112 finally | |
113 { | |
114 if (fs != null) | |
115 { | |
116 fs.Close(); | |
117 } | |
118 if (sw != null) | |
119 { | |
120 sw.Close(); | |
121 } | |
122 } | |
123 } | |
124 | |
125 #endregion | |
126 | |
127 #region Methods: Handling temporary files and directories | |
128 /// <summary> | |
129 /// Returns the first available file name on the form | |
130 /// [baseFileName]i.[extension] | |
131 /// where [i] starts at 1 and increases until there is an available file
name | |
132 /// in the given directory. Also creates an empty file with that name to
mark | |
133 /// that file as occupied. | |
134 /// </summary> | |
135 /// <param name="directory">Directory that the file should live in.</par
am> | |
136 /// <param name="baseFileName"></param> | |
137 /// <param name="extension">may be null, in which case the .[extension]
part | |
138 /// is not added.</param> | |
139 /// <returns>Full file name.</returns> | |
140 public static string GetNewFileName(string directory, string baseFileNam
e, string extension) | |
141 { | |
142 // Get the new file name | |
143 string fileName = GetNewFileOrDirectoryNameWithoutCreatingAnything(d
irectory, baseFileName, extension); | |
144 | |
145 // Create an empty file to mark it as taken | |
146 StreamWriter sw = new StreamWriter(fileName); | |
147 | |
148 sw.Write(""); | |
149 sw.Close(); | |
150 return fileName; | |
151 } | |
152 /// <summary> | |
153 /// Returns the first available directory name on the form | |
154 /// [baseDirectoryName]i | |
155 /// where [i] starts at 1 and increases until there is an available dire
ctory name | |
156 /// in the given directory. Also creates the directory to mark it as occ
upied. | |
157 /// </summary> | |
158 /// <param name="directory">Directory that the file should live in.</par
am> | |
159 /// <param name="baseDirectoryName"></param> | |
160 /// <returns>Full directory name.</returns> | |
161 public static string GetNewDirectoryName(string directory, string baseDi
rectoryName) | |
162 { | |
163 // Get the new file name | |
164 string directoryName = GetNewFileOrDirectoryNameWithoutCreatingAnyth
ing(directory, baseDirectoryName, null); | |
165 | |
166 // Create an empty directory to make it as occupied | |
167 Directory.CreateDirectory(directoryName); | |
168 | |
169 return directoryName; | |
170 } | |
171 | |
172 /// <summary> | |
173 /// | |
174 /// </summary> | |
175 /// <param name="directory"></param> | |
176 /// <param name="baseFileName"></param> | |
177 /// <param name="extension"></param> | |
178 /// <returns></returns> | |
179 private static string GetNewFileOrDirectoryNameWithoutCreatingAnything(s
tring directory, string baseFileName, string extension) | |
180 { | |
181 // - get a file name that we can use | |
182 string fileName; | |
183 int i = 1; | |
184 | |
185 string fullFileName = null; | |
186 while (true) | |
187 { | |
188 // construct next file name | |
189 fileName = baseFileName + i; | |
190 if (extension != null) | |
191 fileName += '.' + extension; | |
192 | |
193 // check if that file exists in the directory | |
194 fullFileName = Path.Combine(directory, fileName); | |
195 | |
196 if (!File.Exists(fullFileName) && !Directory.Exists(fullFileName
)) | |
197 break; | |
198 else | |
199 i++; | |
200 } | |
201 | |
202 return fullFileName; | |
203 } | |
204 #endregion | |
205 | |
206 #region Methods: Handling solutions | |
207 /// <summary> | |
208 /// Closes the currently open solution (if any), and creates a new solut
ion with the given name. | |
209 /// </summary> | |
210 /// <param name="solutionName">Name of new solution.</param> | |
211 public void CreateEmptySolution(string directory, string solutionName) | |
212 { | |
213 CloseCurrentSolution(__VSSLNSAVEOPTIONS.SLNSAVEOPT_NoSave); | |
214 | |
215 string solutionDirectory = GetNewDirectoryName(directory, solutionNa
me); | |
216 | |
217 // Create and force save solution | |
218 IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.Serv
iceProvider.GetService(typeof(IVsSolution)); | |
219 solutionService.CreateSolution(solutionDirectory, solutionName, (uin
t)__VSCREATESOLUTIONFLAGS.CSF_SILENT); | |
220 solutionService.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVE
OPT_ForceSave, null, 0); | |
221 DTE dte = VsIdeTestHostContext.Dte; | |
222 Assert.AreEqual(solutionName + ".sln", Path.GetFileName(dte.Solution
.FileName), "Newly created solution has wrong Filename"); | |
223 } | |
224 | |
225 public void CloseCurrentSolution(__VSSLNSAVEOPTIONS saveoptions) | |
226 { | |
227 // Get solution service | |
228 IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.Serv
iceProvider.GetService(typeof(IVsSolution)); | |
229 | |
230 // Close already open solution | |
231 solutionService.CloseSolutionElement((uint)saveoptions, null, 0); | |
232 } | |
233 | |
234 public void ForceSaveSolution() | |
235 { | |
236 // Get solution service | |
237 IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.Serv
iceProvider.GetService(typeof(IVsSolution)); | |
238 | |
239 // Force-save the solution | |
240 solutionService.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVE
OPT_ForceSave, null, 0); | |
241 } | |
242 | |
243 /// <summary> | |
244 /// Get current number of open project in solution | |
245 /// </summary> | |
246 /// <returns></returns> | |
247 public int ProjectCount() | |
248 { | |
249 // Get solution service | |
250 IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.Serv
iceProvider.GetService(typeof(IVsSolution)); | |
251 object projectCount; | |
252 solutionService.GetProperty((int)__VSPROPID.VSPROPID_ProjectCount, o
ut projectCount); | |
253 return (int)projectCount; | |
254 } | |
255 #endregion | |
256 | |
257 #region Methods: Handling projects | |
258 /// <summary> | |
259 /// Creates a project. | |
260 /// </summary> | |
261 /// <param name="projectName">Name of new project.</param> | |
262 /// <param name="templateName">Name of project template to use</param> | |
263 /// <param name="language">language</param> | |
264 /// <returns>New project.</returns> | |
265 public void CreateProjectFromTemplate(string projectName, string templat
eName, string language, bool exclusive) | |
266 { | |
267 DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeo
f(DTE)); | |
268 | |
269 Solution2 sol = dte.Solution as Solution2; | |
270 string projectTemplate = sol.GetProjectTemplate(templateName, langua
ge); | |
271 | |
272 // - project name and directory | |
273 string solutionDirectory = Directory.GetParent(dte.Solution.FullName
).FullName; | |
274 string projectDirectory = GetNewDirectoryName(solutionDirectory, pro
jectName); | |
275 | |
276 dte.Solution.AddFromTemplate(projectTemplate, projectDirectory, proj
ectName, false); | |
277 } | |
278 #endregion | |
279 | |
280 #region Methods: Handling project items | |
281 /// <summary> | |
282 /// Create a new item in the project | |
283 /// </summary> | |
284 /// <param name="parent">the parent collection for the new item</param> | |
285 /// <param name="templateName"></param> | |
286 /// <param name="language"></param> | |
287 /// <param name="name"></param> | |
288 /// <returns></returns> | |
289 public ProjectItem AddNewItemFromVsTemplate(ProjectItems parent, string
templateName, string language, string name) | |
290 { | |
291 if (parent == null) | |
292 throw new ArgumentException("project"); | |
293 if (name == null) | |
294 throw new ArgumentException("name"); | |
295 | |
296 DTE dte = (DTE)VsIdeTestHostContext.ServiceProvider.GetService(typeo
f(DTE)); | |
297 | |
298 Solution2 sol = dte.Solution as Solution2; | |
299 | |
300 string filename = sol.GetProjectItemTemplate(templateName, language)
; | |
301 | |
302 parent.AddFromTemplate(filename, name); | |
303 | |
304 return parent.Item(name); | |
305 } | |
306 | |
307 /// <summary> | |
308 /// Save an open document. | |
309 /// </summary> | |
310 /// <param name="documentMoniker">for filebased documents this is the fu
ll path to the document</param> | |
311 public void SaveDocument(string documentMoniker) | |
312 { | |
313 // Get document cookie and hierarchy for the file | |
314 IVsRunningDocumentTable runningDocumentTableService = (IVsRunningDoc
umentTable)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsRunningDocu
mentTable)); | |
315 uint docCookie; | |
316 IntPtr docData; | |
317 IVsHierarchy hierarchy; | |
318 uint itemId; | |
319 runningDocumentTableService.FindAndLockDocument( | |
320 (uint)Microsoft.VisualStudio.Shell.Interop._VSRDTFLAGS.RDT_NoLoc
k, | |
321 documentMoniker, | |
322 out hierarchy, | |
323 out itemId, | |
324 out docData, | |
325 out docCookie); | |
326 | |
327 // Save the document | |
328 IVsSolution solutionService = (IVsSolution)VsIdeTestHostContext.Serv
iceProvider.GetService(typeof(IVsSolution)); | |
329 solutionService.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVE
OPT_ForceSave, hierarchy, docCookie); | |
330 } | |
331 | |
332 public void CloseInEditorWithoutSaving(string fullFileName) | |
333 { | |
334 // Get the RDT service | |
335 IVsRunningDocumentTable runningDocumentTableService = (IVsRunningDoc
umentTable)VsIdeTestHostContext.ServiceProvider.GetService(typeof(IVsRunningDocu
mentTable)); | |
336 Assert.IsNotNull(runningDocumentTableService, "Failed to get the Run
ning Document Table Service"); | |
337 | |
338 // Get our document cookie and hierarchy for the file | |
339 uint docCookie; | |
340 IntPtr docData; | |
341 IVsHierarchy hierarchy; | |
342 uint itemId; | |
343 runningDocumentTableService.FindAndLockDocument( | |
344 (uint)Microsoft.VisualStudio.Shell.Interop._VSRDTFLAGS.RDT_NoLoc
k, | |
345 fullFileName, | |
346 out hierarchy, | |
347 out itemId, | |
348 out docData, | |
349 out docCookie); | |
350 | |
351 // Get the SolutionService | |
352 IVsSolution solutionService = VsIdeTestHostContext.ServiceProvider.G
etService(typeof(IVsSolution)) as IVsSolution; | |
353 Assert.IsNotNull(solutionService, "Failed to get IVsSolution service
"); | |
354 | |
355 // Close the document | |
356 solutionService.CloseSolutionElement( | |
357 (uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_NoSave, | |
358 hierarchy, | |
359 docCookie); | |
360 } | |
361 #endregion | |
362 | |
363 #region Methods: Handling Toolwindows | |
364 public bool CanFindToolwindow(Guid persistenceGuid) | |
365 { | |
366 IVsUIShell uiShellService = VsIdeTestHostContext.ServiceProvider.Get
Service(typeof(SVsUIShell)) as IVsUIShell; | |
367 Assert.IsNotNull(uiShellService); | |
368 IVsWindowFrame windowFrame; | |
369 int hr = uiShellService.FindToolWindow((uint)__VSFINDTOOLWIN.FTW_fFi
ndFirst, ref persistenceGuid, out windowFrame); | |
370 Assert.IsTrue(hr == VSConstants.S_OK); | |
371 | |
372 return (windowFrame != null); | |
373 } | |
374 #endregion | |
375 | |
376 #region Methods: Loading packages | |
377 public IVsPackage LoadPackage(Guid packageGuid) | |
378 { | |
379 IVsShell shellService = (IVsShell)VsIdeTestHostContext.ServiceProvid
er.GetService(typeof(SVsShell)); | |
380 IVsPackage package; | |
381 shellService.LoadPackage(ref packageGuid, out package); | |
382 Assert.IsNotNull(package, "Failed to load package"); | |
383 return package; | |
384 } | |
385 #endregion | |
386 | |
387 /// <summary> | |
388 /// Executes a Command (menu item) in the given context | |
389 /// </summary> | |
390 public void ExecuteCommand(CommandID cmd) | |
391 { | |
392 object Customin = null; | |
393 object Customout = null; | |
394 string guidString = cmd.Guid.ToString("B").ToUpper(); | |
395 int cmdId = cmd.ID; | |
396 DTE dte = VsIdeTestHostContext.Dte; | |
397 dte.Commands.Raise(guidString, cmdId, ref Customin, ref Customout); | |
398 } | |
399 | |
400 } | |
401 } | |
OLD | NEW |