UITestHelpers.cs
5.33 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
// This code snippet was provided originally by stanislav.osipov@unity3d.com from #ui-elements slack channel.
using System;
using System.Collections;
using Unity.Cloud.Collaborate.Assets;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Unity.Cloud.Collaborate.Tests
{
internal static class UiTestHelpers
{
#region Events
// In order for tests to run without an EditorWindow but still be able to send
// events, we sometimes need to force the event type. IMGUI::GetEventType() (native) will
// return the event type as Ignore if the proper views haven't yet been
// initialized. This (falsely) breaks tests that rely on the event type. So for tests, we
// just ensure the event type is what we originally set it to when we sent it.
// This original type can be retrieved via Event.rawType.
static Event CreateEvent(Event evt)
{
return evt; //UIElementsUtility.CreateEvent(evt, evt.rawType);
}
public static Event MakeEvent(EventType type)
{
var evt = new Event { type = type };
return CreateEvent(evt);
}
public static Event MakeEvent(EventType type, Vector2 position)
{
var evt = new Event { type = type, mousePosition = position };
return CreateEvent(evt);
}
public static Event MakeKeyEvent(KeyCode code, EventType type = EventType.KeyDown, EventModifiers modifiers = EventModifiers.None, char character = '\0')
{
var evt = new Event { type = type, keyCode = code, character = character, modifiers = modifiers };
return CreateEvent(evt);
}
public static Event MakeMouseEvent(EventType type, Vector2 position, MouseButton button = MouseButton.LeftMouse, EventModifiers modifiers = EventModifiers.None, int clickCount = 1)
{
var evt = new Event { type = type, mousePosition = position, button = (int)button, modifiers = modifiers, clickCount = clickCount };
return CreateEvent(evt);
}
public static Event MakeScrollWheelEvent(Vector2 delta, Vector2 position)
{
var evt = new Event
{
type = EventType.ScrollWheel,
delta = delta,
mousePosition = position
};
return CreateEvent(evt);
}
public static Event MakeCommandEvent(EventType type, string command)
{
var evt = new Event { type = type, commandName = command };
return CreateEvent(evt);
}
#endregion
#region EditorWindow API
public static bool IsCompletelyVisible(EditorWindow window, VisualElement element)
{
if (element.ClassListContains(UiConstants.ussHidden))
{
return false;
}
var windowBounds = window.rootVisualElement.worldBound;
var elementBounds = element.worldBound;
return elementBounds.x >= windowBounds.x
&& elementBounds.y >= windowBounds.y
&& windowBounds.x + windowBounds.width >= elementBounds.x + elementBounds.width
&& windowBounds.y + windowBounds.height >= elementBounds.y + elementBounds.height;
}
public static bool IsPartiallyVisible(EditorWindow window, VisualElement element)
{
if (element.ClassListContains(UiConstants.ussHidden))
{
return false;
}
var windowBounds = window.rootVisualElement.worldBound;
var elementBounds = element.worldBound;
return !(elementBounds.x > windowBounds.x + windowBounds.width)
&& !(elementBounds.x + elementBounds.width < windowBounds.x)
&& !(elementBounds.y > windowBounds.y + windowBounds.height)
&& !(elementBounds.y + elementBounds.height < windowBounds.y);
}
public static void SendMouseDownEvent(EditorWindow window, VisualElement element)
{
var evt = MakeMouseEvent(EventType.MouseDown, element.worldBound.center);
window.SendEvent(evt);
}
public static void SendClickEvent(EditorWindow window, VisualElement element)
{
var evt = MakeMouseEvent(EventType.MouseDown, element.worldBound.center);
window.SendEvent(evt);
evt = MakeMouseEvent(EventType.MouseUp, element.worldBound.center);
window.SendEvent(evt);
}
public static void SimulateTyping(EditorWindow window, string text)
{
foreach (var character in text)
{
var evt = MakeKeyEvent(KeyCode.None, EventType.KeyDown, EventModifiers.None, character);
window.SendEvent(evt);
}
}
public static void SimulateTyping(EditorWindow window, char character, int repetitions)
{
for (var i = 0; i < repetitions; i++)
{
var evt = MakeKeyEvent(KeyCode.None, EventType.KeyDown, EventModifiers.None, character);
window.SendEvent(evt);
}
}
public static IEnumerator Pause(int frames)
{
for (var i = 0; i < frames; i++)
{
yield return null;
}
}
#endregion
}
}