| | 1 | | using System.Collections; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Huds.QuestsPanel; |
| | 4 | | using NUnit.Framework; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.TestTools; |
| | 7 | |
|
| | 8 | | namespace Tests.QuestsPanelHUD |
| | 9 | | { |
| | 10 | | public class QuestsPanelHUDViewShould |
| | 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 QuestsPanelHUDView hudView; |
| | 17 | |
|
| | 18 | | [SetUp] |
| | 19 | | public void SetUp() |
| | 20 | | { |
| 12 | 21 | | QuestModel questMock = new QuestModel |
| | 22 | | { |
| | 23 | | id = MOCK_QUEST_ID, |
| | 24 | | name = "name", |
| | 25 | | description = "description", |
| | 26 | | }; |
| 12 | 27 | | QuestSection sectionMock = new QuestSection { id = MOCK_SECTION_ID }; |
| 12 | 28 | | QuestTask taskMock = new QuestTask |
| | 29 | | { |
| | 30 | | id = MOCK_TASK_ID, |
| | 31 | | type = "single", |
| | 32 | | payload = JsonUtility.ToJson(new TaskPayload_Single { isDone = false }) |
| | 33 | | }; |
| 12 | 34 | | sectionMock.tasks = new [] { taskMock }; |
| 12 | 35 | | questMock.sections = new [] { sectionMock }; |
| 12 | 36 | | DataStore.i.Quests.quests.Set(new [] { (questMock.id, questMock) }); |
| | 37 | |
|
| 12 | 38 | | QuestsPanelHUDView.ENTRIES_PER_FRAME = 1; |
| 12 | 39 | | hudView = Object.Instantiate(Resources.Load<GameObject>("QuestsPanelHUD")).GetComponent<QuestsPanelHUDView>( |
| 12 | 40 | | } |
| | 41 | |
|
| | 42 | | [Test] |
| | 43 | | public void BeEmptyOnInitialize() |
| | 44 | | { |
| 1 | 45 | | Assert.AreEqual(0, hudView.questEntries.Count); |
| 1 | 46 | | Assert.AreEqual(0, hudView.questsToBeAdded.Count); |
| 1 | 47 | | Assert.IsFalse(hudView.questPopup.gameObject.activeSelf); |
| 1 | 48 | | } |
| | 49 | |
|
| | 50 | | [Test] |
| | 51 | | public void ReactToRequestAddOrUpdateQuest() |
| | 52 | | { |
| 1 | 53 | | hudView.RequestAddOrUpdateQuest(MOCK_QUEST_ID); |
| | 54 | |
|
| 1 | 55 | | Assert.AreEqual(1, hudView.questsToBeAdded.Count); |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | [Test] |
| | 59 | | public void ReactToRequestAddOrUpdateQuest_NoRepeat() |
| | 60 | | { |
| 1 | 61 | | hudView.RequestAddOrUpdateQuest(MOCK_QUEST_ID); |
| 1 | 62 | | hudView.RequestAddOrUpdateQuest(MOCK_QUEST_ID); |
| 1 | 63 | | Assert.AreEqual(1, hudView.questsToBeAdded.Count); |
| 1 | 64 | | } |
| | 65 | |
|
| | 66 | | [Test] |
| | 67 | | public void AddOrUpdateQuestProperly_NotExistentQuest() |
| | 68 | | { |
| 1 | 69 | | hudView.AddOrUpdateQuest("NoQuest"); |
| 1 | 70 | | LogAssert.Expect(LogType.Error, $"Couldn't find quest with ID NoQuest in DataStore"); |
| 1 | 71 | | Assert.AreEqual(0, hudView.questEntries.Count); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | [Test] |
| | 75 | | public void AddOrUpdateQuestProperly_AddQuest() |
| | 76 | | { |
| 1 | 77 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID); |
| 1 | 78 | | Assert.AreEqual(1, hudView.questEntries.Count); |
| 1 | 79 | | Assert.IsTrue(hudView.questEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 80 | | Assert.AreEqual("name", hudView.questEntries[MOCK_QUEST_ID].questName.text); |
| 1 | 81 | | Assert.AreEqual("description", hudView.questEntries[MOCK_QUEST_ID].description.text); |
| 1 | 82 | | Assert.IsTrue(hudView.questEntries[MOCK_QUEST_ID].progressInTitle.gameObject.activeSelf); |
| 1 | 83 | | Assert.IsFalse(hudView.questEntries[MOCK_QUEST_ID].completedProgressInTitle.gameObject.activeSelf); |
| 1 | 84 | | Assert.IsFalse(hudView.questEntries[MOCK_QUEST_ID].completedMarkInTitle.gameObject.activeSelf); |
| 1 | 85 | | Assert.AreEqual(0f, hudView.questEntries[MOCK_QUEST_ID].progressInTitle.transform.localScale.x); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | [Test] |
| | 89 | | public void AddOrUpdateQuestProperly_UpdateQuest() |
| | 90 | | { |
| 1 | 91 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID); |
| 1 | 92 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "newName"; |
| 1 | 93 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].description = "newDescription"; |
| 1 | 94 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections[0].progress = 0.8f; |
| | 95 | |
|
| 1 | 96 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID); |
| | 97 | |
|
| 1 | 98 | | Assert.AreEqual(1, hudView.questEntries.Count); |
| 1 | 99 | | Assert.IsTrue(hudView.questEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 100 | | Assert.AreEqual("newName", hudView.questEntries[MOCK_QUEST_ID].questName.text); |
| 1 | 101 | | Assert.AreEqual("newDescription", hudView.questEntries[MOCK_QUEST_ID].description.text); |
| 1 | 102 | | Assert.IsTrue(hudView.questEntries[MOCK_QUEST_ID].progressInTitle.gameObject.activeSelf); |
| 1 | 103 | | Assert.IsFalse(hudView.questEntries[MOCK_QUEST_ID].completedProgressInTitle.gameObject.activeSelf); |
| 1 | 104 | | Assert.IsFalse(hudView.questEntries[MOCK_QUEST_ID].completedMarkInTitle.gameObject.activeSelf); |
| 1 | 105 | | Assert.AreEqual(0.8f, hudView.questEntries[MOCK_QUEST_ID].progressInTitle.transform.localScale.x); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | [Test] |
| | 109 | | public void AddOrUpdateQuestCompletedProperly() |
| | 110 | | { |
| 1 | 111 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].name = "newName"; |
| 1 | 112 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].description = "newDescription"; |
| 1 | 113 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections[0].tasks[0].progress = 1f; |
| 1 | 114 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].sections[0].progress = 1f; |
| 1 | 115 | | DataStore.i.Quests.quests[MOCK_QUEST_ID].status = QuestsLiterals.Status.COMPLETED; |
| | 116 | |
|
| 1 | 117 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID); |
| | 118 | |
|
| 1 | 119 | | Assert.AreEqual(1, hudView.questEntries.Count); |
| 1 | 120 | | Assert.IsTrue(hudView.questEntries.ContainsKey(MOCK_QUEST_ID)); |
| 1 | 121 | | Assert.AreEqual("newName", hudView.questEntries[MOCK_QUEST_ID].questName.text); |
| 1 | 122 | | Assert.AreEqual("newDescription", hudView.questEntries[MOCK_QUEST_ID].description.text); |
| 1 | 123 | | Assert.IsTrue(hudView.questEntries[MOCK_QUEST_ID].completedProgressInTitle.gameObject.activeSelf); |
| 1 | 124 | | Assert.IsTrue(hudView.questEntries[MOCK_QUEST_ID].completedMarkInTitle.gameObject.activeSelf); |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | [Test] |
| | 128 | | public void RemoveQuestProperly_QuestEntries() |
| | 129 | | { |
| 1 | 130 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID); |
| | 131 | |
|
| 1 | 132 | | hudView.RemoveQuest(MOCK_QUEST_ID); |
| 1 | 133 | | Assert.AreEqual(0, hudView.questEntries.Count); |
| 1 | 134 | | } |
| | 135 | |
|
| | 136 | | [Test] |
| | 137 | | public void RemoveQuestProperly_QuestRequest() |
| | 138 | | { |
| 1 | 139 | | hudView.RequestAddOrUpdateQuest(MOCK_QUEST_ID); |
| | 140 | |
|
| 1 | 141 | | hudView.RemoveQuest(MOCK_QUEST_ID); |
| 1 | 142 | | Assert.AreEqual(0, hudView.questsToBeAdded.Count); |
| 1 | 143 | | } |
| | 144 | |
|
| | 145 | | [Test] |
| | 146 | | public void ShowPopup_NoExistentQuest() |
| | 147 | | { |
| 1 | 148 | | hudView.ShowQuestPopup("NoQuest"); |
| | 149 | |
|
| 1 | 150 | | Assert.IsFalse(hudView.questPopup.gameObject.activeSelf); |
| 1 | 151 | | } |
| | 152 | |
|
| | 153 | | [Test] |
| | 154 | | public void ShowPopup() |
| | 155 | | { |
| 1 | 156 | | hudView.AddOrUpdateQuest(MOCK_QUEST_ID); |
| 1 | 157 | | hudView.ShowQuestPopup(MOCK_QUEST_ID); |
| | 158 | |
|
| 1 | 159 | | Assert.IsTrue(hudView.questPopup.gameObject.activeSelf); |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | [UnityTest] |
| | 163 | | public IEnumerator OrderQuests() |
| | 164 | | { |
| 1 | 165 | | DataStore.i.Quests.quests.Add("0", new QuestModel { id = "0", sections = new [] { new QuestSection { id = "0 |
| 1 | 166 | | DataStore.i.Quests.quests.Add("1", new QuestModel { id = "1", sections = new [] { new QuestSection { id = "1 |
| 1 | 167 | | DataStore.i.Quests.quests.Add("2", new QuestModel { id = "2", sections = new [] { new QuestSection { id = "2 |
| 1 | 168 | | DataStore.i.Quests.quests.Add("3", new QuestModel { id = "3", sections = new [] { new QuestSection { id = "3 |
| | 169 | | /* Order should be |
| | 170 | | * Available |
| | 171 | | * 0 |
| | 172 | | * 1 |
| | 173 | | * 3 |
| | 174 | | * |
| | 175 | | * Completed |
| | 176 | | * 2 |
| | 177 | | */ |
| | 178 | |
|
| 1 | 179 | | hudView.AddOrUpdateQuest("0"); |
| 1 | 180 | | hudView.AddOrUpdateQuest("1"); |
| 1 | 181 | | hudView.AddOrUpdateQuest("2"); |
| 1 | 182 | | hudView.AddOrUpdateQuest("3"); |
| | 183 | |
|
| 1 | 184 | | yield return null; |
| | 185 | |
|
| 1 | 186 | | Assert.AreEqual(0, hudView.questEntries["0"].transform.GetSiblingIndex()); |
| 1 | 187 | | Assert.AreEqual(1, hudView.questEntries["1"].transform.GetSiblingIndex()); |
| 1 | 188 | | Assert.AreEqual(2, hudView.questEntries["3"].transform.GetSiblingIndex()); |
| 1 | 189 | | Assert.AreEqual(0, hudView.questEntries["2"].transform.GetSiblingIndex()); |
| 1 | 190 | | Assert.AreEqual(hudView.availableQuestsContainer, hudView.questEntries["0"].transform.parent); |
| 1 | 191 | | Assert.AreEqual(hudView.availableQuestsContainer, hudView.questEntries["1"].transform.parent); |
| 1 | 192 | | Assert.AreEqual(hudView.availableQuestsContainer, hudView.questEntries["3"].transform.parent); |
| 1 | 193 | | Assert.AreEqual(hudView.completedQuestsContainer, hudView.questEntries["2"].transform.parent); |
| 1 | 194 | | } |
| | 195 | |
|
| | 196 | | [TearDown] |
| 24 | 197 | | public void TearDown() { Object.Destroy(hudView.gameObject); } |
| | 198 | | } |
| | 199 | | } |