| | 1 | | using NUnit.Framework; |
| | 2 | | using UnityEngine; |
| | 3 | | using KernelConfigurationTypes; |
| | 4 | |
|
| | 5 | | public class KernelConfigurationShould |
| | 6 | | { |
| | 7 | | [Test] |
| | 8 | | public void TriggerInitializePromiseCorrectly() |
| | 9 | | { |
| | 10 | | const float commRadiusTestValue = 1234; |
| | 11 | | const bool voiceChatEnabledTestValue = false; |
| | 12 | | const bool builderInWorldEnabledTestValue = false; |
| | 13 | | const string regexTestValue = "1234"; |
| 1 | 14 | | KernelConfig.i.initialized = false; |
| | 15 | |
|
| 1 | 16 | | var promiseFromController = KernelConfig.i.EnsureConfigInitialized(); |
| 1 | 17 | | Assert.IsTrue(promiseFromController.keepWaiting, "Promise shouldn't be resolved until first value is set"); |
| | 18 | |
|
| 1 | 19 | | KernelConfig.i.Set(new KernelConfigModel() |
| | 20 | | { |
| | 21 | | comms = new Comms() |
| | 22 | | { |
| | 23 | | commRadius = commRadiusTestValue, |
| | 24 | | voiceChatEnabled = voiceChatEnabledTestValue |
| | 25 | | }, |
| | 26 | | profiles = new Profiles() |
| | 27 | | { |
| | 28 | | nameValidCharacterRegex = regexTestValue, |
| | 29 | | nameValidRegex = regexTestValue |
| | 30 | | }, |
| | 31 | | features = new Features() |
| | 32 | | { |
| | 33 | | enableBuilderInWorld = builderInWorldEnabledTestValue |
| | 34 | | }, |
| | 35 | | }); |
| | 36 | |
|
| 1 | 37 | | Assert.IsFalse(promiseFromController.keepWaiting, "Promise should be resolved"); |
| 1 | 38 | | Assert.AreEqual(commRadiusTestValue, promiseFromController.value?.comms.commRadius, "Promise value should match |
| 1 | 39 | | Assert.AreEqual(voiceChatEnabledTestValue, promiseFromController.value?.comms.voiceChatEnabled, "Promise value s |
| 1 | 40 | | Assert.AreEqual(regexTestValue, promiseFromController.value?.profiles.nameValidCharacterRegex, "Promise value sh |
| 1 | 41 | | Assert.AreEqual(regexTestValue, promiseFromController.value?.profiles.nameValidRegex, "Promise value should matc |
| | 42 | |
|
| 1 | 43 | | bool promiseFromControllerPass = false; |
| | 44 | |
|
| 1 | 45 | | promiseFromController.Then((config) => |
| | 46 | | { |
| 1 | 47 | | promiseFromControllerPass = |
| | 48 | | config.comms.commRadius == commRadiusTestValue && |
| | 49 | | config.comms.voiceChatEnabled == voiceChatEnabledTestValue && |
| | 50 | | config.profiles.nameValidCharacterRegex == regexTestValue && |
| | 51 | | config.profiles.nameValidRegex == regexTestValue; |
| 1 | 52 | | }); |
| | 53 | |
|
| 1 | 54 | | Assert.IsTrue(promiseFromControllerPass); |
| 1 | 55 | | } |
| | 56 | |
|
| | 57 | | [Test] |
| | 58 | | public void TriggerOnChangeCorrectly() |
| | 59 | | { |
| | 60 | | const float commRadiusTestValue = 1234; |
| | 61 | | const float commRadiusTestValue2 = 5678; |
| | 62 | | const bool voiceChatEnabledTestValue = false; |
| | 63 | | const bool voiceChatEnabledTestValue2 = true; |
| | 64 | | const bool builderInWorldEnabledTestValue = false; |
| | 65 | | const bool builderInWorldEnabledTestValue2 = false; |
| | 66 | | const string regexTestValue = "1234"; |
| | 67 | | const string regexTestValue2 = "5678"; |
| | 68 | |
|
| 1 | 69 | | bool onChangeCalled = false; |
| 1 | 70 | | bool onChangePass = false; |
| | 71 | |
|
| 1 | 72 | | KernelConfig.i.Set(new KernelConfigModel()); |
| | 73 | |
|
| 1 | 74 | | KernelConfigModel model = new KernelConfigModel() |
| | 75 | | { |
| | 76 | | comms = new Comms() |
| | 77 | | { |
| | 78 | | commRadius = commRadiusTestValue, |
| | 79 | | voiceChatEnabled = voiceChatEnabledTestValue |
| | 80 | | }, |
| | 81 | | profiles = new Profiles() |
| | 82 | | { |
| | 83 | | nameValidCharacterRegex = regexTestValue, |
| | 84 | | nameValidRegex = regexTestValue |
| | 85 | | }, |
| | 86 | | features = new Features() |
| | 87 | | { |
| | 88 | | enableBuilderInWorld = builderInWorldEnabledTestValue |
| | 89 | | }, |
| | 90 | | }; |
| | 91 | |
|
| 1 | 92 | | KernelConfig.OnKernelConfigChanged onConfigChange = (current, prev) => |
| | 93 | | { |
| 1 | 94 | | onChangeCalled = true; |
| 1 | 95 | | onChangePass = |
| | 96 | | current.comms.commRadius == commRadiusTestValue && |
| | 97 | | current.comms.voiceChatEnabled == voiceChatEnabledTestValue && |
| | 98 | | current.profiles.nameValidCharacterRegex == regexTestValue && |
| | 99 | | current.profiles.nameValidRegex == regexTestValue; |
| 1 | 100 | | }; |
| | 101 | |
|
| 1 | 102 | | KernelConfig.i.OnChange += onConfigChange; |
| | 103 | |
|
| 1 | 104 | | KernelConfig.i.Set(model); |
| 1 | 105 | | Assert.IsTrue(onChangePass); |
| | 106 | |
|
| 1 | 107 | | onChangeCalled = false; |
| 1 | 108 | | onChangePass = false; |
| | 109 | |
|
| 1 | 110 | | KernelConfigModel modelUpdateWithSameValues = model.Clone(); |
| 1 | 111 | | KernelConfig.i.Set(modelUpdateWithSameValues); // this shouldn't trigger onChange cause it has the same values |
| 1 | 112 | | Assert.IsFalse(onChangeCalled, "OnChange was called even if the new value is equal to the new one"); |
| | 113 | |
|
| 1 | 114 | | KernelConfig.i.OnChange -= onConfigChange; |
| | 115 | |
|
| 1 | 116 | | onConfigChange = (current, prev) => |
| | 117 | | { |
| 1 | 118 | | onChangeCalled = true; |
| 1 | 119 | | onChangePass = |
| | 120 | | current.comms.commRadius == commRadiusTestValue2 && prev.comms.commRadius == commRadiusTestValue && |
| | 121 | | current.comms.voiceChatEnabled == voiceChatEnabledTestValue2 && prev.comms.voiceChatEnabled == voiceChat |
| | 122 | | current.profiles.nameValidCharacterRegex == regexTestValue2 && prev.profiles.nameValidRegex == regexTest |
| | 123 | | current.profiles.nameValidRegex == regexTestValue2 && prev.profiles.nameValidRegex == regexTestValue; |
| 1 | 124 | | }; |
| | 125 | |
|
| 1 | 126 | | KernelConfig.i.OnChange += onConfigChange; |
| | 127 | |
|
| 1 | 128 | | KernelConfig.i.Set(new KernelConfigModel() |
| | 129 | | { |
| | 130 | | comms = new Comms() |
| | 131 | | { |
| | 132 | | commRadius = commRadiusTestValue2, |
| | 133 | | voiceChatEnabled = voiceChatEnabledTestValue2 |
| | 134 | | }, |
| | 135 | | profiles = new Profiles() |
| | 136 | | { |
| | 137 | | nameValidCharacterRegex = regexTestValue2, |
| | 138 | | nameValidRegex = regexTestValue2 |
| | 139 | | }, |
| | 140 | | features = new Features() |
| | 141 | | { |
| | 142 | | enableBuilderInWorld = builderInWorldEnabledTestValue2 |
| | 143 | | }, |
| | 144 | | }); |
| 1 | 145 | | Assert.IsTrue(onChangePass); |
| | 146 | |
|
| 1 | 147 | | KernelConfig.i.OnChange -= onConfigChange; |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | [Test] |
| | 151 | | public void ParseJsonCorrectly() |
| | 152 | | { |
| 1 | 153 | | KernelConfigModel model = new KernelConfigModel(); |
| | 154 | |
|
| 1 | 155 | | var worldRange = new WorldRange(-150, -150, 150, 150); |
| 1 | 156 | | model.validWorldRanges.Add(worldRange); |
| | 157 | |
|
| 1 | 158 | | string json = JsonUtility.ToJson(model); |
| 1 | 159 | | KernelConfig.i.Set(json); |
| | 160 | |
|
| 1 | 161 | | Assert.IsTrue(model.Equals(KernelConfig.i.Get())); |
| 1 | 162 | | } |
| | 163 | | } |