|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using ActionUnity; |
| 4 | +using NUnit.Framework; |
| 5 | +using Unity.Mathematics; |
| 6 | +using UnityEngine; |
| 7 | +using UnityEngine.TestTools; |
| 8 | + |
| 9 | +public class InteropTests |
| 10 | +{ |
| 11 | + private GameObject _gameObjectWithInteropHandler; |
| 12 | + private InteropHandlerSample _interopHandlerSample; |
| 13 | + [SetUp] |
| 14 | + public void SetUp() |
| 15 | + { |
| 16 | + _gameObjectWithInteropHandler = new GameObject("TestObject"); |
| 17 | + _interopHandlerSample = _gameObjectWithInteropHandler.AddComponent<InteropHandlerSample>(); |
| 18 | + _gameObjectWithInteropHandler.AddComponent<PluginLoggerReader>(); |
| 19 | + } |
| 20 | + |
| 21 | + [TearDown] |
| 22 | + public void TearDown() |
| 23 | + { |
| 24 | + // Clean up resources and destroy the GameObject |
| 25 | + Object.Destroy(_gameObjectWithInteropHandler); |
| 26 | + } |
| 27 | + |
| 28 | + [UnityTest] |
| 29 | + public IEnumerator TestInteropHandler() |
| 30 | + { |
| 31 | + // Wait for a few seconds to allow the simulation to run |
| 32 | + float simulationTime = 0.5f; |
| 33 | + yield return new WaitForSeconds(simulationTime); |
| 34 | + |
| 35 | + // Now that the simulation has run, run your tests |
| 36 | + // Yield one more frame to ensure everything is updated |
| 37 | + yield return null; |
| 38 | + // Perform your tests as previously described |
| 39 | + TextureContainsExpectedValues(); |
| 40 | + TextureArrayContainsExpectedValues(); |
| 41 | + // ComputeBufferContainsExpectedValues(); |
| 42 | + } |
| 43 | + |
| 44 | +public void TextureContainsExpectedValues() |
| 45 | +{ |
| 46 | + Texture2D originalTexture = _interopHandlerSample.Texture; |
| 47 | + |
| 48 | + // Create a temporary RenderTexture with the same dimensions as the original texture |
| 49 | + RenderTexture tempRenderTexture = new RenderTexture(originalTexture.width, originalTexture.height, 0); |
| 50 | + RenderTexture.active = tempRenderTexture; |
| 51 | + |
| 52 | + // Copy the content of the original Texture2D to the RenderTexture |
| 53 | + Graphics.Blit(originalTexture, tempRenderTexture); |
| 54 | + |
| 55 | + // Create a new Texture2D to read the pixels from the RenderTexture |
| 56 | + Texture2D copiedTexture = new Texture2D(originalTexture.width, originalTexture.height); |
| 57 | + copiedTexture.ReadPixels(new Rect(0, 0, originalTexture.width, originalTexture.height), 0, 0); |
| 58 | + copiedTexture.Apply(); |
| 59 | + |
| 60 | + // Loop through each pixel and check the value |
| 61 | + Color[] pixels = copiedTexture.GetPixels(); |
| 62 | + foreach (Color pixel in pixels) |
| 63 | + { |
| 64 | + // Implement your pixel value verification logic here |
| 65 | + float expectedValue = math.abs(math.cos(Time.time)); |
| 66 | + Assert.IsTrue(math.abs(expectedValue - pixel.g) < 1e-2f); |
| 67 | + } |
| 68 | + |
| 69 | + // Clean up resources |
| 70 | + RenderTexture.active = null; |
| 71 | + Object.Destroy(copiedTexture); |
| 72 | + Object.Destroy(tempRenderTexture); |
| 73 | +} |
| 74 | + |
| 75 | + public void TextureArrayContainsExpectedValues() |
| 76 | + { |
| 77 | + Texture2DArray textureArray = _interopHandlerSample.TextureArray; |
| 78 | + |
| 79 | + // Create a RenderTexture to copy the Texture2DArray. |
| 80 | + RenderTexture renderTexture = new RenderTexture(textureArray.width, textureArray.height, 0, RenderTextureFormat.ARGB32); |
| 81 | + renderTexture.enableRandomWrite = true; |
| 82 | + renderTexture.Create(); |
| 83 | + |
| 84 | + // Set up a temporary camera to render the Texture2DArray to the RenderTexture. |
| 85 | + // Create a temporary camera GameObject and set its target texture. |
| 86 | + GameObject tempCameraObject = new GameObject("TempCamera"); |
| 87 | + Camera tempCamera = tempCameraObject.AddComponent<Camera>(); |
| 88 | + tempCamera.targetTexture = renderTexture; |
| 89 | + tempCamera.RenderWithShader(Shader.Find("Unlit/Texture"), "RenderType"); |
| 90 | + |
| 91 | + // Create a temporary texture to read the pixels from the RenderTexture. |
| 92 | + Texture2D tempTexture = new Texture2D(textureArray.width, textureArray.height, TextureFormat.ARGB32, false); |
| 93 | + |
| 94 | + // Read the pixels from the RenderTexture into the temporary texture. |
| 95 | + RenderTexture.active = renderTexture; |
| 96 | + tempTexture.ReadPixels(new Rect(0, 0, textureArray.width, textureArray.height), 0, 0); |
| 97 | + tempTexture.Apply(); |
| 98 | + |
| 99 | + // Loop through the slices of the Texture2DArray and compare pixel values. |
| 100 | + for (int z = 0; z < textureArray.depth; z++) |
| 101 | + { |
| 102 | + for (int x = 0; x < textureArray.width; x++) |
| 103 | + { |
| 104 | + for (int y = 0; y < textureArray.height; y++) |
| 105 | + { |
| 106 | + Color expectedColor = new Color(z % 2, math.abs((z + 1) * math.cos(Time.time)), 0, 1.0f); |
| 107 | + Color actualColor = tempTexture.GetPixel(x, y); |
| 108 | + Debug.Log(expectedColor + " " + actualColor); |
| 109 | + // Compare the colors with a tolerance. |
| 110 | + Assert.IsTrue((Vector4.Distance(expectedColor, actualColor) < 1e-2)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // Clean up temporary objects. |
| 116 | + Object.Destroy(tempCamera.gameObject); |
| 117 | + Object.Destroy(renderTexture); |
| 118 | + Object.Destroy(tempTexture); |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + public void ComputeBufferContainsExpectedValues() |
| 124 | + { |
| 125 | + ComputeBuffer computeBuffer = _interopHandlerSample.ComputeBuffer; |
| 126 | + |
| 127 | + // Implement your verification logic for the 'ComputeBuffer' here |
| 128 | + Assert.IsNotNull(computeBuffer); |
| 129 | + |
| 130 | + // Set the buffer data to an array to access the values |
| 131 | + float4[] bufferData = new float4[computeBuffer.count]; |
| 132 | + computeBuffer.GetData(bufferData); |
| 133 | + |
| 134 | + // Loop through the buffer data and verify values at each index |
| 135 | + for (int x = 0; x < bufferData.Length; x++) |
| 136 | + { |
| 137 | + float4 expectedValue = new float4 |
| 138 | + ( |
| 139 | + math.cos(2 * math.PI * Time.time / x), |
| 140 | + math.sin(2 * math.PI * Time.time / x), |
| 141 | + 0.0f, |
| 142 | + 1.0f |
| 143 | + ); |
| 144 | + |
| 145 | + // Implement your verification logic for the ComputeBuffer data here |
| 146 | + Assert.AreEqual(expectedValue, bufferData[x]); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments