Skip to content

Commit e100f1c

Browse files
committed
Create resolutions if they don't exist
1 parent a1c4444 commit e100f1c

File tree

3 files changed

+164
-66
lines changed

3 files changed

+164
-66
lines changed

Assets/test/TestHelpers.cs

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,16 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Text.RegularExpressions;
7-
using System.Reflection;
87
using NUnit.Framework;
98
using UnityEngine;
109
using UnityEngine.UI;
1110
using UnityEngine.EventSystems;
1211
using UnityEngine.TestTools;
1312
using UnityEngine.SceneManagement;
14-
using UnityEditor;
1513
using UnityEditor.SceneManagement;
16-
1714
using TMPro;
1815

1916
public abstract class ABaseTest {
20-
private const string DefaultTestResolution = "16:9";
21-
private static readonly string[] ScreenshotResolutions = {
22-
"4:3",
23-
"16:9",
24-
"32:9",
25-
};
26-
2717
[UnitySetUp]
2818
public IEnumerator CommonSetUp() {
2919
string runId = System.Guid.NewGuid().ToString();
@@ -33,7 +23,7 @@ public IEnumerator CommonSetUp() {
3323
GameData.Current.Clear();
3424
ConfigData.Current.Clear();
3525
CheckpointData.Reset();
36-
SetResolution(DefaultTestResolution);
26+
TestResolution.TestDefault.Apply();
3727
yield return null;
3828
}
3929

@@ -42,7 +32,7 @@ public IEnumerator CommonTearDown() {
4232
StopMoving();
4333
StopZooming();
4434
ClearMousePosition();
45-
SetResolution("Free Aspect");
35+
TestResolution.Default.Apply();
4636
yield return null;
4737
}
4838

@@ -530,8 +520,8 @@ protected IEnumerator TakePercyScreenshot(string name) {
530520
backgroundColor.a = 1;
531521
camera.backgroundColor = backgroundColor;
532522

533-
foreach (string resolution in ScreenshotResolutions) {
534-
SetResolution(resolution);
523+
foreach (TestResolution resolution in TestResolution.ScreenshotResolutions) {
524+
resolution.Apply();
535525
yield return null;
536526

537527
RenderTexture screenTexture = new RenderTexture(Screen.width, Screen.height, 16);
@@ -551,65 +541,15 @@ protected IEnumerator TakePercyScreenshot(string name) {
551541
string path = String.Format(
552542
"./Percy/{0}-{1}.png",
553543
name,
554-
resolution.Replace(':', 'x')
544+
resolution.ShortName
555545
);
556546

557547
byte[] byteArray = renderedTexture.EncodeToPNG();
558548
System.IO.File.WriteAllBytes(path, byteArray);
559549
}
560550

561-
SetResolution(DefaultTestResolution);
562-
551+
TestResolution.TestDefault.Apply();
563552
yield return null;
564553
}
565-
566-
private void SetResolution(string key) {
567-
var GameViewSizes = typeof(Editor)
568-
.Assembly
569-
.GetType("UnityEditor.GameViewSizes");
570-
571-
var gameViewSizes = typeof(ScriptableSingleton<>)
572-
.MakeGenericType(GameViewSizes)
573-
.GetProperty("instance")
574-
.GetValue(null, null);
575-
576-
var group = GameViewSizes.GetMethod("GetGroup").Invoke(
577-
gameViewSizes,
578-
new object[] { (int)GameViewSizeGroupType.Standalone }
579-
);
580-
581-
string[] displayTexts = group
582-
.GetType()
583-
.GetMethod("GetDisplayTexts")
584-
.Invoke(group, null) as string[];
585-
586-
int index = Array.FindIndex(displayTexts, (displayText) =>
587-
displayText == key ||
588-
displayText.StartsWith("Test " + key)
589-
);
590-
591-
if (index == -1) {
592-
throw new System.Exception(
593-
String.Format(
594-
"Resolution not found: {0}. Available resolutions: {1}",
595-
key,
596-
String.Join(", ", displayTexts)
597-
)
598-
);
599-
}
600-
601-
var GameView = typeof(Editor)
602-
.Assembly
603-
.GetType("UnityEditor.GameView");
604-
605-
var window = EditorWindow.GetWindow(GameView);
606-
607-
GameView
608-
.GetProperty(
609-
"selectedSizeIndex",
610-
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
611-
)
612-
.SetValue(window, index, null);
613-
}
614554
}
615555
#endif

Assets/test/TestResolution.cs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Reflection;
6+
using UnityEditor;
7+
8+
public class TestResolution {
9+
private static readonly TestResolution Aspect4x3 = new TestResolution(
10+
width: 1280,
11+
height: 960,
12+
aspectWidth: 4,
13+
aspectHeight: 3
14+
);
15+
16+
private static readonly TestResolution Aspect16x9 = new TestResolution(
17+
width: 1920,
18+
height: 1080,
19+
aspectWidth: 16,
20+
aspectHeight: 9
21+
);
22+
23+
private static readonly TestResolution Aspect32x9 = new TestResolution(
24+
width: 5120,
25+
height: 1440,
26+
aspectWidth: 32,
27+
aspectHeight: 9
28+
);
29+
30+
public static readonly TestResolution Default = new TestResolution(
31+
freeAspect: true
32+
);
33+
34+
public static readonly TestResolution TestDefault = Aspect16x9;
35+
36+
public static readonly TestResolution[] ScreenshotResolutions = {
37+
Aspect4x3,
38+
Aspect16x9,
39+
Aspect32x9
40+
};
41+
42+
private int Width, Height, AspectWidth, AspectHeight;
43+
private bool FreeAspect = false;
44+
private Action Create;
45+
private Func<int> GetIndex;
46+
private Action<int> SetSelectedIndex;
47+
48+
public TestResolution(
49+
bool freeAspect = false,
50+
int width = -1,
51+
int height = -1,
52+
int aspectWidth = -1,
53+
int aspectHeight = -1
54+
) {
55+
FreeAspect = freeAspect;
56+
Width = width;
57+
Height = height;
58+
AspectWidth = aspectWidth;
59+
AspectHeight = aspectHeight;
60+
61+
Assembly EditorAssembly = typeof(Editor).Assembly;
62+
63+
Type GameViewSizes = EditorAssembly.GetType("UnityEditor.GameViewSizes");
64+
Type GameViewSize = EditorAssembly.GetType("UnityEditor.GameViewSize");
65+
Type GameViewSizeType = EditorAssembly.GetType("UnityEditor.GameViewSizeType");
66+
Type GameView = EditorAssembly.GetType("UnityEditor.GameView");
67+
68+
object gameViewSizes = typeof(ScriptableSingleton<>)
69+
.MakeGenericType(GameViewSizes)
70+
.GetProperty("instance")
71+
.GetValue(null, null);
72+
73+
MethodInfo getGroup = GameViewSizes.GetMethod("GetGroup");
74+
75+
object group = getGroup.Invoke(
76+
gameViewSizes,
77+
new object[] { (int)GameViewSizeGroupType.Standalone }
78+
);
79+
80+
object window = EditorWindow.GetWindow(GameView);
81+
82+
ConstructorInfo gameViewSizeConstructor = GameViewSize.GetConstructor(
83+
new Type[] { GameViewSizeType, typeof(int), typeof(int), typeof(string) }
84+
);
85+
86+
Create = () => {
87+
object newGameViewSize = gameViewSizeConstructor.Invoke(
88+
new object[] { 1, Width, Height, DisplayText }
89+
);
90+
91+
getGroup
92+
.ReturnType
93+
.GetMethod("AddCustomSize")
94+
.Invoke(group, new object[] { newGameViewSize });
95+
};
96+
97+
GetIndex = () => {
98+
string[] displayTexts = group
99+
.GetType()
100+
.GetMethod("GetDisplayTexts")
101+
.Invoke(group, null) as string[];
102+
103+
return Array.IndexOf(displayTexts, DisplayText);
104+
};
105+
106+
SetSelectedIndex = (index) => {
107+
GameView
108+
.GetProperty(
109+
"selectedSizeIndex",
110+
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic
111+
)
112+
.SetValue(window, index, null);
113+
};
114+
}
115+
116+
public string ShortName => String.Format(
117+
"{0}x{1}",
118+
AspectWidth,
119+
AspectHeight
120+
);
121+
122+
private string DisplayText => FreeAspect
123+
? "Free Aspect"
124+
: String.Format(
125+
"Test {0}:{1} ({2}x{3})",
126+
AspectWidth,
127+
AspectHeight,
128+
Width,
129+
Height
130+
);
131+
132+
public void Apply() {
133+
int index = GetIndex();
134+
135+
if (index == -1) {
136+
Create();
137+
index = GetIndex();
138+
}
139+
140+
if (index == -1) {
141+
throw new Exception("Failed to create resolution");
142+
}
143+
144+
SetSelectedIndex(index);
145+
}
146+
}
147+
#endif

Assets/test/TestResolution.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)