FullLayoutTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Diagnostics;
  12. using Avalonia.Input;
  13. using Avalonia.Platform;
  14. using Avalonia.Rendering;
  15. using Avalonia.Shared.PlatformSupport;
  16. using Avalonia.Styling;
  17. using Avalonia.Themes.Default;
  18. using Avalonia.VisualTree;
  19. using Xunit;
  20. using Avalonia.Media;
  21. using System;
  22. using System.Collections.Generic;
  23. namespace Avalonia.Layout.UnitTests
  24. {
  25. public class FullLayoutTests
  26. {
  27. [Fact]
  28. public void Grandchild_Size_Changed()
  29. {
  30. using (var context = AvaloniaLocator.EnterScope())
  31. {
  32. RegisterServices();
  33. Border border;
  34. TextBlock textBlock;
  35. var window = new Window()
  36. {
  37. SizeToContent = SizeToContent.WidthAndHeight,
  38. Content = border = new Border
  39. {
  40. HorizontalAlignment = HorizontalAlignment.Center,
  41. VerticalAlignment = VerticalAlignment.Center,
  42. Child = new Border
  43. {
  44. Child = textBlock = new TextBlock
  45. {
  46. Width = 400,
  47. Height = 400,
  48. Text = "Hello World!",
  49. },
  50. }
  51. }
  52. };
  53. LayoutManager.Instance.ExecuteInitialLayoutPass(window);
  54. Assert.Equal(new Size(400, 400), border.Bounds.Size);
  55. textBlock.Width = 200;
  56. LayoutManager.Instance.ExecuteLayoutPass();
  57. Assert.Equal(new Size(200, 400), border.Bounds.Size);
  58. }
  59. }
  60. [Fact]
  61. public void Test_ScrollViewer_With_TextBlock()
  62. {
  63. using (var context = AvaloniaLocator.EnterScope())
  64. {
  65. RegisterServices();
  66. ScrollViewer scrollViewer;
  67. TextBlock textBlock;
  68. var window = new Window()
  69. {
  70. Width = 800,
  71. Height = 600,
  72. SizeToContent = SizeToContent.WidthAndHeight,
  73. Content = scrollViewer = new ScrollViewer
  74. {
  75. Width = 200,
  76. Height = 200,
  77. CanScrollHorizontally = true,
  78. HorizontalAlignment = HorizontalAlignment.Center,
  79. VerticalAlignment = VerticalAlignment.Center,
  80. Content = textBlock = new TextBlock
  81. {
  82. Width = 400,
  83. Height = 400,
  84. Text = "Hello World!",
  85. },
  86. }
  87. };
  88. LayoutManager.Instance.ExecuteInitialLayoutPass(window);
  89. Assert.Equal(new Size(800, 600), window.Bounds.Size);
  90. Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size);
  91. Assert.Equal(new Point(300, 200), Position(scrollViewer));
  92. Assert.Equal(new Size(400, 400), textBlock.Bounds.Size);
  93. var scrollBars = scrollViewer.GetTemplateChildren().OfType<ScrollBar>().ToList();
  94. var presenters = scrollViewer.GetTemplateChildren().OfType<ScrollContentPresenter>().ToList();
  95. Assert.Equal(2, scrollBars.Count);
  96. Assert.Equal(1, presenters.Count);
  97. var presenter = presenters[0];
  98. Assert.Equal(new Size(190, 190), presenter.Bounds.Size);
  99. var horzScroll = scrollBars.Single(x => x.Orientation == Orientation.Horizontal);
  100. var vertScroll = scrollBars.Single(x => x.Orientation == Orientation.Vertical);
  101. Assert.True(horzScroll.IsVisible);
  102. Assert.True(vertScroll.IsVisible);
  103. Assert.Equal(new Size(190, 10), horzScroll.Bounds.Size);
  104. Assert.Equal(new Size(10, 190), vertScroll.Bounds.Size);
  105. Assert.Equal(new Point(0, 190), Position(horzScroll));
  106. Assert.Equal(new Point(190, 0), Position(vertScroll));
  107. }
  108. }
  109. private static Point Position(IVisual v)
  110. {
  111. return v.Bounds.Position;
  112. }
  113. class FormattedTextMock : IFormattedTextImpl
  114. {
  115. public FormattedTextMock(string text)
  116. {
  117. Text = text;
  118. }
  119. public Size Constraint { get; set; }
  120. public string Text { get; }
  121. public void Dispose()
  122. {
  123. }
  124. public IEnumerable<FormattedTextLine> GetLines() => new FormattedTextLine[0];
  125. public TextHitTestResult HitTestPoint(Point point) => new TextHitTestResult();
  126. public Rect HitTestTextPosition(int index) => new Rect();
  127. public IEnumerable<Rect> HitTestTextRange(int index, int length) => new Rect[0];
  128. public Size Measure() => Constraint;
  129. public void SetForegroundBrush(IBrush brush, int startIndex, int length)
  130. {
  131. }
  132. }
  133. private void RegisterServices()
  134. {
  135. var globalStyles = new Mock<IGlobalStyles>();
  136. var renderInterface = new Mock<IPlatformRenderInterface>();
  137. renderInterface.Setup(x => x.CreateFormattedText(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<double>(), It.IsAny<FontStyle>(),
  138. It.IsAny<TextAlignment>(), It.IsAny<FontWeight>(), It.IsAny<TextWrapping>()))
  139. .Returns(new FormattedTextMock("TEST"));
  140. var windowImpl = new Mock<IWindowImpl>();
  141. Size clientSize = default(Size);
  142. windowImpl.SetupGet(x => x.ClientSize).Returns(() => clientSize);
  143. windowImpl.Setup(x => x.Resize(It.IsAny<Size>())).Callback<Size>(s => clientSize = s);
  144. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1024, 1024));
  145. windowImpl.SetupGet(x => x.Scaling).Returns(1);
  146. AvaloniaLocator.CurrentMutable
  147. .Bind<IAssetLoader>().ToConstant(new AssetLoader())
  148. .Bind<IInputManager>().ToConstant(new Mock<IInputManager>().Object)
  149. .Bind<IGlobalStyles>().ToConstant(globalStyles.Object)
  150. .Bind<ILayoutManager>().ToConstant(new LayoutManager())
  151. .Bind<IRuntimePlatform>().ToConstant(new AppBuilder().RuntimePlatform)
  152. .Bind<IPlatformRenderInterface>().ToConstant(renderInterface.Object)
  153. .Bind<IStyler>().ToConstant(new Styler())
  154. .Bind<IWindowingPlatform>().ToConstant(new Avalonia.Controls.UnitTests.WindowingPlatformMock(() => windowImpl.Object));
  155. var theme = new DefaultTheme();
  156. globalStyles.Setup(x => x.Styles).Returns(theme);
  157. }
  158. }
  159. }