SubviewTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Xunit.Abstractions;
  2. namespace UnitTests.ViewTests;
  3. public class SubViewTests
  4. {
  5. private readonly ITestOutputHelper _output;
  6. public SubViewTests (ITestOutputHelper output) { _output = output; }
  7. // TODO: This is a poor unit tests. Not clear what it's testing. Refactor.
  8. [Fact]
  9. [AutoInitShutdown]
  10. public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically ()
  11. {
  12. var t = new Toplevel { Id = "0" };
  13. var w = new Window { Id = "t", Width = Dim.Fill (), Height = Dim.Fill () };
  14. var v1 = new View { Id = "v1", Width = Dim.Fill (), Height = Dim.Fill () };
  15. var v2 = new View { Id = "v2", Width = Dim.Fill (), Height = Dim.Fill () };
  16. int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
  17. t.Initialized += (s, e) =>
  18. {
  19. tc++;
  20. Assert.Equal (1, tc);
  21. Assert.Equal (1, wc);
  22. Assert.Equal (1, v1c);
  23. Assert.Equal (1, v2c);
  24. Assert.Equal (0, sv1c); // Added after t in the Application.Iteration.
  25. Assert.True (t.CanFocus);
  26. Assert.True (w.CanFocus);
  27. Assert.False (v1.CanFocus);
  28. Assert.False (v2.CanFocus);
  29. Application.LayoutAndDraw ();
  30. };
  31. w.Initialized += (s, e) =>
  32. {
  33. wc++;
  34. Assert.Equal (t.Viewport.Width, w.Frame.Width);
  35. Assert.Equal (t.Viewport.Height, w.Frame.Height);
  36. };
  37. v1.Initialized += (s, e) =>
  38. {
  39. v1c++;
  40. //Assert.Equal (t.Viewport.Width, v1.Frame.Width);
  41. //Assert.Equal (t.Viewport.Height, v1.Frame.Height);
  42. };
  43. v2.Initialized += (s, e) =>
  44. {
  45. v2c++;
  46. //Assert.Equal (t.Viewport.Width, v2.Frame.Width);
  47. //Assert.Equal (t.Viewport.Height, v2.Frame.Height);
  48. };
  49. w.Add (v1, v2);
  50. t.Add (w);
  51. Application.Iteration += OnApplicationOnIteration;
  52. Application.Run (t);
  53. Application.Iteration -= OnApplicationOnIteration;
  54. t.Dispose ();
  55. Application.Shutdown ();
  56. Assert.Equal (1, tc);
  57. Assert.Equal (1, wc);
  58. Assert.Equal (1, v1c);
  59. Assert.Equal (1, v2c);
  60. Assert.Equal (1, sv1c);
  61. Assert.True (t.CanFocus);
  62. Assert.True (w.CanFocus);
  63. Assert.False (v1.CanFocus);
  64. Assert.False (v2.CanFocus);
  65. return;
  66. void OnApplicationOnIteration (object s, IterationEventArgs a)
  67. {
  68. var sv1 = new View { Id = "sv1", Width = Dim.Fill (), Height = Dim.Fill () };
  69. sv1.Initialized += (s, e) =>
  70. {
  71. sv1c++;
  72. Assert.NotEqual (t.Frame.Width, sv1.Frame.Width);
  73. Assert.NotEqual (t.Frame.Height, sv1.Frame.Height);
  74. Assert.False (sv1.CanFocus);
  75. //Assert.Throws<InvalidOperationException> (() => sv1.CanFocus = true);
  76. Assert.False (sv1.CanFocus);
  77. };
  78. v1.Add (sv1);
  79. Application.LayoutAndDraw ();
  80. t.Running = false;
  81. }
  82. }
  83. }