< Summary

Class:GameViewUtils
Assembly:VisualTests
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Tests/VisualTests/Editor/GameViewUtils.cs
Covered lines:45
Uncovered lines:15
Coverable lines:60
Total lines:124
Line coverage:75% (45 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GameViewUtils()0%110100%
SetSize(...)0%110100%
AddOrGetCustomSize(...)0%220100%
RemoveCustomSize(...)0%110100%
SizeExists(...)0%2100%
FindSize(...)0%20400%
SizeExists(...)0%2100%
FindSize(...)0%440100%
GetGroup(...)0%110100%
GetCurrentGroupType()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Tests/VisualTests/Editor/GameViewUtils.cs

#LineLine coverage
 1using System;
 2using System.Reflection;
 3using UnityEditor;
 4
 5//NOTE(Brian): Code adapted from this answer https://answers.unity.com/questions/956123/add-and-select-game-view-resolut
 6public static class GameViewUtils
 7{
 8    static object gameViewSizesInstance;
 9    static MethodInfo getGroup;
 10
 11    static GameViewUtils()
 12    {
 113        var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
 114        var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
 115        var instanceProp = singleType.GetProperty("instance");
 116        getGroup = sizesType.GetMethod("GetGroup");
 117        gameViewSizesInstance = instanceProp.GetValue(null, null);
 118    }
 19
 20    public enum GameViewSizeType
 21    {
 22        AspectRatio, FixedResolution
 23    }
 24
 25    public static void SetSize(int index)
 26    {
 627        var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
 28
 629        var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
 30            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
 31
 632        var gvWnd = EditorWindow.GetWindow(gvWndType);
 33
 634        selectedSizeIndexProp.SetValue(gvWnd, index, null);
 635    }
 36
 37    public static int AddOrGetCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, 
 38    {
 739        int id = FindSize(sizeGroupType, width, height);
 40
 741        if (id != -1)
 542            return id;
 43
 244        var group = GetGroup(sizeGroupType);
 245        var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType().
 246        var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
 247        var enumType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType");
 248        var ctor = gvsType.GetConstructor(new Type[] { enumType, typeof(int), typeof(int), typeof(string) });
 249        var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
 250        addCustomSize.Invoke(group, new object[] { newSize });
 51
 252        return FindSize(sizeGroupType, width, height);
 53    }
 54
 55    public static void RemoveCustomSize(GameViewSizeGroupType sizeGroupType, int index)
 56    {
 157        var group = GetGroup(sizeGroupType);
 158        var addCustomSize = getGroup.ReturnType.GetMethod("RemoveCustomSize");
 159        addCustomSize.Invoke(group, new object[] { index });
 160    }
 61
 062    public static bool SizeExists(GameViewSizeGroupType sizeGroupType, string text) { return FindSize(sizeGroupType, tex
 63
 64    public static int FindSize(GameViewSizeGroupType sizeGroupType, string text)
 65    {
 066        var group = GetGroup(sizeGroupType);
 067        var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
 068        var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
 069        for (int i = 0; i < displayTexts.Length; i++)
 70        {
 071            string display = displayTexts[i];
 72
 73            // the text we get is "Name (W:H)" if the size has a name, or just "W:H" e.g. 16:9
 74            // so if we're querying a custom size text we substring to only get the name
 75            // You could see the outputs by just logging
 076            int pren = display.IndexOf('(');
 77
 078            if (pren != -1)
 079                display = display.Substring(0, pren - 1); // -1 to remove the space that's before the prens. This is ver
 80
 081            if (display == text)
 082                return i;
 83        }
 084        return -1;
 85    }
 86
 087    public static bool SizeExists(GameViewSizeGroupType sizeGroupType, int width, int height) { return FindSize(sizeGrou
 88
 89    public static int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height)
 90    {
 1191        object group = GetGroup(sizeGroupType);
 1192        var groupType = group.GetType();
 1193        var getBuiltinCount = groupType.GetMethod("GetBuiltinCount");
 1194        var getCustomCount = groupType.GetMethod("GetCustomCount");
 1195        int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null);
 1196        var getGameViewSize = groupType.GetMethod("GetGameViewSize");
 1197        var gvsType = getGameViewSize.ReturnType;
 1198        var widthProp = gvsType.GetProperty("width");
 1199        var heightProp = gvsType.GetProperty("height");
 11100        var indexValue = new object[1];
 101
 184102        for (int i = 0; i < sizesCount; i++)
 103        {
 89104            indexValue[0] = i;
 89105            var size = getGameViewSize.Invoke(group, indexValue);
 89106            int sizeWidth = (int)widthProp.GetValue(size, null);
 89107            int sizeHeight = (int)heightProp.GetValue(size, null);
 108
 89109            if (sizeWidth == width && sizeHeight == height)
 8110                return i;
 111        }
 112
 3113        return -1;
 114    }
 115
 14116    static object GetGroup(GameViewSizeGroupType type) { return getGroup.Invoke(gameViewSizesInstance, new object[] { (i
 117
 118    public static GameViewSizeGroupType GetCurrentGroupType()
 119    {
 0120        var getCurrentGroupTypeProp = gameViewSizesInstance.GetType().GetProperty("currentGroupType");
 121
 0122        return (GameViewSizeGroupType)(int)getCurrentGroupTypeProp.GetValue(gameViewSizesInstance, null);
 123    }
 124}