Skip to content

Commit ea51d3e

Browse files
ekcohduckets
andauthored
FIX: ISX-1954 UI action requirement verification and fixed a problem causing null-reference access when actions are missing from Project-wide Input Actions Asset. (#1903)
* FIX: Eliminated nullref exceptions via checked access of referenced actions since they may not exist. Added editor and run-time warnings to detect non-compliant UI actions. --------- Co-authored-by: Ben Pitt <[email protected]>
1 parent 2aee531 commit ea51d3e

File tree

13 files changed

+697
-110
lines changed

13 files changed

+697
-110
lines changed

Assets/Tests/InputSystem.Editor/ProjectWideInputActionsEditorTests.cs

Lines changed: 126 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
22

33
using System;
4+
using System.Collections.Generic;
45
using System.IO;
56
using System.Text.RegularExpressions;
67
using NUnit.Framework;
78
using UnityEngine;
89
using UnityEditor;
10+
using UnityEditor.SearchService;
911
using UnityEngine.InputSystem;
1012
using UnityEngine.InputSystem.Editor;
1113
using UnityEngine.InputSystem.Utilities;
@@ -214,40 +216,143 @@ public void ProjectWideActionsAsset_DefaultAssetFileHasDefaultContent()
214216
Assert.That(parsedAssetName, Is.EqualTo(expectedName));
215217
}
216218

217-
// This test is only relevant for the InputForUI module which native part was introduced in 2023.2
218-
#if UNITY_2023_2_OR_NEWER
219-
[Test(Description = "Verifies that modifying the default project-wide action UI map generates console warnings")]
219+
private class TestReporter : ProjectWideActionsAsset.IReportInputActionAssetVerificationErrors
220+
{
221+
public const string kExceptionMessage = "Intentional Exception";
222+
public readonly List<string> messages;
223+
public bool throwsException;
224+
225+
public TestReporter(List<string> messages = null, bool throwsException = false)
226+
{
227+
this.messages = messages;
228+
this.throwsException = throwsException;
229+
}
230+
231+
public void Report(string message)
232+
{
233+
if (throwsException)
234+
throw new Exception(kExceptionMessage);
235+
messages?.Add(message);
236+
}
237+
}
238+
239+
[Test(Description = "Verifies that the default asset do not generate any verification errors (Regardless of existing requirements)")]
220240
[Category(kTestCategory)]
221-
public void ProjectWideActions_ShowsErrorWhenUIActionMapHasNameChanges()
241+
public void ProjectWideActions_ShouldSupportAssetVerification_AndHaveNoVerificationErrorsForDefaultAsset()
222242
{
223-
// Create a default template asset that we then modify to generate various warnings
243+
var messages = new List<string>();
224244
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath();
245+
ProjectWideActionsAsset.Verify(asset, new TestReporter(messages));
246+
Assert.That(messages.Count, Is.EqualTo(0));
247+
}
225248

226-
var indexOf = asset.m_ActionMaps.IndexOf(x => x.name == "UI");
227-
var uiMap = asset.m_ActionMaps[indexOf];
249+
class TestVerifier : ProjectWideActionsAsset.IInputActionAssetVerifier
250+
{
251+
public const string kFailureMessage = "Intentional failure";
252+
public InputActionAsset forwardedAsset;
253+
public bool throwsException;
228254

229-
// Change the name of the UI action map
230-
uiMap.m_Name = "UI2";
255+
public TestVerifier(bool throwsException = false)
256+
{
257+
this.throwsException = throwsException;
258+
}
231259

232-
ProjectWideActionsAsset.CheckForDefaultUIActionMapChanges(asset);
260+
public void Verify(InputActionAsset asset, ProjectWideActionsAsset.IReportInputActionAssetVerificationErrors reporter)
261+
{
262+
if (throwsException)
263+
throw new Exception(TestReporter.kExceptionMessage);
264+
forwardedAsset = asset;
265+
reporter.Report(kFailureMessage);
266+
}
267+
}
233268

234-
LogAssert.Expect(LogType.Warning, new Regex("The action map named 'UI' does not exist"));
269+
[Test(Description = "Verifies that the default asset verification registers errors for a registered verifier)")]
270+
[Category(kTestCategory)]
271+
public void ProjectWideActions_ShouldSupportAssetVerification_IfVerifierHasBeenRegistered()
272+
{
273+
var messages = new List<string>();
274+
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath();
275+
var verifier = new TestVerifier();
276+
Func<ProjectWideActionsAsset.IInputActionAssetVerifier> factory = () => verifier;
277+
try
278+
{
279+
Assert.That(ProjectWideActionsAsset.RegisterInputActionAssetVerifier(factory), Is.True);
280+
ProjectWideActionsAsset.Verify(asset, new TestReporter(messages));
281+
Assert.That(messages.Count, Is.EqualTo(1));
282+
Assert.That(messages[0], Is.EqualTo(TestVerifier.kFailureMessage));
283+
Assert.That(verifier.forwardedAsset, Is.EqualTo(asset));
284+
}
285+
finally
286+
{
287+
Assert.That(ProjectWideActionsAsset.UnregisterInputActionAssetVerifier(factory), Is.True);
288+
}
289+
}
235290

