Pos.ViewTests.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using UnitTests;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.Pos;
  4. namespace Terminal.Gui.LayoutTests;
  5. public class PosViewTests (ITestOutputHelper output)
  6. {
  7. private readonly ITestOutputHelper _output = output;
  8. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  9. // TODO: A new test that calls SetRelativeLayout directly is needed.
  10. [Fact]
  11. [TestRespondersDisposed]
  12. public void Subtract_Operator ()
  13. {
  14. Application.Init (new FakeDriver ());
  15. var top = new Toplevel ();
  16. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  17. var field = new TextField { X = 0, Y = 0, Width = 20 };
  18. var count = 20;
  19. List<View> listViews = new ();
  20. for (var i = 0; i < count; i++)
  21. {
  22. field.Text = $"View {i}";
  23. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  24. view.Add (view2);
  25. Assert.Equal ($"View {i}", view2.Text);
  26. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  27. listViews.Add (view2);
  28. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  29. field.Y += 1;
  30. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  31. }
  32. field.KeyDown += (s, k) =>
  33. {
  34. if (k.KeyCode == KeyCode.Enter)
  35. {
  36. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  37. view.Remove (listViews [count - 1]);
  38. listViews [count - 1].Dispose ();
  39. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  40. field.Y -= 1;
  41. count--;
  42. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  43. }
  44. };
  45. Application.Iteration += (s, a) =>
  46. {
  47. while (count > 0)
  48. {
  49. field.NewKeyDownEvent (new (KeyCode.Enter));
  50. }
  51. Application.RequestStop ();
  52. };
  53. var win = new Window ();
  54. win.Add (view);
  55. win.Add (field);
  56. top.Add (win);
  57. Application.Run (top);
  58. top.Dispose ();
  59. Assert.Equal (0, count);
  60. // Shutdown must be called to safely clean up Application if Init has been called
  61. Application.Shutdown ();
  62. }
  63. }