Browse Source

Enhance the layout testing approach

Marcin Ziąbek 3 months ago
parent
commit
5fa39adf33

+ 12 - 12
Source/QuestPDF.LayoutTests/ShrinkTests.cs

@@ -11,7 +11,7 @@ public class ShrinkTests
             {
                 content
                     .Shrink()
-                    .Mock().ContinuousBlock(60, 200);
+                    .Mock("a").ContinuousBlock(60, 200);
             })
             .ExpectDrawResult(document =>
             {
@@ -20,7 +20,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 120)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(60, 120);
+                        page.Mock("a").Position(0, 0).Size(60, 120);
                     });
                 
                 document
@@ -28,7 +28,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 80)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(60, 80);
+                        page.Mock("a").Position(0, 0).Size(60, 80);
                     });
             });
     }
@@ -42,7 +42,7 @@ public class ShrinkTests
             {
                 content
                     .ShrinkVertical()
-                    .Mock().ContinuousBlock(60, 200);
+                    .Mock("a").ContinuousBlock(60, 200);
             })
             .ExpectDrawResult(document =>
             {
@@ -51,7 +51,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 120)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(100, 120);
+                        page.Mock("a").Position(0, 0).Size(100, 120);
                     });
                 
                 document
@@ -59,7 +59,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 80)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(100, 80);
+                        page.Mock("a").Position(0, 0).Size(100, 80);
                     });
             });
     }
@@ -73,7 +73,7 @@ public class ShrinkTests
             {
                 content
                     .ShrinkHorizontal()
-                    .Mock().ContinuousBlock(60, 200);
+                    .Mock("a").ContinuousBlock(60, 200);
             })
             .ExpectDrawResult(document =>
             {
@@ -82,7 +82,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 120)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(60, 120);
+                        page.Mock("a").Position(0, 0).Size(60, 120);
                     });
                 
                 document
@@ -90,7 +90,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 80)
                     .Content(page =>
                     {
-                        page.Mock().Position(0, 0).Size(60, 120);
+                        page.Mock("a").Position(0, 0).Size(60, 120);
                     });
             });
     }
@@ -105,7 +105,7 @@ public class ShrinkTests
                 content
                     .ContentFromRightToLeft()
                     .Shrink()
-                    .Mock().ContinuousBlock(60, 200);
+                    .Mock("a").ContinuousBlock(60, 200);
             })
             .ExpectDrawResult(document =>
             {
@@ -114,7 +114,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 120)
                     .Content(page =>
                     {
-                        page.Mock().Position(40, 0).Size(60, 120);
+                        page.Mock("a").Position(40, 0).Size(60, 120);
                     });
                 
                 document
@@ -122,7 +122,7 @@ public class ShrinkTests
                     .RequiredAreaSize(60, 80)
                     .Content(page =>
                     {
-                        page.Mock().Position(40, 0).Size(60, 80);
+                        page.Mock("a").Position(40, 0).Size(60, 80);
                     });
             });
     }

+ 18 - 5
Source/QuestPDF.LayoutTests/TestEngine/ContinuousBlock.cs

@@ -4,16 +4,14 @@ using QuestPDF.Skia;
 
 namespace QuestPDF.LayoutTests.TestEngine;
 
-internal class ContinuousBlock : Element
+internal class ContinuousBlock : Element, IStateful
 {
     public float TotalWidth { get; set; }
     public float TotalHeight { get; set; }
-    
-    private float HeightOffset { get; set; }
-    
+
     internal override SpacePlan Measure(Size availableSpace)
     {
-        if (TotalWidth > availableSpace.Width)
+        if (TotalWidth > availableSpace.Width + Size.Epsilon)
             return SpacePlan.Wrap("The content requires more horizontal space than available.");
 
         if (availableSpace.Height < Size.Epsilon)
@@ -44,4 +42,19 @@ internal class ContinuousBlock : Element
         if (HeightOffset > TotalHeight - Size.Epsilon)
             HeightOffset = 0;
     }
+    
+    #region IStateful
+        
+    private float HeightOffset { get; set; }
+
+    public void ResetState(bool hardReset = false)
+    {
+        if (hardReset)
+            HeightOffset = 0;
+    }
+        
+    public object GetState() => HeightOffset;
+    public void SetState(object state) => HeightOffset = (float) state;
+    
+    #endregion
 }

+ 2 - 0
Source/QuestPDF.LayoutTests/TestEngine/ElementObserver.cs

@@ -1,4 +1,6 @@
 using System.Diagnostics;