236-
// Change the name of some UI map back to default and change the name of the actions
237-
uiMap.m_Name = "UI";
238-
var defaultActionName0 = uiMap.m_Actions[0].m_Name;
239-
var defaultActionName1 = uiMap.m_Actions[1].m_Name;
291+
[Test(Description = "Verifies that a verification factory cannot be registered twice")]
292+
[Category(kTestCategory)]
293+
public void ProjectWideActions_ShouldReturnError_IfFactoryHasAlreadyBeenRegisteredAndAttemptingToRegisterAgain()
294+
{
295+
Func<ProjectWideActionsAsset.IInputActionAssetVerifier> factory = () => null;
296+
try
297+
{
298+
Assert.That(ProjectWideActionsAsset.RegisterInputActionAssetVerifier(factory), Is.True);
299+
Assert.That(ProjectWideActionsAsset.RegisterInputActionAssetVerifier(factory), Is.False);
300+
}
301+
finally
302+
{
303+
Assert.That(ProjectWideActionsAsset.UnregisterInputActionAssetVerifier(factory), Is.True);
304+
}
305+
}
240306

241-
uiMap.m_Actions[0].Rename("Navigation");
242-
uiMap.m_Actions[1].Rename("Show");
307+
[Test(Description = "Verifies that a verification factory cannot be registered twice")]
308+
[Category(kTestCategory)]
309+
public void ProjectWideActions_ShouldReturnError_IfAttemptingToUnregisterAFactoryThatHasNotBeenRegistered()
310+
{
311+
ProjectWideActionsAsset.IInputActionAssetVerifier Factory() => null;
312+
Assert.That(ProjectWideActionsAsset.UnregisterInputActionAssetVerifier(Factory), Is.False);
313+
}
243314

244-
ProjectWideActionsAsset.CheckForDefaultUIActionMapChanges(asset);
315+
[Test(Description = "Verifies that a throwing reporter is handled gracefully")]
316+
[Category(kTestCategory)]
317+
public void ProjectWideActions_ShouldCatchAndReportException_IfReporterThrows()
318+
{
319+
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath();
320+
var verifier = new TestVerifier();
321+
Func<ProjectWideActionsAsset.IInputActionAssetVerifier> factory = () => verifier;
322+
try
323+
{
324+
// Note that reporter failures shouldn't affect verification result
325+
Assert.That(ProjectWideActionsAsset.RegisterInputActionAssetVerifier(factory), Is.True);
326+
Assert.That(ProjectWideActionsAsset.Verify(asset, new TestReporter(throwsException: true)), Is.True);
327+
}
328+
finally
329+
{
330+
Assert.That(ProjectWideActionsAsset.UnregisterInputActionAssetVerifier(factory), Is.True);
331+
}
245332

246-
LogAssert.Expect(LogType.Warning, new Regex($"The UI action '{defaultActionName0}' name has been modified"));
247-
LogAssert.Expect(LogType.Warning, new Regex($"The UI action '{defaultActionName1}' name has been modified"));
333+
LogAssert.Expect(LogType.Exception, new Regex($"{TestReporter.kExceptionMessage}"));
248334
}
249335

250-
#endif // UNITY_2023_2_OR_NEWER
336+
[Test(Description = "Verifies that a throwing verifier is handled gracefully and reported as a failure")]
337+
[Category(kTestCategory)]
338+
public void ProjectWideActions_ShouldCatchAndReportException_IfVerifierThrows()
339+
{
340+
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath();
341+
var verifier = new TestVerifier(throwsException: true);
342+
Func<ProjectWideActionsAsset.IInputActionAssetVerifier> factory = () => verifier;
343+
try
344+
{
345+
// Note that verifier failures should affect verification result
346+
Assert.That(ProjectWideActionsAsset.RegisterInputActionAssetVerifier(factory), Is.True);
347+
Assert.That(ProjectWideActionsAsset.Verify(asset, new TestReporter()), Is.False);
348+
}
349+
finally
350+
{
351+
Assert.That(ProjectWideActionsAsset.UnregisterInputActionAssetVerifier(factory), Is.True);
352+
}
353+
354+
LogAssert.Expect(LogType.Exception, new Regex($"{TestReporter.kExceptionMessage}"));
355+
}
251356

252357
[Test(Description = "Verifies that when assigning InputSystem.actions a callback is fired if value is different but not when value is not different")]
253358
[Category(kTestCategory)]

0 commit comments

Comments
 (0)