Browse Source

Layout test: improved lifecycle

Marcin Ziąbek 2 years ago
parent
commit
6234a7dc5b

+ 28 - 15
Source/QuestPDF.LayoutTests/TestEngine/LayoutTest.cs

@@ -1,3 +1,5 @@
+using System.Diagnostics;
+using System.Runtime.CompilerServices;
 using QuestPDF.Elements;
 using QuestPDF.Elements;
 using QuestPDF.Fluent;
 using QuestPDF.Fluent;
 using QuestPDF.Infrastructure;
 using QuestPDF.Infrastructure;
@@ -6,18 +8,26 @@ namespace QuestPDF.LayoutTests.TestEngine;
 
 
 internal sealed class LayoutTest
 internal sealed class LayoutTest
 {
 {
+    private string TestIdentifier { get; set; }
     private LayoutTestResult TestResult { get; } = new LayoutTestResult();
     private LayoutTestResult TestResult { get; } = new LayoutTestResult();
   
   
-    public static LayoutTest HavingSpaceOfSize(float width, float height)
+    public static LayoutTest HavingSpaceOfSize(float width, float height, [CallerMemberName] string testIdentifier = "test")
     {
     {
-        var result = new LayoutTest();
-        result.TestResult.PageSize = new Size(width, height);
-        return result;
+        var layoutTest = new LayoutTest
+        {
+            TestIdentifier = testIdentifier,
+            
+            TestResult =
+            {
+                PageSize = new Size(width, height)
+            }
+        };
+
+        return layoutTest;
     }
     }
 
 
     public LayoutTest WithContent(Action<IContainer> handler)
     public LayoutTest WithContent(Action<IContainer> handler)
     {
     {
-        // compose content
         var container = new Container();
         var container = new Container();
         container.Element(handler);
         container.Element(handler);
 
 
@@ -26,18 +36,26 @@ internal sealed class LayoutTest
         return this;
         return this;
     }
     }
 
 
-    public LayoutTest ExpectedDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)
+    public void ExpectedDrawResult(Action<ExpectedDocumentLayoutDescriptor> handler)
     {
     {
         var builder = new ExpectedDocumentLayoutDescriptor();
         var builder = new ExpectedDocumentLayoutDescriptor();
         handler(builder);
         handler(builder);
 
 
         TestResult.ExpectedLayout = builder.DocumentLayout;
         TestResult.ExpectedLayout = builder.DocumentLayout;
-        return this;
+
+        GenerateTestPreview();
+        LayoutTestValidator.Validate(TestResult);
     }
     }
 
 
-    public void CompareVisually()
+    private void GenerateTestPreview()
     {
     {
-        var path = "output.pdf";
+        if (!Debugger.IsAttached)
+        {
+            Console.WriteLine("Debugger is not attached. Skipping test preview generation");
+            return;
+        }
+        
+        var path = Path.Combine(Path.GetTempPath(), $"{TestIdentifier}.pdf");
         
         
         if (File.Exists(path))
         if (File.Exists(path))
             File.Delete(path);
             File.Delete(path);
@@ -46,11 +64,6 @@ internal sealed class LayoutTest
         LayoutTestResultVisualization.Visualize(TestResult, stream);
         LayoutTestResultVisualization.Visualize(TestResult, stream);
         stream.Dispose();
         stream.Dispose();
         
         
-        GenerateExtensions.OpenFileUsingDefaultProgram(path);
-    }
-
-    public void Validate()
-    {
-        LayoutTestValidator.Validate(TestResult);
+        Console.WriteLine($"Generated test case preview: {path}");
     }
     }
 }
 }

+ 16 - 14
Source/QuestPDF.LayoutTests/TestEngine/LayoutTestOutputVisualization.cs

@@ -20,26 +20,28 @@ internal static class LayoutTestResultVisualization
     // grid configuration
     // grid configuration
     private const float GridSize = 10;
     private const float GridSize = 10;
     private const float GridLineThickness = 0.25f;
     private const float GridLineThickness = 0.25f;
-    private const string GridLineColor = Colors.Grey.Darken3;
+    private const byte GridLineTransparency = 48;
     
     
     // mock drawing settings
     // mock drawing settings
     private const byte OccludedMockBorderThickness = 5;
     private const byte OccludedMockBorderThickness = 5;
 
 
     private static readonly string[] DefaultElementColors =
     private static readonly string[] DefaultElementColors =
     {
     {
-        Colors.Green.Medium,
-        Colors.Blue.Medium,
-        Colors.Orange.Medium,
-        Colors.Lime.Darken2,
-        Colors.Cyan.Darken2,
-        Colors.Indigo.Darken2,
+        Colors.DeepPurple.Lighten2,
+        Colors.Blue.Lighten2,
+        Colors.Cyan.Lighten2,
+        Colors.Green.Lighten2,
+        Colors.Lime.Lighten2,
+        Colors.Amber.Lighten2,
+        Colors.Brown.Lighten2,
         
         
-        Colors.Green.Lighten1,
-        Colors.Blue.Lighten1,
-        Colors.Orange.Lighten1,
-        Colors.Lime.Lighten1,
-        Colors.Cyan.Lighten1,
-        Colors.Indigo.Lighten1
+        Colors.DeepPurple.Medium,
+        Colors.Blue.Medium,
+        Colors.Cyan.Medium,
+        Colors.Green.Medium,
+        Colors.Lime.Medium,
+        Colors.Amber.Medium,
+        Colors.Brown.Medium,
     };
     };
     
     
     // implementations
     // implementations
@@ -194,7 +196,7 @@ internal static class LayoutTestResultVisualization
         {
         {
             using var paint = new SKPaint
             using var paint = new SKPaint
             {
             {
-                Color = SKColor.Parse(GridLineColor),
+                Color = SKColor.Parse(Colors.Black).WithAlpha(GridLineTransparency),
                 StrokeWidth = GridLineThickness
                 StrokeWidth = GridLineThickness
             };
             };