|
| 1 | +using NUnit.Framework; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEditor.EditorTools; |
| 4 | +using UnityEditor.ProBuilder; |
| 5 | +using UnityEngine; |
| 6 | +using UnityEngine.ProBuilder; |
| 7 | +using UnityEngine.UIElements; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Test the construction of the contextual menu in the Scene View. |
| 11 | +/// </summary> |
| 12 | +public class ContextualMenuTests |
| 13 | +{ |
| 14 | + [ProBuilderMenuAction] |
| 15 | + class ConfigurableMenuAction : MenuAction |
| 16 | + { |
| 17 | + internal const string actionName = "Action Without File Menu Entry"; |
| 18 | + internal static bool userHasFileMenuEntry { get; set; } |
| 19 | + internal static SelectMode userSelectMode { get; set; } |
| 20 | + internal static bool userEnabled { get; set; } |
| 21 | + |
| 22 | + public override ToolbarGroup group |
| 23 | + { |
| 24 | + get { return ToolbarGroup.Geometry; } |
| 25 | + } |
| 26 | + |
| 27 | + public override Texture2D icon => null; |
| 28 | + |
| 29 | + public override string iconPath => string.Empty; |
| 30 | + |
| 31 | + public override TooltipContent tooltip => new TooltipContent( |
| 32 | + actionName, |
| 33 | + @"This action should not have a file menu entry." |
| 34 | + ); |
| 35 | + |
| 36 | + public ConfigurableMenuAction() |
| 37 | + { |
| 38 | + } |
| 39 | + |
| 40 | + protected override ActionResult PerformActionImplementation() |
| 41 | + { |
| 42 | + return ActionResult.Success; |
| 43 | + } |
| 44 | + |
| 45 | + public override SelectMode validSelectModes => userSelectMode; |
| 46 | + public override bool enabled => userEnabled; |
| 47 | + protected internal override bool hasFileMenuEntry => userHasFileMenuEntry; |
| 48 | + } |
| 49 | + |
| 50 | + ProBuilderMesh m_PBMesh; |
| 51 | + |
| 52 | + [SetUp] |
| 53 | + public void Setup() |
| 54 | + { |
| 55 | + m_PBMesh = ShapeFactory.Instantiate(typeof(UnityEngine.ProBuilder.Shapes.Plane)); |
| 56 | + } |
| 57 | + |
| 58 | + [TearDown] |
| 59 | + public void TearDown() |
| 60 | + { |
| 61 | + if (m_PBMesh) |
| 62 | + Object.DestroyImmediate(m_PBMesh.gameObject); |
| 63 | + } |
| 64 | + |
| 65 | + PositionToolContext PrepareToolContext(SelectMode toolSelectMode) |
| 66 | + { |
| 67 | + MeshSelection.SetSelection(m_PBMesh.gameObject); |
| 68 | + ActiveEditorTracker.sharedTracker.ForceRebuild(); |
| 69 | + |
| 70 | + ToolManager.SetActiveContext<PositionToolContext>(); |
| 71 | + Tools.current = Tool.Move; |
| 72 | + ProBuilderEditor.selectMode = toolSelectMode; |
| 73 | + ActiveEditorTracker.sharedTracker.ForceRebuild(); |
| 74 | + |
| 75 | + PositionToolContext ctx = Resources.FindObjectsOfTypeAll<PositionToolContext>()?[0]; |
| 76 | + Assume.That(ctx, Is.Not.Null); |
| 77 | + return ctx; |
| 78 | + } |
| 79 | + |
| 80 | + bool CheckDropdownMenuHasItem(string name, DropdownMenuAction.Status status, DropdownMenu menu) |
| 81 | + { |
| 82 | + DropdownMenuAction foundInMenu = null; |
| 83 | + foreach (var t in menu.MenuItems()) |
| 84 | + { |
| 85 | + if (t is not DropdownMenuAction menuAction) |
| 86 | + continue; |
| 87 | + |
| 88 | + if (menuAction.name == name) |
| 89 | + { |
| 90 | + foundInMenu = menuAction; |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return (foundInMenu?.status == status); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + [TestCase(true, ExpectedResult = false)] |
| 100 | + [TestCase(false, ExpectedResult = true)] |
| 101 | + public bool MenuActionWithoutMenuItem_hasFileMenuEntry(bool hasFileMenuEntry) |
| 102 | + { |
| 103 | + var ctx = PrepareToolContext(SelectMode.Face); |
| 104 | + |
| 105 | + ConfigurableMenuAction.userHasFileMenuEntry = hasFileMenuEntry; |
| 106 | + ConfigurableMenuAction.userSelectMode = SelectMode.Any; |
| 107 | + ConfigurableMenuAction.userEnabled = true; |
| 108 | + |
| 109 | + DropdownMenu menu = new DropdownMenu(); |
| 110 | + ctx.PopulateMenu(menu); |
| 111 | + menu.PrepareForDisplay(null); |
| 112 | + |
| 113 | + return CheckDropdownMenuHasItem(ConfigurableMenuAction.actionName, DropdownMenuAction.Status.Normal, menu); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + [TestCase(SelectMode.Edge, ExpectedResult = false)] |
| 118 | + [TestCase(SelectMode.Face, ExpectedResult = true)] |
| 119 | + [TestCase(SelectMode.Vertex, ExpectedResult = false)] |
| 120 | + public bool MenuAction_SelectModeSetToFace_EnabledOnlyForFaceSelection(SelectMode mode) |
| 121 | + { |
| 122 | + var ctx = PrepareToolContext(mode); |
| 123 | + |
| 124 | + ConfigurableMenuAction.userHasFileMenuEntry = false; |
| 125 | + ConfigurableMenuAction.userSelectMode = SelectMode.Face; |
| 126 | + ConfigurableMenuAction.userEnabled = true; |
| 127 | + |
| 128 | + DropdownMenu menu = new DropdownMenu(); |
| 129 | + ctx.PopulateMenu(menu); |
| 130 | + menu.PrepareForDisplay(null); |
| 131 | + |
| 132 | + // MenuAction is expected to be present in the menu only when the mode matches. |
| 133 | + return CheckDropdownMenuHasItem(ConfigurableMenuAction.actionName, DropdownMenuAction.Status.Normal, menu); |
| 134 | + } |
| 135 | + |
| 136 | + [Test] |
| 137 | + [TestCase(true, ExpectedResult = true)] |
| 138 | + [TestCase(false, ExpectedResult = false)] |
| 139 | + public bool MenuAction_enabledPropertyIsFollowedByMenu(bool enabled) |
| 140 | + { |
| 141 | + var ctx = PrepareToolContext(SelectMode.Face); |
| 142 | + |
| 143 | + ConfigurableMenuAction.userHasFileMenuEntry = false; |
| 144 | + ConfigurableMenuAction.userSelectMode = SelectMode.Face; |
| 145 | + ConfigurableMenuAction.userEnabled = enabled; |
| 146 | + |
| 147 | + DropdownMenu menu = new DropdownMenu(); |
| 148 | + ctx.PopulateMenu(menu); |
| 149 | + menu.PrepareForDisplay(null); |
| 150 | + |
| 151 | + return CheckDropdownMenuHasItem(ConfigurableMenuAction.actionName, DropdownMenuAction.Status.Normal, menu); |
| 152 | + } |
| 153 | +} |
0 commit comments