Example #1
0
        private static void AreEqualImpl(
            ScreenshotInfo expected,
            Rectangle expectedRect,
            ScreenshotInfo actual,
            Bitmap actualBitmap,
            Rectangle actualRect,
            double expectedToActualScale,
            PixelTolerance tolerance,
            [CallerLineNumber] int line = 0)
        {
            using var assertionScope = new AssertionScope($"{expected.StepName}<=={actual}");
            assertionScope.AddReportable("expectedRect", expectedRect.ToString());
            assertionScope.AddReportable("actualRect", actualRect.ToString());
            assertionScope.AddReportable("expectedToActualScale", expectedToActualScale.ToString(NumberFormatInfo.InvariantInfo));

            var(areEqual, context) = EqualityCheck(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line);

            if (areEqual)
            {
                Console.WriteLine(context.ToString());
            }
            else
            {
                assertionScope.FailWithText(context);
            }
        }
Example #2
0
        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();
        }
Example #3
0
        public void When_Path()
        {
            // For Path, the junction between the begin and the end of the path is not as smooth as WinUI (on iOS),
            // se we increase the pixel offset tolerance to ignore it.
            var tolerance = new PixelTolerance()
                            .WithColor(132)     // We are almost only trying to detect edges
                            .WithOffset(6, 6, LocationToleranceKind.PerPixel)
                            .Discrete(2);

            ValidateShape("Path", tolerance);
        }
Example #4
0
 private static void AreEqualImpl(
     ScreenshotInfo expected,
     Rectangle expectedRect,
     ScreenshotInfo actual,
     Rectangle actualRect,
     double expectedToActualScale,
     PixelTolerance tolerance,
     [CallerLineNumber] int line = 0)
 {
     using var actualBitmap = new Bitmap(actual.File.FullName);
     AreEqualImpl(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line);
 }
Example #5
0
 private static void AreEqualImpl(
     FileInfo expected,
     Rectangle expectedRect,
     FileInfo actual,
     Rectangle actualRect,
     double expectedToActualScale,
     PixelTolerance tolerance,
     int line)
 {
     using (var actualBitmap = new Bitmap(actual.FullName))
     {
         AreEqualImpl(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line);
     }
 }
Example #6
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}")}")
Example #7
0
        private static void AreEqualImpl(
            ScreenshotInfo expected,
            Rectangle expectedRect,
            ScreenshotInfo actual,
            Bitmap actualBitmap,
            Rectangle actualRect,
            double expectedToActualScale,
            PixelTolerance tolerance,
            [CallerLineNumber] int line = 0)
        {
            var(areEqual, context) = EqualityCheck(expected, expectedRect, actual, actualBitmap, actualRect, expectedToActualScale, tolerance, line);

            if (areEqual)
            {
                Console.WriteLine(context.ToString());
            }
            else
            {
                Assert.Fail(context.ToString());
            }
        }
Example #8
0
 public static void AreAlmostEqual(ScreenshotInfo expected, Rectangle expectedRect, Bitmap actual, Rectangle actualRect, double expectedToActualScale, PixelTolerance tolerance, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, expectedRect, null, actual, actualRect, expectedToActualScale, tolerance, line);
Example #9
0
 public static void AreAlmostEqual(ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Rectangle actualRect, int permittedPixelError, double expectedToActualScale = 1, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, expectedRect, actual, actualRect, expectedToActualScale, PixelTolerance.Cummulative(permittedPixelError), line);
Example #10
0
 public static void AreAlmostEqual(ScreenshotInfo expected, ScreenshotInfo actual, Rectangle?rect = null, int permittedPixelError = 5, double expectedToActualScale = 1, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, expectedToActualScale, PixelTolerance.Cummulative(permittedPixelError), line);
Example #11
0
        public void BitmapImage_vs_SvgImageSource_SvgLocal()
        {
            Run("UITests.Windows_UI_Xaml_Controls.ImageTests.BitmapImage_vs_SvgImageSource");

            var url        = _app.Marked("url");
            var btnSvg     = _app.Marked("btnSvg");
            var streamMode = _app.Marked("streamMode");
            var showLow    = _app.Marked("showLow");
            var img        = _app.Marked("img");

            showLow.SetDependencyPropertyValue("IsCheked", "False");

            // Load image from url
            url.SetDependencyPropertyValue("Text", "ms-appx:///Assets/Formats/uno-overalls.svg");
            streamMode.SetDependencyPropertyValue("IsChecked", "False");

            btnSvg.FastTap();

            WaitForBitmapOrSvgLoaded();

            using var screenshotDirect = TakeScreenshot("url_local");

            url.SetDependencyPropertyValue("Text", "https://nv-assets.azurewebsites.net/tests/images/uno-overalls.svg");

            btnSvg.FastTap();

            WaitForBitmapOrSvgLoaded();

            using var screenshotStream = TakeScreenshot("url_remote");

            var rect = _app.GetPhysicalRect(img);

            // Both images should be the same
            ImageAssert.AreEqual(screenshotDirect, screenshotStream, rect, tolerance: PixelTolerance.Exclusive(12));
        }
Example #12
0
 /// <summary>
 /// Asserts that two screenshots are equal to each other inside the area of the nominated rect to within the given pixel error
 /// (ie, the A, R, G, or B values cumulatively differ by more than the permitted error for any pixel).
 /// </summary>
 public static void AreAlmostEqual(ScreenshotInfo expected, IAppRect expectedRect, ScreenshotInfo actual, IAppRect actualRect, int permittedPixelError, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, expectedRect.ToRectangle(), actual, actualRect.ToRectangle(), 1, PixelTolerance.Cummulative(permittedPixelError), line);
Example #13
0
 public static void AreEqual(ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Rectangle actualRect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, expectedRect, actual, actualRect, expectedToActualScale, tolerance, line);
Example #14
0
 public static void AreEqual(ScreenshotInfo expected, ScreenshotInfo actual, Rectangle?rect = null, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, expectedToActualScale, tolerance, line);
Example #15
0
 /// <summary>
 /// Asserts that two screenshots are equal to each other inside the area of the nominated rect to within the given pixel error
 /// (ie, the A, R, G, or B values cumulatively differ by more than the permitted error for any pixel).
 /// </summary>
 public static void AreAlmostEqual(FileInfo expected, Rectangle expectedRect, FileInfo actual, Rectangle actualRect, int permittedPixelError, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, expectedRect, actual, actualRect, 1, PixelTolerance.Cummulative(permittedPixelError), line);
Example #16
0
 /// <summary>
 /// Asserts that two screenshots are equal to each other inside the area of the nominated rect to within the given pixel error
 /// (ie, the A, R, G, or B values cumulatively differ by more than the permitted error for any pixel).
 /// </summary>
 public static void AreAlmostEqual(FileInfo expected, FileInfo actual, Rectangle?rect = null, int permittedPixelError = 5, [CallerLineNumber] int line = 0)
 => AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, 1, PixelTolerance.Cummulative(permittedPixelError), line);