PageRenderTime 40ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/NUnit-2.5.7.10213/src/GuiComponents/UiKit/TestLoaderUI.cs

http://github.com/shouldly/shouldly
C# | 362 lines | 288 code | 48 blank | 26 comment | 55 complexity | d1a3fa99d9d10ecf166960448bbb7672 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. using NUnit.Core;
  10. using NUnit.Util;
  11. namespace NUnit.UiKit
  12. {
  13. /// <summary>
  14. /// FileHandler does all file opening and closing that
  15. /// involves interacting with the user.
  16. /// </summary>
  17. public class TestLoaderUI
  18. {
  19. private static bool VisualStudioSupport
  20. {
  21. get
  22. {
  23. return Services.UserSettings.GetSetting( "Options.TestLoader.VisualStudioSupport", false );
  24. }
  25. }
  26. public static void OpenProject( Form owner )
  27. {
  28. OpenFileDialog dlg = new OpenFileDialog();
  29. System.ComponentModel.ISite site = owner == null ? null : owner.Site;
  30. if ( site != null ) dlg.Site = site;
  31. dlg.Title = "Open Project";
  32. if ( VisualStudioSupport )
  33. {
  34. dlg.Filter =
  35. "Projects & Assemblies(*.nunit,*.csproj,*.vbproj,*.vjsproj, *.vcproj,*.sln,*.dll,*.exe )|*.nunit;*.csproj;*.vjsproj;*.vbproj;*.vcproj;*.sln;*.dll;*.exe|" +
  36. "All Project Types (*.nunit,*.csproj,*.vbproj,*.vjsproj,*.vcproj,*.sln)|*.nunit;*.csproj;*.vjsproj;*.vbproj;*.vcproj;*.sln|" +
  37. "Test Projects (*.nunit)|*.nunit|" +
  38. "Solutions (*.sln)|*.sln|" +
  39. "C# Projects (*.csproj)|*.csproj|" +
  40. "J# Projects (*.vjsproj)|*.vjsproj|" +
  41. "VB Projects (*.vbproj)|*.vbproj|" +
  42. "C++ Projects (*.vcproj)|*.vcproj|" +
  43. "Assemblies (*.dll,*.exe)|*.dll;*.exe";
  44. }
  45. else
  46. {
  47. dlg.Filter =
  48. "Projects & Assemblies(*.nunit,*.dll,*.exe)|*.nunit;*.dll;*.exe|" +
  49. "Test Projects (*.nunit)|*.nunit|" +
  50. "Assemblies (*.dll,*.exe)|*.dll;*.exe";
  51. }
  52. dlg.FilterIndex = 1;
  53. dlg.FileName = "";
  54. if ( dlg.ShowDialog( owner ) == DialogResult.OK )
  55. OpenProject( owner, dlg.FileName );
  56. }
  57. public static void OpenProject( Form owner, string testFileName, string configName, string testName )
  58. {
  59. TestLoader loader = Services.TestLoader;
  60. if ( loader.IsProjectLoaded && SaveProjectIfDirty( owner ) == DialogResult.Cancel )
  61. return;
  62. loader.LoadProject( testFileName, configName );
  63. if ( loader.IsProjectLoaded )
  64. {
  65. NUnitProject testProject = loader.TestProject;
  66. if ( testProject.Configs.Count == 0 )
  67. UserMessage.DisplayInfo( "Loaded project contains no configuration data" );
  68. else if ( testProject.ActiveConfig == null )
  69. UserMessage.DisplayInfo( "Loaded project has no active configuration" );
  70. else if ( testProject.ActiveConfig.Assemblies.Count == 0 )
  71. UserMessage.DisplayInfo( "Active configuration contains no assemblies" );
  72. else
  73. loader.LoadTest( testName );
  74. }
  75. }
  76. public static void OpenProject( Form owner, string testFileName )
  77. {
  78. OpenProject( owner, testFileName, null, null );
  79. }
  80. // public static void OpenResults( Form owner )
  81. // {
  82. // TestLoader loader = Services.TestLoader;
  83. //
  84. // OpenFileDialog dlg = new OpenFileDialog();
  85. // System.ComponentModel.ISite site = owner == null ? null : owner.Site;
  86. // if ( site != null ) dlg.Site = site;
  87. // dlg.Title = "Open Test Results";
  88. //
  89. // dlg.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
  90. // dlg.FilterIndex = 1;
  91. // dlg.FileName = "";
  92. //
  93. // if ( dlg.ShowDialog( owner ) == DialogResult.OK )
  94. // OpenProject( owner, dlg.FileName );
  95. // }
  96. public static void AddToProject( Form owner )
  97. {
  98. AddToProject( owner, null );
  99. }
  100. public static void AddToProject( Form owner, string configName )
  101. {
  102. TestLoader loader = Services.TestLoader;
  103. ProjectConfig config = configName == null
  104. ? loader.TestProject.ActiveConfig
  105. : loader.TestProject.Configs[configName];
  106. OpenFileDialog dlg = new OpenFileDialog();
  107. dlg.Title = "Add Assemblies To Project";
  108. dlg.InitialDirectory = config.BasePath;
  109. if ( VisualStudioSupport )
  110. dlg.Filter =
  111. "Projects & Assemblies(*.csproj,*.vbproj,*.vjsproj, *.vcproj,*.dll,*.exe )|*.csproj;*.vjsproj;*.vbproj;*.vcproj;*.dll;*.exe|" +
  112. "Visual Studio Projects (*.csproj,*.vjsproj,*.vbproj,*.vcproj)|*.csproj;*.vjsproj;*.vbproj;*.vcproj|" +
  113. "C# Projects (*.csproj)|*.csproj|" +
  114. "J# Projects (*.vjsproj)|*.vjsproj|" +
  115. "VB Projects (*.vbproj)|*.vbproj|" +
  116. "C++ Projects (*.vcproj)|*.vcproj|" +
  117. "Assemblies (*.dll,*.exe)|*.dll;*.exe";
  118. else
  119. dlg.Filter = "Assemblies (*.dll,*.exe)|*.dll;*.exe";
  120. dlg.FilterIndex = 1;
  121. dlg.FileName = "";
  122. if ( dlg.ShowDialog( owner ) != DialogResult.OK )
  123. return;
  124. if (PathUtils.IsAssemblyFileType(dlg.FileName))
  125. {
  126. config.Assemblies.Add(dlg.FileName);
  127. return;
  128. }
  129. else if (VSProject.IsProjectFile(dlg.FileName))
  130. try
  131. {
  132. VSProject vsProject = new VSProject(dlg.FileName);
  133. MessageBoxButtons buttons;
  134. string msg;
  135. if (configName != null && vsProject.Configs.Contains(configName))
  136. {
  137. msg = "The project being added may contain multiple configurations;\r\r" +
  138. "Select\tYes to add all configurations found.\r" +
  139. "\tNo to add only the " + configName + " configuration.\r" +
  140. "\tCancel to exit without modifying the project.";
  141. buttons = MessageBoxButtons.YesNoCancel;
  142. }
  143. else
  144. {
  145. msg = "The project being added may contain multiple configurations;\r\r" +
  146. "Select\tOK to add all configurations found.\r" +
  147. "\tCancel to exit without modifying the project.";
  148. buttons = MessageBoxButtons.OKCancel;
  149. }
  150. DialogResult result = UserMessage.Ask(msg, buttons);
  151. if (result == DialogResult.Yes || result == DialogResult.OK)
  152. {
  153. loader.TestProject.Add(vsProject);
  154. return;
  155. }
  156. else if (result == DialogResult.No)
  157. {
  158. foreach (string assembly in vsProject.Configs[configName].Assemblies)
  159. config.Assemblies.Add(assembly);
  160. return;
  161. }
  162. }
  163. catch (Exception ex)
  164. {
  165. UserMessage.DisplayFailure(ex.Message, "Invalid VS Project");
  166. }
  167. }
  168. public static void AddAssembly( Form owner )
  169. {
  170. AddAssembly( owner, null );
  171. }
  172. public static void AddAssembly( Form owner, string configName )
  173. {
  174. TestLoader loader = Services.TestLoader;
  175. ProjectConfig config = configName == null
  176. ? loader.TestProject.ActiveConfig
  177. : loader.TestProject.Configs[configName];
  178. OpenFileDialog dlg = new OpenFileDialog();
  179. dlg.Title = "Add Assembly";
  180. dlg.InitialDirectory = config.BasePath;
  181. dlg.Filter = "Assemblies (*.dll,*.exe)|*.dll;*.exe";
  182. dlg.FilterIndex = 1;
  183. dlg.FileName = "";
  184. if (dlg.ShowDialog(owner) == DialogResult.OK)
  185. config.Assemblies.Add(dlg.FileName);
  186. }
  187. public static void AddVSProject( Form owner )
  188. {
  189. TestLoader loader = Services.TestLoader;
  190. OpenFileDialog dlg = new OpenFileDialog();
  191. dlg.Title = "Add Visual Studio Project";
  192. dlg.Filter =
  193. "All Project Types (*.csproj,*.vjsproj,*.vbproj,*.vcproj)|*.csproj;*.vjsproj;*.vbproj;*.vcproj|" +
  194. "C# Projects (*.csproj)|*.csproj|" +
  195. "J# Projects (*.vjsproj)|*.vjsproj|" +
  196. "VB Projects (*.vbproj)|*.vbproj|" +
  197. "C++ Projects (*.vcproj)|*.vcproj|" +
  198. "All Files (*.*)|*.*";
  199. dlg.FilterIndex = 1;
  200. dlg.FileName = "";
  201. if ( dlg.ShowDialog( owner ) == DialogResult.OK )
  202. {
  203. try
  204. {
  205. VSProject vsProject = new VSProject( dlg.FileName );
  206. loader.TestProject.Add( vsProject );
  207. }
  208. catch( Exception ex )
  209. {
  210. UserMessage.DisplayFailure( ex.Message, "Invalid VS Project" );
  211. }
  212. }
  213. }
  214. private static bool CanWriteProjectFile( string path )
  215. {
  216. return !File.Exists( path ) ||
  217. ( File.GetAttributes( path ) & FileAttributes.ReadOnly ) == 0;
  218. }
  219. public static void SaveProject( Form owner )
  220. {
  221. TestLoader loader = Services.TestLoader;
  222. if ( Path.IsPathRooted( loader.TestProject.ProjectPath ) &&
  223. NUnitProject.IsNUnitProjectFile( loader.TestProject.ProjectPath ) &&
  224. CanWriteProjectFile( loader.TestProject.ProjectPath ) )
  225. loader.TestProject.Save();
  226. else
  227. SaveProjectAs( owner );
  228. }
  229. public static void SaveProjectAs( Form owner )
  230. {
  231. TestLoader loader = Services.TestLoader;
  232. SaveFileDialog dlg = new SaveFileDialog();
  233. dlg.Title = "Save Test Project";
  234. dlg.Filter = "NUnit Test Project (*.nunit)|*.nunit|All Files (*.*)|*.*";
  235. string path = NUnitProject.ProjectPathFromFile( loader.TestProject.ProjectPath );
  236. if ( CanWriteProjectFile( path ) )
  237. dlg.FileName = path;
  238. dlg.DefaultExt = "nunit";
  239. dlg.ValidateNames = true;
  240. dlg.OverwritePrompt = true;
  241. while( dlg.ShowDialog( owner ) == DialogResult.OK )
  242. {
  243. if ( !CanWriteProjectFile( dlg.FileName ) )
  244. UserMessage.DisplayInfo( string.Format( "File {0} is write-protected. Select another file name.", dlg.FileName ) );
  245. else
  246. {
  247. loader.TestProject.Save( dlg.FileName );
  248. return;
  249. }
  250. }
  251. }
  252. public static void NewProject( Form owner )
  253. {
  254. TestLoader loader = Services.TestLoader;
  255. SaveFileDialog dlg = new SaveFileDialog();
  256. dlg.Title = "New Test Project";
  257. dlg.Filter = "NUnit Test Project (*.nunit)|*.nunit|All Files (*.*)|*.*";
  258. dlg.FileName = Services.ProjectService.GenerateProjectName();
  259. dlg.DefaultExt = "nunit";
  260. dlg.ValidateNames = true;
  261. dlg.OverwritePrompt = true;
  262. if ( dlg.ShowDialog( owner ) == DialogResult.OK )
  263. loader.NewProject( dlg.FileName );
  264. }
  265. public static DialogResult CloseProject( Form owner )
  266. {
  267. DialogResult result = SaveProjectIfDirty( owner );
  268. if( result != DialogResult.Cancel )
  269. Services.TestLoader.UnloadProject();
  270. return result;
  271. }
  272. private static DialogResult SaveProjectIfDirty( Form owner )
  273. {
  274. DialogResult result = DialogResult.OK;
  275. NUnitProject project = Services.TestLoader.TestProject;
  276. if( project.IsDirty )
  277. {
  278. string msg = string.Format(
  279. "Project {0} has been changed. Do you want to save changes?",project.Name);
  280. result = UserMessage.Ask( msg, MessageBoxButtons.YesNoCancel );
  281. if ( result == DialogResult.Yes )
  282. SaveProject( owner );
  283. }
  284. return result;
  285. }
  286. public static void SaveLastResult( Form owner )
  287. {
  288. //TODO: Save all results
  289. TestLoader loader = Services.TestLoader;
  290. SaveFileDialog dlg = new SaveFileDialog();
  291. dlg.Title = "Save Test Results as XML";
  292. dlg.Filter = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*";
  293. dlg.FileName = "TestResult.xml";
  294. dlg.InitialDirectory = Path.GetDirectoryName( loader.TestFileName );
  295. dlg.DefaultExt = "xml";
  296. dlg.ValidateNames = true;
  297. dlg.OverwritePrompt = true;
  298. if ( dlg.ShowDialog( owner ) == DialogResult.OK )
  299. {
  300. try
  301. {
  302. string fileName = dlg.FileName;
  303. loader.SaveLastResult( fileName );
  304. string msg = String.Format( "Results saved as {0}", fileName );
  305. UserMessage.DisplayInfo( msg, "Save Results as XML" );
  306. }
  307. catch( Exception exception )
  308. {
  309. UserMessage.DisplayFailure( exception, "Unable to Save Results" );
  310. }
  311. }
  312. }
  313. }
  314. }