| | 1 | | using DCL.Configuration; |
| | 2 | | using System.Collections; |
| | 3 | | using System.IO; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Assertions; |
| | 6 | |
|
| | 7 | | namespace DCL.Helpers |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Visual tests helper class used to validate Asset Bundle conversions. Based on 'Scripts/Tests/VisualTests/VisualT |
| | 11 | | /// </summary> |
| | 12 | | public static class AssetBundlesVisualTestHelpers |
| | 13 | | { |
| 0 | 14 | | public static string testImagesPath = Application.dataPath + "/../TestResources/VisualTests/CurrentTestImages/"; |
| | 15 | |
|
| 0 | 16 | | public static string baselineImagesPath = Application.dataPath + "/../TestResources/VisualTests/BaselineImages/" |
| | 17 | |
|
| 0 | 18 | | public static bool generateBaseline = false; |
| | 19 | |
|
| | 20 | | public static IEnumerator TakeSnapshot(string snapshotName, Camera camera, Vector3? shotPosition = null, Vector3 |
| | 21 | | { |
| 0 | 22 | | if (shotPosition.HasValue || shotTarget.HasValue) |
| | 23 | | { |
| 0 | 24 | | RepositionVisualTestsCamera(camera, shotPosition, shotTarget); |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | yield return null; |
| 0 | 28 | | yield return null; |
| | 29 | |
|
| 0 | 30 | | int snapshotsWidth = TestSettings.VISUAL_TESTS_SNAPSHOT_WIDTH; |
| 0 | 31 | | int snapshotsHeight = TestSettings.VISUAL_TESTS_SNAPSHOT_HEIGHT; |
| | 32 | |
|
| 0 | 33 | | if (generateBaseline || !File.Exists(baselineImagesPath + snapshotName)) |
| | 34 | | { |
| 0 | 35 | | yield return TakeSnapshot(baselineImagesPath, snapshotName, camera, |
| | 36 | | snapshotsWidth, snapshotsHeight); |
| 0 | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| 0 | 40 | | yield return TakeSnapshot(testImagesPath, snapshotName, camera, snapshotsWidth, snapshotsHeight); |
| | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | public static bool TestSnapshot(string baselineImagePathWithFilename, string testImagePathWithFilename, float ra |
| | 45 | | { |
| 0 | 46 | | if (generateBaseline || !File.Exists(baselineImagePathWithFilename)) |
| 0 | 47 | | return false; |
| | 48 | |
|
| 0 | 49 | | float ratioResult = |
| | 50 | | ComputeImageAffinityPercentage(baselineImagePathWithFilename, testImagePathWithFilename); |
| | 51 | |
|
| 0 | 52 | | if (assert) |
| | 53 | | { |
| 0 | 54 | | Assert.IsTrue(ratioResult > ratio, |
| | 55 | | $"{Path.GetFileName(baselineImagePathWithFilename)} has {ratioResult}% affinity, the minimum is {rat |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | return ratioResult > ratio; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// This coroutine will take a visual test snapshot using the camera provided, with the given image size. |
| | 63 | | /// The image will be saved to disk as png. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="snapshotPath">Path to the directory where the image will be saved. Will be created if not exist |
| | 66 | | /// <param name="snapshotName">output filename, it should include the png extension</param> |
| | 67 | | /// <param name="camera">camera used to take the shot</param> |
| | 68 | | /// <param name="width">Width of the final image</param> |
| | 69 | | /// <param name="height">Height of the final image</param> |
| | 70 | | public static IEnumerator TakeSnapshot(string snapshotPath, string snapshotName, Camera camera, int width, |
| | 71 | | int height) |
| | 72 | | { |
| 0 | 73 | | if (string.IsNullOrEmpty(snapshotName) || camera == null) |
| | 74 | | { |
| 0 | 75 | | Debug.Log("snapshot name or camera is not valid. Snapshot aborted."); |
| 0 | 76 | | yield break; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | var previousQualityLevel = QualitySettings.GetQualityLevel(); |
| 0 | 80 | | QualitySettings.SetQualityLevel((int) QualityLevel.Good, true); |
| | 81 | |
|
| 0 | 82 | | string finalPath = snapshotPath + snapshotName; |
| | 83 | |
|
| 0 | 84 | | if (File.Exists(finalPath)) |
| | 85 | | { |
| 0 | 86 | | File.Delete(finalPath); |
| | 87 | |
|
| | 88 | | // Just in case, wait until the file is deleted |
| 0 | 89 | | yield return new WaitUntil(() => { return !File.Exists(finalPath); }); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | // We should only read the screen buffer after rendering is complete |
| 0 | 93 | | yield return null; |
| | 94 | |
|
| 0 | 95 | | RenderTexture renderTexture = new RenderTexture(width, height, 24); |
| 0 | 96 | | camera.targetTexture = renderTexture; |
| 0 | 97 | | camera.Render(); |
| | 98 | |
|
| 0 | 99 | | RenderTexture.active = renderTexture; |
| 0 | 100 | | Texture2D currentSnapshot = new Texture2D(width, height, TextureFormat.RGB24, false); |
| 0 | 101 | | currentSnapshot.ReadPixels(new Rect(0, 0, width, height), 0, 0); |
| 0 | 102 | | currentSnapshot.Apply(); |
| | 103 | |
|
| 0 | 104 | | yield return null; |
| | 105 | |
|
| 0 | 106 | | if (!Directory.Exists(snapshotPath)) |
| | 107 | | { |
| 0 | 108 | | Directory.CreateDirectory(snapshotPath); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | byte[] bytes = currentSnapshot.EncodeToPNG(); |
| 0 | 112 | | File.WriteAllBytes(finalPath, bytes); |
| | 113 | |
|
| | 114 | | // Just in case, wait until the file is created |
| 0 | 115 | | yield return new WaitUntil(() => { return File.Exists(finalPath); }); |
| | 116 | |
|
| 0 | 117 | | RenderTexture.active = null; |
| 0 | 118 | | renderTexture.Release(); |
| | 119 | |
|
| 0 | 120 | | yield return new WaitForSeconds(0.2f); |
| | 121 | |
|
| 0 | 122 | | QualitySettings.SetQualityLevel(previousQualityLevel, true); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | public static float ComputeImageAffinityPercentage(string baselineImagePathWithFilename, |
| | 126 | | string testImagePathWithFilename) |
| | 127 | | { |
| 0 | 128 | | Texture2D baselineSnapshot = new Texture2D(TestSettings.VISUAL_TESTS_SNAPSHOT_WIDTH, |
| | 129 | | TestSettings.VISUAL_TESTS_SNAPSHOT_HEIGHT, TextureFormat.RGB24, false); |
| 0 | 130 | | baselineSnapshot.LoadImage(File.ReadAllBytes(baselineImagePathWithFilename)); |
| | 131 | |
|
| 0 | 132 | | Texture2D currentSnapshot = new Texture2D(TestSettings.VISUAL_TESTS_SNAPSHOT_WIDTH, |
| | 133 | | TestSettings.VISUAL_TESTS_SNAPSHOT_HEIGHT, TextureFormat.RGB24, false); |
| 0 | 134 | | currentSnapshot.LoadImage(File.ReadAllBytes(testImagePathWithFilename)); |
| | 135 | |
|
| 0 | 136 | | string finalDiffPath = Path.GetDirectoryName(testImagePathWithFilename) + "/" + |
| | 137 | | Path.GetFileNameWithoutExtension(testImagePathWithFilename) + "_diff" + |
| | 138 | | Path.GetExtension(testImagePathWithFilename); |
| | 139 | |
|
| 0 | 140 | | return ComputeImageAffinityPercentage(baselineSnapshot, currentSnapshot, finalDiffPath); |
| | 141 | | } |
| | 142 | |
|
| | 143 | | /// <summary> |
| | 144 | | /// This will compare the pixels of two images in order to make visual tests. |
| | 145 | | /// </summary> |
| | 146 | | /// <param name="baselineImage">Reference or "golden" image</param> |
| | 147 | | /// <param name="testImage">Image to compare</param> |
| | 148 | | /// <param name="diffImagePath"></param> |
| | 149 | | /// <returns>Affinity percentage</returns> |
| | 150 | | public static float ComputeImageAffinityPercentage(Texture2D baselineImage, Texture2D testImage, |
| | 151 | | string diffImagePath) |
| | 152 | | { |
| 0 | 153 | | baselineImage = DuplicateTextureAsReadable(baselineImage); |
| 0 | 154 | | testImage = DuplicateTextureAsReadable(testImage); |
| | 155 | |
|
| 0 | 156 | | if (string.IsNullOrEmpty(diffImagePath)) |
| | 157 | | { |
| 0 | 158 | | Debug.Log("diff image path is not valid. Image affinity percentage check aborted."); |
| | 159 | |
|
| 0 | 160 | | return -1; |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | if (baselineImage.width != testImage.width || baselineImage.height != testImage.height) |
| | 164 | | { |
| 0 | 165 | | Debug.Log("CAN'T COMPARE IMAGES WITH DIFFERENT DIMENSIONS:"); |
| 0 | 166 | | Debug.Log("baseline image dimensions: " + baselineImage.width + "," + baselineImage.height); |
| 0 | 167 | | Debug.Log("test image dimensions: " + testImage.width + "," + testImage.height); |
| | 168 | |
|
| 0 | 169 | | return -1; |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | Color32[] baselineImagePixels = baselineImage.GetPixels32(); |
| 0 | 173 | | Color32[] testImagePixels = testImage.GetPixels32(); |
| 0 | 174 | | Color32[] diffImagePixels = new Color32[testImagePixels.Length]; |
| 0 | 175 | | Color32 diffColor = new Color32(255, 0, 0, 255); |
| 0 | 176 | | int differentPixels = 0; |
| | 177 | |
|
| 0 | 178 | | for (int i = 0; i < testImagePixels.Length; i++) |
| | 179 | | { |
| 0 | 180 | | if (!IsSamePixel(testImagePixels[i], baselineImagePixels[i], |
| | 181 | | TestSettings.VISUAL_TESTS_PIXELS_CHECK_THRESHOLD)) |
| | 182 | | { |
| 0 | 183 | | differentPixels++; |
| 0 | 184 | | diffImagePixels[i] = diffColor; |
| 0 | 185 | | } |
| | 186 | | else |
| | 187 | | { |
| 0 | 188 | | diffImagePixels[i] = baselineImagePixels[i]; |
| | 189 | | } |
| | 190 | | } |
| | 191 | |
|
| | 192 | | // Calculate Image Affinity |
| 0 | 193 | | float imageAffinity = ((testImagePixels.Length - differentPixels) * 100) / testImagePixels.Length; |
| | 194 | |
|
| | 195 | | // Save diff image |
| 0 | 196 | | if (imageAffinity < TestSettings.VISUAL_TESTS_APPROVED_AFFINITY) |
| | 197 | | { |
| 0 | 198 | | Texture2D diffImage = new Texture2D(baselineImage.width, baselineImage.height); |
| 0 | 199 | | diffImage.SetPixels32(diffImagePixels); |
| 0 | 200 | | diffImage.Apply(); |
| 0 | 201 | | byte[] bytes = diffImage.EncodeToPNG(); |
| 0 | 202 | | File.WriteAllBytes(diffImagePath, bytes); |
| 0 | 203 | | } |
| 0 | 204 | | else if (File.Exists(diffImagePath)) |
| | 205 | | { |
| 0 | 206 | | File.Delete(diffImagePath); |
| | 207 | |
|
| 0 | 208 | | if (File.Exists(diffImagePath + ".meta")) |
| 0 | 209 | | File.Delete(diffImagePath + ".meta"); |
| | 210 | | } |
| | 211 | |
|
| 0 | 212 | | return imageAffinity; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | public static Texture2D DuplicateTextureAsReadable(Texture2D source) |
| | 216 | | { |
| 0 | 217 | | RenderTexture renderTex = RenderTexture.GetTemporary( |
| | 218 | | source.width, |
| | 219 | | source.height, |
| | 220 | | 0, |
| | 221 | | RenderTextureFormat.Default, |
| | 222 | | RenderTextureReadWrite.Linear); |
| | 223 | |
|
| 0 | 224 | | Graphics.Blit(source, renderTex); |
| | 225 | |
|
| 0 | 226 | | RenderTexture previous = RenderTexture.active; |
| 0 | 227 | | RenderTexture.active = renderTex; |
| | 228 | |
|
| 0 | 229 | | Texture2D readableText = new Texture2D(source.width, source.height); |
| 0 | 230 | | readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0); |
| 0 | 231 | | readableText.Apply(); |
| | 232 | |
|
| 0 | 233 | | RenderTexture.active = previous; |
| 0 | 234 | | RenderTexture.ReleaseTemporary(renderTex); |
| | 235 | |
|
| 0 | 236 | | return readableText; |
| | 237 | | } |
| | 238 | |
|
| | 239 | | public static bool IsSamePixel(Color32 pixelA, Color32 pixelB, float checkThreshold) |
| | 240 | | { |
| 0 | 241 | | return (pixelA.r > pixelB.r - checkThreshold && pixelA.r < pixelB.r + checkThreshold) && |
| | 242 | | (pixelA.g > pixelB.g - checkThreshold && pixelA.g < pixelB.g + checkThreshold) && |
| | 243 | | (pixelA.b > pixelB.b - checkThreshold && pixelA.b < pixelB.b + checkThreshold); |
| | 244 | | } |
| | 245 | |
|
| | 246 | | public static void RepositionVisualTestsCamera(Transform cameraTransform, Vector3? position = null, Vector3? tar |
| | 247 | | { |
| 0 | 248 | | if (position.HasValue) |
| | 249 | | { |
| 0 | 250 | | cameraTransform.position = position.Value; |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | if (target.HasValue) |
| | 254 | | { |
| 0 | 255 | | cameraTransform.forward = target.Value - cameraTransform.position; |
| | 256 | | } |
| 0 | 257 | | } |
| | 258 | |
|
| 0 | 259 | | public static void RepositionVisualTestsCamera(Camera camera, Vector3? position = null, Vector3? target = null) |
| | 260 | | } |
| | 261 | | } |