+using QuestPDF.Helpers;
+using QuestPDF.Skia;
 
 namespace QuestPDF.LayoutTests.TestEngine;
 

+ 14 - 3
Source/QuestPDF.LayoutTests/TestEngine/FluentExtensions.cs

@@ -1,3 +1,5 @@
+using QuestPDF.Helpers;
+
 namespace QuestPDF.LayoutTests.TestEngine;
 
 internal class ExpectedDocumentLayoutDescriptor(DrawingRecorder DrawingRecorder)
@@ -33,7 +35,7 @@ internal class ExpectedPageLayoutDescriptor(DrawingRecorder DrawingRecorder, int
 
 internal class ExpectedPageContentDescriptor(DrawingRecorder drawingRecorder, int CurrentPageNumber)
 {
-    public ExpectedMockPositionDescriptor Mock(string mockId = FluentExtensions.DefaultMockId)
+    public ExpectedMockPositionDescriptor Mock(string mockId)
     {
         var elementDrawingEvent = new ElementDrawingEvent
         {
@@ -65,7 +67,7 @@ internal static class FluentExtensions
 {
     public const string DefaultMockId = "$mock";
     
-    public static IContainer Mock(this IContainer element, string id = DefaultMockId)
+    public static IContainer Mock(this IContainer element, string id)
     {
         return element.Element(new ElementObserver
         {
@@ -86,7 +88,7 @@ internal static class FluentExtensions
         return element.Width(width).Height(height);
     }     
     
-    public static void ContinuousBlock(this IContainer element, float width, float height)
+    public static void ContinuousBlock(this IContainer element, float width = 1f, float height = 1f)
     {
         element.Element(new ContinuousBlock
         {
@@ -94,4 +96,13 @@ internal static class FluentExtensions
             TotalHeight = height
         });
     } 
+    
+    public static void SolidBlock(this IContainer element, float width = 1f, float height = 1f)
+    {
+        element.Element(new SolidBlock
+        {
+            TotalWidth = width, 
+            TotalHeight = height
+        });
+    } 
 }

+ 18 - 0
Source/QuestPDF.LayoutTests/TestEngine/LayoutTest.cs

@@ -1,5 +1,6 @@
 using System.Runtime.CompilerServices;
 using System.Text;
+using QuestPDF.Drawing.Exceptions;
 using QuestPDF.Elements;
 using QuestPDF.Helpers;
 
@@ -145,6 +146,23 @@ internal class LayoutTest
         }
     }
 
+    public void ExpectLayoutException()
+    {
+        try
+        {
+            QuestPDF.Settings.EnableDebugging = true;
+            PerformTest();
+        }
+        catch (DocumentLayoutException e)
+        {
+            Assert.Pass($"The expected exception was thrown: {e.Message}");
+        }
+        catch
+        {
+            Assert.Fail("Un expected exception was thrown.");
+        }
+    }
+
     private void PerformTest()
     {
         Document

+ 49 - 0
Source/QuestPDF.LayoutTests/TestEngine/SolidBlock.cs

@@ -0,0 +1,49 @@
+using QuestPDF.Drawing;
+using QuestPDF.Helpers;
+using QuestPDF.Skia;
+
+namespace QuestPDF.LayoutTests.TestEngine;
+
+internal class SolidBlock : Element, IStateful
+{
+    public float TotalWidth { get; set; } 
+    public float TotalHeight { get; set; }
+
+    internal override SpacePlan Measure(Size availableSpace)
+    {
+        if (IsRendered)
+            return SpacePlan.Empty();
+        
+        if (TotalWidth > availableSpace.Width + Size.Epsilon)
+            return SpacePlan.Wrap("The content requires more horizontal space than available.");
+
+        if (TotalHeight > availableSpace.Height + Size.Epsilon)
+            return SpacePlan.Wrap("The content requires more vertical space than available.");
+
+        return SpacePlan.FullRender(TotalWidth, TotalHeight);
+    }
+
+    internal override void Draw(Size availableSpace)
+    {
+        using var paint = new SkPaint();
+        paint.SetSolidColor(Colors.Grey.Medium);
+        Canvas.DrawRectangle(Position.Zero, availableSpace, paint);
+        
+        IsRendered = true;
+    }
+    
+    #region IStateful
+        
+    private bool IsRendered { get; set; }
+
+    public void ResetState(bool hardReset = false)
+    {
+        if (hardReset)
+            IsRendered = false;
+    }
+        
+    public object GetState() => IsRendered;
+    public void SetState(object state) => IsRendered = (bool) state;
+    
+    #endregion
+}