|
| 1 | +#if UNITY_EDITOR |
| 2 | +using System.IO; |
| 3 | +using MenSharp; |
| 4 | +using NUnit.Framework; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEditor.SceneManagement; |
| 7 | +using UnityEngine; |
| 8 | +using VRC.Udon; |
| 9 | + |
| 10 | +/// The pair between a proxy and its backing UdonBehaviour has to survive a |
| 11 | +/// trip through a unitypackage: in the receiving project the program asset |
| 12 | +/// the UdonBehaviour carried is another object or missing, and the hidden |
| 13 | +/// flag may be gone. The proxy remembers its UdonBehaviour, and a program |
| 14 | +/// asset's GUID is derived from its class so the reference resolves again. |
| 15 | +public class MenSharpPairingTests |
| 16 | +{ |
| 17 | + private const string Proxy = "MenSharpRuntimeSmoke"; |
| 18 | + |
| 19 | + [SetUp] |
| 20 | + public void SetUp() |
| 21 | + { |
| 22 | + MenSharpCompiler.CompileAll(); |
| 23 | + AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); |
| 24 | + EditorSceneManager.NewScene(NewSceneSetup.EmptyScene, NewSceneMode.Single); |
| 25 | + } |
| 26 | + |
| 27 | + private static (MenSharpBehaviour proxy, UdonBehaviour udon) Pair(GameObject target) |
| 28 | + { |
| 29 | + var proxy = (MenSharpBehaviour)target.AddComponent(MenSharpTestScene.FindType(Proxy)); |
| 30 | + var pairs = MenSharpProxy.SyncPairs(target, quiet: true, undoable: false); |
| 31 | + Assert.AreEqual(1, pairs.Count); |
| 32 | + Assert.AreSame(proxy, pairs[0].proxy); |
| 33 | + return (proxy, pairs[0].udon); |
| 34 | + } |
| 35 | + |
| 36 | + /// What an import from another project leaves: the UdonBehaviour is |
| 37 | + /// there, its Program Source is not, and it is not hidden either. |
| 38 | + [Test] |
| 39 | + public void AnImportedBackingBehaviourIsPairedAgainByReference() |
| 40 | + { |
| 41 | + GameObject target = new GameObject("Imported"); |
| 42 | + (MenSharpBehaviour proxy, UdonBehaviour udon) = Pair(target); |
| 43 | + MenSharpProgramAsset program = MenSharpProxy.FindProgram(proxy.GetType()); |
| 44 | + Assert.AreEqual(program, udon.programSource); |
| 45 | + |
| 46 | + udon.programSource = null; |
| 47 | + udon.hideFlags = HideFlags.None; |
| 48 | + |
| 49 | + Assert.AreSame(udon, MenSharpProxy.FindPaired(proxy)); |
| 50 | + var pairs = MenSharpProxy.SyncPairs(target, quiet: true, undoable: false); |
| 51 | + Assert.AreEqual(1, pairs.Count); |
| 52 | + Assert.AreSame(udon, pairs[0].udon, "the remembered UdonBehaviour is reused, not replaced"); |
| 53 | + Assert.AreEqual(program, udon.programSource); |
| 54 | + Assert.AreEqual(1, target.GetComponents<UdonBehaviour>().Length, "no second UdonBehaviour"); |
| 55 | + } |
| 56 | + |
| 57 | + /// The reference also wins over a stale program of another class's. |
| 58 | + [Test] |
| 59 | + public void ABackingBehaviourCarryingAnotherProgramIsRepointed() |
| 60 | + { |
| 61 | + GameObject target = new GameObject("Swapped"); |
| 62 | + (MenSharpBehaviour proxy, UdonBehaviour udon) = Pair(target); |
| 63 | + MenSharpProgramAsset other = MenSharpProxy.FindProgram(MenSharpTestScene.FindType("Door")); |
| 64 | + Assert.IsNotNull(other); |
| 65 | + udon.programSource = other; |
| 66 | + |
| 67 | + var pairs = MenSharpProxy.SyncPairs(target, quiet: true, undoable: false); |
| 68 | + Assert.AreSame(udon, pairs[0].udon); |
| 69 | + Assert.AreEqual(MenSharpProxy.FindProgram(proxy.GetType()), udon.programSource); |
| 70 | + Assert.AreEqual(1, target.GetComponents<UdonBehaviour>().Length); |
| 71 | + } |
| 72 | + |
| 73 | + /// A proxy pasted onto another GameObject remembers the original's |
| 74 | + /// UdonBehaviour; that one belongs to the original and is left alone. |
| 75 | + [Test] |
| 76 | + public void APastedProxyDoesNotClaimAnotherObjectsBackingBehaviour() |
| 77 | + { |
| 78 | + GameObject original = new GameObject("Original"); |
| 79 | + (MenSharpBehaviour proxy, UdonBehaviour udon) = Pair(original); |
| 80 | + |
| 81 | + GameObject copy = new GameObject("Copy"); |
| 82 | + var pasted = (MenSharpBehaviour)copy.AddComponent(MenSharpTestScene.FindType(Proxy)); |
| 83 | + var serialized = new SerializedObject(pasted); |
| 84 | + serialized.FindProperty("menSharpBacking").objectReferenceValue = udon; |
| 85 | + serialized.ApplyModifiedPropertiesWithoutUndo(); |
| 86 | + |
| 87 | + var pairs = MenSharpProxy.SyncPairs(copy, quiet: true, undoable: false); |
| 88 | + Assert.AreEqual(1, pairs.Count); |
| 89 | + Assert.AreNotSame(udon, pairs[0].udon); |
| 90 | + Assert.AreEqual(copy, pairs[0].udon.gameObject); |
| 91 | + Assert.AreSame(udon, MenSharpProxy.FindPaired(proxy)); |
| 92 | + Assert.AreSame(pairs[0].udon, MenSharpProxy.FindPaired(pasted)); |
| 93 | + } |
| 94 | + |
| 95 | + /// Two proxies of one class on one object, both remembering the same |
| 96 | + /// UdonBehaviour (a duplicated component): the second gets its own. |
| 97 | + [Test] |
| 98 | + public void ADuplicatedProxyGetsItsOwnBackingBehaviour() |
| 99 | + { |
| 100 | + GameObject target = new GameObject("Twice"); |
| 101 | + (MenSharpBehaviour first, UdonBehaviour udon) = Pair(target); |
| 102 | + var second = (MenSharpBehaviour)target.AddComponent(MenSharpTestScene.FindType(Proxy)); |
| 103 | + var serialized = new SerializedObject(second); |
| 104 | + serialized.FindProperty("menSharpBacking").objectReferenceValue = udon; |
| 105 | + serialized.ApplyModifiedPropertiesWithoutUndo(); |
| 106 | + |
| 107 | + var pairs = MenSharpProxy.SyncPairs(target, quiet: true, undoable: false); |
| 108 | + Assert.AreEqual(2, pairs.Count); |
| 109 | + Assert.AreSame(udon, pairs[0].udon); |
| 110 | + Assert.AreNotSame(udon, pairs[1].udon); |
| 111 | + Assert.AreEqual(2, target.GetComponents<UdonBehaviour>().Length); |
| 112 | + Assert.AreSame(pairs[1].udon, MenSharpProxy.FindPaired(second)); |
| 113 | + } |
| 114 | + |
| 115 | + /// A program asset created from scratch gets the GUID its class implies. |
| 116 | + [Test] |
| 117 | + public void ANewProgramAssetGetsTheGuidDerivedFromItsClass() |
| 118 | + { |
| 119 | + string uasm = Path.Combine("Library", "MenSharp", "Door.uasm"); |
| 120 | + string meta = Path.Combine("Library", "MenSharp", "Door.meta.json"); |
| 121 | + Assert.IsTrue(File.Exists(uasm), uasm); |
| 122 | + string folder = "Assets/MenSharp/Programs/GuidProbe"; |
| 123 | + MenSharpTestScene.EnsureFolder(folder); |
| 124 | + string assetPath = folder + "/GuidProbe.Door.asset"; |
| 125 | + AssetDatabase.DeleteAsset(assetPath); |
| 126 | + try |
| 127 | + { |
| 128 | + MenSharpProgramAsset asset = MenSharpImporter.CreateOrUpdate(uasm, meta, assetPath, false, out _); |
| 129 | + Assert.IsNotNull(asset); |
| 130 | + string expected = MenSharpImporter.StableGuid("GuidProbe.Door"); |
| 131 | + Assert.AreEqual(expected, AssetDatabase.AssetPathToGUID(assetPath)); |
| 132 | + Assert.AreEqual(asset, AssetDatabase.LoadAssetAtPath<MenSharpProgramAsset>(assetPath)); |
| 133 | + Assert.IsNotNull(asset.SerializedProgramAsset, "the serialized program is made after the GUID is settled"); |
| 134 | + // and an update keeps it |
| 135 | + MenSharpImporter.CreateOrUpdate(uasm, meta, assetPath, true, out _); |
| 136 | + Assert.AreEqual(expected, AssetDatabase.AssetPathToGUID(assetPath)); |
| 137 | + } |
| 138 | + finally |
| 139 | + { |
| 140 | + var made = AssetDatabase.LoadAssetAtPath<MenSharpProgramAsset>(assetPath); |
| 141 | + string serializedPath = made != null && made.SerializedProgramAsset != null |
| 142 | + ? AssetDatabase.GetAssetPath(made.SerializedProgramAsset) |
| 143 | + : null; |
| 144 | + AssetDatabase.DeleteAsset(assetPath); |
| 145 | + if (!string.IsNullOrEmpty(serializedPath)) |
| 146 | + { |
| 147 | + AssetDatabase.DeleteAsset(serializedPath); |
| 148 | + } |
| 149 | + AssetDatabase.DeleteAsset(folder); |
| 150 | + MenSharpSources.InvalidateProgramIndex(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | +#endif |
0 commit comments