6
6
7
7
partial class CoreTests
8
8
{
9
+ #region Helpers
10
+
9
11
private static InputActionAsset CreateAssetWithTwoActions ( )
10
12
{
11
13
var map1 = new InputActionMap ( "map1" ) ;
@@ -25,68 +27,130 @@ private static InputActionAsset CreateAssetWithSingleAction()
25
27
return asset ;
26
28
}
27
29
28
- [ Test ]
29
- public void Actions_Reference_AssetAndActionsReturnsNull_IfNotSet ( )
30
+ private void AssertDefaults ( InputActionReference reference )
30
31
{
31
- var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
32
32
Assert . That ( reference . asset , Is . Null ) ;
33
33
Assert . That ( reference . action , Is . Null ) ;
34
+ Assert . That ( reference . name , Is . EqualTo ( string . Empty ) ) ;
34
35
Assert . That ( reference . ToDisplayName ( ) , Is . Null ) ;
36
+ Assert . That ( reference . ToString ( ) , Is . EqualTo ( $ " ({ typeof ( InputActionReference ) . FullName } )") ) ;
35
37
}
36
38
39
+ #endregion
40
+
37
41
[ Test ]
38
- public void Actions_Reference_SetNull ( )
42
+ [ Category ( "Actions" ) ]
43
+ public void Actions_Reference_Defaults ( )
39
44
{
45
+ AssertDefaults ( ScriptableObject . CreateInstance < InputActionReference > ( ) ) ;
46
+ }
47
+
48
+ [ TestCase ( false , "map1" , "action1" ) ]
49
+ [ TestCase ( true , null , "action1" ) ]
50
+ [ TestCase ( true , "map1" , null ) ]
51
+ [ Category ( "Actions" ) ]
52
+ public void Actions_Reference_SetByNameThrows_IfAnyArgumentIsNull ( bool validAsset , string mapName , string actionName )
53
+ {
54
+ var asset = CreateAssetWithTwoActions ( ) ;
40
55
var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
41
- reference . Set ( null ) ;
56
+ Assert . Throws < ArgumentNullException > ( ( ) => reference . Set ( validAsset ? asset : null , mapName , actionName ) ) ;
42
57
}
43
58
44
- [ Test ]
59
+ [ TestCase ( "doesNotExist" , "action1" ) ]
60
+ [ TestCase ( "map1" , "doesNotExist" ) ]
45
61
[ Category ( "Actions" ) ]
46
- public void Actions_Reference_CanResolveAction ( )
62
+ public void Actions_Reference_SetByNameThrows_IfNoMatchingMapOrActionExists ( string mapName , string actionName )
47
63
{
48
64
var asset = CreateAssetWithTwoActions ( ) ;
49
65
var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
66
+ Assert . Throws < ArgumentException > ( ( ) => reference . Set ( asset , mapName , actionName ) ) ;
67
+ }
50
68
51
- reference . Set ( asset , "map1" , "action2" ) ;
69
+ [ Test ]
70
+ [ Category ( "Actions" ) ]
71
+ public void Actions_Reference_SetByReferenceThrows_IfActionDoNotBelongToAnAsset ( )
72
+ {
73
+ // Case 1: Action was never part of any asset
74
+ var action = new InputAction ( "action" ) ;
75
+ var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
76
+ Assert . Throws < InvalidOperationException > ( ( ) => reference . Set ( action ) ) ;
52
77
53
- Assert . That ( reference . action , Is . SameAs ( asset . FindAction ( "map1/action2" ) ) ) ;
78
+ // Case 2: Action was part of an asset but then removed
79
+ var asset = CreateAssetWithTwoActions ( ) ;
80
+ var action1 = asset . FindAction ( "map1/action1" ) ;
81
+ asset . RemoveAction ( "map1/action1" ) ;
82
+ Assert . Throws < InvalidOperationException > ( ( ) => reference . Set ( action1 ) ) ;
54
83
}
55
84
56
85
[ Test ]
57
86
[ Category ( "Actions" ) ]
58
- public void Actions_Reference_CanResolveAction_EvenAfterActionHasBeenRenamed ( )
87
+ public void Actions_Reference_CreateReturnsReference_WhenCreatedFromValidAction ( )
59
88
{
60
- var map = new InputActionMap ( "map" ) ;
61
- var action = map . AddAction ( "oldName" ) ;
62
- var asset = ScriptableObject . CreateInstance < InputActionAsset > ( ) ;
63
- asset . AddActionMap ( map ) ;
89
+ var asset = CreateAssetWithTwoActions ( ) ;
90
+ var action = asset . FindAction ( "map1/action2" ) ;
91
+
92
+ var reference = InputActionReference . Create ( action ) ;
93
+ Assert . That ( reference . action , Is . SameAs ( action ) ) ;
94
+ }
95
+
96
+ [ Test ]
97
+ [ Category ( "Actions" ) ]
98
+ public void Actions_Reference_CreateReturnsNullReferenceObject_IfActionIsNull ( )
99
+ {
100
+ var reference = InputActionReference . Create ( null ) ;
101
+ Assert . That ( reference . action , Is . Null ) ;
102
+ }
103
+
104
+ [ TestCase ( false ) ]
105
+ [ TestCase ( true ) ]
106
+ [ Category ( "Actions" ) ]
107
+ public void Actions_Reference_CanResolveAction_WhenSet ( bool setByReference )
108
+ {
109
+ var asset = CreateAssetWithTwoActions ( ) ;
110
+ var action = asset . FindAction ( "map1/action2" ) ;
64
111
65
112
var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
66
- reference . Set ( asset , "map" , "oldName" ) ;
113
+ if ( setByReference )
114
+ reference . Set ( action ) ;
115
+ else
116
+ reference . Set ( asset , "map1" , "action2" ) ;
67
117
68
- action . Rename ( "newName" ) ;
118
+ Assert . That ( reference . action , Is . SameAs ( action ) ) ;
119
+ }
120
+
121
+ [ Test ]
122
+ [ Category ( "Actions" ) ]
123
+ public void Actions_Reference_CanResolveAction_AfterActionHasBeenRenamed ( )
124
+ {
125
+ var asset = CreateAssetWithSingleAction ( ) ;
126
+ var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
127
+ reference . Set ( asset , "map2" , "action3" ) ;
128
+ var action = asset . FindAction ( "map2/action3" ) ;
69
129
70
- var referencedAction = reference . action ;
130
+ action . Rename ( "newName" ) ;
131
+ Assert . That ( reference . action , Is . SameAs ( action ) ) ;
132
+ }
71
133
72
- Assert . That ( referencedAction , Is . SameAs ( action ) ) ;
134
+ [ Test ]
135
+ [ Category ( "Actions" ) ]
136
+ public void Actions_Reference_CanResolveToDefaultState_AfterActionHasBeenSetToNull ( )
137
+ {
138
+ var asset = CreateAssetWithTwoActions ( ) ;
139
+ var reference = InputActionReference . Create ( asset . FindAction ( "map1/action1" ) ) ;
140
+ reference . Set ( null ) ;
141
+ AssertDefaults ( reference ) ;
73
142
}
74
143
75
144
[ Test ( Description = "https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-1584" ) ]
76
145
[ Category ( "Actions" ) ]
77
- public void Actions_Reference_CanResolveActionAfterReassignment ( )
146
+ public void Actions_Reference_CanResolveActionAfterReassignmentToActionFromAnotherAsset ( )
78
147
{
79
148
var asset1 = CreateAssetWithTwoActions ( ) ;
80
149
var asset2 = CreateAssetWithSingleAction ( ) ;
81
150
82
151
var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
83
152
reference . Set ( asset1 , "map1" , "action1" ) ;
84
- Assert . That ( reference . action , Is . Not . Null ) ;
85
- Assert . That ( reference . action , Is . SameAs ( asset1 . FindAction ( "map1/action1" ) ) ) ; // Redundant, but important for test case
86
- Assert . That ( reference . asset , Is . SameAs ( asset1 ) ) ;
87
- Assert . That ( reference . name , Is . EqualTo ( "map1/action1" ) ) ;
88
- Assert . That ( reference . ToDisplayName ( ) , Is . EqualTo ( "map1/action1" ) ) ;
89
- Assert . That ( reference . ToString ( ) , Is . EqualTo ( ":map1/action1" ) ) ;
153
+ Assert . That ( reference . action , Is . Not . Null ) ; // Looks redundant, but important for test case to resolve
90
154
91
155
reference . Set ( asset2 , "map2" , "action3" ) ;
92
156
Assert . That ( reference . action , Is . Not . Null ) ;
@@ -100,35 +164,38 @@ public void Actions_Reference_CanResolveActionAfterReassignment()
100
164
[ TestCase ( typeof ( InputAction ) ) ]
101
165
[ TestCase ( typeof ( InputActionMap ) ) ]
102
166
[ TestCase ( typeof ( InputActionAsset ) ) ]
103
- public void Actions_Reference_NameShouldReflectReferencedAction ( Type typeToDelete )
167
+ public void Actions_Reference_ShouldReevaluateIfAssociatedEntityIsDeleted ( Type typeToDelete )
104
168
{
105
- var map = new InputActionMap ( "map" ) ;
106
- var action1 = map . AddAction ( "action1" ) ;
107
- var asset = ScriptableObject . CreateInstance < InputActionAsset > ( ) ;
108
- asset . AddActionMap ( map ) ;
169
+ var asset = CreateAssetWithTwoActions ( ) ;
170
+ var action = asset . FindAction ( "map1/action1" ) ;
171
+
172
+ // var map = new InputActionMap("map");
173
+ // var action1 = map.AddAction("action1");
174
+ // var asset = ScriptableObject.CreateInstance<InputActionAsset>();
175
+ // asset.AddActionMap(map);
109
176
110
177
var reference = ScriptableObject . CreateInstance < InputActionReference > ( ) ;
111
- reference . Set ( asset , "map " , "action1" ) ;
178
+ reference . Set ( asset , "map1 " , "action1" ) ;
112
179
113
- Assert . That ( reference . action , Is . SameAs ( action1 ) ) ;
180
+ Assert . That ( reference . action , Is . SameAs ( action ) ) ;
114
181
Assert . That ( reference . asset , Is . SameAs ( asset ) ) ;
115
- Assert . That ( reference . name , Is . EqualTo ( "map /action1" ) ) ;
116
- Assert . That ( reference . ToDisplayName ( ) , Is . EqualTo ( "map /action1" ) ) ;
117
- Assert . That ( reference . ToString ( ) , Is . EqualTo ( ":map /action1" ) ) ;
182
+ Assert . That ( reference . name , Is . EqualTo ( "map1 /action1" ) ) ;
183
+ Assert . That ( reference . ToDisplayName ( ) , Is . EqualTo ( "map1 /action1" ) ) ;
184
+ Assert . That ( reference . ToString ( ) , Is . EqualTo ( ":map1 /action1" ) ) ;
118
185
119
186
// Delete the referenced action directly or indirectly
120
187
if ( typeToDelete == typeof ( InputAction ) )
121
- asset . RemoveAction ( "map /action1" ) ;
188
+ asset . RemoveAction ( "map1 /action1" ) ;
122
189
else if ( typeToDelete == typeof ( InputActionMap ) )
123
- asset . RemoveActionMap ( "map " ) ;
190
+ asset . RemoveActionMap ( "map1 " ) ;
124
191
else if ( typeToDelete == typeof ( InputActionAsset ) )
125
192
UnityEngine . Object . DestroyImmediate ( asset ) ;
126
193
127
194
// TODO reference need to react to this
128
195
Assert . That ( reference . action , Is . Null ) ;
129
196
Assert . That ( reference . asset , Is . SameAs ( asset ) ) ;
130
- Assert . That ( reference . name , Is . EqualTo ( "map /action1" ) ) ; // Unexpected when no longer existing
131
- Assert . That ( reference . ToDisplayName ( ) , Is . EqualTo ( "map /action1" ) ) ; // Unexpected when no longer existing
197
+ Assert . That ( reference . name , Is . EqualTo ( "map1 /action1" ) ) ; // Unexpected when no longer existing
198
+ Assert . That ( reference . ToDisplayName ( ) , Is . EqualTo ( "map1 /action1" ) ) ; // Unexpected when no longer existing
132
199
//Assert.That(reference.ToString(), Is.EqualTo(":" + new Guid(action1.m_Id))); // Unexpected when no longer existing
133
200
}
134
201
@@ -141,7 +208,8 @@ public void Actions_Reference_NameShouldReflectReferencedAction(Type typeToDelet
141
208
// InputActionReference - Do not invalidate if action, action map or asset is deleted/destroyed. .action still returns the action.
142
209
// (FIXED) InputActionAsset.RemoveActionMap - Do not remove actions within the map and they keep a stale reference to the removed map.
143
210
// (FIXED) InputActionAsset.RemoveAction - Throws exception if action do not have any bindings.
144
-
211
+ // (FIXED) InputActionReference.Set(null) - Does not update m_Action. Hence .actions returns an incorrect reference. Also doesn't update ScriptableObject.name consistently with default ScriptableObject.
212
+ // (FIXED) InputActionReference.Create - Returns null if action is null which is not inline with xmldoc description and inconsistent since it is allowed to create a reference and set it to null. If you want a null reference you should not call Create in the first place.
145
213
146
214
// TODO Make a test where action map is deleted
147
215
// TODO Make a test where asset is deleted
0 commit comments