using System; using System.Collections.Generic; using NUnit.Framework; using UnityEngine.TestTools; using UnityEngine; namespace UnityEditor.Rendering.Universal.Tools { [Category("Graphics Tools")] class ConverterTests { class NotAConverterType { } [Test] public void BatchModeFails() { bool ok = Converters.RunInBatchMode(new List() { typeof(NotAConverterType) }); Assert.IsFalse(ok); } [Test] public void RunInBatchMode_LogsUsageWarning() { LogAssert.Expect( LogType.Warning, "Using this API can lead to incomplete or unpredictable conversion outcomes. " + "For reliable results, please perform the conversion via the dedicated window: " + "Window > Rendering > Render Pipeline Converter." ); bool _ = Converters.RunInBatchMode(new List() {}); } public static IEnumerable TestCases() { yield return new TestCaseData( "BuiltInToURP", new List { "Material" }, true, new List { typeof(BuiltInToURP3DMaterialUpgrader) } ).SetName("When Using Inclusive filter with Material in the correct category. The Filter only returns that converter"); yield return new TestCaseData( "BuiltInToURP", new List { "ParametricToFreeformLight" }, true, new List() ).SetName("When Using Inclusive filter with Light in the wrong category. The Filter returns nothing"); yield return new TestCaseData( "BuiltInToURP", new List { "RenderSettings", "PPv2" }, false, new List { typeof(AnimationClipConverter), typeof(BuiltInToURP3DMaterialUpgrader), typeof(BuiltInToURP3DReadonlyMaterialConverter), } ).SetName("When Using Exclusive filter. The filter returns everything except the given ids"); yield return new TestCaseData( "BuiltInToURP", new List(), true, new List() // No converters match ).SetName("When Using Inclusive filter with no converters. The filter returns nothing"); yield return new TestCaseData( "BuiltInToURP", new List(), false, new List { typeof(PPv2Converter), typeof(BuiltInToURP3DRenderSettingsConverter), typeof(AnimationClipConverter), typeof(BuiltInToURP3DMaterialUpgrader), typeof(BuiltInToURP3DReadonlyMaterialConverter), } ).SetName("BuiltInToURP - When Using Exclusive filter with no converters. The filter returns everything"); yield return new TestCaseData( "BuiltInToURP2D", new List(), false, new List { typeof(BuiltInToURP2DRenderSettingsConverter), typeof(BuiltInToURP2DReadonlyMaterialConverter), } ).SetName("BuiltInToURP2D - When Using Exclusive filter with no converters. The filter returns everything"); yield return new TestCaseData( "UpgradeURP2DAssets", new List(), false, new List { typeof(ParametricToFreeformLightUpgrader) } ).SetName("UpgradeURP2DAssets - When Using Exclusive filter with no converters. The filter returns everything"); } [TestCaseSource(nameof(TestCases))] public void FilterConverters_ShouldReturnExpectedConverters( string containerName, List filterList, bool filterModeIsInclusive, List expectedTypes) { var actualTypes = Converters.FilterConverters(containerName, filterList, filterModeIsInclusive); CollectionAssert.AreEquivalent(expectedTypes, actualTypes); } [Test] public void CommandLine_ArgumentsParsedProperly() { // case 1 - working command var dummyArgs = "-batchmode -executeMethod UnityEditor.Rendering.Universal.Converters.RunInBatchModeCmdLine --flagA -paramA 1 2 3 --flagB --flagC -paramB --flagD"; var actualArgs = Converters.ParseArgs(dummyArgs.Split(' ')); Dictionary> expectedArgs = new() { { "-executeMethod", new List {"UnityEditor.Rendering.Universal.Converters.RunInBatchModeCmdLine"} }, { "-paramA", new List {"1", "2", "3"} }, { "-paramB", new List {} }, { "Flags", new List {"--flagA", "--flagB", "--flagC", "--flagD"} } }; CollectionAssert.AreEquivalent(expectedArgs, actualArgs); // case 2 - Error: No -batchmode flag dummyArgs = "-executeMethod UnityEditor.Rendering.Universal.Converters.RunInBatchModeCmdLine --flagA -paramA 1 2 3"; var exception = Assert.Throws(() => Converters.ParseArgs(dummyArgs.Split(' '))); Assert.That(exception.Message, Is.EqualTo("No -batchmode argument found. Exiting.")); // case 3 - Error: Adding values without a key dummyArgs = "-batchmode -executeMethod UnityEditor.Rendering.Universal.Converters.RunInBatchModeCmdLine --flagA wrongArgument"; exception = Assert.Throws(() => Converters.ParseArgs(dummyArgs.Split(' '))); Assert.That(exception.Message, Is.EqualTo("Unrecognized argument: wrongArgument")); } #pragma warning disable CS0618 // Type or member is obsolete public static IEnumerable TestCasesDeprecated() { yield return new TestCaseData( ConverterContainerId.BuiltInToURP, new List { ConverterId.Material }, ConverterFilter.Inclusive, new List { typeof(BuiltInToURP3DMaterialUpgrader) } ).SetName("When Using Inclusive filter with Material in the correct category. The Filter only returns that converter"); yield return new TestCaseData( ConverterContainerId.BuiltInToURP, new List { ConverterId.ParametricToFreeformLight }, ConverterFilter.Inclusive, new List() ).SetName("When Using Inclusive filter with Light in the wrong category. The Filter returns nothing"); yield return new TestCaseData( ConverterContainerId.BuiltInToURP, new List { ConverterId.RenderSettings, #if PPV2_EXISTS ConverterId.PPv2 #endif }, ConverterFilter.Exclusive, new List { typeof(AnimationClipConverter), typeof(BuiltInToURP3DMaterialUpgrader), typeof(BuiltInToURP3DReadonlyMaterialConverter), } ).SetName("When Using Exclusive filter. The filter returns everything except the given ids"); yield return new TestCaseData( ConverterContainerId.BuiltInToURP, new List(), ConverterFilter.Inclusive, new List() // No converters match ).SetName("When Using Inclusive filter with no converters. The filter returns nothing"); yield return new TestCaseData( ConverterContainerId.BuiltInToURP, new List(), ConverterFilter.Exclusive, new List { #if PPV2_EXISTS typeof(PPv2Converter), #endif typeof(BuiltInToURP3DRenderSettingsConverter), typeof(AnimationClipConverter), typeof(BuiltInToURP3DMaterialUpgrader), typeof(BuiltInToURP3DReadonlyMaterialConverter), } ).SetName("BuiltInToURP - When Using Exclusive filter with no converters. The filter returns everything"); yield return new TestCaseData( ConverterContainerId.BuiltInToURP2D, new List(), ConverterFilter.Exclusive, new List { typeof(BuiltInToURP2DRenderSettingsConverter), typeof(BuiltInToURP2DReadonlyMaterialConverter), } ).SetName("BuiltInToURP2D - When Using Exclusive filter with no converters. The filter returns everything"); yield return new TestCaseData( ConverterContainerId.UpgradeURP2DAssets, new List(), ConverterFilter.Exclusive, new List { typeof(URP3DToURP2DReadonlyMaterialConverter), typeof(ParametricToFreeformLightUpgrader) } ).SetName("UpgradeURP2DAssets - When Using Exclusive filter with no converters. The filter returns everything"); } [TestCaseSource(nameof(TestCasesDeprecated))] public void FilterConverters_ShouldReturnExpectedConverters_DeprecatedAPI( ConverterContainerId containerId, List filterList, ConverterFilter filterMode, List expectedTypes) { var actualTypes = Converters.FilterConverters(containerId, filterList, filterMode); CollectionAssert.AreEquivalent(expectedTypes, actualTypes); } [Test] public void CommandLine_SuggestCorrectCommands() { var expectedOutputTemplate = "The method you're trying to use is deprecated. Try running the following command in the command line:\n{0}"; // case 1 var containerID = ConverterContainerId.BuiltInToURP; var filterList = new List { ConverterId.Material, ConverterId.RenderSettings }; Converters.SuggestUpdatedCommand(containerID.ToString(), filterList.ConvertAll(id => id.ToString()), true); var suggestedCommand = " -projectPath -batchmode -executeMethod UnityEditor.Rendering.Universal.Converters.RunInBatchModeCmdLine --inclusive -container BuiltInToURP -typesFilter Material RenderSettings"; LogAssert.Expect(LogType.Log, String.Format(expectedOutputTemplate, suggestedCommand)); // case 2 containerID = ConverterContainerId.BuiltInToURP2D; filterList = new List { }; Converters.SuggestUpdatedCommand(containerID.ToString(), filterList.ConvertAll(id => id.ToString()), false); suggestedCommand = " -projectPath -batchmode -executeMethod UnityEditor.Rendering.Universal.Converters.RunInBatchModeCmdLine --exclusive -container BuiltInToURP2D"; LogAssert.Expect(LogType.Log, String.Format(expectedOutputTemplate, suggestedCommand)); } #pragma warning restore CS0618 // Type or member is obsolete } }