PageRenderTime 122ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/Tests/Core.UI/ProjectHomeTests.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 270 lines | 200 code | 48 blank | 22 comment | 6 complexity | 347d6ed64d6683d54741f6e33074863e MD5 | raw file
  1. // Python Tools for Visual Studio
  2. // Copyright(c) Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the License); you may not use
  6. // this file except in compliance with the License. You may obtain a copy of the
  7. // License at http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  10. // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
  11. // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. //
  14. // See the Apache Version 2.0 License for specific language governing
  15. // permissions and limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Windows.Input;
  22. using EnvDTE;
  23. using EnvDTE80;
  24. using Microsoft.PythonTools.Project.Automation;
  25. using Microsoft.VisualStudio.TestTools.UnitTesting;
  26. using Microsoft.VisualStudioTools.Project.Automation;
  27. using TestUtilities;
  28. using TestUtilities.Python;
  29. using TestUtilities.UI;
  30. using Keyboard = TestUtilities.UI.Keyboard;
  31. using Mouse = TestUtilities.UI.Mouse;
  32. using Path = System.IO.Path;
  33. namespace PythonToolsUITests {
  34. [TestClass]
  35. public class ProjectHomeTests {
  36. [ClassInitialize]
  37. public static void DoDeployment(TestContext context) {
  38. AssertListener.Initialize();
  39. PythonTestData.Deploy();
  40. }
  41. [TestMethod, Priority(1)]
  42. [HostType("VSTestHost"), TestCategory("Installed")]
  43. public void LoadRelativeProjects() {
  44. using (var app = new VisualStudioApp()) {
  45. string fullPath = TestData.GetPath(@"TestData\ProjectHomeProjects.sln");
  46. app.OpenProject(@"TestData\ProjectHomeProjects.sln", expectedProjects: 9);
  47. foreach (var project in app.Dte.Solution.Projects.OfType<Project>()) {
  48. var name = Path.GetFileName(project.FileName);
  49. if (name.StartsWith("ProjectA")) {
  50. // Should have ProgramA.py, Subfolder\ProgramB.py and Subfolder\Subsubfolder\ProgramC.py
  51. var programA = project.ProjectItems.Item("ProgramA.py");
  52. Assert.IsNotNull(programA);
  53. var subfolder = project.ProjectItems.Item("Subfolder");
  54. var programB = subfolder.ProjectItems.Item("ProgramB.py");
  55. Assert.IsNotNull(programB);
  56. var subsubfolder = subfolder.ProjectItems.Item("Subsubfolder");
  57. var programC = subsubfolder.ProjectItems.Item("ProgramC.py");
  58. Assert.IsNotNull(programC);
  59. } else if (name.StartsWith("ProjectB")) {
  60. // Should have ProgramB.py and Subsubfolder\ProgramC.py
  61. var programB = project.ProjectItems.Item("ProgramB.py");
  62. Assert.IsNotNull(programB);
  63. var subsubfolder = project.ProjectItems.Item("Subsubfolder");
  64. var programC = subsubfolder.ProjectItems.Item("ProgramC.py");
  65. Assert.IsNotNull(programC);
  66. } else if (name.StartsWith("ProjectSln")) {
  67. // Should have ProjectHomeProjects\ProgramA.py,
  68. // ProjectHomeProjects\Subfolder\ProgramB.py and
  69. // ProjectHomeProjects\Subfolder\Subsubfolder\ProgramC.py
  70. var projectHome = project.ProjectItems.Item("ProjectHomeProjects");
  71. var programA = projectHome.ProjectItems.Item("ProgramA.py");
  72. Assert.IsNotNull(programA);
  73. var subfolder = projectHome.ProjectItems.Item("Subfolder");
  74. var programB = subfolder.ProjectItems.Item("ProgramB.py");
  75. Assert.IsNotNull(programB);
  76. var subsubfolder = subfolder.ProjectItems.Item("Subsubfolder");
  77. var programC = subsubfolder.ProjectItems.Item("ProgramC.py");
  78. Assert.IsNotNull(programC);
  79. } else {
  80. Assert.Fail("Wrong project file name", name);
  81. }
  82. }
  83. }
  84. }
  85. [TestMethod, Priority(1)]
  86. [HostType("VSTestHost"), TestCategory("Installed")]
  87. public void AddDeleteItem() {
  88. using (var app = new VisualStudioApp()) {
  89. var project = app.OpenProject(@"TestData\ProjectHomeSingleProject.sln");
  90. Assert.AreEqual("ProjectSingle.pyproj", Path.GetFileName(project.FileName));
  91. project.ProjectItems.AddFromTemplate(((Solution2)app.Dte.Solution).GetProjectItemTemplate("PyClass.zip", "pyproj"), "TemplateItem.py");
  92. var newItem = project.ProjectItems.Item("TemplateItem.py");
  93. Assert.IsNotNull(newItem);
  94. Assert.AreEqual(false, project.Saved);
  95. project.Save();
  96. Assert.AreEqual(true, project.Saved);
  97. Assert.IsTrue(File.Exists(TestData.GetPath(@"TestData\ProjectHomeProjects\TemplateItem.py")));
  98. newItem.Delete();
  99. Assert.AreEqual(false, project.Saved);
  100. project.Save();
  101. Assert.AreEqual(true, project.Saved);
  102. Assert.IsFalse(File.Exists(TestData.GetPath(@"TestData\ProjectHomeProjects\TemplateItem.py")));
  103. }
  104. }
  105. [TestMethod, Priority(1)]
  106. [HostType("VSTestHost"), TestCategory("Installed")]
  107. public void AddDeleteItem2() {
  108. using (var app = new VisualStudioApp()) {
  109. var project = app.OpenProject(@"TestData\ProjectHomeSingleProject.sln");
  110. var folder = project.ProjectItems.Item("Subfolder");
  111. Assert.AreEqual("ProjectSingle.pyproj", Path.GetFileName(project.FileName));
  112. folder.ProjectItems.AddFromTemplate(((Solution2)app.Dte.Solution).GetProjectItemTemplate("PyClass.zip", "pyproj"), "TemplateItem.py");
  113. var newItem = folder.ProjectItems.Item("TemplateItem.py");
  114. Assert.IsNotNull(newItem);
  115. Assert.AreEqual(false, project.Saved);
  116. project.Save();
  117. Assert.AreEqual(true, project.Saved);
  118. Assert.IsTrue(File.Exists(TestData.GetPath(@"TestData\ProjectHomeProjects\Subfolder\TemplateItem.py")));
  119. newItem.Delete();
  120. Assert.AreEqual(false, project.Saved);
  121. project.Save();
  122. Assert.AreEqual(true, project.Saved);
  123. Assert.IsFalse(File.Exists(TestData.GetPath(@"TestData\ProjectHomeProjects\Subfolder\TemplateItem.py")));
  124. }
  125. }
  126. [TestMethod, Priority(1)]
  127. [HostType("VSTestHost"), TestCategory("Installed")]
  128. public void AddDeleteFolder() {
  129. using (var app = new VisualStudioApp()) {
  130. var project = app.OpenProject(@"TestData\ProjectHomeSingleProject.sln");
  131. Assert.AreEqual("ProjectSingle.pyproj", Path.GetFileName(project.FileName));
  132. project.ProjectItems.AddFolder("NewFolder");
  133. var newFolder = project.ProjectItems.Item("NewFolder");
  134. Assert.IsNotNull(newFolder);
  135. Assert.AreEqual(TestData.GetPath(@"TestData\ProjectHomeProjects\NewFolder\"), newFolder.Properties.Item("FullPath").Value);
  136. newFolder.Delete();
  137. }
  138. }
  139. [TestMethod, Priority(1)]
  140. [HostType("VSTestHost"), TestCategory("Installed")]
  141. public void AddDeleteSubfolder() {
  142. using (var app = new VisualStudioApp()) {
  143. var project = app.OpenProject(@"TestData\ProjectHomeSingleProject.sln");
  144. var folder = project.ProjectItems.Item("Subfolder");
  145. Assert.AreEqual("ProjectSingle.pyproj", Path.GetFileName(project.FileName));
  146. folder.ProjectItems.AddFolder("NewFolder");
  147. var newFolder = folder.ProjectItems.Item("NewFolder");
  148. Assert.IsNotNull(newFolder);
  149. Assert.AreEqual(TestData.GetPath(@"TestData\ProjectHomeProjects\Subfolder\NewFolder\"), newFolder.Properties.Item("FullPath").Value);
  150. newFolder.Delete();
  151. }
  152. }
  153. [TestMethod, Priority(1)]
  154. [HostType("VSTestHost"), TestCategory("Installed")]
  155. public void SaveProjectAs() {
  156. using (var app = new VisualStudioApp()) {
  157. EnvDTE.Project project;
  158. try {
  159. project = app.OpenProject(@"TestData\HelloWorld.sln");
  160. project.SaveAs(TestData.GetPath(@"TestData\ProjectHomeProjects\TempFile.pyproj"));
  161. Assert.AreEqual(TestData.GetPath(@"TestData\HelloWorld\"),
  162. ((OAProject)project).ProjectNode.ProjectHome);
  163. app.Dte.Solution.SaveAs("HelloWorldRelocated.sln");
  164. } finally {
  165. app.Dte.Solution.Close();
  166. GC.Collect();
  167. GC.WaitForPendingFinalizers();
  168. }
  169. project = app.OpenProject(@"TestData\HelloWorldRelocated.sln");
  170. Assert.AreEqual("TempFile.pyproj", project.FileName);
  171. Assert.AreEqual(TestData.GetPath(@"TestData\HelloWorld\"),
  172. ((OAProject)project).ProjectNode.ProjectHome);
  173. }
  174. }
  175. [TestMethod, Priority(1)]
  176. [HostType("VSTestHost"), TestCategory("Installed")]
  177. public void DragDropTest() {
  178. using (var app = new VisualStudioApp()) {
  179. app.OpenProject(@"TestData\DragDropRelocatedTest.sln");
  180. app.OpenSolutionExplorer();
  181. var window = app.SolutionExplorerTreeView;
  182. var folder = window.FindItem("Solution 'DragDropRelocatedTest' (1 project)", "DragDropTest", "TestFolder", "SubItem.py");
  183. var point = folder.GetClickablePoint();
  184. Mouse.MoveTo(point);
  185. Mouse.Down(MouseButton.Left);
  186. var projectItem = window.FindItem("Solution 'DragDropRelocatedTest' (1 project)", "DragDropTest");
  187. point = projectItem.GetClickablePoint();
  188. Mouse.MoveTo(point);
  189. Mouse.Up(MouseButton.Left);
  190. Assert.AreNotEqual(null, window.WaitForItem("Solution 'DragDropRelocatedTest' (1 project)", "DragDropTest", "SubItem.py"));
  191. app.Dte.Solution.Close(true);
  192. // Ensure file was moved and the path was updated correctly.
  193. var project = app.OpenProject(@"TestData\DragDropRelocatedTest.sln");
  194. foreach (var item in project.ProjectItems.OfType<OAFileItem>()) {
  195. Assert.IsTrue(File.Exists((string)item.Properties.Item("FullPath").Value), (string)item.Properties.Item("FullPath").Value);
  196. }
  197. }
  198. }
  199. [TestMethod, Priority(1)]
  200. [HostType("VSTestHost"), TestCategory("Installed")]
  201. public void CutPasteTest() {
  202. using (var app = new VisualStudioApp()) {
  203. app.OpenProject(@"TestData\CutPasteRelocatedTest.sln");
  204. app.OpenSolutionExplorer();
  205. var window = app.SolutionExplorerTreeView;
  206. var folder = window.FindItem("Solution 'CutPasteRelocatedTest' (1 project)", "CutPasteTest", "TestFolder", "SubItem.py");
  207. AutomationWrapper.Select(folder);
  208. app.ExecuteCommand("Edit.Cut");
  209. var projectItem = window.FindItem("Solution 'CutPasteRelocatedTest' (1 project)", "CutPasteTest");
  210. AutomationWrapper.Select(projectItem);
  211. app.ExecuteCommand("Edit.Paste");
  212. Assert.IsNotNull(window.WaitForItem("Solution 'CutPasteRelocatedTest' (1 project)", "CutPasteTest", "SubItem.py"));
  213. app.Dte.Solution.Close(true);
  214. // Ensure file was moved and the path was updated correctly.
  215. var project = app.OpenProject(@"TestData\CutPasteRelocatedTest.sln");
  216. foreach (var item in project.ProjectItems.OfType<OAFileItem>()) {
  217. Assert.IsTrue(File.Exists((string)item.Properties.Item("FullPath").Value), (string)item.Properties.Item("FullPath").Value);
  218. }
  219. }
  220. }
  221. }
  222. }