WindowBaseTests.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Reactive;
  5. using System.Reactive.Subjects;
  6. using Moq;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Input;
  10. using Avalonia.Input.Raw;
  11. using Avalonia.Layout;
  12. using Avalonia.Platform;
  13. using Avalonia.Rendering;
  14. using Avalonia.Styling;
  15. using Avalonia.UnitTests;
  16. using Ploeh.AutoFixture;
  17. using Ploeh.AutoFixture.AutoMoq;
  18. using Xunit;
  19. namespace Avalonia.Controls.UnitTests
  20. {
  21. public class WindowBaseTests
  22. {
  23. [Fact]
  24. public void Impl_ClientSize_Should_Be_Set_After_Layout_Pass()
  25. {
  26. using (UnitTestApplication.Start(TestServices.StyledWindow))
  27. {
  28. var impl = Mock.Of<IWindowBaseImpl>(x => x.Scaling == 1);
  29. Mock.Get(impl).Setup(x => x.Resize(It.IsAny<Size>())).Callback(() => { });
  30. var target = new TestWindowBase(impl)
  31. {
  32. Template = CreateTemplate(),
  33. Content = new TextBlock
  34. {
  35. Width = 321,
  36. Height = 432,
  37. },
  38. IsVisible = true,
  39. };
  40. LayoutManager.Instance.ExecuteInitialLayoutPass(target);
  41. Mock.Get(impl).Verify(x => x.Resize(new Size(321, 432)));
  42. }
  43. }
  44. [Fact]
  45. public void Activate_Should_Call_Impl_Activate()
  46. {
  47. using (UnitTestApplication.Start(TestServices.StyledWindow))
  48. {
  49. var impl = new Mock<IWindowBaseImpl>();
  50. var target = new TestWindowBase(impl.Object);
  51. target.Activate();
  52. impl.Verify(x => x.Activate());
  53. }
  54. }
  55. [Fact]
  56. public void Impl_Activate_Should_Call_Raise_Activated_Event()
  57. {
  58. using (UnitTestApplication.Start(TestServices.StyledWindow))
  59. {
  60. var impl = new Mock<IWindowBaseImpl>();
  61. impl.SetupAllProperties();
  62. bool raised = false;
  63. var target = new TestWindowBase(impl.Object);
  64. target.Activated += (s, e) => raised = true;
  65. impl.Object.Activated();
  66. Assert.True(raised);
  67. }
  68. }
  69. [Fact]
  70. public void Impl_Deactivate_Should_Call_Raise_Deativated_Event()
  71. {
  72. using (UnitTestApplication.Start(TestServices.StyledWindow))
  73. {
  74. var impl = new Mock<IWindowBaseImpl>();
  75. impl.SetupAllProperties();
  76. bool raised = false;
  77. var target = new TestWindowBase(impl.Object);
  78. target.Deactivated += (s, e) => raised = true;
  79. impl.Object.Deactivated();
  80. Assert.True(raised);
  81. }
  82. }
  83. private FuncControlTemplate<TestWindowBase> CreateTemplate()
  84. {
  85. return new FuncControlTemplate<TestWindowBase>(x =>
  86. new ContentPresenter
  87. {
  88. Name = "PART_ContentPresenter",
  89. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  90. });
  91. }
  92. private class TestWindowBase : WindowBase
  93. {
  94. public bool IsClosed { get; private set; }
  95. public TestWindowBase(IWindowBaseImpl impl)
  96. : base(impl)
  97. {
  98. }
  99. protected override void HandleApplicationExiting()
  100. {
  101. base.HandleApplicationExiting();
  102. IsClosed = true;
  103. }
  104. }
  105. }
  106. }