ViewDisposalTest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using SixLabors.ImageSharp.Processing.Processors.Quantization;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Terminal.Gui;
  7. using Xunit;
  8. using Xunit.Abstractions;
  9. namespace Terminal.Gui.ViewTests {
  10. public class ViewDisposalTest {
  11. #nullable enable
  12. Dictionary<Type, object? []?> special_params = new Dictionary<Type, object? []?> ();
  13. #nullable restore
  14. readonly ITestOutputHelper output;
  15. public ViewDisposalTest (ITestOutputHelper output)
  16. {
  17. {
  18. this.output = output;
  19. }
  20. }
  21. [Theory]
  22. [InlineData (true)]
  23. [InlineData (false)]
  24. public void TestViewsDisposeCorrectly (bool callShutdown)
  25. {
  26. var refs = DoTest (callShutdown);
  27. //var reference = refs [0];
  28. for (var i = 0; i < 10 && refs [0].IsAlive; i++) {
  29. GC.Collect ();
  30. GC.WaitForPendingFinalizers ();
  31. }
  32. foreach (var reference in refs) {
  33. if (reference.IsAlive) {
  34. #if DEBUG_IDISPOSABLE
  35. Assert.True (((View)reference.Target).WasDisposed);
  36. #endif
  37. string alive = ""; // Instead of just checking the subviews of the container, we now iterate through a list
  38. foreach (var r in refs) { // of Weakreferences Referencing every View that was tested. This makes more sense because
  39. if (r.IsAlive) { // View.Dispose removes all of its subviews, wich is why View.Subviews is always empty
  40. if (r == refs [0]) { // after View.Dispose has run. Luckily I didnt discover any more bugs or this wouldv'e
  41. alive += "\n View (Container)"; // been a little bit annoying to find an answer for. Thanks to BDisp for listening to
  42. } // me and giving his best to help me fix this thing. If you take a look at the commit log
  43. alive += ",\n--"; // you will find that he did most of the work. -a-usr
  44. alive += r.Target.GetType ().Name;
  45. } // NOTE: DELETE BEFORE NEXT COMMIT
  46. }
  47. Assert.Fail ($"Some Views didnt get Garbage Collected: {alive}");
  48. }
  49. }
  50. if (!callShutdown) {
  51. Application.Shutdown ();
  52. }
  53. }
  54. void getSpecialParams ()
  55. {
  56. special_params.Clear ();
  57. //special_params.Add (typeof (LineView), new object [] { Orientation.Horizontal });
  58. }
  59. List<WeakReference> DoTest (bool callShutdown)
  60. {
  61. var driver = new FakeDriver ();
  62. Application.Init (driver, new FakeMainLoop (driver));
  63. getSpecialParams ();
  64. View Container = new View ();
  65. List<WeakReference> refs = new List<WeakReference> { new WeakReference (Container, true) };
  66. Container.Add (new View ());
  67. Toplevel top = new ();
  68. var state = Application.Begin (top);
  69. var views = GetViews ();
  70. foreach (var view in views) {
  71. View instance;
  72. //Create instance of view and add to container
  73. if (special_params.ContainsKey (view)) {
  74. instance = (View)Activator.CreateInstance (view, special_params [view]);
  75. } else {
  76. instance = (View)Activator.CreateInstance (view);
  77. }
  78. Assert.NotNull (instance);
  79. Container.Add (instance);
  80. refs.Add (new WeakReference (instance, true));
  81. output.WriteLine ($"Added instance of {view}!");
  82. }
  83. top.Add (Container);
  84. // make sure the application is doing to the views whatever its supposed to do to the views
  85. for (var i = 0; i < 100; i++) {
  86. Application.Refresh ();
  87. }
  88. top.Remove (Container);
  89. Application.End (state);
  90. Assert.True (refs.All (r => r.IsAlive));
  91. #if DEBUG_IDISPOSABLE
  92. Assert.True (top.WasDisposed);
  93. Assert.False (Container.WasDisposed);
  94. #endif
  95. Assert.Null (Application.Top);
  96. Container.Dispose ();
  97. #if DEBUG_IDISPOSABLE
  98. Assert.True (Container.WasDisposed);
  99. #endif
  100. if (callShutdown) {
  101. Application.Shutdown ();
  102. }
  103. return refs;
  104. }
  105. /// <summary>
  106. /// Get all types derived from <see cref="View"/> using reflection
  107. /// </summary>
  108. /// <returns></returns>
  109. List<Type> GetViews ()
  110. {
  111. List<Type> valid = new ();
  112. // Filter all types that can be instantiated, are public, arent generic, aren't the view type itself, but derive from view
  113. foreach (var type in Assembly.GetAssembly (typeof (View)).GetTypes ().Where (T => { //body of anonymous check function
  114. return ((!T.IsAbstract) && T.IsPublic && T.IsClass && T.IsAssignableTo (typeof (View)) && !T.IsGenericType && !(T == typeof (View)));
  115. })) //end of body of anonymous check function
  116. { //body of the foreach loop
  117. output.WriteLine ($"Found Type {type.Name}");
  118. Assert.DoesNotContain (type, valid);
  119. Assert.True (type.IsAssignableTo (typeof (IDisposable)));// Just to be safe
  120. valid.Add (type);
  121. output.WriteLine (" -Added!");
  122. } //end body of foreach loop
  123. return valid;
  124. }
  125. }
  126. }