Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
49 changes: 49 additions & 0 deletions Assets/Tests/InputSystem/CoreTests_Analytics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,55 @@ public void Analytics_ShouldReportPlayerInputManagerData()
}
}

[Test]
[Category("Analytics")]
public void Analytics_ShouldReportCodeAuthoringAnalytic()
{
CollectAnalytics(InputExitPlayModeAnalytic.kEventName);

// NOTE: We do not want to trigger entering/exiting play-mode for this small data-sanity check
// so just stick to triggering it explicitly. A better test would have been an editor test
// going in and out of play-mode for real but not clear if this is really possible.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is possible - there are other tests that do this E.g. texture streaming tests in trunk

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would love to learn how to do that properly. I tried it but when I programmatically exited play-mode the test aborted. We state its possible in the docs, but couldn't see any reference to how: https://docs.unity3d.com/Packages/com.unity.test-framework@1.1/manual/edit-mode-vs-play-mode-tests.html

Note: You can also control entering and exiting Play Mode from your Edit Mode test. This allow your test to make changes before entering Play Mode.

EditorApplication.playMode doesn't seem to do the job for me....

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In and edit and play mode test You should be able to use code like this:

yield return new EnterPlayMode();
yield return new ExitPlayMode();

See this for more details internally:
https://github.cds.internal.unity3d.com/unity/com.unity.memoryprofiler/blob/46687aa966bea49d093041968fb6d40cc8fd39f4/com.unity.memoryprofiler/Tests/Editor/SceneRootsAndAssetBundles/ReferenceToTests.cs#L58

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will land this as-is and file a separate task on this. if ok @lyndon-unity ? There are many places to change to that / correct triggers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A ticket have now been filed.


// Pretend we are entering play-mode
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.ExitingEditMode);
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.EnteredPlayMode);

// Assert no data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(0));

// Pretend we are exiting play-mode
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.ExitingPlayMode);
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.EnteredEditMode);

// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputExitPlayModeAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputExitPlayModeAnalytic.Data>());

var data0 = (InputExitPlayModeAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data0.uses_code_authoring, Is.False);

// Pretend we are entering play-mode
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.ExitingEditMode);
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.EnteredPlayMode);

var action = new InputAction("Dance");
action.AddBinding("<Keyboard>/Space");

// Pretend we are exiting play-mode
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.ExitingPlayMode);
InputExitPlayModeAnalytic.OnPlayModeStateChange(PlayModeStateChange.EnteredEditMode);

// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(2));
Assert.That(sentAnalyticsEvents[1].name, Is.EqualTo(InputExitPlayModeAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[1].data, Is.TypeOf<InputExitPlayModeAnalytic.Data>());

var data1 = (InputExitPlayModeAnalytic.Data)sentAnalyticsEvents[1].data;
Assert.That(data1.uses_code_authoring, Is.True);
}

#if UNITY_INPUT_SYSTEM_ENABLE_UI
[Test]
[Category("Analytics")]
Expand Down
1 change: 1 addition & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ however, it has to be formatted properly to pass verification tests.
- Added tests for Input Action Editor UI for managing action maps (List, create, rename, delete) (ISX-2087)
- Added automatic loading of custom extensions of InputProcessor, InputInteraction and InputBindingComposite [ISXB-856]](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-856).
- Added an IME Input sample scene.
- Added analytics for programmatic `InputAction` setup via `InputActionSetupExtensions`.

## [1.10.0] - 2024-07-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ public static BindingSyntax AddBinding(this InputAction action, string path, str
});
}

/// <summary>
/// Conditionally compiled helper for logging API usage of code-authored actions.
/// </summary>
/// <param name="api">The associated API function.</param>
/// <remarks>
/// Be extremely carefully to review for indirect calls and overloads to not register analytics twice.
/// Be extremely careful in enabling/disabling tracking before internal calls since those may otherwise
/// be incorrectly registered.
/// </remarks>
#if UNITY_EDITOR
private static void RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api api)
{
UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Register(api);
}

#endif

/// <summary>
/// Add a binding that references the given <paramref name="control"/> and triggers
/// the given <paramref cref="action"/>.
Expand Down Expand Up @@ -349,6 +366,10 @@ public static BindingSyntax AddBinding(this InputAction action, InputControl con
/// </remarks>
public static BindingSyntax AddBinding(this InputAction action, InputBinding binding = default)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.AddBinding);
#endif

if (action == null)
throw new ArgumentNullException(nameof(action));

Expand Down Expand Up @@ -478,6 +499,10 @@ public static BindingSyntax AddBinding(this InputActionMap actionMap, string pat
/// <seealso cref="InputActionMap.bindings"/>
public static BindingSyntax AddBinding(this InputActionMap actionMap, InputBinding binding)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.AddBinding);
#endif

if (actionMap == null)
throw new ArgumentNullException(nameof(actionMap));
if (binding.path == null)
Expand All @@ -501,6 +526,10 @@ public static BindingSyntax AddBinding(this InputActionMap actionMap, InputBindi
public static CompositeSyntax AddCompositeBinding(this InputAction action, string composite,
string interactions = null, string processors = null)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.AddCompositeBinding);
#endif

if (action == null)
throw new ArgumentNullException(nameof(action));
if (string.IsNullOrEmpty(composite))
Expand Down Expand Up @@ -580,6 +609,10 @@ private static int AddBindingInternal(InputActionMap map, InputBinding binding,
/// of <paramref name="action"/>).</exception>
public static BindingSyntax ChangeBinding(this InputAction action, int index)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ChangeBinding);
#endif

if (action == null)
throw new ArgumentNullException(nameof(action));

