Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions TestProjects/ShaderGraph/Assets/CommonAssets/Editor/FileMoveTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
using System.Collections;
using NUnit.Framework;
using UnityEditor.Experimental.GraphView;
using UnityEditor.ShaderGraph;
using UnityEditor.ShaderGraph.Drawing;
using System.Reflection;
using UnityEngine;
using UnityEngine.TestTools;
using UnityEngine.UIElements;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;

/* Changes:
* Made ShaderGraphImporterEditor.ShowGraphEditWindow public
* Made MaterialGraphEditWindow.graphEditorView public
* Altered MasterPreviewView.OnMouseDragPreviewMesh slightly
*/

namespace UnityEditor.ShaderGraph.UnitTests
{
[TestFixture]
internal class FileMoveTests
{
GraphEditorView m_GraphEditorView;
MaterialGraphEditWindow m_Window;
GraphData m_Graph;

static string sourceDirectoryPath => Application.dataPath + "/../MoveTest";
static string sourceDirectoryPathSub => Application.dataPath + "/../MoveTest/SubFolder";
static string targetDirectoryPath => Application.dataPath + "/Testing/MoveTest";
static string targetDirectoryPath2 => Application.dataPath + "/Testing/MoveTest2";
static string targetDirectoryPathSub => Application.dataPath + "/Testing/MoveTest/SubFolder";
static string targetUnityDirectoryPath => "Assets/Testing/MoveTest";
static string targetUnityDirectoryPath2 => "Assets/Testing/MoveTest2";

[OneTimeSetUp]
public void Setup()
{
// recursive delete
if (Directory.Exists(targetDirectoryPath2))
{
Directory.Delete(targetDirectoryPath2, true);

// sync AssetDatabase
AssetDatabase.DeleteAsset(targetUnityDirectoryPath2);
}

Directory.CreateDirectory(targetDirectoryPath);
Directory.CreateDirectory(targetDirectoryPathSub);

try
{
AssetDatabase.StartAssetEditing();

// copy all files from source directory to target directory
string[] sourceFiles = Directory.GetFiles(sourceDirectoryPath, "*", SearchOption.TopDirectoryOnly);
foreach (var sourceFilePath in sourceFiles)
{
string fileName = Path.GetFileName(sourceFilePath);
File.Copy(sourceFilePath, targetDirectoryPath + "/" + fileName);
}

// copy all files from source directory to target directory
sourceFiles = Directory.GetFiles(sourceDirectoryPathSub, "*", SearchOption.TopDirectoryOnly);
foreach (var sourceFilePath in sourceFiles)
{
string fileName = Path.GetFileName(sourceFilePath);
File.Copy(sourceFilePath, targetDirectoryPathSub + "/" + fileName);
}
}
finally
{
AssetDatabase.StopAssetEditing();
}

// after all files are copied, make sure everything in the directory is imported
AssetDatabase.ImportAsset(targetUnityDirectoryPath, ImportAssetOptions.ImportRecursive);
}

public void OpenGraphWindow(string graphName)
{
// Open up the window
if (!ShaderGraphImporterEditor.ShowGraphEditWindow(graphName))
{
Assert.Fail("ShaderGraphImporterEditor.ShowGraphEditWindow could not open " + graphName);
}

m_Window = EditorWindow.GetWindow<MaterialGraphEditWindow>();

if (m_Window == null)
{
Assert.Fail("Could not open MaterialGraphEditWindow");
}

// EditorWindow.GetWindow will return a new window if one is not found. A new window will have graphObject == null.
if (m_Window.graphObject == null)
{
Assert.Fail("Existing Shader Graph window of " + graphName + " not found.");
}

m_GraphEditorView = m_Window.graphEditorView;
}

[TearDown]
public void CloseGraphWindow()
{
if (m_Window != null)
{
m_Window.graphObject = null; // Don't spawn ask-to-save dialog
m_Window.Close();
}
m_Window = null;
m_GraphEditorView = null;
}

[OneTimeTearDown]
public void Cleanup()
{
try
{

AssetDatabase.StartAssetEditing();

// delete everything from target directory (via assetdatabase to clear anything relevant)
foreach (string assetGuid in AssetDatabase.FindAssets("*", new string[] { targetUnityDirectoryPath }))
{
AssetDatabase.DeleteAsset(AssetDatabase.GUIDToAssetPath(assetGuid));
}

foreach (string assetGuid in AssetDatabase.FindAssets("*", new string[] { targetUnityDirectoryPath2 }))
{
AssetDatabase.DeleteAsset(AssetDatabase.GUIDToAssetPath(assetGuid));
}

FileUtil.DeleteFileOrDirectory(targetDirectoryPath2);
}
finally
{
AssetDatabase.StopAssetEditing();
}

if (Directory.Exists(targetDirectoryPath2))
{
Directory.Delete(targetDirectoryPath2, true);

// sync AssetDatabase
AssetDatabase.DeleteAsset(targetUnityDirectoryPath2);
}
}

// Tests that loading and saving a fully versioned graph file doesn't change the file on disk.
[UnityTest]
public IEnumerator MoveDirectoryTests()
{
yield return null;

// rename the directory
AssetDatabase.MoveAsset(targetUnityDirectoryPath, targetUnityDirectoryPath2);

yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;

var graphPath = targetUnityDirectoryPath2 + "/ShaderGraph.shadergraph";
OpenGraphWindow(graphPath);

yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;

// try adding the subgraph node -- see if the asset still works after the files have been moved
m_Graph = m_Window.graphObject.graph as GraphData;
var subgraph = AssetDatabase.LoadAssetAtPath<SubGraphAsset>(graphPath);
var node = new SubGraphNode { asset = subgraph };
m_Graph.AddNode(node);

yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;
yield return null;

CloseGraphWindow();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions TestProjects/ShaderGraph/MoveTest/Shader Graphs_ShaderGraph.mat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Shader Graphs_ShaderGraph
m_Shader: {fileID: -6465566751694194690, guid: e729d15e0112af8449c81fa47e54974d,
type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- unity_Lightmaps:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_LightmapsInd:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- unity_ShadowMasks:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Ints: []
m_Floats: []
m_Colors: []
m_BuildTextureStacks: []

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading