Skip to content

Commit a1c4444

Browse files
committed
Use multiple resolutions for Percy screenshots
1 parent 72c140b commit a1c4444

File tree

1 file changed

+87
-12
lines changed

1 file changed

+87
-12
lines changed

Assets/test/TestHelpers.cs

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
#if UNITY_EDITOR
2+
using System;
23
using System.Collections;
34
using System.Collections.Generic;
45
using System.Linq;
56
using System.Text.RegularExpressions;
7+
using System.Reflection;
68
using NUnit.Framework;
79
using UnityEngine;
810
using UnityEngine.UI;
911
using UnityEngine.EventSystems;
1012
using UnityEngine.TestTools;
1113
using UnityEngine.SceneManagement;
14+
using UnityEditor;
1215
using UnityEditor.SceneManagement;
16+
1317
using TMPro;
1418

1519
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+
1627
[UnitySetUp]
1728
public IEnumerator CommonSetUp() {
1829
string runId = System.Guid.NewGuid().ToString();
@@ -22,6 +33,7 @@ public IEnumerator CommonSetUp() {
2233
GameData.Current.Clear();
2334
ConfigData.Current.Clear();
2435
CheckpointData.Reset();
36+
SetResolution(DefaultTestResolution);
2537
yield return null;
2638
}
2739

@@ -30,6 +42,7 @@ public IEnumerator CommonTearDown() {
3042
StopMoving();
3143
StopZooming();
3244
ClearMousePosition();
45+
SetResolution("Free Aspect");
3346
yield return null;
3447
}
3548

@@ -517,24 +530,86 @@ protected IEnumerator TakePercyScreenshot(string name) {
517530
backgroundColor.a = 1;
518531
camera.backgroundColor = backgroundColor;
519532

520-
RenderTexture screenTexture = new RenderTexture(Screen.width, Screen.height, 16);
521-
RenderTexture previousTargetTexture = camera.targetTexture;
533+
foreach (string resolution in ScreenshotResolutions) {
534+
SetResolution(resolution);
535+
yield return null;
536+
537+
RenderTexture screenTexture = new RenderTexture(Screen.width, Screen.height, 16);
538+
RenderTexture previousTargetTexture = camera.targetTexture;
522539

523-
camera.targetTexture = screenTexture;
524-
RenderTexture.active = screenTexture;
525-
camera.Render();
540+
camera.targetTexture = screenTexture;
541+
RenderTexture.active = screenTexture;
542+
camera.Render();
526543

527-
Texture2D renderedTexture = new Texture2D(Screen.width, Screen.height);
528-
renderedTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
544+
Texture2D renderedTexture = new Texture2D(Screen.width, Screen.height);
545+
renderedTexture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
529546

530-
// Clean up
531-
camera.targetTexture = previousTargetTexture;
532-
RenderTexture.active = null;
547+
// Clean up
548+
camera.targetTexture = previousTargetTexture;
549+
RenderTexture.active = null;
533550

534-
byte[] byteArray = renderedTexture.EncodeToPNG();
535-
System.IO.File.WriteAllBytes("./Percy/" + name + ".png", byteArray);
551+
string path = String.Format(
552+
"./Percy/{0}-{1}.png",
553+
name,
554+
resolution.Replace(':', 'x')
555+
);
556+
557+
byte[] byteArray = renderedTexture.EncodeToPNG();
558+
System.IO.File.WriteAllBytes(path, byteArray);
559+
}
560+
561+
SetResolution(DefaultTestResolution);
536562

537563
yield return null;
538564
}
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+
}
539614
}
540615
#endif

0 commit comments

Comments
 (0)