LoadedTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Avalonia.Platform;
  2. using Avalonia.Threading;
  3. using Avalonia.UnitTests;
  4. using Moq;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests;
  7. public class LoadedTests : ScopedTestBase
  8. {
  9. [Fact]
  10. public void Window_Loads_And_Unloads()
  11. {
  12. // Some other tests are populating the queue and are not resetting the dispatcher, so we need to purge it
  13. Control.ResetLoadedQueueForUnitTests();
  14. using (UnitTestApplication.Start(TestServices.StyledWindow))
  15. {
  16. int loadedCount = 0, unloadedCount = 0;
  17. var target = new Window();
  18. target.Loaded += (_, _) => loadedCount++;
  19. target.Unloaded += (_, _) => unloadedCount++;
  20. Assert.Equal(0, loadedCount);
  21. Assert.Equal(0, unloadedCount);
  22. target.Show();
  23. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  24. Assert.True(target.IsLoaded);
  25. Assert.Equal(1, loadedCount);
  26. Assert.Equal(0, unloadedCount);
  27. target.Close();
  28. Assert.Equal(1, loadedCount);
  29. Assert.Equal(1, unloadedCount);
  30. Assert.False(target.IsLoaded);
  31. }
  32. }
  33. [Fact]
  34. public void Control_Loads_And_Unloads()
  35. {
  36. // Some other tests are populating the queue and are not resetting the dispatcher, so we need to purge it
  37. Control.ResetLoadedQueueForUnitTests();
  38. using (UnitTestApplication.Start(TestServices.StyledWindow))
  39. {
  40. int loadedCount = 0, unloadedCount = 0;
  41. var window = new Window();
  42. window.Show();
  43. var target = new Button();
  44. target.Loaded += (_, _) => loadedCount++;
  45. target.Unloaded += (_, _) => unloadedCount++;
  46. Assert.Equal(0, loadedCount);
  47. Assert.Equal(0, unloadedCount);
  48. window.Content = target;
  49. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  50. Assert.True(target.IsLoaded);
  51. Assert.Equal(1, loadedCount);
  52. Assert.Equal(0, unloadedCount);
  53. window.Content = null;
  54. Assert.Equal(1, loadedCount);
  55. Assert.Equal(1, unloadedCount);
  56. Assert.False(target.IsLoaded);
  57. }
  58. }
  59. [Fact]
  60. public void Loaded_Should_Not_Be_Raised_If_Detached_From_Visual_Tree()
  61. {
  62. using var app = UnitTestApplication.Start(TestServices.StyledWindow);
  63. var loadedCount = 0;
  64. var unloadedCount = 0;
  65. var window = new Window();
  66. window.Show();
  67. var target = new Button();
  68. target.Loaded += (_, _) => loadedCount++;
  69. target.Unloaded += (_, _) => unloadedCount++;
  70. Assert.Equal(0, loadedCount);
  71. Assert.Equal(0, unloadedCount);
  72. // Attach to, then immediately detach from the visual tree.
  73. window.Content = target;
  74. window.Content = null;
  75. // Attach to another logical parent (this can actually happen outside tests with overlay popups)
  76. ((ISetLogicalParent) target).SetParent(new Window());
  77. Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded);
  78. // At this point, the control shouldn't have been loaded at all.
  79. Assert.Null(target.VisualParent);
  80. Assert.False(target.IsLoaded);
  81. Assert.Equal(0, loadedCount);
  82. Assert.Equal(0, unloadedCount);
  83. }
  84. }