Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

URPMigrationTestBase.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using NUnit.Framework;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using UnityEngine.Rendering.Universal;
  5. namespace UnityEditor.Rendering.Universal.Test
  6. {
  7. interface IGlobalSettingsMigrationTestCaseBase
  8. {
  9. void SetUp(UniversalRenderPipelineGlobalSettings globalSettingsAsset, UniversalRenderPipelineAsset renderPipelineAsset);
  10. void TearDown(UniversalRenderPipelineGlobalSettings globalSettingsAsset, UniversalRenderPipelineAsset renderPipelineAsset)
  11. {
  12. }
  13. }
  14. interface IGlobalSettingsMigrationTestCase: IGlobalSettingsMigrationTestCaseBase
  15. {
  16. bool IsMigrationCorrect(out string message);
  17. }
  18. interface IRenderPipelineGraphicsSettingsTestCase<TRenderPipelineGraphicsSettings> : IGlobalSettingsMigrationTestCaseBase
  19. where TRenderPipelineGraphicsSettings : class, IRenderPipelineGraphicsSettings
  20. {
  21. bool IsMigrationCorrect(TRenderPipelineGraphicsSettings settings, out string message);
  22. }
  23. abstract class GlobalSettingsMigrationTestBase
  24. {
  25. protected UniversalRenderPipelineGlobalSettings m_Instance;
  26. protected UniversalRenderPipelineGlobalSettings m_OldInstance;
  27. protected UniversalRenderPipelineAsset m_Asset;
  28. [OneTimeSetUp]
  29. public void OneTimeSetup()
  30. {
  31. if (GraphicsSettings.currentRenderPipeline is not UniversalRenderPipelineAsset rpAsset)
  32. {
  33. Assert.Ignore($"This test will be only executed when URP is the current pipeline");
  34. return;
  35. }
  36. m_Asset = rpAsset;
  37. m_OldInstance = EditorGraphicsSettings.GetRenderPipelineGlobalSettingsAsset<UniversalRenderPipeline>() as UniversalRenderPipelineGlobalSettings;
  38. m_Instance = ScriptableObject.CreateInstance<UniversalRenderPipelineGlobalSettings>();
  39. m_Instance.name = $"{typeof(GlobalSettingsMigrationTestBase).Name}";
  40. EditorGraphicsSettings.SetRenderPipelineGlobalSettingsAsset<UniversalRenderPipeline>(m_Instance);
  41. string assetPath = $"Assets/URP/MigrationTests/{m_Instance.name}.asset";
  42. CoreUtils.EnsureFolderTreeInAssetFilePath(assetPath);
  43. AssetDatabase.CreateAsset(m_Instance, assetPath);
  44. AssetDatabase.SaveAssets();
  45. }
  46. [OneTimeTearDown]
  47. public void OneTimeTearDown()
  48. {
  49. EditorGraphicsSettings.SetRenderPipelineGlobalSettingsAsset<UniversalRenderPipeline>(m_OldInstance);
  50. if (m_Instance != null)
  51. {
  52. string assetPath = AssetDatabase.GetAssetPath(m_Instance);
  53. AssetDatabase.DeleteAsset(assetPath);
  54. }
  55. }
  56. }
  57. abstract class GlobalSettingsMigrationTest : GlobalSettingsMigrationTestBase
  58. {
  59. protected void DoTest(IGlobalSettingsMigrationTestCase testCase)
  60. {
  61. Assert.IsNotNull(testCase);
  62. testCase.SetUp(m_Instance, m_Asset);
  63. UniversalRenderPipelineGlobalSettings.UpgradeAsset(m_Instance.GetInstanceID());
  64. bool migrationIsPerformed = m_Instance.m_AssetVersion == UniversalRenderPipelineGlobalSettings.k_LastVersion;
  65. bool migrationIsCorrect = false;
  66. string errorMessage = string.Empty;
  67. if (migrationIsPerformed)
  68. {
  69. migrationIsCorrect = testCase.IsMigrationCorrect(out errorMessage);
  70. }
  71. testCase.TearDown(m_Instance, m_Asset);
  72. Assert.IsTrue(migrationIsPerformed, "Unable to perform the migration");
  73. Assert.IsTrue(migrationIsCorrect, errorMessage);
  74. }
  75. }
  76. abstract class RenderPipelineGraphicsSettingsMigrationTestBase<TRenderPipelineGraphicsSettings> : GlobalSettingsMigrationTestBase
  77. where TRenderPipelineGraphicsSettings : class, IRenderPipelineGraphicsSettings
  78. {
  79. protected void DoTest(IRenderPipelineGraphicsSettingsTestCase<TRenderPipelineGraphicsSettings> testCase)
  80. {
  81. Assert.IsNotNull(testCase);
  82. m_Instance.name = $"{typeof(TRenderPipelineGraphicsSettings).Name}";
  83. testCase.SetUp(m_Instance, m_Asset);
  84. UniversalRenderPipelineGlobalSettings.UpgradeAsset(m_Instance.GetInstanceID());
  85. bool migrationIsPerformed = m_Instance.m_AssetVersion == UniversalRenderPipelineGlobalSettings.k_LastVersion;
  86. bool migrationIsCorrect = false;
  87. string errorMessage = string.Empty;
  88. if (migrationIsPerformed)
  89. {
  90. migrationIsCorrect = testCase.IsMigrationCorrect(
  91. GraphicsSettings.GetRenderPipelineSettings<TRenderPipelineGraphicsSettings>(),
  92. out errorMessage);
  93. }
  94. testCase.TearDown(m_Instance, m_Asset);
  95. Assert.IsTrue(migrationIsPerformed, "Unable to perform the migration");
  96. Assert.IsTrue(migrationIsCorrect, errorMessage);
  97. }
  98. }
  99. }