FullLayoutTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) The Perspex 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 Perspex.Controls;
  8. using Perspex.Controls.Presenters;
  9. using Perspex.Controls.Primitives;
  10. using Perspex.Controls.Templates;
  11. using Perspex.Controls.UnitTests;
  12. using Perspex.Controls.UnitTests.Primitives;
  13. using Perspex.Diagnostics;
  14. using Perspex.Input;
  15. using Perspex.Platform;
  16. using Perspex.Rendering;
  17. using Perspex.Shared.PlatformSupport;
  18. using Perspex.Styling;
  19. using Perspex.Themes.Default;
  20. using Ploeh.AutoFixture;
  21. using Ploeh.AutoFixture.AutoMoq;
  22. using Xunit;
  23. namespace Perspex.Layout.UnitTests
  24. {
  25. public class FullLayoutTests
  26. {
  27. [Fact]
  28. public void Grandchild_Size_Changed()
  29. {
  30. using (var context = PerspexLocator.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 = PerspexLocator.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. private void RegisterServices()
  114. {
  115. var fixture = new Fixture().Customize(new AutoMoqCustomization());
  116. var formattedText = fixture.Create<IFormattedTextImpl>();
  117. var globalStyles = new Mock<IGlobalStyles>();
  118. var renderInterface = fixture.Create<IPlatformRenderInterface>();
  119. var renderManager = fixture.Create<IRenderQueueManager>();
  120. var windowImpl = new Mock<IWindowImpl>();
  121. windowImpl.SetupProperty(x => x.ClientSize);
  122. windowImpl.Setup(x => x.MaxClientSize).Returns(new Size(1024, 1024));
  123. PerspexLocator.CurrentMutable
  124. .Bind<IAssetLoader>().ToConstant(new AssetLoader())
  125. .Bind<IInputManager>().ToConstant(new Mock<IInputManager>().Object)
  126. .Bind<IGlobalStyles>().ToConstant(globalStyles.Object)
  127. .Bind<ILayoutManager>().ToConstant(new LayoutManager())
  128. .Bind<IPclPlatformWrapper>().ToConstant(new PclPlatformWrapper())
  129. .Bind<IPlatformRenderInterface>().ToConstant(renderInterface)
  130. .Bind<IRenderQueueManager>().ToConstant(renderManager)
  131. .Bind<IStyler>().ToConstant(new Styler())
  132. .Bind<IWindowingPlatform>().ToConstant(new WindowingPlatformMock(() => windowImpl.Object));
  133. var theme = new DefaultTheme();
  134. globalStyles.Setup(x => x.Styles).Returns(theme);
  135. }
  136. }
  137. }