Browse Source

Improve Layout tests

Marcin Ziąbek 8 months ago
parent
commit
b4a696dde9

+ 119 - 0
Source/QuestPDF.LayoutTests/ColumnTests.cs

@@ -41,4 +41,123 @@ public class ColumnTests
                     });
             });
     }
+
+    [Test]
+    public void SingleItem()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(100, 100)
+            .WithContent(content =>
+            {
+                content.Shrink().Column(column =>
+                {
+                    column.Spacing(10);
+                    column.Item().Mock("a").Size(50, 30);
+                });
+            })
+            .ExpectedDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(50, 30)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 0).Size(50, 30);
+                    });
+            });
+    }
+
+    [Test]
+    public void ZeroHeightItemDoesNotConsumeSpacing()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(100, 100)
+            .WithContent(content =>
+            {
+                content.Shrink().Column(column =>
+                {
+                    column.Spacing(10);
+                    
+                    column.Item().Mock("a").Size(50, 30);
+                    column.Item().Mock("b").Size(50, 0);
+                    column.Item().Mock("c").Size(70, 20);
+                });
+            })
+            .ExpectedDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(70, 60)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 0).Size(70, 30);
+                        page.Mock("b").Position(0, 30).Size(70, 0);
+                        page.Mock("c").Position(0, 40).Size(70, 20);
+                    });
+            });
+    }
+
+    [Test]
+    public void NoSpacing()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(100, 100)
+            .WithContent(content =>
+            {
+                content.Shrink().Column(column =>
+                {
+                    column.Spacing(0);
+                    
+                    column.Item().Mock("a").Size(50, 30);
+                    column.Item().Mock("b").Size(40, 20);
+                });
+            })
+            .ExpectedDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(50, 50)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 0).Size(50, 30);
+                        page.Mock("b").Position(0, 30).Size(50, 20);
+                    });
+            });
+    }
+
+    [Test]
+    public void PartialRenderItem()
+    {
+        LayoutTest
+            .HavingSpaceOfSize(100, 80)
+            .WithContent(content =>
+            {
+                content.Shrink().Column(column =>
+                {
+                    column.Spacing(5);
+                    
+                    column.Item().Mock("a").Size(50, 40);
+                    column.Item().Mock("b").Size(60, 50);
+                });
+            })
+            .ExpectedDrawResult(document =>
+            {
+                document
+                    .Page()
+                    .RequiredAreaSize(60, 80)
+                    .Content(page =>
+                    {
+                        page.Mock("a").Position(0, 0).Size(60, 40);
+                        page.Mock("b").Position(0, 45).Size(60, 35);
+                    });
+                
+                document
+                    .Page()
+                    .RequiredAreaSize(60, 15)
+                    .Content(page =>
+                    {
+                        page.Mock("b").Position(0, 0).Size(60, 15);
+                    });
+            });
+    }
 }

+ 2 - 1
Source/QuestPDF.LayoutTests/TestEngine/LayoutTestExecutor.cs

@@ -11,6 +11,7 @@ internal static class LayoutTestExecutor
     public static LayoutTestResult.DocumentLayout Execute(Size pageSize, Container container)
     {
         var (pageSizes, generatesInfiniteLayout) = GenerateDocument();
+        container.ReleaseDisposableChildren();
 
         return new LayoutTestResult.DocumentLayout
         {
@@ -24,7 +25,7 @@ internal static class LayoutTestExecutor
             var pageContext = new PageContext();
             pageContext.ProceedToNextRenderingPhase();
 
-            var canvas = new CompanionCanvas();
+            using var canvas = new CompanionCanvas();
         
             container.InjectDependencies(pageContext, canvas);
         

+ 2 - 0
Source/QuestPDF/Drawing/CompanionCanvas.cs

@@ -81,6 +81,8 @@ namespace QuestPDF.Drawing
 
             PictureRecorder?.Dispose();
             PictureRecorder = null;
+            
+            Canvas?.Dispose();
         }
 
         public override void EndDocument() { }