| | 1 | | using System.Collections; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Huds.QuestsTracker; |
| | 4 | | using NUnit.Framework; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.TestTools; |
| | 7 | |
|
| | 8 | | namespace Tests.QuestsTrackerHUD |
| | 9 | | { |
| | 10 | | public class QuestsTrackerHUDViewShould |
| | 11 | | { |
| | 12 | | private const string MOCK_QUEST_ID = "quest0"; |
| | 13 | | private const string MOCK_SECTION_ID = "section0"; |
| | 14 | | private const string MOCK_TASK_ID = "task0"; |
| | 15 | |
|
| | 16 | | private QuestsTrackerHUDView hudView; |
| | 17 | |
|
| | 18 | | [SetUp] |
| | 19 | | public void SetUp() |
| | 20 | | { |
| 11 | 21 | | QuestModel questMock = new QuestModel |
| | 22 | | { |
| | 23 | | id = MOCK_QUEST_ID, name = "name", description = "description", |
| | 24 | | }; |
| 11 | 25 | | QuestSection sectionMock = new QuestSection |
| | 26 | | { |
| | 27 | | id = MOCK_SECTION_ID |
| | 28 | | }; |
| 11 | 29 | | QuestTask taskMock = new QuestTask |
| | 30 | | { |
| | 31 | | id = MOCK_TASK_ID, |
| | 32 | | type = "single", |
| | 33 | | payload = JsonUtility.ToJson(new TaskPayload_Single |
| | 34 | | { |
| | 35 | | isDone = false |
| | 36 | | }) |
| | 37 | | }; |
| 11 | 38 | | sectionMock.tasks = new [] |
| | 39 | | { |
| | 40 | | taskMock |
| | 41 | | }; |
| 11 | 42 | | questMock.sections = new [] |
| | 43 | | { |
| | 44 | | sectionMock |
| | 45 | | }; |
| 11 | 46 | | DataStore.i.Quests.quests.Set(new [] |
| | 47 | | { |
| | 48 | | (questMock.id, questMock) |
| | 49 | | }); |
| | 50 | |
|
| 11 | 51 | | QuestsTrackerHUDView.ENTRIES_PER_FRAME = int.MaxValue; |
| | 52 | |
|
| 11 | 53 | | hudView = Object.Instantiate(Resources.Load<GameObject>("QuestsTrackerHUD")).GetComponent<QuestsTrackerHUDVi |
| 11 | 54 | | } |
| | 55 | |
|
| | 56 | | [Test] |
| 2 | 57 | | public void BeEmptyOnInitialize() { Assert.AreEqual(0, hudView.currentEntries.Count); } |
| | 58 | |
|
| | 59 | | [Test] |
| | 60 | | public void TrackQuestProgress() |
| | 61 | | { |
| 1 | 62 | | hudView.UpdateQuest(MOCK_QUEST_ID, true); |
| | 63 | |
|
| 1 | 64 | | Assert.AreEqual(1, hudView.currentEntries.Count); |
| 1 | 65 | | Assert.IsTrue(hudView.currentEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | [Test] |
| | 69 | | public void AddPinnedQuestToTracker() |
| | 70 | | { |
| 1 | 71 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, true); |
| | 72 | |
|
| 1 | 73 | | Assert.AreEqual(1, hudView.questsContainer.childCount); |
| 1 | 74 | | Assert.IsTrue(hudView.currentEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 75 | | Assert.IsTrue(hudView.currentEntries[MOCK_QUEST_ID].isPinned); |
| 1 | 76 | | } |
| | 77 | |
|
| | 78 | | [Test] |
| | 79 | | public void AddNotPinnedQuestToTracker() |
| | 80 | | { |
| 1 | 81 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, false); |
| | 82 | |
|
| 1 | 83 | | Assert.AreEqual(1, hudView.questsContainer.childCount); |
| 1 | 84 | | Assert.IsTrue(hudView.currentEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 85 | | Assert.IsFalse(hudView.currentEntries[MOCK_QUEST_ID].isPinned); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | [Test] |
| | 89 | | public void PinUntrackedQuestProperly() |
| | 90 | | { |
| 1 | 91 | | hudView.PinQuest(MOCK_QUEST_ID); |
| 1 | 92 | | Assert.IsTrue(hudView.currentEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 93 | | } |
| | 94 | |
|
| | 95 | | [Test] |
| | 96 | | public void PinTrackedQuestProperly() |
| | 97 | | { |
| 1 | 98 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, false); |
| | 99 | |
|
| 1 | 100 | | hudView.PinQuest(MOCK_QUEST_ID); |
| | 101 | |
|
| 1 | 102 | | Assert.IsTrue(hudView.currentEntries[MOCK_QUEST_ID].isPinned); |
| 1 | 103 | | } |
| | 104 | |
|
| | 105 | | [Test] |
| | 106 | | public void UnpinTrackedQuestProperly() |
| | 107 | | { |
| 1 | 108 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, true); |
| | 109 | |
|
| 1 | 110 | | hudView.UnpinQuest(MOCK_QUEST_ID); |
| | 111 | |
|
| 1 | 112 | | Assert.IsFalse(hudView.currentEntries[MOCK_QUEST_ID].isPinned); |
| 1 | 113 | | } |
| | 114 | |
|
| | 115 | | [UnityTest] |
| | 116 | | public IEnumerator RemoveTrackedEntry() |
| | 117 | | { |
| 1 | 118 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, false); |
| | 119 | |
|
| 1 | 120 | | hudView.RemoveEntry(MOCK_QUEST_ID); |
| | 121 | |
|
| 1 | 122 | | yield return null; //Let Unity destroy the entry properly |
| | 123 | |
|
| 1 | 124 | | Assert.AreEqual(0, hudView.currentEntries.Count); |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | [UnityTest] |
| | 128 | | public IEnumerator PopulateQuestEntryProperly_SingleTask() |
| | 129 | | { |
| 1 | 130 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "questName"; |
| 1 | 131 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections = new QuestSection[2]; |
| 6 | 132 | | for (int i = 0; i < 2; i++) |
| | 133 | | { |
| 2 | 134 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections[i] = new QuestSection() |
| | 135 | | { |
| | 136 | | id = $"section{i}", |
| | 137 | | name = $"sectionName{i}", |
| | 138 | | progress = 1 - i, //So the first section is completed and the second one not |
| | 139 | | tasks = new [] |
| | 140 | | { |
| | 141 | | new QuestTask |
| | 142 | | { |
| | 143 | | id = $"task{i}", |
| | 144 | | name = $"task{i}", |
| | 145 | | type = "single", |
| | 146 | | payload = JsonUtility.ToJson(new TaskPayload_Single { isDone = false }) |
| | 147 | | } |
| | 148 | | } |
| | 149 | | }; |
| | 150 | | } |
| | 151 | |
|
| 1 | 152 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "questName"; |
| 1 | 153 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, false); |
| | 154 | |
|
| 1 | 155 | | yield return null; //wait for all the instantiation/destruction of items to be done by unity |
| | 156 | |
|
| 1 | 157 | | Assert.AreEqual( "questName", hudView.currentEntries[MOCK_QUEST_ID].questTitle.text); |
| 1 | 158 | | Assert.AreEqual( 0, hudView.currentEntries[MOCK_QUEST_ID].progress.fillAmount); |
| 1 | 159 | | Assert.AreEqual( 2, hudView.currentEntries[MOCK_QUEST_ID].sectionContainer.childCount); |
| 1 | 160 | | Assert.AreEqual( 1, hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section0"].taskEntries.Count); |
| | 161 | |
|
| 1 | 162 | | var taskEntry = hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section0"].taskContainer.GetComponentI |
| 1 | 163 | | Assert.NotNull(taskEntry); |
| 1 | 164 | | Assert.AreEqual("task0" , taskEntry.taskTitle.text); |
| 1 | 165 | | Assert.AreEqual("0/1" , taskEntry.progressText.text); |
| | 166 | |
|
| 1 | 167 | | var taskEntry1 = hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section1"].taskContainer.GetComponent |
| 1 | 168 | | Assert.NotNull(taskEntry1); |
| 1 | 169 | | Assert.AreEqual("task1" , taskEntry1.taskTitle.text); |
| 1 | 170 | | Assert.AreEqual("0/1" , taskEntry1.progressText.text); |
| 1 | 171 | | } |
| | 172 | |
|
| | 173 | | [UnityTest] |
| | 174 | | public IEnumerator PopulateQuestEntryProperly_NumericTask() |
| | 175 | | { |
| 1 | 176 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "questName"; |
| 1 | 177 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections = new QuestSection[2]; |
| 6 | 178 | | for (int i = 0; i < 2; i++) |
| | 179 | | { |
| 2 | 180 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections[i] = new QuestSection() |
| | 181 | | { |
| | 182 | | id = $"section{i}", |
| | 183 | | name = $"sectionName{i}", |
| | 184 | | progress = 1 - i, //So the first section is completed and the second one not |
| | 185 | | tasks = new [] |
| | 186 | | { |
| | 187 | | new QuestTask |
| | 188 | | { |
| | 189 | | id = $"task{i}", |
| | 190 | | name = $"task{i}", |
| | 191 | | type = "numeric", |
| | 192 | | payload = JsonUtility.ToJson(new TaskPayload_Numeric { start = 10, current = 15, end = 20 }) |
| | 193 | | } |
| | 194 | | } |
| | 195 | | }; |
| | 196 | | } |
| | 197 | |
|
| 1 | 198 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "questName"; |
| 1 | 199 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, false); |
| | 200 | |
|
| 1 | 201 | | yield return null; //wait for all the instantiation/destruction of items to be done by unity |
| | 202 | |
|
| 1 | 203 | | Assert.AreEqual( "questName", hudView.currentEntries[MOCK_QUEST_ID].questTitle.text); |
| 1 | 204 | | Assert.AreEqual( 0, hudView.currentEntries[MOCK_QUEST_ID].progress.fillAmount); |
| 1 | 205 | | Assert.AreEqual( 2, hudView.currentEntries[MOCK_QUEST_ID].sectionContainer.childCount); |
| 1 | 206 | | Assert.AreEqual( 1, hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section0"].taskEntries.Count); |
| | 207 | |
|
| 1 | 208 | | var taskEntry = hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section0"].taskContainer.GetComponentI |
| 1 | 209 | | Assert.NotNull(taskEntry); |
| 1 | 210 | | Assert.AreEqual("task0" , taskEntry.taskTitle.text); |
| 1 | 211 | | Assert.AreEqual("15/20" , taskEntry.progressText.text); |
| | 212 | |
|
| 1 | 213 | | var taskEntry1 = hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section1"].taskContainer.GetComponent |
| 1 | 214 | | Assert.NotNull(taskEntry1); |
| 1 | 215 | | Assert.AreEqual("task1" , taskEntry1.taskTitle.text); |
| 1 | 216 | | Assert.AreEqual("15/20" , taskEntry1.progressText.text); |
| 1 | 217 | | } |
| | 218 | |
|
| | 219 | | [UnityTest] |
| | 220 | | public IEnumerator PopulateQuestEntryProperly_StepBased() |
| | 221 | | { |
| 1 | 222 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "questName"; |
| 1 | 223 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections = new QuestSection[2]; |
| 6 | 224 | | for (int i = 0; i < 2; i++) |
| | 225 | | { |
| 2 | 226 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections[i] = new QuestSection() |
| | 227 | | { |
| | 228 | | id = $"section{i}", |
| | 229 | | name = $"sectionName{i}", |
| | 230 | | progress = 1 - i, //So the first section is completed and the second one not, |
| | 231 | | tasks = new [] |
| | 232 | | { |
| | 233 | | new QuestTask |
| | 234 | | { |
| | 235 | | id = $"task{i}", |
| | 236 | | name = $"task{i}", |
| | 237 | | type = "step-based", |
| | 238 | | payload = JsonUtility.ToJson(new TaskPayload_Numeric { start = 10, current = 15, end = 20 }) |
| | 239 | | } |
| | 240 | | } |
| | 241 | | }; |
| | 242 | | } |
| | 243 | |
|
| 1 | 244 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "questName"; |
| 1 | 245 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID, false); |
| | 246 | |
|
| 1 | 247 | | yield return null; //wait for all the instantiation/destruction of items to be done by unity |
| | 248 | |
|
| 1 | 249 | | Assert.AreEqual( "questName", hudView.currentEntries[MOCK_QUEST_ID].questTitle.text); |
| 1 | 250 | | Assert.AreEqual( 0, hudView.currentEntries[MOCK_QUEST_ID].progress.fillAmount); |
| 1 | 251 | | Assert.AreEqual( 2, hudView.currentEntries[MOCK_QUEST_ID].sectionContainer.childCount); |
| 1 | 252 | | Assert.AreEqual( 1, hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section0"].taskEntries.Count); |
| | 253 | |
|
| 1 | 254 | | var taskEntry = hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section0"].taskContainer.GetComponentI |
| 1 | 255 | | Assert.NotNull(taskEntry); |
| 1 | 256 | | Assert.AreEqual("task0" , taskEntry.taskTitle.text); |
| 1 | 257 | | Assert.AreEqual("15/20" , taskEntry.progressText.text); |
| | 258 | |
|
| 1 | 259 | | var taskEntry1 = hudView.currentEntries[MOCK_QUEST_ID].sectionEntries["section1"].taskContainer.GetComponent |
| 1 | 260 | | Assert.NotNull(taskEntry1); |
| 1 | 261 | | Assert.AreEqual("task1" , taskEntry1.taskTitle.text); |
| 1 | 262 | | Assert.AreEqual("15/20" , taskEntry1.progressText.text); |
| 1 | 263 | | } |
| | 264 | | [TearDown] |
| 22 | 265 | | public void TearDown() { Object.Destroy(hudView.gameObject); } |
| | 266 | | } |
| | 267 | | } |