FullLayoutTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using Moq;
  7. using Avalonia.Controls;
  8. using Avalonia.Controls.Presenters;
  9. using Avalonia.Controls.Primitives;
  10. using Avalonia.Controls.Templates;
  11. using Avalonia.Controls.UnitTests;
  12. using Avalonia.Controls.UnitTests.Primitives;
  13. using Avalonia.Diagnostics;
  14. using Avalonia.Input;
  15. using Avalonia.Platform;
  16. using Avalonia.Rendering;
  17. using Avalonia.Shared.PlatformSupport;
  18. using Avalonia.Styling;
  19. using Avalonia.Themes.Default;
  20. using Avalonia.VisualTree;
  21. using Ploeh.AutoFixture;
  22. using Ploeh.AutoFixture.AutoMoq;
  23. using Xunit;
  24. namespace Avalonia.Layout.UnitTests
  25. {
  26. public class FullLayoutTests
  27. {
  28. [Fact]
  29. public void Grandchild_Size_Changed()
  30. {
  31. using (var context = AvaloniaLocator.EnterScope())
  32. {
  33. RegisterServices();
  34. Border border;
  35. TextBlock textBlock;
  36. var window = new Window()
  37. {
  38. SizeToContent = SizeToContent.WidthAndHeight,
  39. Content = border = new Border
  40. {
  41. HorizontalAlignment = HorizontalAlignment.Center,
  42. VerticalAlignment = VerticalAlignment.Center,
  43. Child = new Border
  44. {
  45. Child = textBlock = new TextBlock
  46. {
  47. Width = 400,
  48. Height = 400,
  49. Text = "Hello World!",
  50. },
  51. }
  52. }
  53. };
  54. window.Show();
  55. LayoutManager.Instance.ExecuteInitialLayoutPass(window);
  56. Assert.Equal(new Size(400, 400), border.Bounds.Size);
  57. textBlock.Width = 200;
  58. LayoutManager.Instance.ExecuteLayoutPass();
  59. Assert.Equal(new Size(200, 400), border.Bounds.Size);
  60. }
  61. }
  62. [Fact]
  63. public void Test_ScrollViewer_With_TextBlock()
  64. {
  65. using (var context = AvaloniaLocator.EnterScope())
  66. {
  67. RegisterServices();
  68. ScrollViewer scrollViewer;
  69. TextBlock textBlock;
  70. var window = new Window()
  71. {
  72. Width = 800,
  73. Height = 600,
  74. SizeToContent = SizeToContent.WidthAndHeight,
  75. Content = scrollViewer = new ScrollViewer
  76. {
  77. Width = 200,
  78. Height = 200,
  79. CanScrollHorizontally = true,
  80. HorizontalAlignment = HorizontalAlignment.Center,
  81. VerticalAlignment = VerticalAlignment.Center,
  82. Content = textBlock = new TextBlock
  83. {
  84. Width = 400,
  85. Height = 400,
  86. Text = "Hello World!",
  87. },
  88. }
  89. };
  90. window.Show();
  91. LayoutManager.Instance.ExecuteInitialLayoutPass(window);
  92. Assert.Equal(new Size(800, 600), window.Bounds.Size);
  93. Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size);
  94. Assert.Equal(new Point(300, 200), Position(scrollViewer));
  95. Assert.Equal(new Size(400, 400), textBlock.Bounds.Size);
  96. var scrollBars = scrollViewer.GetTemplateChildren().OfType<ScrollBar>().ToList();
  97. var presenters = scrollViewer.GetTemplateChildren().OfType<ScrollContentPresenter>().ToList();
  98. Assert.Equal(2, scrollBars.Count);
  99. Assert.Equal(1, presenters.Count);
  100. var presenter = presenters[0];
  101. Assert.Equal(new Size(190, 190), presenter.Bounds.Size);
  102. var horzScroll = scrollBars.Single(x => x.Orientation == Orientation.Horizontal);
  103. var vertScroll = scrollBars.Single(x => x.Orientation == Orientation.Vertical);
  104. Assert.True(horzScroll.IsVisible);
  105. Assert.True(vertScroll.IsVisible);
  106. Assert.Equal(new Size(190, 10), horzScroll.Bounds.Size);
  107. Assert.Equal(new Size(10, 190), vertScroll.Bounds.Size);
  108. Assert.Equal(new Point(0, 190), Position(horzScroll));
  109. Assert.Equal(new Point(190, 0), Position(vertScroll));
  110. }
  111. }
  112. private static Point Position(IVisual v)
  113. {
  114. return v.Bounds.Position;
  115. }
  116. private void RegisterServices()
  117. {
  118. var fixture = new Fixture().Customize(new AutoMoqCustomization());
  119. var formattedText = fixture.Create<IFormattedTextImpl>();
  120. var globalStyles = new Mock<IGlobalStyles>();
  121. var renderInterface = fixture.Create<IPlatformRenderInterface>();
  122. var windowImpl = new Mock<IWindowImpl>();
  123. windowImpl.SetupProperty(x => x.ClientSize);
  124. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1024, 1024));
  125. windowImpl.SetupGet(x => x.Scaling).Returns(1);
  126. AvaloniaLocator.CurrentMutable
  127. .Bind<IAssetLoader>().ToConstant(new AssetLoader())
  128. .Bind<IInputManager>().ToConstant(new Mock<IInputManager>().Object)
  129. .Bind<IGlobalStyles>().ToConstant(globalStyles.Object)
  130. .Bind<ILayoutManager>().ToConstant(new LayoutManager())
  131. .Bind<IRuntimePlatform>().ToConstant(new AppBuilder().RuntimePlatform)
  132. .Bind<IPlatformRenderInterface>().ToConstant(renderInterface)
  133. .Bind<IStyler>().ToConstant(new Styler())
  134. .Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock(() => windowImpl.Object));
  135. var theme = new DefaultTheme();
  136. globalStyles.Setup(x => x.Styles).Returns(theme);
  137. }
  138. }
  139. }