Example #1
0
        private static void AreEqualImpl(
            ScreenshotInfo expected,
            Rectangle expectedRect,
            ScreenshotInfo actual,
            Rectangle actualRect,
            double expectedToActualScale,
            PixelTolerance tolerance,
            [CallerLineNumber] int line = 0)
        {
            var actualBitmap = actual.GetBitmap();

            AreEqualImpl(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line);
        }
Example #2
0
        private static PointF GetPixelPositionWithColor(ScreenshotInfo screenshotInfo, IAppRect boundingRect, Color expectedColor)
        {
            var bitmap = screenshotInfo.GetBitmap();

            for (var x = boundingRect.X; x < boundingRect.Right; x++)
            {
                for (var y = boundingRect.Y; y < boundingRect.Bottom; y++)
                {
                    var pixel = bitmap.GetPixel((int)x, (int)y);
                    if (pixel.ToArgb() == expectedColor.ToArgb())
                    {
                        return(new PointF(x, y));
                    }
                }
            }

            throw new InvalidOperationException($"Color {expectedColor} was not found.");
        }
Example #3
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)
        {
            var expectedBitmap = expected.GetBitmap();

            if (expectedRect != FirstQuadrant && actualRect != FirstQuadrant)
            {
                Assert.AreEqual(expectedRect.Size, actualRect.Size, WithContext("Compare rects don't have the same size"));
            }

            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} {expectedBitmap.Size}){(expectedRect == FirstQuadrant ? null : $" in {expectedRect}")}")