private static void AssertAreEqual(FailedImageMessage deserialized, FailedImageMessage message)
 {
     Assert.That(deserialized.ImageName, Is.EqualTo(message.ImageName));
     Assert.That(deserialized.PathName, Is.EqualTo(message.PathName));
     Assert.That(deserialized.ExpectedImage, Is.EqualTo(message.ExpectedImage));
     Assert.That(deserialized.ActualImage, Is.EqualTo(message.ActualImage));
     Assert.That(deserialized.DiffImage, Is.EqualTo(message.DiffImage));
 }
        public void SerializationRoundtrip_DefaultInstance()
        {
            var message = new FailedImageMessage();

            var data         = message.Serialize();
            var deserialized = FailedImageMessage.Deserialize(data);

            AssertAreEqual(deserialized, message);
        }
        public void SerializationRoundtrip_AllFieldsAreSerializedAndDeserialized(string pathName, string imageName, byte[] expectedImage, byte[] actualImage, byte[] diffImage)
        {
            var message = new FailedImageMessage
            {
                PathName      = pathName,
                ImageName     = imageName,
                ExpectedImage = expectedImage,
                ActualImage   = actualImage,
                DiffImage     = diffImage,
            };

            AssertAreEqual(deserialized, message);
        }
Beispiel #4
0
    public void SaveImage(FailedImageMessage failedImageMessage)
    {
        if (!Directory.Exists(failedImageMessage.PathName))
        {
            Directory.CreateDirectory(failedImageMessage.PathName);
        }

        var actualImagePath = Path.Combine(failedImageMessage.PathName, $"{failedImageMessage.ImageName}.png");

        File.WriteAllBytes(actualImagePath, failedImageMessage.ActualImage);
        ReportArtifact(actualImagePath);

        if (failedImageMessage.DiffImage != null)
        {
            var diffImagePath = Path.Combine(failedImageMessage.PathName, $"{failedImageMessage.ImageName}.diff.png");
            File.WriteAllBytes(diffImagePath, failedImageMessage.DiffImage);
            ReportArtifact(diffImagePath);

            var expectedImagesPath =
                Path.Combine(failedImageMessage.PathName, $"{failedImageMessage.ImageName}.expected.png");
            File.WriteAllBytes(expectedImagesPath, failedImageMessage.ExpectedImage);
            ReportArtifact(expectedImagesPath);
        }
    }
Beispiel #5
0
    public void HandleFailedImageEvent(MessageEventArgs messageEventArgs)
    {
        var failedImageMessage = FailedImageMessage.Deserialize(messageEventArgs.data);

        SaveImage(failedImageMessage);
    }
Beispiel #6
0
        /// <summary>
        /// Compares an image to a 'reference' image to see if it looks correct.
        /// </summary>
        /// <param name="expected">What the image is supposed to look like.</param>
        /// <param name="actual">What the image actually looks like.</param>
        /// <param name="settings">Optional settings that control how the comparison is performed. Can be null, in which case the images are required to be exactly identical.</param>
        public static void AreEqual(Texture2D expected, Texture2D actual, ImageComparisonSettings settings = null)
        {
            if (actual == null)
            {
                throw new ArgumentNullException(nameof(actual));
            }

            var dirName            = Path.Combine("Assets/ActualImages", string.Format("{0}/{1}/{2}", UseGraphicsTestCasesAttribute.ColorSpace, UseGraphicsTestCasesAttribute.Platform, UseGraphicsTestCasesAttribute.GraphicsDevice));
            var failedImageMessage = new FailedImageMessage
            {
                PathName  = dirName,
                ImageName = TestContext.CurrentContext.Test.Name,
            };

            try
            {
                Assert.That(expected, Is.Not.Null, "No reference image was provided.");

                Assert.That(actual.width, Is.EqualTo(expected.width),
                            "The expected image had width {0}px, but the actual image had width {1}px.", expected.width,
                            actual.width);
                Assert.That(actual.height, Is.EqualTo(expected.height),
                            "The expected image had height {0}px, but the actual image had height {1}px.", expected.height,
                            actual.height);

                Assert.That(actual.format, Is.EqualTo(expected.format),
                            "The expected image had format {0} but the actual image had format {1}.", expected.format,
                            actual.format);

                using (var expectedPixels = new NativeArray <Color32>(expected.GetPixels32(0), Allocator.TempJob))
                    using (var actualPixels = new NativeArray <Color32>(actual.GetPixels32(0), Allocator.TempJob))
                        using (var diffPixels = new NativeArray <Color32>(expectedPixels.Length, Allocator.TempJob))
                            using (var sumOverThreshold = new NativeArray <float>(Mathf.CeilToInt(expectedPixels.Length / (float)k_BatchSize), Allocator.TempJob))
                            {
                                if (settings == null)
                                {
                                    settings = new ImageComparisonSettings();
                                }

                                new ComputeDiffJob
                                {
                                    expected         = expectedPixels,
                                    actual           = actualPixels,
                                    diff             = diffPixels,
                                    sumOverThreshold = sumOverThreshold,
                                    pixelThreshold   = settings.PerPixelCorrectnessThreshold
                                }.Schedule(expectedPixels.Length, k_BatchSize).Complete();

                                float averageDeltaE = sumOverThreshold.Sum() / (expected.width * expected.height);

                                try
                                {
                                    Assert.That(averageDeltaE, Is.LessThanOrEqualTo(settings.AverageCorrectnessThreshold));
                                }
                                catch (AssertionException)
                                {
                                    var diffImage       = new Texture2D(expected.width, expected.height, TextureFormat.RGB24, false);
                                    var diffPixelsArray = new Color32[expected.width * expected.height];
                                    diffPixels.CopyTo(diffPixelsArray);
                                    diffImage.SetPixels32(diffPixelsArray, 0);
                                    diffImage.Apply(false);

                                    TestContext.CurrentContext.Test.Properties.Set("DiffImage", Convert.ToBase64String(diffImage.EncodeToPNG()));

                                    failedImageMessage.DiffImage     = diffImage.EncodeToPNG();
                                    failedImageMessage.ExpectedImage = expected.EncodeToPNG();
                                    throw;
                                }
                            }
            }
            catch (AssertionException)
            {
                failedImageMessage.ActualImage = actual.EncodeToPNG();
#if UNITY_EDITOR
                ImageHandler.instance.SaveImage(failedImageMessage);
#else
                PlayerConnection.instance.Send(FailedImageMessage.MessageId, failedImageMessage.Serialize());
#endif
                TestContext.CurrentContext.Test.Properties.Set("Image", Convert.ToBase64String(actual.EncodeToPNG()));
                throw;
            }
        }