Expand Down Expand Up @@ -638,6 +671,10 @@ public static BindingSyntax ChangeBinding(this InputAction action, string name)
/// of <paramref name="actionMap"/>).</exception>
public static BindingSyntax ChangeBinding(this InputActionMap actionMap, int index)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ChangeBinding);
#endif

if (actionMap == null)
throw new ArgumentNullException(nameof(actionMap));
if (index < 0 || index >= actionMap.m_Bindings.LengthSafe())
Expand Down Expand Up @@ -836,6 +873,10 @@ public static BindingSyntax ChangeBinding(this InputAction action, InputBinding
/// <seealso cref="InputBindingComposite"/>
public static BindingSyntax ChangeCompositeBinding(this InputAction action, string compositeName)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ChangeCompositeBinding);
#endif

if (action == null)
throw new ArgumentNullException(nameof(action));
if (string.IsNullOrEmpty(compositeName))
Expand Down Expand Up @@ -877,6 +918,10 @@ public static BindingSyntax ChangeCompositeBinding(this InputAction action, stri
/// </remarks>
public static void Rename(this InputAction action, string newName)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.Rename);
#endif

if (action == null)
throw new ArgumentNullException(nameof(action));
if (string.IsNullOrEmpty(newName))
Expand Down Expand Up @@ -919,6 +964,10 @@ public static void Rename(this InputAction action, string newName)
/// </remarks>
public static void AddControlScheme(this InputActionAsset asset, InputControlScheme controlScheme)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.AddControlScheme);
#endif

if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (string.IsNullOrEmpty(controlScheme.name))
Expand Down Expand Up @@ -987,6 +1036,10 @@ public static ControlSchemeSyntax AddControlScheme(this InputActionAsset asset,
/// </remarks>
public static void RemoveControlScheme(this InputActionAsset asset, string name)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.RemoveControlScheme);
#endif

if (asset == null)
throw new ArgumentNullException(nameof(asset));
if (string.IsNullOrEmpty(name))
Expand All @@ -1007,33 +1060,57 @@ public static void RemoveControlScheme(this InputActionAsset asset, string name)
/// <returns><paramref name="scheme"/></returns>
public static InputControlScheme WithBindingGroup(this InputControlScheme scheme, string bindingGroup)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ControlSchemeWithBindingGroup);
#endif

return new ControlSchemeSyntax(scheme).WithBindingGroup(bindingGroup).Done();
}

public static InputControlScheme WithDevice(this InputControlScheme scheme, string controlPath, bool required)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ControlSchemeWithDevice);
#endif

if (required)
return new ControlSchemeSyntax(scheme).WithRequiredDevice(controlPath).Done();
return new ControlSchemeSyntax(scheme).WithOptionalDevice(controlPath).Done();
}

public static InputControlScheme WithRequiredDevice(this InputControlScheme scheme, string controlPath)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ControlSchemeWithRequiredDevice);
#endif

return new ControlSchemeSyntax(scheme).WithRequiredDevice(controlPath).Done();
}

public static InputControlScheme WithOptionalDevice(this InputControlScheme scheme, string controlPath)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ControlSchemeWithOptionalDevice);
#endif

return new ControlSchemeSyntax(scheme).WithOptionalDevice(controlPath).Done();
}

public static InputControlScheme OrWithRequiredDevice(this InputControlScheme scheme, string controlPath)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ControlSchemeOrWithRequiredDevice);
#endif

return new ControlSchemeSyntax(scheme).OrWithRequiredDevice(controlPath).Done();
}

public static InputControlScheme OrWithOptionalDevice(this InputControlScheme scheme, string controlPath)
{
#if UNITY_EDITOR
RegisterApiUsage(UnityEngine.InputSystem.Editor.InputExitPlayModeAnalytic.Api.ControlSchemeOrWithOptionalDevice);
#endif

return new ControlSchemeSyntax(scheme).OrWithOptionalDevice(controlPath).Done();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public void End()

#region IInputAnalytic Interface

#if UNITY_EDITOR && UNITY_2023_2_OR_NEWER
#if UNITY_2023_2_OR_NEWER
public bool TryGatherData(out UnityEngine.Analytics.IAnalytic.IData data, out Exception error)
#else
public bool TryGatherData(out InputAnalytics.IInputAnalyticData data, out Exception error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public InputBuildAnalytic(BuildReport buildReport)
public InputAnalytics.InputAnalyticInfo info =>
new InputAnalytics.InputAnalyticInfo(kEventName, kMaxEventsPerHour, kMaxNumberOfElements);

#if UNITY_EDITOR && UNITY_2023_2_OR_NEWER
#if UNITY_2023_2_OR_NEWER
public bool TryGatherData(out UnityEngine.Analytics.IAnalytic.IData data, out Exception error)
#else
public bool TryGatherData(out InputAnalytics.IInputAnalyticData data, out Exception error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public InputComponentEditorAnalytic(InputSystemComponent component)
m_Component = component;
}

#if UNITY_EDITOR && UNITY_2023_2_OR_NEWER
#if UNITY_2023_2_OR_NEWER
public bool TryGatherData(out UnityEngine.Analytics.IAnalytic.IData data, out Exception error)
#else
public bool TryGatherData(out InputAnalytics.IInputAnalyticData data, out Exception error)
Expand All @@ -86,4 +86,4 @@ public bool TryGatherData(out InputAnalytics.IInputAnalyticData data, out Except
public InputAnalytics.InputAnalyticInfo info { get; }
}
}
#endif
#endif // UNITY_EDITOR
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace UnityEngine.InputSystem.Editor
{
internal static partial class InputEditorAnalytics
internal static class InputEditorAnalytics
{
/// <summary>
/// Represents notification behavior setting associated with <see cref="PlayerInput"/> and
Expand Down
Loading