1- //#define NAVMESHSURFACE_CLEANUP_LEAKED_DATA_ASSETS
1+ //#define KEEP_ARTIFACTS_FOR_INSPECTION
22
33using System ;
4- using UnityEngine ;
5- using UnityEngine . TestTools ;
6- using NUnit . Framework ;
74using System . Collections ;
85using System . IO ;
6+ using NUnit . Framework ;
97using UnityEditor ;
108using UnityEditor . AI ;
119using UnityEditor . Experimental . SceneManagement ;
1210using UnityEditor . SceneManagement ;
11+ using UnityEngine ;
1312using UnityEngine . AI ;
1413using UnityEngine . SceneManagement ;
14+ using UnityEngine . TestTools ;
1515using Object = UnityEngine . Object ;
1616
1717[ Category ( "PrefabsWithNavMeshComponents" ) ]
@@ -26,9 +26,6 @@ public class NavMeshSurfaceInPrefabTests
2626 string m_TempScenePath ;
2727 int m_TestCounter ;
2828
29- const int k_BlueArea = 0 ;
30- const int k_PinkArea = 3 ;
31- const int k_GreenArea = 4 ;
3229 const int k_GrayArea = 7 ;
3330 const int k_BrownArea = 10 ;
3431 const int k_RedArea = 18 ;
@@ -37,21 +34,13 @@ public class NavMeshSurfaceInPrefabTests
3734
3835 const int k_PrefabDefaultArea = k_YellowArea ;
3936
40- #if NAVMESHSURFACE_CLEANUP_LEAKED_DATA_ASSETS
41- string m_InitialPrefabNavMeshDataPath ;
42- #endif
43-
4437 [ OneTimeSetUp ]
4538 public void OneTimeSetup ( )
4639 {
47- //if (System.IO.Directory.Exists(m_TempFolder))
48- AssetDatabase . DeleteAsset ( m_TempFolder ) ;
40+ AssetDatabase . DeleteAsset ( m_TempFolder ) ;
4941
50- //if (!System.IO.Directory.Exists(m_TempFolder))
51- //{
52- var folderGUID = AssetDatabase . CreateFolder ( k_ParentFolder , k_TempFolderName ) ;
53- m_TempFolder = AssetDatabase . GUIDToAssetPath ( folderGUID ) ;
54- //}
42+ var folderGUID = AssetDatabase . CreateFolder ( k_ParentFolder , k_TempFolderName ) ;
43+ m_TempFolder = AssetDatabase . GUIDToAssetPath ( folderGUID ) ;
5544
5645 SessionState . SetBool ( k_AutoSaveKey , PrefabStageAutoSavingUtil . GetPrefabStageAutoSave ( ) ) ;
5746 PrefabStageAutoSavingUtil . SetPrefabStageAutoSave ( false ) ;
@@ -77,9 +66,9 @@ public void OneTimeTearDown()
7766 EditorSceneManager . NewScene ( NewSceneSetup . DefaultGameObjects , NewSceneMode . Single ) ;
7867 }
7968
80- //File.Delete(m_TempScenePath);
81- //if (System.IO.Directory.Exists (m_TempFolder))
82- AssetDatabase . DeleteAsset ( m_TempFolder ) ;
69+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
70+ AssetDatabase . DeleteAsset ( m_TempFolder ) ;
71+ #endif
8372 }
8473
8574 [ UnitySetUp ]
@@ -90,9 +79,6 @@ public IEnumerator Setup()
9079 var surface = plane . AddComponent < NavMeshSurface > ( ) ;
9180 surface . collectObjects = CollectObjects . Children ;
9281
93- #if NAVMESHSURFACE_CLEANUP_LEAKED_DATA_ASSETS
94- m_InitialPrefabNavMeshDataPath = AssetDatabase . GetAssetPath ( surface . navMeshData ) ;
95- #endif
9682 m_PrefabPath = Path . Combine ( m_TempFolder , plane . name + ".prefab" ) ;
9783 var planePrefab = PrefabUtility . SaveAsPrefabAsset ( plane , m_PrefabPath ) ;
9884 Object . DestroyImmediate ( plane ) ;
@@ -112,32 +98,12 @@ public IEnumerator Setup()
11298 [ UnityTearDown ]
11399 public IEnumerator TearDown ( )
114100 {
115- //if (System.IO.File.Exists(m_PrefabPath))
116- //{
117- // var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(m_PrefabPath);
118- // AssetDatabase.OpenAsset(prefab);
119- // var prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
120- // if (prefabStage != null && prefabStage.prefabContentsRoot != null)
121- // {
122- // var prefabSurface = prefabStage.prefabContentsRoot.GetComponent<NavMeshSurface>();
123- // if (prefabSurface != null)
124- // {
125- // NavMeshDataAssetManager.instance.ClearSurfaces(new Object[] { prefabSurface });
126- // }
127- // }
128-
129- // AssetDatabase.DeleteAsset(m_PrefabPath);
130- //}
131-
132101 var prefabStage = PrefabStageUtility . GetCurrentPrefabStage ( ) ;
133102 if ( prefabStage != null )
134103 prefabStage . ClearDirtiness ( ) ;
135104
136105 StageUtility . GoToMainStage ( ) ;
137106
138- #if NAVMESHSURFACE_CLEANUP_LEAKED_DATA_ASSETS
139- AssetDatabase . DeleteAsset ( m_InitialPrefabNavMeshDataPath ) ;
140- #endif
141107 yield return null ;
142108 }
143109
@@ -150,7 +116,7 @@ static void TestNavMeshExistsAloneAtPosition(int expectedArea, Vector3 pos)
150116 Debug . Log ( " mask=" + expectedAreaMask . ToString ( "x8" ) + " area " + expectedArea + " Exists=" + areaExists + " otherAreasExist=" + otherAreasExist + " at position " + pos ) ;
151117 if ( otherAreasExist )
152118 {
153- for ( int i = 0 ; i < 32 ; i ++ )
119+ for ( var i = 0 ; i < 32 ; i ++ )
154120 {
155121 if ( i == expectedArea )
156122 continue ;
@@ -264,9 +230,10 @@ public IEnumerator NavMeshSurfacePrefab_WhenInstantiated_ReferencesTheSameNavMes
264230
265231 StageUtility . GoToMainStage ( ) ;
266232
233+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
267234 Object . DestroyImmediate ( instance ) ;
268235 Object . DestroyImmediate ( instanceClone ) ;
269-
236+ #endif
270237 yield return null ;
271238 }
272239
@@ -292,8 +259,9 @@ public IEnumerator NavMeshSurfacePrefab_WhenEmptyAndInstantiated_InstanceHasEmpt
292259 var expectedAreaMask = 1 << k_PrefabDefaultArea ;
293260 Assert . IsFalse ( HasNavMeshAtPosition ( Vector3 . zero , expectedAreaMask ) ) ;
294261
262+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
295263 Object . DestroyImmediate ( instance ) ;
296-
264+ #endif
297265 yield return null ;
298266 }
299267
@@ -333,9 +301,10 @@ public IEnumerator NavMeshSurfacePrefab_WhenBakesNewNavMesh_UpdatesTheInstance()
333301 TestNavMeshExistsAloneAtPosition ( k_BrownArea , Vector3 . zero ) ;
334302 TestNavMeshExistsAloneAtPosition ( k_RedArea , instanceTwo . transform . position ) ;
335303
304+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
336305 Object . DestroyImmediate ( instanceOne ) ;
337306 Object . DestroyImmediate ( instanceTwo ) ;
338-
307+ #endif
339308 yield return null ;
340309 }
341310
@@ -377,9 +346,10 @@ public IEnumerator NavMeshSurfacePrefab_WhenInstanceRebaked_HasDifferentNavMeshD
377346
378347 StageUtility . GoToMainStage ( ) ;
379348
349+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
380350 Object . DestroyImmediate ( instance ) ;
381351 Object . DestroyImmediate ( instanceClone ) ;
382-
352+ #endif
383353 yield return null ;
384354 }
385355
@@ -420,9 +390,10 @@ public IEnumerator NavMeshSurfacePrefab_WhenInstanceCleared_InstanceHasEmptyNavM
420390
421391 StageUtility . GoToMainStage ( ) ;
422392
393+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
423394 Object . DestroyImmediate ( instance ) ;
424395 Object . DestroyImmediate ( instanceClone ) ;
425-
396+ #endif
426397 yield return null ;
427398 }
428399
@@ -452,8 +423,9 @@ public IEnumerator NavMeshSurfacePrefab_WhenInstanceCleared_PrefabKeepsNavMeshDa
452423
453424 StageUtility . GoToMainStage ( ) ;
454425
426+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
455427 Object . DestroyImmediate ( instance ) ;
456-
428+ #endif
457429 yield return null ;
458430 }
459431
@@ -492,8 +464,9 @@ public IEnumerator NavMeshSurfacePrefab_WhenRebakedButInstanceModified_DoesNotCh
492464 StageUtility . GoToMainStage ( ) ;
493465 Assert . AreSame ( instanceNavMeshData , instanceSurface . navMeshData ) ;
494466
467+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
495468 Object . DestroyImmediate ( instance ) ;
496-
469+ #endif
497470 yield return null ;
498471 }
499472
@@ -539,12 +512,12 @@ public IEnumerator NavMeshSurfacePrefab_WhenRebakedButNotSaved_TheRebakedAssetNo
539512 yield return BakeNavMeshAsync ( ( ) => prefabSurface , k_GrayArea ) ;
540513 var rebakedAssetPath = AssetDatabase . GetAssetPath ( prefabSurface . navMeshData ) ;
541514
542- Assert . IsTrue ( System . IO . File . Exists ( rebakedAssetPath ) , "NavMeshData file must exist. ({0})" , rebakedAssetPath ) ;
515+ Assert . IsTrue ( File . Exists ( rebakedAssetPath ) , "NavMeshData file must exist. ({0})" , rebakedAssetPath ) ;
543516
544517 prefabStage . ClearDirtiness ( ) ;
545518 StageUtility . GoToMainStage ( ) ;
546519
547- Assert . IsFalse ( System . IO . File . Exists ( rebakedAssetPath ) , "NavMeshData file still exists after discarding the changes. ({0})" , rebakedAssetPath ) ;
520+ Assert . IsFalse ( File . Exists ( rebakedAssetPath ) , "NavMeshData file still exists after discarding the changes. ({0})" , rebakedAssetPath ) ;
548521
549522 yield return null ;
550523 }
@@ -561,12 +534,12 @@ public IEnumerator NavMeshSurfacePrefab_WhenRebaked_TheOldAssetExistsUntilSaving
561534
562535 // Assert.IsNull cannot verify correctly that an UnityEngine.Object is null
563536 Assert . IsTrue ( initialNavMeshData != null , "Prefab must have some NavMeshData." ) ;
564- Assert . IsTrue ( System . IO . File . Exists ( initialAssetPath ) , "NavMeshData file must exist. ({0})" , initialAssetPath ) ;
537+ Assert . IsTrue ( File . Exists ( initialAssetPath ) , "NavMeshData file must exist. ({0})" , initialAssetPath ) ;
565538
566539 yield return BakeNavMeshAsync ( ( ) => prefabSurface , k_GrayArea ) ;
567540
568541 Assert . IsTrue ( initialNavMeshData != null , "The initial NavMeshData must still exist immediately after prefab re-bake." ) ;
569- Assert . IsTrue ( System . IO . File . Exists ( initialAssetPath ) , "The initial NavMeshData file must exist after prefab re-bake. ({0})" , initialAssetPath ) ;
542+ Assert . IsTrue ( File . Exists ( initialAssetPath ) , "The initial NavMeshData file must exist after prefab re-bake. ({0})" , initialAssetPath ) ;
570543
571544 Assert . IsTrue ( prefabSurface . navMeshData != null , "NavMeshSurface must have NavMeshData after baking." ) ;
572545 var unsavedRebakedNavMeshData = prefabSurface . navMeshData ;
@@ -577,7 +550,7 @@ public IEnumerator NavMeshSurfacePrefab_WhenRebaked_TheOldAssetExistsUntilSaving
577550 Assert . IsTrue ( prefabSurface . navMeshData != null , "NavMeshSurface must have NavMeshData after baking." ) ;
578551
579552 PrefabSavingUtil . SavePrefab ( prefabStage ) ;
580- Assert . IsFalse ( System . IO . File . Exists ( initialAssetPath ) , "NavMeshData file still exists after saving. ({0})" , initialAssetPath ) ;
553+ Assert . IsFalse ( File . Exists ( initialAssetPath ) , "NavMeshData file still exists after saving. ({0})" , initialAssetPath ) ;
581554 Assert . IsTrue ( initialNavMeshData == null , "The initial NavMeshData must no longer exist after saving the prefab." ) ;
582555
583556 // ReSharper disable once HeuristicUnreachableCode - initialNavMeshData is affected by BakeNavMeshAsync()
@@ -618,7 +591,7 @@ public IEnumerator NavMeshSurfacePrefab_WhenRebakedAndAutoSaved_InstanceHasTheNe
618591 yield return null ;
619592 }
620593
621- [ Ignore ( "Deletion of the old asset is expected to be done manually for the time being ." ) ]
594+ [ Ignore ( "Currently the deletion of the old asset must be done manually." ) ]
622595 [ UnityTest ]
623596 public IEnumerator NavMeshSurfacePrefab_AfterModifiedInstanceAppliedBack_TheOldAssetNoLongerExists ( )
624597 {
@@ -633,21 +606,22 @@ public IEnumerator NavMeshSurfacePrefab_AfterModifiedInstanceAppliedBack_TheOldA
633606
634607 var initialInstanceAssetPath = AssetDatabase . GetAssetPath ( instanceSurface . navMeshData ) ;
635608
636- Assert . IsTrue ( System . IO . File . Exists ( initialInstanceAssetPath ) , "Prefab's NavMeshData file must exist. ({0})" , initialInstanceAssetPath ) ;
609+ Assert . IsTrue ( File . Exists ( initialInstanceAssetPath ) , "Prefab's NavMeshData file must exist. ({0})" , initialInstanceAssetPath ) ;
637610
638611 yield return BakeNavMeshAsync ( ( ) => instanceSurface , k_RedArea ) ;
639612
640- Assert . IsTrue ( System . IO . File . Exists ( initialInstanceAssetPath ) ,
613+ Assert . IsTrue ( File . Exists ( initialInstanceAssetPath ) ,
641614 "Prefab's NavMeshData file exists after the instance has changed. ({0})" , initialInstanceAssetPath ) ;
642615
643616 PrefabUtility . ApplyPrefabInstance ( instance , InteractionMode . AutomatedAction ) ;
644617
645- Assert . IsFalse ( System . IO . File . Exists ( initialInstanceAssetPath ) ,
618+ Assert . IsFalse ( File . Exists ( initialInstanceAssetPath ) ,
646619 "Prefab's NavMeshData file still exists after the changes from the instance have been applied back to the prefab. ({0})" ,
647620 initialInstanceAssetPath ) ;
648621
622+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
649623 Object . DestroyImmediate ( instance ) ;
650-
624+ #endif
651625 yield return null ;
652626 }
653627
@@ -690,9 +664,10 @@ public IEnumerator NavMeshSurfacePrefab_AfterModifiedInstanceAppliedBack_Updated
690664 TestNavMeshExistsAloneAtPosition ( k_GrayArea , Vector3 . zero ) ;
691665 TestNavMeshExistsAloneAtPosition ( k_GrayArea , instanceTwo . transform . position ) ;
692666
667+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
693668 Object . DestroyImmediate ( instanceOne ) ;
694669 Object . DestroyImmediate ( instanceTwo ) ;
695-
670+ #endif
696671 yield return null ;
697672 }
698673
@@ -724,8 +699,9 @@ public IEnumerator NavMeshSurfacePrefab_AfterClearedInstanceAppliedBack_HasEmpty
724699
725700 StageUtility . GoToMainStage ( ) ;
726701
702+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
727703 Object . DestroyImmediate ( instance ) ;
728-
704+ #endif
729705 yield return null ;
730706 }
731707
@@ -748,8 +724,9 @@ public IEnumerator NavMeshSurfacePrefab_WhenInstanceRevertsBack_InstanceIsLikePr
748724
749725 TestNavMeshExistsAloneAtPosition ( k_PrefabDefaultArea , Vector3 . zero ) ;
750726
727+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
751728 Object . DestroyImmediate ( instance ) ;
752-
729+ #endif
753730 yield return null ;
754731 }
755732
@@ -769,23 +746,24 @@ public IEnumerator NavMeshSurfacePrefab_WhenInstanceRevertsBack_TheInstanceAsset
769746
770747 var instanceAssetPath = AssetDatabase . GetAssetPath ( instanceSurface . navMeshData ) ;
771748
772- Assert . IsTrue ( System . IO . File . Exists ( instanceAssetPath ) , "Instance's NavMeshData file must exist. ({0})" , instanceAssetPath ) ;
749+ Assert . IsTrue ( File . Exists ( instanceAssetPath ) , "Instance's NavMeshData file must exist. ({0})" , instanceAssetPath ) ;
773750
774751 PrefabUtility . RevertPrefabInstance ( instance , InteractionMode . AutomatedAction ) ;
775752
776- Assert . IsFalse ( System . IO . File . Exists ( instanceAssetPath ) , "Instance's NavMeshData file still exists after revert. ({0})" , instanceAssetPath ) ;
753+ Assert . IsFalse ( File . Exists ( instanceAssetPath ) , "Instance's NavMeshData file still exists after revert. ({0})" , instanceAssetPath ) ;
777754
755+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
778756 Object . DestroyImmediate ( instance ) ;
779-
757+ #endif
780758 yield return null ;
781759 }
782760
783- [ Ignore ( "Undefined expected behaviour for the time being ." ) ]
761+ [ Ignore ( "The expected behaviour has not been decided ." ) ]
784762 [ UnityTest ]
785763 public IEnumerator NavMeshSurfacePrefab_WhenDeleted_InstancesMakeCopiesOfData ( )
786764 {
787- Assert . IsTrue ( false ) ;
788765 yield return null ;
766+ Assert . Fail ( "not implemented yet" ) ;
789767 }
790768
791769 [ UnityTest ]
@@ -815,9 +793,10 @@ public IEnumerator NavMeshSurfacePrefab_WhenBakingInPreviewScene_CollectsOnlyPre
815793 "NavMesh with the prefab's area exists at position {1}, outside the prefab's plane. ({0})" ,
816794 k_RedArea , posNearby ) ;
817795
796+ #if ! KEEP_ARTIFACTS_FOR_INSPECTION
818797 Object . DestroyImmediate ( instance ) ;
819798 Object . DestroyImmediate ( mainScenePlane ) ;
820-
799+ #endif
821800 yield return null ;
822801 }
823802
0 commit comments