Pos.ViewTests.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #nullable enable
  2. using JetBrains.Annotations;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.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. [SetupFakeApplication]
  12. public void Subtract_Operator ()
  13. {
  14. var top = new Runnable ();
  15. var view = new View { X = 0, Y = 0, Width = 20, Height = 20 };
  16. var field = new TextField { X = 0, Y = 0, Width = 20 };
  17. var count = 20;
  18. List<View> listViews = new ();
  19. for (var i = 0; i < count; i++)
  20. {
  21. field.Text = $"View {i}";
  22. var view2 = new View { X = 0, Y = field.Y, Width = 20, Text = field.Text };
  23. view.Add (view2);
  24. Assert.Equal ($"View {i}", view2.Text);
  25. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  26. listViews.Add (view2);
  27. Assert.Equal ($"Absolute({i})", field.Y.ToString ());
  28. field.Y += 1;
  29. Assert.Equal ($"Absolute({i + 1})", field.Y.ToString ());
  30. }
  31. field.KeyDown += (s, k) =>
  32. {
  33. if (k.KeyCode == KeyCode.Enter)
  34. {
  35. Assert.Equal ($"View {count - 1}", listViews [count - 1].Text);
  36. view.Remove (listViews [count - 1]);
  37. listViews [count - 1].Dispose ();
  38. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  39. field.Y -= 1;
  40. count--;
  41. Assert.Equal ($"Absolute({count})", field.Y.ToString ());
  42. }
  43. };
  44. Application.Iteration += OnApplicationOnIteration;
  45. var win = new Window ();
  46. win.Add (view);
  47. win.Add (field);
  48. top.Add (win);
  49. Application.Run (top);
  50. Application.Iteration -= OnApplicationOnIteration;
  51. top.Dispose ();
  52. Assert.Equal (0, count);
  53. // Shutdown must be called to safely clean up Application if Init has been called
  54. Application.Shutdown ();
  55. return;
  56. void OnApplicationOnIteration (object? s, EventArgs<IApplication?> a)
  57. {
  58. while (count > 0)
  59. {
  60. field.NewKeyDownEvent (new (KeyCode.Enter));
  61. }
  62. Application.RequestStop ();
  63. }
  64. }
  65. }