FullLayoutTests.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. window.Show();
  54. LayoutManager.Instance.ExecuteInitialLayoutPass(window);
  55. Assert.Equal(new Size(400, 400), border.Bounds.Size);
  56. textBlock.Width = 200;
  57. LayoutManager.Instance.ExecuteLayoutPass();
  58. Assert.Equal(new Size(200, 400), border.Bounds.Size);
  59. }
  60. }
  61. [Fact]
  62. public void Test_ScrollViewer_With_TextBlock()
  63. {
  64. using (var context = AvaloniaLocator.EnterScope())
  65. {
  66. RegisterServices();
  67. ScrollViewer scrollViewer;
  68. TextBlock textBlock;
  69. var window = new Window()
  70. {
  71. Width = 800,
  72. Height = 600,
  73. SizeToContent = SizeToContent.WidthAndHeight,
  74. Content = scrollViewer = new ScrollViewer
  75. {
  76. Width = 200,
  77. Height = 200,
  78. CanScrollHorizontally = true,
  79. HorizontalAlignment = HorizontalAlignment.Center,
  80. VerticalAlignment = VerticalAlignment.Center,
  81. Content = textBlock = new TextBlock
  82. {
  83. Width = 400,
  84. Height = 400,
  85. Text = "Hello World!",
  86. },
  87. }
  88. };
  89. window.Show();
  90. LayoutManager.Instance.ExecuteInitialLayoutPass(window);
  91. Assert.Equal(new Size(800, 600), window.Bounds.Size);
  92. Assert.Equal(new Size(200, 200), scrollViewer.Bounds.Size);
  93. Assert.Equal(new Point(300, 200), Position(scrollViewer));
  94. Assert.Equal(new Size(400, 400), textBlock.Bounds.Size);
  95. var scrollBars = scrollViewer.GetTemplateChildren().OfType<ScrollBar>().ToList();
  96. var presenters = scrollViewer.GetTemplateChildren().OfType<ScrollContentPresenter>().ToList();
  97. Assert.Equal(2, scrollBars.Count);
  98. Assert.Equal(1, presenters.Count);
  99. var presenter = presenters[0];
  100. Assert.Equal(new Size(190, 190), presenter.Bounds.Size);
  101. var horzScroll = scrollBars.Single(x => x.Orientation == Orientation.Horizontal);
  102. var vertScroll = scrollBars.Single(x => x.Orientation == Orientation.Vertical);
  103. Assert.True(horzScroll.IsVisible);
  104. Assert.True(vertScroll.IsVisible);
  105. Assert.Equal(new Size(190, 10), horzScroll.Bounds.Size);
  106. Assert.Equal(new Size(10, 190), vertScroll.Bounds.Size);
  107. Assert.Equal(new Point(0, 190), Position(horzScroll));
  108. Assert.Equal(new Point(190, 0), Position(vertScroll));
  109. }
  110. }
  111. private static Point Position(IVisual v)
  112. {
  113. return v.Bounds.Position;
  114. }
  115. class FormattedTextMock : IFormattedTextImpl
  116. {
  117. public FormattedTextMock(string text)
  118. {
  119. Text = text;
  120. }
  121. public Size Constraint { get; set; }
  122. public string Text { get; }
  123. public Size Size => new Size();
  124. public void Dispose()
  125. {
  126. }
  127. public IEnumerable<FormattedTextLine> GetLines() => new FormattedTextLine[0];
  128. public TextHitTestResult HitTestPoint(Point point) => new TextHitTestResult();
  129. public Rect HitTestTextPosition(int index) => new Rect();
  130. public IEnumerable<Rect> HitTestTextRange(int index, int length) => new Rect[0];
  131. public Size Measure() => Constraint;
  132. }
  133. private void RegisterServices()
  134. {
  135. var globalStyles = new Mock<IGlobalStyles>();
  136. var globalStylesResources = globalStyles.As<IResourceHost>();
  137. var outObj = (object)10;
  138. globalStylesResources.Setup(x => x.TryGetResource("FontSizeNormal", out outObj)).Returns(true);
  139. var renderInterface = new Mock<IPlatformRenderInterface>();
  140. renderInterface.Setup(x =>
  141. x.CreateFormattedText(
  142. It.IsAny<string>(),
  143. It.IsAny<Typeface>(),
  144. It.IsAny<TextAlignment>(),
  145. It.IsAny<TextWrapping>(),
  146. It.IsAny<Size>(),
  147. It.IsAny<IReadOnlyList<FormattedTextStyleSpan>>()))
  148. .Returns(new FormattedTextMock("TEST"));
  149. var windowImpl = new Mock<IWindowImpl>();
  150. Size clientSize = default(Size);
  151. windowImpl.SetupGet(x => x.ClientSize).Returns(() => clientSize);
  152. windowImpl.Setup(x => x.Resize(It.IsAny<Size>())).Callback<Size>(s => clientSize = s);
  153. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1024, 1024));
  154. windowImpl.SetupGet(x => x.Scaling).Returns(1);
  155. AvaloniaLocator.CurrentMutable
  156. .Bind<IAssetLoader>().ToConstant(new AssetLoader())
  157. .Bind<IInputManager>().ToConstant(new Mock<IInputManager>().Object)
  158. .Bind<IGlobalStyles>().ToConstant(globalStyles.Object)
  159. .Bind<ILayoutManager>().ToConstant(new LayoutManager())
  160. .Bind<IRuntimePlatform>().ToConstant(new AppBuilder().RuntimePlatform)
  161. .Bind<IPlatformRenderInterface>().ToConstant(renderInterface.Object)
  162. .Bind<IStyler>().ToConstant(new Styler())
  163. .Bind<IWindowingPlatform>().ToConstant(new Avalonia.Controls.UnitTests.WindowingPlatformMock(() => windowImpl.Object));
  164. var theme = new DefaultTheme();
  165. globalStyles.Setup(x => x.Styles).Returns(theme);
  166. }
  167. }
  168. }