예제 #1
0
파일: ImageAssert.cs 프로젝트: jokm1/uno-2
        private static void AreEqualImpl(
            FileInfo expected,
            Rectangle expectedRect,
            FileInfo actual,
            Bitmap actualBitmap,
            Rectangle actualRect,
            double expectedToActualScale,
            PixelTolerance tolerance,
            int line)
        {
            if (expectedRect != FirstQuadrant && actualRect != FirstQuadrant)
            {
                Assert.AreEqual(expectedRect.Size, actualRect.Size, WithContext("Compare rects don't have the same size"));
            }

            using (var expectedBitmap = new Bitmap(expected.FullName))
            {
                if (expectedRect == FirstQuadrant && actualRect == FirstQuadrant)
                {
                    var effectiveExpectedBitmapSize = new Size(
                        (int)(expectedBitmap.Size.Width * expectedToActualScale),
                        (int)(expectedBitmap.Size.Height * expectedToActualScale));
                    Assert.AreEqual(effectiveExpectedBitmapSize, actualBitmap.Size, WithContext("Screenshots don't have the same size"));
                }

                expectedRect = Normalize(expectedRect, expectedBitmap.Size);
                actualRect   = Normalize(actualRect, actualBitmap.Size);

                var expectedPixels = ExpectedPixels
                                     .At(actualRect.Location)
                                     .Pixels(expectedBitmap, expectedRect)
                                     .Named(expected.Name)
                                     .WithTolerance(tolerance);

                var report = GetContext();
                if (Validate(expectedPixels, actualBitmap, expectedToActualScale, report))
                {
                    Console.WriteLine(report.ToString());
                }
                else
                {
                    Assert.Fail(report.ToString());
                }
            }

            StringBuilder GetContext()
            => new StringBuilder()
            .AppendLine($"ImageAssert.AreEqual @ line {line}")
            .AppendLine("pixelTolerance: " + tolerance)
            .AppendLine("expected: " + expected?.Name + (expectedRect == FirstQuadrant ? null : $" in {expectedRect}"))
            .AppendLine("actual  : " + (actual?.Name ?? "--unknown--") + (actualRect == FirstQuadrant ? null : $" in {actualRect}"))
            .AppendLine("====================");

            string WithContext(string message)
            => GetContext()
            .AppendLine(message)
            .ToString();
        }
예제 #2
0
        private static (bool areEqual, string context) EqualityCheck(
            ScreenshotInfo expected,
            Rectangle expectedRect,
            ScreenshotInfo actual,
            Bitmap actualBitmap,
            Rectangle actualRect,
            double expectedToActualScale,
            PixelTolerance tolerance,
            [CallerLineNumber] int line = 0)
        {
            if (expectedRect != FirstQuadrant && actualRect != FirstQuadrant)
            {
                Assert.AreEqual(expectedRect.Size, actualRect.Size, WithContext("Compare rects don't have the same size"));
            }

            using (var expectedBitmap = new Bitmap(expected.File.FullName))
            {
                if (expectedRect == FirstQuadrant && actualRect == FirstQuadrant)
                {
                    var effectiveExpectedBitmapSize = new Size(
                        (int)(expectedBitmap.Size.Width * expectedToActualScale),
                        (int)(expectedBitmap.Size.Height * expectedToActualScale));
                    Assert.AreEqual(effectiveExpectedBitmapSize, actualBitmap.Size, WithContext("Screenshots don't have the same size"));
                }

                expectedRect = Normalize(expectedRect, expectedBitmap.Size);
                actualRect   = Normalize(actualRect, actualBitmap.Size);

                var expectedPixels = ExpectedPixels
                                     .At(actualRect.Location)
                                     .Pixels(expectedBitmap, expectedRect)
                                     .Named(expected.StepName)
                                     .WithTolerance(tolerance);

                var report = GetContext();
                var result = Validate(expectedPixels, actualBitmap, expectedToActualScale, report);

                return(result, report.ToString());
            }

            StringBuilder GetContext()
            => new StringBuilder()
            .AppendLine($"ImageAssert.AreEqual @ line {line}")
            .AppendLine("pixelTolerance: " + tolerance)
            .AppendLine($"expected: {expected?.StepName} ({expected?.File.Name}){(expectedRect == FirstQuadrant ? null : $" in {expectedRect}")}")