| | 1 | | using NUnit.Framework; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.EventSystems; |
| | 4 | |
|
| | 5 | | namespace Tests.BuildModeHUDViews |
| | 6 | | { |
| | 7 | | public class QuickBarViewShould |
| | 8 | | { |
| | 9 | | private QuickBarView quickBarView; |
| | 10 | |
|
| | 11 | | [SetUp] |
| | 12 | | public void SetUp() |
| | 13 | | { |
| 10 | 14 | | quickBarView = QuickBarView.Create(); |
| 10 | 15 | | quickBarView.generalCanvas = new GameObject().AddComponent<Canvas>(); |
| 10 | 16 | | } |
| | 17 | |
|
| | 18 | | [TearDown] |
| 20 | 19 | | public void TearDown() { Object.Destroy(quickBarView.gameObject); } |
| | 20 | |
|
| | 21 | | [Test] |
| | 22 | | public void SelectQuickBarObjectCorrectly() |
| | 23 | | { |
| | 24 | | // Arrange |
| 1 | 25 | | int selectedObjectIndex = -1; |
| 1 | 26 | | int indexToSelect = 3; |
| 2 | 27 | | quickBarView.OnQuickBarObjectSelected += (index) => selectedObjectIndex = index; |
| | 28 | |
|
| | 29 | | // Act |
| 1 | 30 | | quickBarView.QuickBarObjectSelected(indexToSelect); |
| | 31 | |
|
| | 32 | | // Assert |
| 1 | 33 | | Assert.AreEqual(indexToSelect, selectedObjectIndex, "The selected object index does not match!"); |
| 1 | 34 | | } |
| | 35 | |
|
| | 36 | | [Test] |
| | 37 | | public void SetIndexToBeginDragCorrectly() |
| | 38 | | { |
| | 39 | | // Arrange |
| 1 | 40 | | int draggedObjectIndex = -1; |
| 1 | 41 | | int indexToBeginDrag = 3; |
| 2 | 42 | | quickBarView.OnSetIndexToBeginDrag += (index) => draggedObjectIndex = index; |
| | 43 | |
|
| | 44 | | // Act |
| 1 | 45 | | quickBarView.SetIndexToBeginDrag(indexToBeginDrag); |
| | 46 | |
|
| | 47 | | // Assert |
| 1 | 48 | | Assert.AreEqual(indexToBeginDrag, draggedObjectIndex, "The dragged object index does not match!"); |
| 1 | 49 | | } |
| | 50 | |
|
| | 51 | | [Test] |
| | 52 | | public void SetIndexToDropCorrectly() |
| | 53 | | { |
| | 54 | | // Arrange |
| 1 | 55 | | int dropObjectIndex = -1; |
| 1 | 56 | | int indexToDrop = 3; |
| 2 | 57 | | quickBarView.OnSetIndexToDrop += (index) => dropObjectIndex = index; |
| | 58 | |
|
| | 59 | | // Act |
| 1 | 60 | | quickBarView.SetIndexToDrop(indexToDrop); |
| | 61 | |
|
| | 62 | | // Assert |
| 1 | 63 | | Assert.AreEqual(indexToDrop, dropObjectIndex, "The drop object index does not match!"); |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | | [Test] |
| | 67 | | public void DropSceneObjectFromQuickBarCorrectly() |
| | 68 | | { |
| | 69 | | // Arrange |
| 1 | 70 | | Texture testTexture = null; |
| 1 | 71 | | int testFromIndex = 0; |
| 1 | 72 | | int testToIndex = 1; |
| | 73 | | Texture returnedTexture; |
| 1 | 74 | | int returnedFromIndex = 0; |
| 1 | 75 | | int returnedToIndex = 1; |
| | 76 | |
|
| 1 | 77 | | quickBarView.OnSceneObjectDroppedFromQuickBar += (fromIndex, toIndex, texture) => |
| | 78 | | { |
| 1 | 79 | | returnedFromIndex = fromIndex; |
| 1 | 80 | | returnedToIndex = toIndex; |
| 1 | 81 | | returnedTexture = texture; |
| 1 | 82 | | }; |
| | 83 | |
|
| | 84 | | // Act |
| 1 | 85 | | quickBarView.SceneObjectDroppedFromQuickBar(testFromIndex, testToIndex, testTexture); |
| | 86 | |
|
| | 87 | | // Assert |
| 1 | 88 | | Assert.AreEqual(returnedFromIndex, testFromIndex, "The returnedFromIndex does not match!"); |
| 1 | 89 | | Assert.AreEqual(returnedToIndex, testToIndex, "The returnedToIndex does not match!"); |
| 1 | 90 | | } |
| | 91 | |
|
| | 92 | | [Test] |
| | 93 | | public void DropSceneObjectFromCatalogCorrectly() |
| | 94 | | { |
| | 95 | | // Arrange |
| 1 | 96 | | BaseEventData droppedObject = null; |
| 1 | 97 | | BaseEventData objectToDrop = new BaseEventData(null); |
| 2 | 98 | | quickBarView.OnSceneObjectDroppedFromCatalog += (data) => droppedObject = data; |
| | 99 | |
|
| | 100 | | // Act |
| 1 | 101 | | quickBarView.SceneObjectDroppedFromCatalog(objectToDrop); |
| | 102 | |
|
| | 103 | | // Assert |
| 1 | 104 | | Assert.IsNotNull(droppedObject, "The dropped object is null!"); |
| 1 | 105 | | Assert.AreEqual(objectToDrop, droppedObject, "The dropped object does not match!"); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | [Test] |
| | 109 | | public void TriggerQuickBarInputCorrectly() |
| | 110 | | { |
| | 111 | | // Arrange |
| 1 | 112 | | int triggeredIndex = -1; |
| 1 | 113 | | int indexToDrop = 3; |
| 2 | 114 | | quickBarView.OnQuickBarInputTriggered += (index) => triggeredIndex = index; |
| | 115 | |
|
| | 116 | | // Act |
| 1 | 117 | | quickBarView.OnQuickBarInputTriggedered(indexToDrop); |
| | 118 | |
|
| | 119 | | // Assert |
| 1 | 120 | | Assert.AreEqual(indexToDrop, triggeredIndex, "The triggered index does not match!"); |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | [Test] |
| | 124 | | public void BeginDragSlotCorrectly() |
| | 125 | | { |
| | 126 | | // Arrange |
| 1 | 127 | | int testIndex = 0; |
| 1 | 128 | | quickBarView.lastIndexToBeginDrag = -1; |
| 1 | 129 | | quickBarView.shortcutsImgs[0].SetTexture(new Texture2D(10, 10)); |
| | 130 | |
|
| | 131 | |
|
| | 132 | | // Act |
| 1 | 133 | | quickBarView.BeginDragSlot(testIndex); |
| | 134 | |
|
| | 135 | | // Assert |
| 1 | 136 | | Assert.AreEqual(testIndex, quickBarView.lastIndexToBeginDrag, "The lastIndexToBeginDrag does not match!"); |
| 1 | 137 | | Assert.IsTrue(quickBarView.draggedSlot.isActiveAndEnabled, "The draggedSlot is not active!"); |
| 1 | 138 | | Assert.IsTrue(quickBarView.draggedSlot.image.isActiveAndEnabled, "The draggedSlot image is not active!"); |
| 1 | 139 | | } |
| | 140 | |
|
| | 141 | | [Test] |
| | 142 | | public void DragSlotCorrectly() |
| | 143 | | { |
| | 144 | | // Arrange |
| 1 | 145 | | PointerEventData testEventData = new PointerEventData(null); |
| 1 | 146 | | testEventData.position = new Vector2(5, 3); |
| 1 | 147 | | int testIndex = 0; |
| 1 | 148 | | quickBarView.draggedSlot.slotTransform.position = Vector3.zero; |
| 1 | 149 | | quickBarView.shortcutsImgs[0].SetTexture(new Texture2D(10, 10)); |
| | 150 | |
|
| | 151 | |
|
| | 152 | | // Act |
| 1 | 153 | | quickBarView.DragSlot(testEventData, testIndex); |
| | 154 | |
|
| | 155 | | // Assert |
| 1 | 156 | | Assert.AreEqual(testEventData.position, (Vector2)quickBarView.draggedSlot.slotTransform.position, "The dragg |
| 1 | 157 | | } |
| | 158 | |
|
| | 159 | | [Test] |
| | 160 | | public void EndDragSlotCorrectly() |
| | 161 | | { |
| | 162 | | // Arrange |
| 1 | 163 | | int testIndex = 0; |
| 1 | 164 | | int returnedIndex = -1; |
| 1 | 165 | | quickBarView.shortcutsImgs[0].SetTexture(new Texture2D(10, 10)); |
| 3 | 166 | | quickBarView.OnQuickBarObjectSelected += (index) => { returnedIndex = index; }; |
| | 167 | |
|
| | 168 | |
|
| | 169 | | // Act |
| 1 | 170 | | quickBarView.EndDragSlot(testIndex); |
| | 171 | |
|
| | 172 | | // Assert |
| 1 | 173 | | Assert.IsFalse(quickBarView.draggedSlot.image.isActiveAndEnabled, "The draggedSlot image is active!"); |
| 1 | 174 | | Assert.IsFalse(quickBarView.draggedSlot.isActiveAndEnabled, "The draggedSlot is active!"); |
| 1 | 175 | | Assert.AreEqual(testIndex, returnedIndex, "The selected index does not match!"); |
| 1 | 176 | | } |
| | 177 | |
|
| | 178 | | [Test] |
| | 179 | | public void CancelCurrentDraggingCorrectly() |
| | 180 | | { |
| | 181 | | // Arrange |
| 1 | 182 | | quickBarView.lastIndexToBeginDrag = 5; |
| | 183 | |
|
| | 184 | | // Act |
| 1 | 185 | | quickBarView.CancelCurrentDragging(); |
| | 186 | |
|
| | 187 | | // Assert |
| 1 | 188 | | Assert.AreEqual(-1, quickBarView.lastIndexToBeginDrag, "The lastIndexToBeginDrag does not match!"); |
| 1 | 189 | | } |
| | 190 | | } |
| | 191 | | } |