FileDialogFluentTests.cs 7.5 KB

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