|
| 1 | +using System.Collections; |
| 2 | +using NUnit.Framework; |
| 3 | +using UnityEditor.Experimental.GraphView; |
| 4 | +using UnityEditor.ShaderGraph; |
| 5 | +using UnityEditor.ShaderGraph.Drawing; |
| 6 | +using System.Reflection; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.TestTools; |
| 9 | +using UnityEngine.UIElements; |
| 10 | +using System.IO; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Text.RegularExpressions; |
| 13 | + |
| 14 | +/* Changes: |
| 15 | + * Made ShaderGraphImporterEditor.ShowGraphEditWindow public |
| 16 | + * Made MaterialGraphEditWindow.graphEditorView public |
| 17 | + * Altered MasterPreviewView.OnMouseDragPreviewMesh slightly |
| 18 | + */ |
| 19 | + |
| 20 | +namespace UnityEditor.ShaderGraph.UnitTests |
| 21 | +{ |
| 22 | + [TestFixture] |
| 23 | + internal class FileMoveTests |
| 24 | + { |
| 25 | + GraphEditorView m_GraphEditorView; |
| 26 | + MaterialGraphEditWindow m_Window; |
| 27 | + GraphData m_Graph; |
| 28 | + |
| 29 | + static string sourceDirectoryPath => Application.dataPath + "/../MoveTest"; |
| 30 | + static string sourceDirectoryPathSub => Application.dataPath + "/../MoveTest/SubFolder"; |
| 31 | + static string targetDirectoryPath => Application.dataPath + "/Testing/MoveTest"; |
| 32 | + static string targetDirectoryPath2 => Application.dataPath + "/Testing/MoveTest2"; |
| 33 | + static string targetDirectoryPathSub => Application.dataPath + "/Testing/MoveTest/SubFolder"; |
| 34 | + static string targetUnityDirectoryPath => "Assets/Testing/MoveTest"; |
| 35 | + static string targetUnityDirectoryPath2 => "Assets/Testing/MoveTest2"; |
| 36 | + |
| 37 | + [OneTimeSetUp] |
| 38 | + public void Setup() |
| 39 | + { |
| 40 | + // recursive delete |
| 41 | + if (Directory.Exists(targetDirectoryPath2)) |
| 42 | + { |
| 43 | + Directory.Delete(targetDirectoryPath2, true); |
| 44 | + |
| 45 | + // sync AssetDatabase |
| 46 | + AssetDatabase.DeleteAsset(targetUnityDirectoryPath2); |
| 47 | + } |
| 48 | + |
| 49 | + Directory.CreateDirectory(targetDirectoryPath); |
| 50 | + Directory.CreateDirectory(targetDirectoryPathSub); |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + AssetDatabase.StartAssetEditing(); |
| 55 | + |
| 56 | + // copy all files from source directory to target directory |
| 57 | + string[] sourceFiles = Directory.GetFiles(sourceDirectoryPath, "*", SearchOption.TopDirectoryOnly); |
| 58 | + foreach (var sourceFilePath in sourceFiles) |
| 59 | + { |
| 60 | + string fileName = Path.GetFileName(sourceFilePath); |
| 61 | + File.Copy(sourceFilePath, targetDirectoryPath + "/" + fileName); |
| 62 | + } |
| 63 | + |
| 64 | + // copy all files from source directory to target directory |
| 65 | + sourceFiles = Directory.GetFiles(sourceDirectoryPathSub, "*", SearchOption.TopDirectoryOnly); |
| 66 | + foreach (var sourceFilePath in sourceFiles) |
| 67 | + { |
| 68 | + string fileName = Path.GetFileName(sourceFilePath); |
| 69 | + File.Copy(sourceFilePath, targetDirectoryPathSub + "/" + fileName); |
| 70 | + } |
| 71 | + } |
| 72 | + finally |
| 73 | + { |
| 74 | + AssetDatabase.StopAssetEditing(); |
| 75 | + } |
| 76 | + |
| 77 | + // after all files are copied, make sure everything in the directory is imported |
| 78 | + AssetDatabase.ImportAsset(targetUnityDirectoryPath, ImportAssetOptions.ImportRecursive); |
| 79 | + } |
| 80 | + |
| 81 | + public void OpenGraphWindow(string graphName) |
| 82 | + { |
| 83 | + // Open up the window |
| 84 | + if (!ShaderGraphImporterEditor.ShowGraphEditWindow(graphName)) |
| 85 | + { |
| 86 | + Assert.Fail("ShaderGraphImporterEditor.ShowGraphEditWindow could not open " + graphName); |
| 87 | + } |
| 88 | + |
| 89 | + m_Window = EditorWindow.GetWindow<MaterialGraphEditWindow>(); |
| 90 | + |
| 91 | + if (m_Window == null) |
| 92 | + { |
| 93 | + Assert.Fail("Could not open MaterialGraphEditWindow"); |
| 94 | + } |
| 95 | + |
| 96 | + // EditorWindow.GetWindow will return a new window if one is not found. A new window will have graphObject == null. |
| 97 | + if (m_Window.graphObject == null) |
| 98 | + { |
| 99 | + Assert.Fail("Existing Shader Graph window of " + graphName + " not found."); |
| 100 | + } |
| 101 | + |
| 102 | + m_GraphEditorView = m_Window.graphEditorView; |
| 103 | + } |
| 104 | + |
| 105 | + [TearDown] |
| 106 | + public void CloseGraphWindow() |
| 107 | + { |
| 108 | + if (m_Window != null) |
| 109 | + { |
| 110 | + m_Window.graphObject = null; // Don't spawn ask-to-save dialog |
| 111 | + m_Window.Close(); |
| 112 | + } |
| 113 | + m_Window = null; |
| 114 | + m_GraphEditorView = null; |
| 115 | + } |
| 116 | + |
| 117 | + [OneTimeTearDown] |
| 118 | + public void Cleanup() |
| 119 | + { |
| 120 | + try |
| 121 | + { |
| 122 | + |
| 123 | + AssetDatabase.StartAssetEditing(); |
| 124 | + |
| 125 | + // delete everything from target directory (via assetdatabase to clear anything relevant) |
| 126 | + foreach (string assetGuid in AssetDatabase.FindAssets("*", new string[] { targetUnityDirectoryPath })) |
| 127 | + { |
| 128 | + AssetDatabase.DeleteAsset(AssetDatabase.GUIDToAssetPath(assetGuid)); |
| 129 | + } |
| 130 | + |
| 131 | + foreach (string assetGuid in AssetDatabase.FindAssets("*", new string[] { targetUnityDirectoryPath2 })) |
| 132 | + { |
| 133 | + AssetDatabase.DeleteAsset(AssetDatabase.GUIDToAssetPath(assetGuid)); |
| 134 | + } |
| 135 | + |
| 136 | + FileUtil.DeleteFileOrDirectory(targetDirectoryPath2); |
| 137 | + } |
| 138 | + finally |
| 139 | + { |
| 140 | + AssetDatabase.StopAssetEditing(); |
| 141 | + } |
| 142 | + |
| 143 | + if (Directory.Exists(targetDirectoryPath2)) |
| 144 | + { |
| 145 | + Directory.Delete(targetDirectoryPath2, true); |
| 146 | + |
| 147 | + // sync AssetDatabase |
| 148 | + AssetDatabase.DeleteAsset(targetUnityDirectoryPath2); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Tests that loading and saving a fully versioned graph file doesn't change the file on disk. |
| 153 | + [UnityTest] |
| 154 | + public IEnumerator MoveDirectoryTests() |
| 155 | + { |
| 156 | + yield return null; |
| 157 | + |
| 158 | + // rename the directory |
| 159 | + AssetDatabase.MoveAsset(targetUnityDirectoryPath, targetUnityDirectoryPath2); |
| 160 | + |
| 161 | + yield return null; |
| 162 | + yield return null; |
| 163 | + yield return null; |
| 164 | + yield return null; |
| 165 | + yield return null; |
| 166 | + yield return null; |
| 167 | + yield return null; |
| 168 | + yield return null; |
| 169 | + |
| 170 | + var graphPath = targetUnityDirectoryPath2 + "/ShaderGraph.shadergraph"; |
| 171 | + OpenGraphWindow(graphPath); |
| 172 | + |
| 173 | + yield return null; |
| 174 | + yield return null; |
| 175 | + yield return null; |
| 176 | + yield return null; |
| 177 | + yield return null; |
| 178 | + yield return null; |
| 179 | + yield return null; |
| 180 | + yield return null; |
| 181 | + |
| 182 | + // try adding the subgraph node -- see if the asset still works after the files have been moved |
| 183 | + m_Graph = m_Window.graphObject.graph as GraphData; |
| 184 | + var subgraph = AssetDatabase.LoadAssetAtPath<SubGraphAsset>(graphPath); |
| 185 | + var node = new SubGraphNode { asset = subgraph }; |
| 186 | + m_Graph.AddNode(node); |
| 187 | + |
| 188 | + yield return null; |
| 189 | + yield return null; |
| 190 | + yield return null; |
| 191 | + yield return null; |
| 192 | + yield return null; |
| 193 | + yield return null; |
| 194 | + yield return null; |
| 195 | + yield return null; |
| 196 | + |
| 197 | + CloseGraphWindow(); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments