Skip to content

Commit 707e876

Browse files
authored
ci: run XR tests for all test projects (except ECS) (#481)
* add and use UTSGraphicsTestConstants * add XR tests for the Editor (not standalone) * manually determine the path of XR images to be used for XR tests, because the path passed to UseGraphicsTestCases attribute is defined in compile time, when XR is still disabled. * setup GraphicsTest assembly * rename XRUtility.EnableXR() to XRUtility.EnableXRInEditor() * add null check when enabling/disable XR * add ReferenceImagePathLog when executing ImageAssert.AreEqual * Exclude ECS from XR tests
1 parent 454be2d commit 707e876

File tree

6 files changed

+105
-51
lines changed

6 files changed

+105
-51
lines changed

com.unity.toon-graphics-test/Runtime/SetupUTSGraphicsTestCases.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Unity.ToonShader.GraphicsTest
2+
{
3+
public class UTSGraphicsTestConstants {
4+
#if UTS_TEST_USE_HDRP
5+
internal const string ReferenceImagePath = "Packages/com.unity.toon-reference-images/HDRP";
6+
#elif UTS_TEST_USE_URP
7+
internal const string ReferenceImagePath = "Packages/com.unity.toon-reference-images/URP";
8+
#else
9+
internal const string ReferenceImagePath = "Packages/com.unity.toon-reference-images/Built-In";
10+
#endif
11+
12+
}
13+
14+
} //end namespace

com.unity.toon-graphics-test/Runtime/SetupUTSGraphicsTestCases.cs.meta renamed to com.unity.toon-graphics-test/Runtime/UTSGraphicsTestConstants.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

com.unity.toon-graphics-test/Runtime/UTS_GraphicsTests.cs

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,75 @@
66
using UnityEngine.TestTools.Graphics;
77
using UnityEngine.SceneManagement;
88
using System.IO;
9+
using Unity.ToonShader.GraphicsTest;
10+
using UnityEditor;
911

1012

1113
namespace Tests
1214
{
15+
#if UNITY_EDITOR
16+
public class UTSGraphicsTestsXR {
1317

18+
[UnityTest]
19+
[UseGraphicsTestCases(UTSGraphicsTestConstants.ReferenceImagePath)]
20+
[Timeout(3600000)] //1 hour
21+
public IEnumerator Run(GraphicsTestCase testCase) {
22+
//[TODO-sin: 2025-7-18] ECS projects were never tested with XR, and currently they don't support XR.
23+
string projectName = Path.GetFileName(Path.GetDirectoryName(UnityEngine.Application.dataPath));
24+
if (!string.IsNullOrEmpty(projectName) && projectName.Contains("ECS")) {
25+
Assert.Ignore();
26+
}
27+
28+
//Enable XR
29+
XRUtility.EnableXRInEditor();
30+
31+
//Rendering both eyes in XR requires backbuffer, which depends on the game view resolution
32+
object gameViewSizeObj = UnityEditor.TestTools.Graphics.GameViewSize.SetCustomSize(1920, 1080);
33+
Assert.IsNotNull(gameViewSizeObj, "Failed to add custom game view size for XR tests.");
34+
UnityEditor.TestTools.Graphics.GameViewSize.SelectSize(gameViewSizeObj);
35+
36+
string loadedXRDevice = UseGraphicsTestCasesAttribute.LoadedXRDevice;
37+
38+
//Manually load the reference image for XR. Ex: URP/Linear/WindowsEditor/Vulkan/None/AngelRing.png
39+
Assert.IsNotNull(testCase.ReferenceImage);
40+
string imagePath = AssetDatabase.GetAssetPath(testCase.ReferenceImage);
41+
string imageFileName = Path.GetFileName(imagePath);
42+
string imageFolderName = Path.GetDirectoryName(Path.GetDirectoryName(imagePath));
43+
Assert.IsNotNull(imageFolderName);
44+
string xrImagePath = Path.Combine(imageFolderName, loadedXRDevice,imageFileName);
45+
testCase.ReferenceImagePathLog = xrImagePath;
46+
Assert.IsTrue(File.Exists(xrImagePath),$"XR Reference image not found at: {xrImagePath}");
47+
testCase.ReferenceImage = AssetDatabase.LoadAssetAtPath<Texture2D>(xrImagePath);
48+
49+
//Unity.ToonShader.GraphicsTest.SetupUTSGraphicsXRTestCases.Setup();
50+
yield return UTS_GraphicsTests.RunInternal(testCase, isXR:true);
51+
52+
XRUtility.DisableXR();
53+
}
54+
55+
}
1456

15-
public class UTS_GraphicsTests
16-
{
17-
#if UTS_TEST_USE_HDRP
18-
private const string ReferenceImagePath = "Packages/com.unity.toon-reference-images/HDRP";
19-
#elif UTS_TEST_USE_URP
20-
private const string ReferenceImagePath = "Packages/com.unity.toon-reference-images/URP";
21-
#else
22-
private const string ReferenceImagePath = "Packages/com.unity.toon-reference-images/Built-In";
23-
#endif
24-
57+
#endif //UNITY_EDITOR
58+
59+
public class UTSGraphicsTestsNonXR {
60+
[UnityTest]
61+
[UseGraphicsTestCases(UTSGraphicsTestConstants.ReferenceImagePath)]
62+
[Timeout(3600000)] //1 hour
63+
public IEnumerator Run(GraphicsTestCase testCase) {
64+
//[TODO-sin: 2025-7-2] Hack for now to disable XR for non-Stereo projects
65+
string projectName = Path.GetFileName(Path.GetDirectoryName(UnityEngine.Application.dataPath));
66+
if (!string.IsNullOrEmpty(projectName) && !projectName.Contains("Stereo")) {
67+
XRUtility.DisableXR();
68+
}
2569

26-
[UnityTest]
27-
[PrebuildSetup(typeof(Unity.ToonShader.GraphicsTest.SetupUTSGraphicsTestCases))]
28-
[UseGraphicsTestCases(ReferenceImagePath)]
29-
[Timeout(3600000)] //1 hour
30-
public IEnumerator Run(GraphicsTestCase testCase)
31-
{
70+
yield return UTS_GraphicsTests.RunInternal(testCase);
71+
}
72+
}
3273

74+
//----------------------------------------------------------------------------------------------------------------------
75+
76+
public class UTS_GraphicsTests {
77+
internal static IEnumerator RunInternal(GraphicsTestCase testCase, bool isXR = false) {
3378
SceneManager.LoadScene(testCase.ScenePath);
3479

3580
// Always wait one frame for scene load
@@ -39,6 +84,13 @@ public IEnumerator Run(GraphicsTestCase testCase)
3984
UTS_GraphicsTestSettings settings = Object.FindFirstObjectByType<UTS_GraphicsTestSettings>();
4085
Assert.IsNotNull(settings, "Invalid test scene, couldn't find UTS_GraphicsTestSettings");
4186

87+
if (isXR) {
88+
settings.ImageComparisonSettings.UseBackBuffer = true; //results using both eyes need backbuffer
89+
90+
//[TODO-sin: 2025-7-9] Hack for now. The resolution will be set to this later
91+
settings.ImageComparisonSettings.ImageResolution = ImageComparisonSettings.Resolution.w1920h1080;
92+
}
93+
4294

4395
int waitFrames = settings.WaitFrames;
4496

@@ -51,7 +103,8 @@ public IEnumerator Run(GraphicsTestCase testCase)
51103
for (int i = 0; i < waitFrames; i++)
52104
yield return new WaitForEndOfFrame();
53105

54-
ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);
106+
ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null),
107+
settings.ImageComparisonSettings, testCase.ReferenceImagePathLog);
55108

56109
// Does it allocate memory when it renders what's on the main camera?
57110
bool allocatesMemory = false;

com.unity.toon-graphics-test/Runtime/Unity.ToonShader.GraphicsTest.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"references": [
55
"GUID:c081bc530f560634bb5c21d4b323a7f1",
66
"GUID:27619889b8ba8c24980f49ee34dbb44a",
7-
"GUID:e40ba710768534012815d3193fa296cb"
7+
"GUID:e40ba710768534012815d3193fa296cb",
8+
"GUID:e18141520846dcc44b725b2f74e91229"
89
],
910
"includePlatforms": [],
1011
"excludePlatforms": [],

com.unity.toon-graphics-test/Runtime/XRUtility.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using UnityEngine;
43
using UnityEngine.XR;
54
using UnityEngine.XR.Management;
65

6+
#if UNITY_EDITOR
7+
using UnityEditor;
8+
#endif
9+
710
namespace Unity.ToonShader.GraphicsTest {
811

912
public static class XRUtility {
1013

11-
public static void EnableXR() {
14+
#if UNITY_EDITOR
15+
public static void EnableXRInEditor() {
16+
17+
if (null == XRGeneralSettings.Instance) {
18+
XRGeneralSettings.Instance = AssetDatabase.LoadAssetAtPath<XRGeneralSettings>(
19+
"Assets/XR/XRGeneralSettingsPerBuildTarget.asset");
20+
}
21+
1222
//Disable everything first
1323
if (XRGeneralSettings.Instance.Manager.activeLoader ||
1424
XRGeneralSettings.Instance.Manager.isInitializationComplete)
@@ -36,13 +46,15 @@ public static void EnableXR() {
3646

3747
}
3848

49+
#endif //UNITY_EDITOR
3950

4051
public static void DisableXR() {
4152

42-
if (XRGeneralSettings.Instance.Manager.isInitializationComplete)
53+
XRManagerSettings xrManager = XRGeneralSettings.Instance?.Manager;
54+
if (null!= xrManager && xrManager.isInitializationComplete)
4355
{
44-
XRGeneralSettings.Instance.Manager.StopSubsystems();
45-
XRGeneralSettings.Instance.Manager.DeinitializeLoader();
56+
xrManager.StopSubsystems();
57+
xrManager.DeinitializeLoader();
4658
}
4759

4860

@@ -52,10 +64,8 @@ public static void DisableXR() {
5264
for (int i = 0; i < count; i++) {
5365
xrDisplaySubsystems[i].Stop();
5466
}
55-
5667
}
5768

58-
5969
}
6070

6171
} //end namespace

0 commit comments

Comments
 (0)