FullLayoutTests.cs 8.0 KB

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