Skip to content

Commit 85bc33a

Browse files
committed
Added a new testing stub for simulating the animator
1 parent 87d0e41 commit 85bc33a

File tree

6 files changed

+222
-0
lines changed

6 files changed

+222
-0
lines changed

Assets/AdncAnimatorHelpers/Editor/Testing/AnimatorStub.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using AdncAnimatorHelpers.Editor.Testing.Utilities;
2+
using NUnit.Framework;
3+
using UnityEngine;
4+
5+
namespace Adnc.AnimatorHelpers.Editors.Testing.Examples {
6+
[Category("Examples")]
7+
public class TestAnimatorStub {
8+
private GameObject _go;
9+
private AnimatorStub _animStub;
10+
11+
[SetUp]
12+
public void Setup () {
13+
_go = new GameObject("AnimatorStub");
14+
}
15+
16+
[TearDown]
17+
public void Teardown () {
18+
Object.DestroyImmediate(_go);
19+
}
20+
21+
[Test]
22+
public void FailsCreationIfNoGameObject () {
23+
var stub = new AnimatorStub(null);
24+
25+
Assert.IsFalse(stub.IsValid);
26+
}
27+
28+
[Test]
29+
public void CreatesIfGameObjectPassedIn () {
30+
var stub = new AnimatorStub(_go);
31+
32+
Assert.IsTrue(stub.IsValid);
33+
}
34+
35+
[Test]
36+
public void AttachRuntimeController () {
37+
var stub = new AnimatorStub(_go);
38+
39+
stub.InjectCtrl();
40+
41+
Assert.AreSame(stub.Animator.runtimeAnimatorController, stub.AnimatorCtrl);
42+
}
43+
44+
[Test]
45+
public void GetAnimatorParameter () {
46+
var stub = new AnimatorStub(_go);
47+
const string param = "test";
48+
49+
stub.AnimatorCtrl.AddParameter(param, AnimatorControllerParameterType.Bool);
50+
stub.InjectCtrl();
51+
var result = stub.Animator.GetBool(param);
52+
53+
Assert.IsFalse(result);
54+
}
55+
56+
[Test]
57+
public void SetAnimatorParameter () {
58+
var stub = new AnimatorStub(_go);
59+
const string param = "test";
60+
61+
stub.AnimatorCtrl.AddParameter(param, AnimatorControllerParameterType.Bool);
62+
stub.InjectCtrl();
63+
stub.Animator.SetBool(param, true);
64+
var result = stub.Animator.GetBool(param);
65+
66+
Assert.IsTrue(result);
67+
}
68+
69+
[Test]
70+
public void CreateNewLayerReturnsLayer () {
71+
var stub = new AnimatorStub(_go);
72+
var layer = stub.AddLayer("Test");
73+
74+
Assert.IsNotNull(layer);
75+
}
76+
77+
[Test]
78+
public void CreateNewLayerAddsAnotherLayer () {
79+
var stub = new AnimatorStub(_go);
80+
stub.AddLayer("Test");
81+
82+
Assert.AreEqual(stub.AnimatorCtrl.layers.Length, 2);
83+
}
84+
85+
[Test]
86+
public void CreateNewLayerSetsName () {
87+
var layerName = "Test";
88+
var stub = new AnimatorStub(_go);
89+
var layer = stub.AddLayer(layerName);
90+
91+
Assert.AreEqual(layerName, layer.name);
92+
}
93+
94+
[Test]
95+
public void CreateNewLayerSetsStateMachine () {
96+
var stub = new AnimatorStub(_go);
97+
var layer = stub.AddLayer("Test");
98+
99+
Assert.IsNotNull(layer.stateMachine);
100+
}
101+
102+
[Test]
103+
public void CreateNewLayerCreatesAtLeastOneState () {
104+
var stub = new AnimatorStub(_go);
105+
var layer = stub.AddLayer("Test");
106+
107+
Assert.IsTrue(layer.stateMachine.states.Length >= 1);
108+
}
109+
110+
[Test]
111+
public void CreateNewLayerSetsDefaultState () {
112+
var stub = new AnimatorStub(_go);
113+
var layer = stub.AddLayer("Test");
114+
115+
Assert.IsNotNull(layer.stateMachine.defaultState);
116+
}
117+
118+
[Test]
119+
public void PlayAdvancesToTheNextState () {
120+
var stub = new AnimatorStub(_go);
121+
var layer = stub.AnimatorCtrl.layers[0];
122+
const string stateName = "New State";
123+
var state = layer.stateMachine.AddState(stateName);
124+
var trans = layer.stateMachine.defaultState.AddTransition(state);
125+
126+
layer.stateMachine.defaultState.AddTransition(state);
127+
trans.hasExitTime = true;
128+
stub.InjectCtrl();
129+
stub.Animator.Update(10);
130+
131+
var stateInfo = stub.Animator.GetCurrentAnimatorStateInfo(0);
132+
Assert.IsTrue(stateInfo.IsName(stateName));
133+
}
134+
}
135+
}

