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,
            };

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

            AssertAreEqual(deserialized, message);
        }
Beispiel #3
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;
            }
        }