-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathCoreTests_Analytics.cs
More file actions
749 lines (641 loc) · 38.9 KB
/
CoreTests_Analytics.cs
File metadata and controls
749 lines (641 loc) · 38.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// We always send analytics in the editor (though the actual sending may be disabled in Pro) but we
// only send analytics in the player if enabled.
#if UNITY_ANALYTICS || UNITY_EDITOR
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.PerformanceTesting.Data;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.InputSystem.OnScreen;
using UnityEngine.InputSystem.UI;
using Editor = UnityEditor.Editor;
using InputAnalytics = UnityEngine.InputSystem.InputAnalytics;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEngine.InputSystem.Editor;
#endif
////TODO: restricting startup event to first run after installation (in player only)
partial class CoreTests
{
[Test]
[Category("Analytics")]
public void Analytics_ReceivesStartupEventOnFirstUpdate()
{
var registeredNames = new List<string>();
string receivedName = null;
object receivedData = null;
runtime.onRegisterAnalyticsEvent = (name, maxPerHour, maxPropertiesPerEvent) =>
{
registeredNames.Add(name);
};
runtime.onSendAnalyticsEvent =
(name, data) =>
{
#if UNITY_EDITOR && UNITY_2023_2_OR_NEWER
// Registration handled by framework
Assert.That(registeredNames.Count, Is.EqualTo(0));
#else
Assert.That(registeredNames.Contains(name));
#endif
Assert.That(receivedName, Is.Null);
receivedName = name;
receivedData = data;
};
// Add some data to the system.
runtime.ReportNewInputDevice(new InputDeviceDescription
{
product = "TestProductA",
manufacturer = "TestManufacturerA",
deviceClass = "Mouse",
interfaceName = "TestA"
}.ToJson());
runtime.ReportNewInputDevice(new InputDeviceDescription
{
product = "TestProductB",
manufacturer = "TestManufacturerB",
deviceClass = "Keyboard",
interfaceName = "TestB"
}.ToJson());
runtime.ReportNewInputDevice(new InputDeviceDescription // Unrecognized; won't result in device.
{
product = "TestProductC",
manufacturer = "TestManufacturerC",
deviceClass = "Unknown",
interfaceName = "Other"
}.ToJson());
InputSystem.AddDevice<Gamepad>();
InputSystem.Update();
Assert.That(receivedName, Is.EqualTo(InputAnalytics.StartupEventAnalytic.kEventName));
Assert.That(receivedData, Is.TypeOf<InputAnalytics.StartupEventData>());
var startupData = (InputAnalytics.StartupEventData)receivedData;
Assert.That(startupData.version, Is.EqualTo(InputSystem.version.ToString()));
Assert.That(startupData.devices, Is.Not.Null.And.Length.EqualTo(3));
Assert.That(startupData.unrecognized_devices, Is.Not.Null.And.Length.EqualTo(1));
Assert.That(startupData.devices[0].product, Is.Null);
Assert.That(startupData.devices[0].@interface, Is.Null);
Assert.That(startupData.devices[0].layout, Is.EqualTo("Gamepad"));
Assert.That(startupData.devices[0].native, Is.False);
Assert.That(startupData.devices[1].product, Is.EqualTo("TestManufacturerA TestProductA"));
Assert.That(startupData.devices[1].@interface, Is.EqualTo("TestA"));
Assert.That(startupData.devices[1].layout, Is.EqualTo("Mouse"));
Assert.That(startupData.devices[1].native, Is.True);
Assert.That(startupData.devices[2].product, Is.EqualTo("TestManufacturerB TestProductB"));
Assert.That(startupData.devices[2].@interface, Is.EqualTo("TestB"));
Assert.That(startupData.devices[2].layout, Is.EqualTo("Keyboard"));
Assert.That(startupData.devices[2].native, Is.True);
Assert.That(startupData.unrecognized_devices[0].product, Is.EqualTo("TestManufacturerC TestProductC"));
Assert.That(startupData.unrecognized_devices[0].@interface, Is.EqualTo("Other"));
Assert.That(startupData.unrecognized_devices[0].layout, Is.EqualTo("Unknown"));
Assert.That(startupData.unrecognized_devices[0].native, Is.True);
}
[Test]
[Category("Analytics")]
public void Analytics_SendsStartupEventOnlyOnFirstUpdate()
{
var numReceivedCalls = 0;
runtime.onSendAnalyticsEvent =
(name, data) => ++ numReceivedCalls;
InputSystem.Update();
InputSystem.Update();
Assert.That(numReceivedCalls, Is.EqualTo(1));
}
#if UNITY_EDITOR
[Test]
[Category("Analytics")]
public void Analytics_InEditor_StartupEventTransmitsBackendEnabledStatus()
{
// Save current player settings so we can restore them.
var oldEnabled = EditorPlayerSettingHelpers.oldSystemBackendsEnabled;
var newEnabled = EditorPlayerSettingHelpers.newSystemBackendsEnabled;
try
{
// Enable new and disable old.
EditorPlayerSettingHelpers.newSystemBackendsEnabled = true;
EditorPlayerSettingHelpers.oldSystemBackendsEnabled = false;
object receivedData = null;
runtime.onSendAnalyticsEvent =
(name, data) =>
{
Assert.That(receivedData, Is.Null);
receivedData = data;
};
InputSystem.Update();
var startupData = (InputAnalytics.StartupEventData)receivedData;
Assert.That(startupData.new_enabled, Is.True);
Assert.That(startupData.old_enabled, Is.False);
}
finally
{
EditorPlayerSettingHelpers.oldSystemBackendsEnabled = oldEnabled;
EditorPlayerSettingHelpers.newSystemBackendsEnabled = newEnabled;
}
}
#endif
////FIXME: these don't seem to actually make it out and to the analytics server
[Test]
[Category("Analytics")]
public void Analytics_ReceivesEventOnShutdown()
{
// Add and pump some data so we're getting some meaningful metrics.
var gamepad = InputSystem.AddDevice<Gamepad>();
InputSystem.AddDevice<Gamepad>();
InputSystem.QueueStateEvent(gamepad, new GamepadState());
InputSystem.QueueStateEvent(gamepad, new GamepadState());
InputSystem.Update();
InputSystem.QueueStateEvent(gamepad, new GamepadState());
InputSystem.Update();
var registeredNames = new List<string>();
string receivedName = null;
object receivedData = null;
runtime.onRegisterAnalyticsEvent = (name, maxPerHour, maxPropertiesPerEvent) =>
{
registeredNames.Add(name);
};
runtime.onSendAnalyticsEvent =
(name, data) =>
{
#if UNITY_EDITOR && UNITY_2023_2_OR_NEWER
// Registration handled by framework
Assert.That(registeredNames.Count, Is.EqualTo(0));
#else
Assert.That(registeredNames.Contains(name));
#endif
Assert.That(receivedData, Is.Null);
receivedName = name;
receivedData = data;
};
// Simulate shutdown.
runtime.onShutdown();
Assert.That(receivedName, Is.EqualTo(InputAnalytics.ShutdownEventDataAnalytic.kEventName));
Assert.That(receivedData, Is.TypeOf<InputAnalytics.ShutdownEventData>());
var shutdownData = (InputAnalytics.ShutdownEventData)receivedData;
var metrics = InputSystem.metrics;
Assert.That(shutdownData.max_num_devices, Is.EqualTo(metrics.maxNumDevices));
Assert.That(shutdownData.max_state_size_in_bytes, Is.EqualTo(metrics.maxStateSizeInBytes));
Assert.That(shutdownData.total_event_bytes, Is.EqualTo(metrics.totalEventBytes));
Assert.That(shutdownData.total_event_count, Is.EqualTo(metrics.totalEventCount));
Assert.That(shutdownData.total_frame_count, Is.EqualTo(metrics.totalUpdateCount));
Assert.That(shutdownData.total_event_processing_time, Is.EqualTo(metrics.totalEventProcessingTime).Within(0.00001));
}
////TODO: for this one to make sense, we first need noise filtering to be able to tell real user interaction from garbage data
[Test]
[Category("Analytics")]
[Ignore("TODO")]
public void TODO_Analytics_ReceivesEventOnFirstUserInteraction()
{
Assert.Fail();
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportEditorSessionAnalytics_IfAccordingToEditorSessionAnalyticsFiniteStateMachine()
{
CollectAnalytics(InputActionsEditorSessionAnalytic.kEventName);
// Editor session analytics is stateful and instantiated
var session = new InputActionsEditorSessionAnalytic(
InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings);
session.Begin(); // the user opens project settings and navigates to Input Actions
session.RegisterEditorFocusIn(); // when window opens, it receives edit focus directly
runtime.currentTime += 5; // the user is just grasping what is on the screen for 5 seconds
session.RegisterActionMapEdit(); // the user adds an action map or renames and action map or deletes one
session.RegisterActionEdit(); // the user adds an action, or renames it, or deletes one or add binding
session.RegisterBindingEdit(); // the user modifies a binding configuration
session.RegisterEditorFocusOut(); // the window looses focus due to user closing e.g. project settings
session.End(); // the window is destroyed and the session ends.
// Assert: Registration
#if (UNITY_2023_2_OR_NEWER && UNITY_EDITOR)
// Registration is a responsibility of the framework
Assert.That(registeredAnalytics.Count, Is.EqualTo(0));
#else
Assert.That(registeredAnalytics.Count, Is.EqualTo(1));
Assert.That(registeredAnalytics[0].name, Is.EqualTo(InputActionsEditorSessionAnalytic.kEventName));
Assert.That(registeredAnalytics[0].maxPerHour, Is.EqualTo(InputActionsEditorSessionAnalytic.kMaxEventsPerHour));
Assert.That(registeredAnalytics[0].maxPropertiesPerEvent, Is.EqualTo(InputActionsEditorSessionAnalytic.kMaxNumberOfElements));
#endif // (UNITY_2023_2_OR_NEWER && UNITY_EDITOR)
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputActionsEditorSessionAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputActionsEditorSessionAnalytic.Data>());
// Assert: Data content
var data = (InputActionsEditorSessionAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.kind, Is.EqualTo(InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings));
Assert.That(data.explicit_save_count, Is.EqualTo(0));
Assert.That(data.auto_save_count, Is.EqualTo(0));
Assert.That(data.session_duration_seconds, Is.EqualTo(5.0));
Assert.That(data.session_focus_duration_seconds, Is.EqualTo(5.0));
Assert.That(data.session_focus_switch_count, Is.EqualTo(1)); // TODO Unclear name
Assert.That(data.action_map_modification_count, Is.EqualTo(1));
Assert.That(data.action_modification_count, Is.EqualTo(1));
Assert.That(data.binding_modification_count, Is.EqualTo(1));
Assert.That(data.control_scheme_modification_count, Is.EqualTo(0));
Assert.That(data.reset_count, Is.EqualTo(0));
}
private void TestMultipleEditorFocusSessions(InputActionsEditorSessionAnalytic session = null)
{
CollectAnalytics(InputActionsEditorSessionAnalytic.kEventName);
session.Begin(); // the user opens project settings and navigates to Input Actions
session.RegisterEditorFocusIn(); // when window opens, it receives edit focus directly
runtime.currentTime += 5; // the user is just grasping what is on the screen for 5 seconds
session.RegisterActionMapEdit(); // the user adds an action map or renames and action map or deletes one
session.RegisterActionEdit(); // the user adds an action, or renames it, or deletes one or add binding
session.RegisterBindingEdit(); // the user modifies a binding configuration
session.RegisterControlSchemeEdit();// the user modifies control schemes
session.RegisterEditorFocusOut(); // the window looses focus due to user closing e.g. project settings
session.RegisterAutoSave(); // the asset is saved by automatic trigger
runtime.currentTime += 30; // the user has switched to something else but still has the window open.
session.RegisterEditorFocusIn(); // the user switches back to the window
runtime.currentTime += 2; // the user spends some time in edit focus
session.RegisterBindingEdit(); // the user is editing a binding.
session.RegisterEditorFocusOut(); // the user is dismissing the window and loosing focus
session.RegisterAutoSave(); // the asset is saved by automatic trigger
session.End(); // the window is destroyed and the session ends.
// Assert: Registration
#if (UNITY_2023_2_OR_NEWER && UNITY_EDITOR)
// Registration is a responsibility of the framework
Assert.That(registeredAnalytics.Count, Is.EqualTo(0));
#else
Assert.That(registeredAnalytics.Count, Is.EqualTo(1));
Assert.That(registeredAnalytics[0].name, Is.EqualTo(InputActionsEditorSessionAnalytic.kEventName));
Assert.That(registeredAnalytics[0].maxPerHour, Is.EqualTo(InputActionsEditorSessionAnalytic.kMaxEventsPerHour));
Assert.That(registeredAnalytics[0].maxPropertiesPerEvent, Is.EqualTo(InputActionsEditorSessionAnalytic.kMaxNumberOfElements));
#endif // (UNITY_2023_2_OR_NEWER && UNITY_EDITOR)
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputActionsEditorSessionAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputActionsEditorSessionAnalytic.Data>());
// Assert: Data content
var data = (InputActionsEditorSessionAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.kind, Is.EqualTo(InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings));
Assert.That(data.explicit_save_count, Is.EqualTo(0));
Assert.That(data.auto_save_count, Is.EqualTo(2));
Assert.That(data.session_duration_seconds, Is.EqualTo(37.0));
Assert.That(data.session_focus_duration_seconds, Is.EqualTo(7.0));
Assert.That(data.session_focus_switch_count, Is.EqualTo(2)); // TODO Unclear name
Assert.That(data.action_map_modification_count, Is.EqualTo(1));
Assert.That(data.action_modification_count, Is.EqualTo(1));
Assert.That(data.binding_modification_count, Is.EqualTo(2));
Assert.That(data.control_scheme_modification_count, Is.EqualTo(1));
Assert.That(data.reset_count, Is.EqualTo(0));
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportEditorSessionAnalyticsWithFocusTime_IfHavingMultipleFocusSessionsWithinSession()
{
TestMultipleEditorFocusSessions(
new InputActionsEditorSessionAnalytic(InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings));
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportEditorSessionAnalyticsWithFocusTime_WhenActionsDriveImplicitConditions()
{
CollectAnalytics(InputActionsEditorSessionAnalytic.kEventName);
// Editor session analytics is stateful and instantiated
var session = new InputActionsEditorSessionAnalytic(
InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings);
session.Begin(); // the user opens project settings and navigates to Input Actions
// session.RegisterEditorFocusIn(); // assumes we fail to capture focus-in event due to UI framework malfunction
runtime.currentTime += 5; // the user is just grasping what is on the screen for 5 seconds
session.RegisterActionMapEdit(); // the user adds an action map or renames and action map or deletes one
session.RegisterActionMapEdit(); // the user adds an action map or renames and action map or deletes one
session.RegisterBindingEdit(); // the user modifies a binding configuration
runtime.currentTime += 25; // the user spends some time in edit focus
// session.RegisterEditorFocusOut();// assumes we fail to detect focus out event due to UI framework malfunction
session.RegisterExplicitSave(); // the user presses a save button
session.End(); // the window is destroyed and the session ends.
// Assert: Registration
#if (UNITY_2023_2_OR_NEWER && UNITY_EDITOR)
// Registration is a responsibility of the framework
Assert.That(registeredAnalytics.Count, Is.EqualTo(0));
#else
Assert.That(registeredAnalytics.Count, Is.EqualTo(1));
Assert.That(registeredAnalytics[0].name, Is.EqualTo(InputActionsEditorSessionAnalytic.kEventName));
Assert.That(registeredAnalytics[0].maxPerHour, Is.EqualTo(InputActionsEditorSessionAnalytic.kMaxEventsPerHour));
Assert.That(registeredAnalytics[0].maxPropertiesPerEvent, Is.EqualTo(InputActionsEditorSessionAnalytic.kMaxNumberOfElements));
#endif // (UNITY_2023_2_OR_NEWER && UNITY_EDITOR)
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputActionsEditorSessionAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputActionsEditorSessionAnalytic.Data>());
// Assert: Data content
var data = (InputActionsEditorSessionAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.kind, Is.EqualTo(InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings));
Assert.That(data.explicit_save_count, Is.EqualTo(1));
Assert.That(data.auto_save_count, Is.EqualTo(0));
Assert.That(data.session_duration_seconds, Is.EqualTo(30.0));
Assert.That(data.session_focus_duration_seconds, Is.EqualTo(25.0));
Assert.That(data.session_focus_switch_count, Is.EqualTo(1)); // TODO Unclear name
Assert.That(data.action_map_modification_count, Is.EqualTo(2));
Assert.That(data.action_modification_count, Is.EqualTo(0));
Assert.That(data.binding_modification_count, Is.EqualTo(1));
Assert.That(data.control_scheme_modification_count, Is.EqualTo(0));
Assert.That(data.reset_count, Is.EqualTo(0));
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportEditorSessionAnalytics_IfMultipleSessionsAreReportedUsingTheSameInstance()
{
// We reuse an existing test case to prove that the object is reset properly and can be reused after
// ending the session. We currently let CollectAnalytics reset test harness state which is fine for
// the targeted verification aspect since only affecting test harness data.
var session = new InputActionsEditorSessionAnalytic(
InputActionsEditorSessionAnalytic.Data.Kind.EmbeddedInProjectSettings);
TestMultipleEditorFocusSessions(session);
TestMultipleEditorFocusSessions(session);
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportBuildAnalytics_WhenNotHavingSettingsAsset()
{
CollectAnalytics(InputBuildAnalytic.kEventName);
var storedSettings = InputSystem.s_Manager.settings;
InputSettings defaultSettings = null;
try
{
defaultSettings = ScriptableObject.CreateInstance<InputSettings>();
InputSystem.settings = defaultSettings;
// Simulate a build (note that we cannot create a proper build report)
var processor = new InputBuildAnalytic.ReportProcessor();
processor.OnPostprocessBuild(null); // Note that we cannot create a report
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputBuildAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputBuildAnalytic.InputBuildAnalyticData>());
// Assert: Data content
var data = (InputBuildAnalytic.InputBuildAnalyticData)sentAnalyticsEvents[0].data;
Assert.That(data.build_guid, Is.EqualTo(string.Empty));
Assert.That(data.has_projectwide_input_action_asset, Is.EqualTo(InputSystem.actions != null));
Assert.That(data.has_settings_asset, Is.False);
Assert.That(data.has_default_settings, Is.True);
Assert.That(data.update_mode, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.UpdateMode.ProcessEventsInDynamicUpdate));
Assert.That(data.compensate_for_screen_orientation, Is.EqualTo(defaultSettings.compensateForScreenOrientation));
Assert.That(data.default_deadzone_min, Is.EqualTo(defaultSettings.defaultDeadzoneMin));
Assert.That(data.default_deadzone_max, Is.EqualTo(defaultSettings.defaultDeadzoneMax));
Assert.That(data.default_button_press_point, Is.EqualTo(defaultSettings.defaultButtonPressPoint));
Assert.That(data.button_release_threshold, Is.EqualTo(defaultSettings.buttonReleaseThreshold));
Assert.That(data.default_tap_time, Is.EqualTo(defaultSettings.defaultTapTime));
Assert.That(data.default_slow_tap_time, Is.EqualTo(defaultSettings.defaultSlowTapTime));
Assert.That(data.default_hold_time, Is.EqualTo(defaultSettings.defaultHoldTime));
Assert.That(data.tap_radius, Is.EqualTo(defaultSettings.tapRadius));
Assert.That(data.multi_tap_delay_time, Is.EqualTo(defaultSettings.multiTapDelayTime));
Assert.That(data.background_behavior, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.BackgroundBehavior.ResetAndDisableNonBackgroundDevices));
Assert.That(data.editor_input_behavior_in_playmode, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.EditorInputBehaviorInPlayMode.PointersAndKeyboardsRespectGameViewFocus));
Assert.That(data.input_action_property_drawer_mode, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.InputActionPropertyDrawerMode.Compact));
Assert.That(data.max_event_bytes_per_update, Is.EqualTo(defaultSettings.maxEventBytesPerUpdate));
Assert.That(data.max_queued_events_per_update, Is.EqualTo(defaultSettings.maxQueuedEventsPerUpdate));
Assert.That(data.supported_devices, Is.EqualTo(defaultSettings.supportedDevices));
Assert.That(data.disable_redundant_events_merging, Is.EqualTo(defaultSettings.disableRedundantEventsMerging));
Assert.That(data.shortcut_keys_consume_input, Is.EqualTo(defaultSettings.shortcutKeysConsumeInput));
Assert.That(data.feature_optimized_controls_enabled, Is.EqualTo(defaultSettings.IsFeatureEnabled(InputFeatureNames.kUseOptimizedControls)));
Assert.That(data.feature_read_value_caching_enabled, Is.EqualTo(defaultSettings.IsFeatureEnabled(InputFeatureNames.kUseReadValueCaching)));
Assert.That(data.feature_paranoid_read_value_caching_checks_enabled, Is.EqualTo(defaultSettings.IsFeatureEnabled(InputFeatureNames.kParanoidReadValueCachingChecks)));
Assert.That(data.feature_disable_unity_remote_support, Is.EqualTo(defaultSettings.IsFeatureEnabled(InputFeatureNames.kDisableUnityRemoteSupport)));
Assert.That(data.feature_run_player_updates_in_editmode, Is.EqualTo(defaultSettings.IsFeatureEnabled(InputFeatureNames.kRunPlayerUpdatesInEditMode)));
}
finally
{
InputSystem.s_Manager.settings = storedSettings;
if (defaultSettings != null)
Object.DestroyImmediate(defaultSettings);
}
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportBuildAnalytics_WhenHavingSettingsAssetWithCustomSettings()
{
CollectAnalytics(InputBuildAnalytic.kEventName);
var storedSettings = InputSystem.s_Manager.settings;
InputSettings customSettings = null;
try
{
customSettings = ScriptableObject.CreateInstance<InputSettings>();
customSettings.updateMode = InputSettings.UpdateMode.ProcessEventsInFixedUpdate;
customSettings.compensateForScreenOrientation = true;
customSettings.defaultDeadzoneMin = 0.4f;
customSettings.defaultDeadzoneMax = 0.6f;
customSettings.defaultButtonPressPoint = 0.1f;
customSettings.buttonReleaseThreshold = 0.7f;
customSettings.defaultTapTime = 1.3f;
customSettings.defaultSlowTapTime = 2.3f;
customSettings.defaultHoldTime = 3.3f;
customSettings.tapRadius = 0.1f;
customSettings.multiTapDelayTime = 1.2f;
customSettings.backgroundBehavior = InputSettings.BackgroundBehavior.IgnoreFocus;
customSettings.editorInputBehaviorInPlayMode =
InputSettings.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView;
customSettings.inputActionPropertyDrawerMode =
InputSettings.InputActionPropertyDrawerMode.MultilineEffective;
customSettings.maxEventBytesPerUpdate = 11;
customSettings.maxQueuedEventsPerUpdate = 12;
customSettings.supportedDevices = Array.Empty<string>();
customSettings.disableRedundantEventsMerging = true;
customSettings.shortcutKeysConsumeInput = true;
customSettings.SetInternalFeatureFlag(InputFeatureNames.kUseOptimizedControls, true);
customSettings.SetInternalFeatureFlag(InputFeatureNames.kParanoidReadValueCachingChecks, true);
customSettings.SetInternalFeatureFlag(InputFeatureNames.kDisableUnityRemoteSupport, true);
customSettings.SetInternalFeatureFlag(InputFeatureNames.kRunPlayerUpdatesInEditMode, true);
customSettings.SetInternalFeatureFlag(InputFeatureNames.kUseReadValueCaching, true);
InputSystem.settings = customSettings;
// Simulate a build (note that we cannot create a proper build report)
var processor = new InputBuildAnalytic.ReportProcessor();
processor.OnPostprocessBuild(null); // Note that we cannot create a report
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputBuildAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputBuildAnalytic.InputBuildAnalyticData>());
// Assert: Data content
var data = (InputBuildAnalytic.InputBuildAnalyticData)sentAnalyticsEvents[0].data;
Assert.That(data.build_guid, Is.EqualTo(string.Empty));
Assert.That(data.has_projectwide_input_action_asset, Is.EqualTo(InputSystem.actions != null));
Assert.That(data.has_settings_asset, Is.False); // Note: We just don't write any file in this test, hence false
Assert.That(data.has_default_settings, Is.False);
Assert.That(data.update_mode, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.UpdateMode.ProcessEventsInFixedUpdate));
Assert.That(data.compensate_for_screen_orientation, Is.EqualTo(true));
Assert.That(data.default_deadzone_min, Is.EqualTo(0.4f));
Assert.That(data.default_deadzone_max, Is.EqualTo(0.6f));
Assert.That(data.default_button_press_point, Is.EqualTo(0.1f));
Assert.That(data.button_release_threshold, Is.EqualTo(0.7f));
Assert.That(data.default_tap_time, Is.EqualTo(1.3f));
Assert.That(data.default_slow_tap_time, Is.EqualTo(2.3f));
Assert.That(data.default_hold_time, Is.EqualTo(3.3f));
Assert.That(data.tap_radius, Is.EqualTo(customSettings.tapRadius));
Assert.That(data.multi_tap_delay_time, Is.EqualTo(customSettings.multiTapDelayTime));
Assert.That(data.background_behavior, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.BackgroundBehavior.IgnoreFocus));
Assert.That(data.editor_input_behavior_in_playmode, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.EditorInputBehaviorInPlayMode.AllDeviceInputAlwaysGoesToGameView));
Assert.That(data.input_action_property_drawer_mode, Is.EqualTo(InputBuildAnalytic.InputBuildAnalyticData.InputActionPropertyDrawerMode.MultilineEffective));
Assert.That(data.max_event_bytes_per_update, Is.EqualTo(customSettings.maxEventBytesPerUpdate));
Assert.That(data.max_queued_events_per_update, Is.EqualTo(customSettings.maxQueuedEventsPerUpdate));
Assert.That(data.supported_devices, Is.EqualTo(customSettings.supportedDevices));
Assert.That(data.disable_redundant_events_merging, Is.EqualTo(customSettings.disableRedundantEventsMerging));
Assert.That(data.shortcut_keys_consume_input, Is.EqualTo(customSettings.shortcutKeysConsumeInput));
Assert.That(data.feature_optimized_controls_enabled, Is.True);
Assert.That(data.feature_read_value_caching_enabled, Is.True);
Assert.That(data.feature_paranoid_read_value_caching_checks_enabled, Is.True);
Assert.That(data.feature_disable_unity_remote_support, Is.True);
Assert.That(data.feature_run_player_updates_in_editmode, Is.True);
}
finally
{
InputSystem.s_Manager.settings = storedSettings;
if (customSettings != null)
Object.DestroyImmediate(customSettings);
}
}
[TestCase(InputSystemComponent.PlayerInput, typeof(PlayerInput))]
[TestCase(InputSystemComponent.PlayerInputManager, typeof(PlayerInputManager))]
[TestCase(InputSystemComponent.InputSystemUIInputModule, typeof(InputSystemUIInputModule))]
[TestCase(InputSystemComponent.StandaloneInputModule, typeof(StandaloneInputModule))]
// [TestCase(InputSystemComponent.VirtualMouseInput, typeof(VirtualMouseInput))]
// [TestCase(InputSystemComponent.TouchSimulation, typeof(TouchSimulation))]
[TestCase(InputSystemComponent.OnScreenButton, typeof(OnScreenButton))]
[TestCase(InputSystemComponent.OnScreenStick, typeof(OnScreenStick))]
[Category("Analytics")]
public void Analytics_ShouldReportComponentAnalytics_WhenEditorIsCreatedAndDestroyed(
InputSystemComponent componentEnum, Type componentType)
{
CollectAnalytics(InputComponentEditorAnalytic.kEventName);
using (var gameObject = Scoped.Object(new GameObject()))
{
var component = gameObject.value.AddComponent(componentType);
Object.DestroyImmediate(Editor.CreateEditor(component));
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(InputComponentEditorAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<InputComponentEditorAnalytic.Data>());
// Assert: Data content
var data = (InputComponentEditorAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.component, Is.EqualTo(componentEnum));
}
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportPlayerInputData()
{
CollectAnalytics(PlayerInputEditorAnalytic.kEventName);
using (var gameObject = Scoped.Object(new GameObject()))
{
var playerInput = gameObject.value.AddComponent<PlayerInput>();
Object.DestroyImmediate(Editor.CreateEditor(playerInput));
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(PlayerInputEditorAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<PlayerInputEditorAnalytic.Data>());
// Assert: Data content
var data = (PlayerInputEditorAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.behavior, Is.EqualTo(InputEditorAnalytics.PlayerNotificationBehavior.SendMessages));
Assert.That(data.has_actions, Is.False);
Assert.That(data.has_default_map, Is.False);
Assert.That(data.has_ui_input_module, Is.False);
Assert.That(data.has_camera, Is.False);
}
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportPlayerInputManagerData()
{
CollectAnalytics(PlayerInputManagerEditorAnalytic.kEventName);
using (var gameObject = Scoped.Object(new GameObject()))
{
var playerInputManager = gameObject.value.AddComponent<PlayerInputManager>();
Object.DestroyImmediate(Editor.CreateEditor(playerInputManager));
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(PlayerInputManagerEditorAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<PlayerInputManagerEditorAnalytic.Data>());
// Assert: Data content
var data = (PlayerInputManagerEditorAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.behavior, Is.EqualTo(InputEditorAnalytics.PlayerNotificationBehavior.SendMessages));
Assert.That(data.join_behavior, Is.EqualTo(PlayerInputManagerEditorAnalytic.Data.PlayerJoinBehavior.JoinPlayersWhenButtonIsPressed));
Assert.That(data.joining_enabled_by_default, Is.True);
Assert.That(data.max_player_count, Is.EqualTo(-1));
}
}
[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.
// 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")]
public void Analytics_ShouldReportOnScreenStickData()
{
CollectAnalytics(OnScreenStickEditorAnalytic.kEventName);
using (var gameObject = Scoped.Object(new GameObject()))
{
var onScreenStick = gameObject.value.AddComponent<OnScreenStick>();
Object.DestroyImmediate(Editor.CreateEditor(onScreenStick));
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(OnScreenStickEditorAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<OnScreenStickEditorAnalytic.Data>());
// Assert: Data content
var data = (OnScreenStickEditorAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.behavior, Is.EqualTo(OnScreenStickEditorAnalytic.Data.OnScreenStickBehaviour.RelativePositionWithStaticOrigin));
Assert.That(data.movement_range, Is.EqualTo(50.0f));
Assert.That(data.dynamic_origin_range, Is.EqualTo(100.0f));
Assert.That(data.use_isolated_input_actions, Is.False);
}
}
[Test]
[Category("Analytics")]
public void Analytics_ShouldReportVirtualMouseInputData()
{
CollectAnalytics(VirtualMouseInputEditorAnalytic.kEventName);
using (var gameObject = Scoped.Object(new GameObject()))
{
var virtualMouseInput = gameObject.value.AddComponent<VirtualMouseInput>();
Object.DestroyImmediate(Editor.CreateEditor(virtualMouseInput));
// Assert: Data received
Assert.That(sentAnalyticsEvents.Count, Is.EqualTo(1));
Assert.That(sentAnalyticsEvents[0].name, Is.EqualTo(VirtualMouseInputEditorAnalytic.kEventName));
Assert.That(sentAnalyticsEvents[0].data, Is.TypeOf<VirtualMouseInputEditorAnalytic.Data>());
// Assert: Data content
var data = (VirtualMouseInputEditorAnalytic.Data)sentAnalyticsEvents[0].data;
Assert.That(data.cursor_mode, Is.EqualTo(VirtualMouseInputEditorAnalytic.Data.CursorMode.SoftwareCursor));
Assert.That(data.cursor_speed, Is.EqualTo(400.0f));
Assert.That(data.scroll_speed, Is.EqualTo(45.0f));
}
}
#endif // #if UNITY_INPUT_SYSTEM_ENABLE_UI
// Note: Currently not testing proper analytics reporting when editor is enabled/disabled since unclear how
// to achieve this with test framework. This would be a good future improvement.
}
#endif // UNITY_ANALYTICS || UNITY_EDITOR