Assets/AdncAnimatorHelpers/Editor/Testing/AnimatorStub/TestAnimatorStub.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/AdncAnimatorHelpers/Editor/Testing/Utilities.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using UnityEditor.Animations;
2+
using UnityEngine;
3+
4+
namespace AdncAnimatorHelpers.Editor.Testing.Utilities {
5+
public class AnimatorStub {
6+
/// <summary>
7+
/// Reference to the Animator attached to the passed GameObject
8+
/// </summary>
9+
public Animator Animator { get; private set; }
10+
11+
/// <summary>
12+
/// Reference to an automatically generated AnimatorController
13+
/// </summary>
14+
public AnimatorController AnimatorCtrl { get; private set; }
15+
16+
/// <summary>
17+
/// Is this a valid AnimatorStub?
18+
/// </summary>
19+
public bool IsValid {
20+
get { return Animator != null && AnimatorCtrl != null; }
21+
}
22+
23+
/// <summary>
24+
/// Inject the animator stub onto a GameObject
25+
/// </summary>
26+
/// <param name="target"></param>
27+
public AnimatorStub (GameObject target) {
28+
if (target == null) {
29+
LogError("Please pass in a target to verify. Failed to generate an animator stub");
30+
31+
return;
32+
}
33+
34+
Animator = target.AddComponent<Animator>();
35+
AnimatorCtrl = new AnimatorController {name = "AnimatorDefault"};
36+
37+
AddLayer("Default");
38+
}
39+
40+
/// <summary>
41+
/// Create new layers on the Animator with a specific name
42+
/// </summary>
43+
/// <param name="name"></param>
44+
/// <returns></returns>
45+
public AnimatorControllerLayer AddLayer (string name) {
46+
var layer = new AnimatorControllerLayer {
47+
name = name,
48+
stateMachine = new AnimatorStateMachine {
49+
name = "Default"
50+
}
51+
};
52+
53+
layer.stateMachine.defaultState = layer.stateMachine.AddState("Default");
54+
AnimatorCtrl.AddLayer(layer);
55+
56+
return layer;
57+
}
58+
59+
/// <summary>
60+
/// Should be called when you want to inject your AnimatorController into you Animator. WARNING! Once you
61+
/// do this it creates a static instance of your AnimatorController and you cannot change it.
62+
/// </summary>
63+
public void InjectCtrl () {
64+
Animator.runtimeAnimatorController = AnimatorCtrl;
65+
}
66+
67+
void LogError (string error) {
68+
if (!Application.isPlaying) {
69+
return;
70+
}
71+
72+
Debug.LogError(error);
73+
}
74+
}
75+
}

Assets/AdncAnimatorHelpers/Editor/Testing/Utilities/AnimatorStub.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)