1
0

LoadedTests.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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
  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. }