FileDialogFluentTests.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System.IO.Abstractions;
  2. using System.IO.Abstractions.TestingHelpers;
  3. using System.Runtime.InteropServices;
  4. using Terminal.Gui;
  5. using TerminalGuiFluentTesting;
  6. using TerminalGuiFluentTestingXunit;
  7. using Xunit.Abstractions;
  8. namespace IntegrationTests.FluentTests;
  9. public class FileDialogFluentTests
  10. {
  11. private readonly TextWriter _out;
  12. public FileDialogFluentTests (ITestOutputHelper outputHelper) { _out = new TestOutputWriter (outputHelper); }
  13. private MockFileSystem CreateExampleFileSystem ()
  14. {
  15. // Optional: use Ordinal to simulate Linux-style case sensitivity
  16. var mockFileSystem = new MockFileSystem (new Dictionary<string, MockFileData> ());
  17. string testDir = mockFileSystem.Path.Combine ("test-dir");
  18. string subDir = mockFileSystem.Path.Combine (testDir, "sub-dir");
  19. string logsDir = "logs";
  20. string emptyDir = "empty-dir";
  21. // Add files
  22. mockFileSystem.AddFile (mockFileSystem.Path.Combine (testDir, "file1.txt"), new MockFileData ("Hello, this is file 1."));
  23. mockFileSystem.AddFile (mockFileSystem.Path.Combine (testDir, "file2.txt"), new MockFileData ("Hello, this is file 2."));
  24. mockFileSystem.AddFile (mockFileSystem.Path.Combine (subDir, "nested-file.txt"), new MockFileData ("This is a nested file."));
  25. mockFileSystem.AddFile (mockFileSystem.Path.Combine (logsDir, "log1.log"), new MockFileData ("Log entry 1"));
  26. mockFileSystem.AddFile (mockFileSystem.Path.Combine (logsDir, "log2.log"), new MockFileData ("Log entry 2"));
  27. // Create an empty directory
  28. mockFileSystem.AddDirectory (emptyDir);
  29. return mockFileSystem;
  30. }
  31. [Theory]
  32. [ClassData (typeof (V2TestDrivers))]
  33. public void CancelFileDialog_UsingEscape (V2TestDriver d)
  34. {
  35. var sd = new SaveDialog ( CreateExampleFileSystem ());
  36. using var c = With.A (sd, 100, 20, d)
  37. .ScreenShot ("Save dialog",_out)
  38. .Escape()
  39. .Stop ();
  40. Assert.True (sd.Canceled);
  41. }
  42. [Theory]
  43. [ClassData (typeof (V2TestDrivers))]
  44. public void CancelFileDialog_UsingCancelButton_TabThenEnter (V2TestDriver d)
  45. {
  46. var sd = new SaveDialog (CreateExampleFileSystem ());
  47. using var c = With.A (sd, 100, 20, d)
  48. .ScreenShot ("Save dialog", _out)
  49. .Focus <Button>(b=> b.Text == "_Cancel")
  50. .Enter ()
  51. .Stop ();
  52. Assert.True (sd.Canceled);
  53. }
  54. [Theory]
  55. [ClassData (typeof (V2TestDrivers))]
  56. public void CancelFileDialog_UsingCancelButton_LeftClickButton (V2TestDriver d)
  57. {
  58. var sd = new SaveDialog (CreateExampleFileSystem ());
  59. using var c = With.A (sd, 100, 20, d)
  60. .ScreenShot ("Save dialog", _out)
  61. .LeftClick <Button> (b => b.Text == "_Cancel")
  62. .Stop ()
  63. .WriteOutLogs (_out);
  64. Assert.True (sd.Canceled);
  65. }
  66. [Theory]
  67. [ClassData (typeof (V2TestDrivers))]
  68. public void CancelFileDialog_UsingCancelButton_AltC (V2TestDriver d)
  69. {
  70. var sd = new SaveDialog (CreateExampleFileSystem ());
  71. using var c = With.A (sd, 100, 20, d)
  72. .ScreenShot ("Save dialog", _out)
  73. .Send (Key.C.WithAlt)
  74. .WriteOutLogs (_out)
  75. .Stop ();
  76. Assert.True (sd.Canceled);
  77. }
  78. [Theory]
  79. [ClassData (typeof (V2TestDrivers))]
  80. public void SaveFileDialog_UsingOkButton_Enter (V2TestDriver d)
  81. {
  82. var fs = CreateExampleFileSystem ();
  83. var sd = new SaveDialog (fs);
  84. using var c = With.A (sd, 100, 20, d)
  85. .ScreenShot ("Save dialog", _out)
  86. .LeftClick<Button> (b => b.Text == "_Save")
  87. .WriteOutLogs (_out)
  88. .Stop ();
  89. Assert.False (sd.Canceled);
  90. AssertIsFileSystemRoot (fs, sd);
  91. }
  92. [Theory]
  93. [ClassData (typeof (V2TestDrivers))]
  94. public void SaveFileDialog_UsingOkButton_AltS (V2TestDriver d)
  95. {
  96. var fs = CreateExampleFileSystem ();
  97. var sd = new SaveDialog (fs);
  98. using var c = With.A (sd, 100, 20, d)
  99. .ScreenShot ("Save dialog", _out)
  100. .Send (Key.S.WithAlt)
  101. .WriteOutLogs (_out)
  102. .Stop ();
  103. Assert.False (sd.Canceled);
  104. AssertIsFileSystemRoot (fs, sd);
  105. }
  106. [Theory]
  107. [ClassData (typeof (V2TestDrivers))]
  108. public void SaveFileDialog_UsingOkButton_TabEnter (V2TestDriver d)
  109. {
  110. var fs = CreateExampleFileSystem ();
  111. var sd = new SaveDialog (fs);
  112. using var c = With.A (sd, 100, 20, d)
  113. .ScreenShot ("Save dialog", _out)
  114. .Focus <Button> (b => b.Text == "_Save")
  115. .Enter ()
  116. .WriteOutLogs (_out)
  117. .Stop ();
  118. Assert.False (sd.Canceled);
  119. AssertIsFileSystemRoot (fs,sd);
  120. }
  121. private void AssertIsFileSystemRoot (IFileSystem fs, SaveDialog sd)
  122. {
  123. var expectedPath =
  124. RuntimeInformation.IsOSPlatform (OSPlatform.Windows) ?
  125. $@"C:{fs.Path.DirectorySeparatorChar}" :
  126. "/";
  127. Assert.Equal (expectedPath, sd.FileName);
  128. }
  129. [Theory]
  130. [ClassData (typeof (V2TestDrivers))]
  131. public void SaveFileDialog_PressingPopTree_ShouldNotChangeCancel (V2TestDriver d)
  132. {
  133. var sd = new SaveDialog (CreateExampleFileSystem ()) { Modal = true };
  134. using var c = With.A (sd, 100, 20, d)
  135. .ScreenShot ("Save dialog", _out)
  136. .AssertTrue (sd.Canceled)
  137. .Focus<Button> (b => b.Text == "►►")
  138. .Enter ()
  139. .ScreenShot ("After pop tree", _out)
  140. .AssertTrue (sd.Canceled)
  141. .WriteOutLogs (_out)
  142. .Stop ();
  143. Assert.True(sd.Canceled);
  144. }
  145. [Theory]
  146. [ClassData (typeof (V2TestDrivers))]
  147. public void SaveFileDialog_PopTree_AndNavigate (V2TestDriver d)
  148. {
  149. var sd = new SaveDialog (CreateExampleFileSystem ()) { Modal = true };
  150. using var c = With.A (sd, 100, 20, d)
  151. .ScreenShot ("Save dialog", _out)
  152. .AssertTrue (sd.Canceled)
  153. .LeftClick <Button> (b => b.Text == "►►")
  154. .ScreenShot ("After pop tree", _out)
  155. .Focus <TreeView<IFileSystemInfo>> (_ => true)
  156. .Right ()
  157. .ScreenShot ("After expand tree", _out)
  158. .Down ()
  159. .ScreenShot ("After navigate down in tree", _out)
  160. .Enter ()
  161. .WaitIteration ()
  162. .AssertFalse (sd.Canceled)
  163. .AssertContains ("empty-dir", sd.FileName)
  164. .WriteOutLogs (_out)
  165. .Stop ();
  166. Assert.False (sd.Canceled);
  167. }
  168. }