-
Notifications
You must be signed in to change notification settings - Fork 338
Expand file tree
/
Copy pathInputForUITests.cs
More file actions
775 lines (682 loc) · 32 KB
/
Copy pathInputForUITests.cs
File metadata and controls
775 lines (682 loc) · 32 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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#if UNITY_2023_2_OR_NEWER // UnityEngine.InputForUI Module unavailable in earlier releases
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.InputForUI;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.TestTools;
using UnityEngine.TestTools.Constraints;
#if UNITY_EDITOR
using UnityEngine.InputSystem.Editor;
#endif
using UnityEngine.InputSystem.Plugins.InputForUI;
using Event = UnityEngine.InputForUI.Event;
using EventProvider = UnityEngine.InputForUI.EventProvider;
using Is = NUnit.Framework.Is;
// Note that these tests do not verify InputForUI default bindings at all.
// It only verifies integration with Project-wide Input Actions.
// Note that with current design and fixture, testing play-mode integration without Project-wide Input Actions
// would require a separate assembly, or potentially e.g. delete all actions to prevent matching.
//
// These tests are not meant to test the InputForUI module itself, but rather the integration between the InputForUI
// module and the InputSystem package.
// Be aware that these tests don't account for events dispatched by the InputEventPartialProvider. Those events are
// already tested in the Input Manager provider.
// Also, the internals to test InputEventPartialProvider are not exposed publicly, so we can't test them here.
[PrebuildSetup(typeof(ProjectWideActionsBuildSetup))]
[PostBuildCleanup(typeof(ProjectWideActionsBuildSetup))]
public class InputForUITests : InputTestFixture
{
private const string kTestCategory = "InputForUI";
readonly List<Event> m_InputForUIEvents = new List<Event>();
private int m_CurrentInputEventToCheck;
InputSystemProvider m_InputSystemProvider;
private InputActionAsset storedActions;
[SetUp]
public override void Setup()
{
base.Setup();
m_CurrentInputEventToCheck = 0;
storedActions = InputSystem.actions;
m_InputSystemProvider = new InputSystemProvider();
EventProvider.SetMockProvider(m_InputSystemProvider);
// Register at least one consumer so the mock update gets invoked
EventProvider.Subscribe(InputForUIOnEvent);
}
[TearDown]
public override void TearDown()
{
EventProvider.Unsubscribe(InputForUIOnEvent);
EventProvider.ClearMockProvider();
m_InputForUIEvents.Clear();
InputSystem.s_Manager.actions = storedActions;
#if UNITY_EDITOR
if (File.Exists(kAssetPath))
UnityEditor.AssetDatabase.DeleteAsset(kAssetPath);
#endif
base.TearDown();
}
internal Event GetNextRecordedUIEvent()
{
return m_InputForUIEvents[m_CurrentInputEventToCheck++];
}
private bool InputForUIOnEvent(in Event ev)
{
m_InputForUIEvents.Add(ev);
return true;
}
[Test]
[Category(kTestCategory)]
public void InputSystemActionAssetIsNotNull()
{
// Test assumes a compatible action asset configuration exists for UI
Assert.IsTrue(m_InputSystemProvider.ActionAssetIsNotNull(),
"Test is invalid since InputSystemProvider actions are not available");
}
[Test]
[Category(kTestCategory)]
// Checks that mouse events are ignored when a touch is active.
// This is to workaround the issue ISXB-269 on Windows.
public void TouchIsPressedAndMouseEventsAreIgnored()
{
var touch = InputSystem.AddDevice<Touchscreen>();
var mouse = InputSystem.AddDevice<Mouse>();
// Set initial mouse position to (0.5, 0.5) so that we get a delta when the mouse is moved, to dispatch
// a pointer move event
Set(mouse.position, new Vector2(0.5f, 0.5f));
Update();
// Start touch and move mouse to the same position to replicate the issue of duplicated Mouse events for
// Touch events on Windows.
BeginTouch(1, new Vector2(100f, 0.5f));
Move(mouse.position, new Vector2(100f, 0.5f));
Update();
Assert.AreEqual(1, m_InputForUIEvents.Count);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonPressed,
eventSource: EventSource.Touch }
});
}
#region UI_Input_Actions
// Test all default UI actions, and sure that InputSystemProvider works with and without project-wide actions asset
// so that there is no impact in receiving the necessary input events for UI.
// When there are no project-wide actions asset, the InputSystemProvider should still work as it currently gets
// the actions from DefaultActionsAsset().asset.
// Utility functions
void PressUpdateReleaseUpdate(ButtonControl button)
{
Press(button);
Update();
Release(button);
Update();
}
void TestGamepadNavigationCardinalDirections()
{
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Left, eventSource: EventSource.Gamepad }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Up, eventSource: EventSource.Gamepad }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Right, eventSource: EventSource.Gamepad }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Down, eventSource: EventSource.Gamepad }
});
}
void TestKeyboardNavigationCardinalDirections()
{
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Left, eventSource: EventSource.Keyboard }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Up, eventSource: EventSource.Keyboard }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Right, eventSource: EventSource.Keyboard }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Down, eventSource: EventSource.Keyboard }
});
}
[Test]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(false)]
public void UIActionNavigation_FiresUINavigationEvents_FromInputsGamepadJoystickAndKeyboard(bool useProjectWideActionsAsset)
{
Update();
if (!useProjectWideActionsAsset)
{
// Remove the project-wide actions asset in play mode and player.
// It will call InputSystem.onActionChange and re-set InputSystemProvider.actionAsset
// This the case where no project-wide actions asset is available in the project.
InputSystem.s_Manager.actions = null;
}
Update();
var gamepad = InputSystem.AddDevice<Gamepad>();
PressUpdateReleaseUpdate(gamepad.leftStick.left);
PressUpdateReleaseUpdate(gamepad.leftStick.up);
PressUpdateReleaseUpdate(gamepad.leftStick.right);
PressUpdateReleaseUpdate(gamepad.leftStick.down);
TestGamepadNavigationCardinalDirections();
PressUpdateReleaseUpdate(gamepad.rightStick.left);
PressUpdateReleaseUpdate(gamepad.rightStick.up);
PressUpdateReleaseUpdate(gamepad.rightStick.right);
PressUpdateReleaseUpdate(gamepad.rightStick.down);
TestGamepadNavigationCardinalDirections();
PressUpdateReleaseUpdate(gamepad.dpad.left);
PressUpdateReleaseUpdate(gamepad.dpad.up);
PressUpdateReleaseUpdate(gamepad.dpad.right);
PressUpdateReleaseUpdate(gamepad.dpad.down);
TestGamepadNavigationCardinalDirections();
var joystick = InputSystem.AddDevice<Joystick>();
PressUpdateReleaseUpdate(joystick.stick.left);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Left, eventSource: EventSource.Unspecified}
});
PressUpdateReleaseUpdate(joystick.stick.up);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Up, eventSource: EventSource.Unspecified}
});
PressUpdateReleaseUpdate(joystick.stick.right);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Right, eventSource: EventSource.Unspecified}
});
PressUpdateReleaseUpdate(joystick.stick.down);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Move, direction: NavigationEvent.Direction.Down, eventSource: EventSource.Unspecified}
});
var keyboard = InputSystem.AddDevice<Keyboard>();
PressUpdateReleaseUpdate(keyboard.aKey);
PressUpdateReleaseUpdate(keyboard.wKey);
PressUpdateReleaseUpdate(keyboard.dKey);
PressUpdateReleaseUpdate(keyboard.sKey);
TestKeyboardNavigationCardinalDirections();
PressUpdateReleaseUpdate(keyboard.leftArrowKey);
PressUpdateReleaseUpdate(keyboard.upArrowKey);
PressUpdateReleaseUpdate(keyboard.rightArrowKey);
PressUpdateReleaseUpdate(keyboard.downArrowKey);
TestKeyboardNavigationCardinalDirections();
}
[Test]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(false)]
public void UIActionSubmit_FiresUISubmitEvents_FromInputsGamepadJoystickAndKeyboard(bool useProjectWideActionsAsset)
{
Update();
if (!useProjectWideActionsAsset)
{
InputSystem.s_Manager.actions = null;
}
Update();
PressUpdateReleaseUpdate(InputSystem.AddDevice<Gamepad>().buttonSouth);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Submit, eventSource: EventSource.Gamepad }
});
PressUpdateReleaseUpdate(InputSystem.AddDevice<Joystick>().trigger);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Submit, eventSource: EventSource.Unspecified }
});
PressUpdateReleaseUpdate(InputSystem.AddDevice<Keyboard>().enterKey);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Submit, eventSource: EventSource.Keyboard }
});
Assert.AreEqual(3, m_InputForUIEvents.Count);
}
[Test]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(false)]
public void UIActionCancel_FiresUICancelEvents_FromInputsGamepadAndKeyboard(bool useProjectWideActionsAsset)
{
Update();
if (!useProjectWideActionsAsset)
{
InputSystem.s_Manager.actions = null;
}
Update();
PressUpdateReleaseUpdate(InputSystem.AddDevice<Gamepad>().buttonEast);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Cancel, eventSource: EventSource.Gamepad }
});
PressUpdateReleaseUpdate(InputSystem.AddDevice<Keyboard>().escapeKey);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.NavigationEvent,
asNavigationEvent: { type: NavigationEvent.Type.Cancel, eventSource: EventSource.Keyboard }
});
Assert.AreEqual(2, m_InputForUIEvents.Count);
}
[Test]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(false)]
public void UIActionPoint_FiresUIPointEvents_FromInputsMousePenAndTouch(bool useProjectWideActionsAsset)
{
Update();
if (!useProjectWideActionsAsset)
{
InputSystem.s_Manager.actions = null;
}
Update();
var mouse = InputSystem.AddDevice<Mouse>();
Set(mouse.position, new Vector2(0.5f, 0.5f));
Update();
Move(mouse.position, new Vector2(100f, 0.5f));
Update();
var pen = InputSystem.AddDevice<Pen>();
Set(pen.position, new Vector2(0.5f, 0.5f));
Update();
Move(pen.position, new Vector2(100f, 0.5f));
Update();
InputSystem.AddDevice<Touchscreen>();
BeginTouch(1, new Vector2(0.5f, 0.5f));
Update();
EndTouch(1, new Vector2(100f, 100f));
Update();
// Touch screen move is three actions: press, move, release
Assert.AreEqual(5, m_InputForUIEvents.Count);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.PointerMoved, eventSource: EventSource.Mouse }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.PointerMoved, eventSource: EventSource.Pen }
});
// Skip button down event that touch generates
++m_CurrentInputEventToCheck;
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.PointerMoved, eventSource: EventSource.Touch }
});
}
[Test]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(false)]
public void UIActionClick_FiresUIClickEvents_FromInputsMousePenAndTouch(bool useProjectWideActionsAsset)
{
Update();
if (!useProjectWideActionsAsset)
{
InputSystem.s_Manager.actions = null;
}
Update();
var mouse = InputSystem.AddDevice<Mouse>();
Click(mouse.leftButton);
Update();
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonPressed, eventSource: EventSource.Mouse, button: PointerEvent.Button.MouseLeft, clickCount: 1 }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonReleased, eventSource: EventSource.Mouse, button: PointerEvent.Button.MouseLeft, clickCount: 1 }
});
Click(mouse.rightButton);
Update();
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonPressed, eventSource: EventSource.Mouse, button: PointerEvent.Button.MouseRight, clickCount: 1 }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonReleased, eventSource: EventSource.Mouse, button: PointerEvent.Button.MouseRight, clickCount: 1 }
});
Click(mouse.middleButton);
Update();
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonPressed, eventSource: EventSource.Mouse, button: PointerEvent.Button.MouseMiddle, clickCount: 1 }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonReleased, eventSource: EventSource.Mouse, button: PointerEvent.Button.MouseMiddle, clickCount: 1 }
});
var pen = InputSystem.AddDevice<Pen>();
Click(pen.tip);
Update();
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonPressed, eventSource: EventSource.Pen, button: PointerEvent.Button.MouseLeft, clickCount: 1 }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonReleased, eventSource: EventSource.Pen, button: PointerEvent.Button.MouseLeft, clickCount: 1 }
});
InputSystem.AddDevice<Touchscreen>();
BeginTouch(1, new Vector2(0.0f, 0.5f));
EndTouch(1, new Vector2(0.0f, 0.5f));
Update();
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonPressed, eventSource: EventSource.Touch, button: PointerEvent.Button.MouseLeft, clickCount: 1 }
});
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.ButtonReleased, eventSource: EventSource.Touch, button: PointerEvent.Button.MouseLeft, clickCount: 1 }
});
Assert.AreEqual(10, m_InputForUIEvents.Count);
}
const float kScrollUGUIScaleFactor = 3.0f; // See InputSystemProvider OnScrollWheelPerformed() callback
[Test]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(false)]
public void UIActionScroll_FiresUIScrollEvents_FromInputMouse(bool useProjectWideActionsAsset)
{
Update();
if (!useProjectWideActionsAsset)
{
InputSystem.s_Manager.actions = null;
}
Update();
var mouse = InputSystem.AddDevice<Mouse>();
Update();
// Make the minimum step of scroll delta to be ±1.0f
Set(mouse.scroll.y, -1f / kScrollUGUIScaleFactor);
Update();
Assert.AreEqual(1, m_InputForUIEvents.Count);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.Scroll, eventSource: EventSource.Mouse, scroll: {x: 0, y: 1} }
});
}
#if UNITY_INPUT_SYSTEM_PLATFORM_SCROLL_DELTA
[Category(kTestCategory)]
[TestCase(1.0f)]
[TestCase(120.0f)]
public void UIActionScroll_ReceivesNormalizedScrollWheelDelta(float scrollWheelDeltaPerTick)
{
var mouse = InputSystem.AddDevice<Mouse>();
Update();
// Set scroll delta with a custom range.
((InputTestRuntime)InputRuntime.s_Instance).scrollWheelDeltaPerTick = scrollWheelDeltaPerTick;
Set(mouse.scroll, new Vector2(0, scrollWheelDeltaPerTick));
Update();
// UI should receive scroll delta in its expected range.
Assert.AreEqual(1, m_InputForUIEvents.Count);
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.Scroll, eventSource: EventSource.Mouse, scroll: {x: 0, y: -kScrollUGUIScaleFactor} }
});
}
#endif
#endregion
#if UNITY_EDITOR
// These tests shouldn't really be in a non editor-only assembly but for now we guard them until moved.
private const string kAssetPath = "Assets/InputSystem_InputForUI_TestActions.inputactions";
[Test(Description = "Verifies that default actions (Project-wide) OR default actions have no verification errors.")]
[Category(kTestCategory)]
[TestCase(true)]
[TestCase(true)]
public void DefaultActions_ShouldNotGenerateAnyVerificationWarnings(bool useProjectWideActions)
{
if (!useProjectWideActions)
InputSystem.s_Manager.actions = null;
Update();
LogAssert.NoUnexpectedReceived();
}
[Test(Description = "Verifies that user-supplied project-wide actions do not generate warnings if action map is missing. We use default actions in this case.")]
[Category(kTestCategory)]
public void ActionsWithoutUIMap_ShouldNotGenerateWarnings()
{
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath(kAssetPath);
asset.RemoveActionMap(asset.FindActionMap("UI", throwIfNotFound: true));
InputSystem.s_Manager.actions = asset;
Update();
LogAssert.NoUnexpectedReceived();
}
[Test(Description = "Verifies that user-supplied project-wide input actions generates warnings if the UI map is present but actions are missing.")]
[Category(kTestCategory)]
public void ActionsWithUIMap_MissingActions_ShouldGenerateWarnings()
{
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath(kAssetPath);
asset.RemoveActionMap(asset.FindActionMap("UI", throwIfNotFound: true));
asset.AddActionMap(new InputActionMap("UI")); // An empty UI map should log warnings.
InputSystem.s_Manager.actions = asset;
Update();
var link = EditorHelpers.GetHyperlink(kAssetPath);
if (InputActionAssetVerifier.DefaultReportPolicy == InputActionAssetVerifier.ReportPolicy.ReportAll)
{
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/Point' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/Navigate' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/Submit' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/Cancel' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/Click' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/MiddleClick' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/RightClick' in asset '{link}' could not be found."));
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path 'UI/ScrollWheel' in asset '{link}' could not be found."));
}
}
[Test(Description = "Verifies that user-supplied project-wide input actions generates warnings if any required action is missing.")]
[Category(kTestCategory)]
[TestCase("UI/Point")]
[TestCase("UI/Navigate")]
[TestCase("UI/Submit")]
[TestCase("UI/Cancel")]
[TestCase("UI/Click")]
[TestCase("UI/MiddleClick")]
[TestCase("UI/RightClick")]
[TestCase("UI/ScrollWheel")]
public void ActionMapWithNonExistentRequiredAction_ShouldGenerateWarning(string actionPath)
{
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath(kAssetPath);
var action = asset.FindAction(actionPath);
action.Rename("Other");
InputSystem.s_Manager.actions = asset;
Update();
//var link = AssetDatabase.GetAssetPath()//EditorHelpers.GetHyperlink(kAssetPath);
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path '{actionPath}' in asset \"{kAssetPath}\" could not be found."));
LogAssert.NoUnexpectedReceived();
}
[Test(Description = "Verifies that user-supplied project-wide input actions generates warnings if they lack bindings.")]
[Category(kTestCategory)]
[TestCase("UI/Point")]
[TestCase("UI/Navigate")]
[TestCase("UI/Submit")]
[TestCase("UI/Cancel")]
[TestCase("UI/Click")]
[TestCase("UI/MiddleClick")]
[TestCase("UI/RightClick")]
[TestCase("UI/ScrollWheel")]
public void ActionMapWithUnboundRequiredAction_ShouldGenerateWarning(string actionPath)
{
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath(kAssetPath);
var map = asset.FindActionMap("UI");
// Recreate a map with selected action bindings removed (unfortunately there is no remove so this is what we got)
asset.RemoveActionMap(map);
var newMap = new InputActionMap(map.name);
var n = map.actions.Count;
for (var i = 0; i < n; ++i)
{
// Create a cloned action with only binding changed
var source = map.actions[i];
var action = newMap.AddAction(name: source.name,
type: source.type,
binding: actionPath == map.name + '/' + source.name ? null : "SomeIrrelevantBindingThatWillNeverResolve",
processors: source.processors,
interactions: source.interactions,
expectedControlLayout: null,
groups: null);
action.expectedControlType = source.expectedControlType;
}
asset.AddActionMap(newMap);
InputSystem.s_Manager.actions = asset;
Update();
LogAssert.Expect(LogType.Warning, new Regex($"^InputAction with path '{actionPath}' in asset \"{kAssetPath}\" do not have any configured bindings."));
LogAssert.NoUnexpectedReceived();
}
[Test(Description =
"Verifies that user-supplied project-wide input actions generates warnings if they have a different action type")]
[TestCase("UI/Point", InputActionType.Button)]
[TestCase("UI/Navigate", InputActionType.Button)]
[TestCase("UI/Submit", InputActionType.Value)]
[TestCase("UI/Cancel", InputActionType.Value)]
[TestCase("UI/Click", InputActionType.Value)]
[TestCase("UI/MiddleClick", InputActionType.Value)]
[TestCase("UI/RightClick", InputActionType.Value)]
[TestCase("UI/ScrollWheel", InputActionType.Button)]
public void ActionWithUnexpectedActionType_ShouldGenerateWarning(string actionPath, InputActionType unexpectedType)
{
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath(kAssetPath);
var action = asset.FindAction(actionPath);
Debug.Assert(action.type != unexpectedType); // not really an assert, sanity check test assumption for correctness
var expectedType = action.type;
action.m_Type = unexpectedType; // change directly via internals for now
InputSystem.s_Manager.actions = asset;
Update();
LogAssert.Expect(LogType.Warning,
new Regex($"^InputAction with path '{actionPath}' in asset \"{kAssetPath}\" has 'type' set to 'InputActionType.{unexpectedType}'"));
LogAssert.NoUnexpectedReceived();
}
[Test(Description =
"Verifies that user-supplied project-wide input actions generates warnings if they have a specified expected control type")]
[TestCase("UI/Point", "Quaternion")]
[TestCase("UI/Navigate", "Touch")]
[TestCase("UI/Submit", "Vector2")]
[TestCase("UI/Cancel", "Vector3")]
[TestCase("UI/Click", "Axis")]
[TestCase("UI/MiddleClick", "Delta")]
[TestCase("UI/RightClick", "Bone")]
[TestCase("UI/ScrollWheel", "Eyes")]
public void ActionWithDifferentExpectedControlType_ShouldGenerateWarning(string actionPath, string unexpectedControlType)
{
var asset = ProjectWideActionsAsset.CreateDefaultAssetAtPath(kAssetPath);
var action = asset.FindAction(actionPath);
Debug.Assert(action.expectedControlType != unexpectedControlType); // not really an assert, sanity check test assumption for correctness
var expectedControlType = action.expectedControlType;
action.expectedControlType = unexpectedControlType;
InputSystem.s_Manager.actions = asset;
Update();
LogAssert.Expect(LogType.Warning,
new Regex($"^InputAction with path '{actionPath}' in asset \"{kAssetPath}\" has 'expectedControlType' set to '{unexpectedControlType}'"));
LogAssert.NoUnexpectedReceived();
}
[Test]
[Category("Actions")]
[Description("Tests that that only the latest event after focus is regained is able to trigger the action." +
"Depends on background behavior. " +
"Similar to CoreTests_Actions.Actions_DoNotGetTriggeredByOutOfFocusEventInEditor but with InputForUI nuances.")]
[TestCase(InputSettings.BackgroundBehavior.IgnoreFocus)]
[TestCase(InputSettings.BackgroundBehavior.ResetAndDisableNonBackgroundDevices)]
[TestCase(InputSettings.BackgroundBehavior.ResetAndDisableAllDevices)]
public void UIActions_DoNotGetTriggeredByOutOfFocusEventInEditor(InputSettings.BackgroundBehavior backgroundBehavior)
{
InputSystem.settings.backgroundBehavior = backgroundBehavior;
var mouse = InputSystem.AddDevice<Mouse>();
Vector2 focusPosition = new Vector2(800f, 600f);
Vector2 outOfFocusPosition = new Vector2(100f, 500f);
// Simulate moving the mouse, losing focus, moving out of focus, regaining focus, and moving again to emulate
// a device sync that happens when regaining focus in play mode.
Update();
currentTime += 1.0f;
Set(mouse.position, new Vector2(1.0f, 1.0f), queueEventOnly: true);
currentTime += 1.0f;
Update();
currentTime += 1.0f;
ScheduleFocusEvent(false);
currentTime += 1.0f;
Set(mouse.position, outOfFocusPosition , queueEventOnly: true);
currentTime += 1.0f;
ScheduleFocusEvent(true);
currentTime += 1.0f;
Set(mouse.position, focusPosition, queueEventOnly: true);
currentTime += 1.0f;
// We call specific updates to simulate editor behavior when regaining focus.
InputSystem.Update(InputUpdateType.Editor);
Assert.AreEqual(0, m_InputForUIEvents.Count);
InputSystem.Update(InputUpdateType.Dynamic);
// Calling the event provider update after we call InputSystem updates so that we trigger InputForUI events
EventProvider.NotifyUpdate();
// Convert the input coordinates to UI panel space. We only assume 1 display for the tests.
var focusPositionInUI = InputSystemProvider.ScreenBottomLeftToPanelPosition(focusPosition, 0);
var outOfFocusPositionInUI = InputSystemProvider.ScreenBottomLeftToPanelPosition(outOfFocusPosition, 0);
// If we don't ignore focus, we only expect one event (the last one after focus is regained).
Assert.AreEqual(backgroundBehavior != InputSettings.BackgroundBehavior.IgnoreFocus ? 1 : 2, m_InputForUIEvents.Count);
// There will be an out of focus event only if we are ignoring focus. Validate its position.
if (backgroundBehavior == InputSettings.BackgroundBehavior.IgnoreFocus)
{
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.PointerMoved, eventSource: EventSource.Mouse, position: var outOfFocusVector2 },
} && Mathf.Approximately(outOfFocusVector2.y, outOfFocusPositionInUI.y) && Mathf.Approximately(outOfFocusVector2.x, outOfFocusPositionInUI.x));
}
// Validate that we only we get the event for the position after focus is regained. Make sure its position is correct.
Assert.That(GetNextRecordedUIEvent() is
{
type: Event.Type.PointerEvent,
asPointerEvent: { type: PointerEvent.Type.PointerMoved, eventSource: EventSource.Mouse, position: var focusPositionVector2 },
} && Mathf.Approximately(focusPositionVector2.y, focusPositionInUI.y) && Mathf.Approximately(focusPositionVector2.x, focusPositionInUI.x));
}
#endif // UNITY_EDITOR
[Test]
public void Update_DoesntAllocate()
{
Update(); // Warm-up internals
Assert.That(Update, Is.Not.AllocatingGCMemory());
}
static void Update()
{
EventProvider.NotifyUpdate();
InputSystem.Update();
}
}
#endif