88 lines
4.7 KiB
C#
88 lines
4.7 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using NUnit.Framework;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace Ironspore.Tests
|
|
{
|
|
public sealed class EquipmentHudTests
|
|
{
|
|
[Test]
|
|
public void EmptyWeaponSlotsUpdateAmmoAndGridCapacityWithoutLosingTheProtectedWeapon()
|
|
{
|
|
string folder = Path.Combine(Path.GetTempPath(), "IronsporeHudTests", Guid.NewGuid().ToString("N"));
|
|
var host = new GameObject("EquipmentHudTest");
|
|
host.SetActive(false);
|
|
InputActionAsset input = null;
|
|
ChassisDefinition frame = null;
|
|
SettingsService settings = null;
|
|
var catalog = AssetDatabase.LoadAssetAtPath<LocalizationCatalog>("Assets/Game/Localization/LocalizationCatalog.asset");
|
|
GameLocalization.Initialize(catalog);
|
|
string locale = GameLocalization.CurrentCode;
|
|
try
|
|
{
|
|
GameLocalization.SelectLocale("en", false);
|
|
var store = new ProfileStore(Path.Combine(folder, "profile-v2.json"));
|
|
Assert.IsTrue(store.Begin(0, new[] { 0, 0, 0, 0 }, 100, 1));
|
|
var game = host.AddComponent<GameSession>();
|
|
var pilot = host.AddComponent<MechPilot>();
|
|
var primary = AssetDatabase.LoadAssetAtPath<EquipmentDefinition>("Assets/Game/Data/Equipment/rapid.asset");
|
|
var secondary = AssetDatabase.LoadAssetAtPath<EquipmentDefinition>("Assets/Game/Data/Equipment/guided.asset");
|
|
Assert.IsNotNull(primary);
|
|
Assert.IsNotNull(secondary);
|
|
frame = ScriptableObject.CreateInstance<ChassisDefinition>();
|
|
game.chassis = new[] { frame };
|
|
game.equipment = new[] { primary, secondary };
|
|
input = InputActionAsset.FromJson(File.ReadAllText("Assets/Game/Input/GameInput.inputactions"));
|
|
settings = new SettingsService(new SettingsStore(Path.Combine(folder, "settings.json")), input, new GameSettingsData());
|
|
typeof(GameSession).GetProperty("Store").SetValue(game, store);
|
|
typeof(GameSession).GetProperty("Pilot").SetValue(game, pilot);
|
|
typeof(GameSession).GetProperty("Settings").SetValue(game, settings);
|
|
var fields = BindingFlags.Instance | BindingFlags.NonPublic;
|
|
typeof(MechPilot).GetField("game", fields).SetValue(pilot, game);
|
|
typeof(MechPilot).GetField("visual", fields).SetValue(pilot, host.transform);
|
|
pilot.ApplyLoadout();
|
|
Assert.AreEqual(primary.magazineSize, pilot.Rounds);
|
|
string primaryUid = store.Data.run.installed.Find(i => i.itemId == "rapid").uid;
|
|
string secondaryUid = store.Data.run.installed.Find(i => i.itemId == "guided").uid;
|
|
|
|
Assert.IsTrue(store.Uninstall(0, Hold.Cargo, 0, 0, false));
|
|
Assert.IsTrue(store.Uninstall(1, Hold.SafeCase, 0, 0, true));
|
|
pilot.ApplyLoadout();
|
|
Assert.IsNull(pilot.Primary);
|
|
Assert.AreEqual(0, pilot.Rounds);
|
|
Assert.AreEqual(0, pilot.ReloadRemaining);
|
|
var field = new VisualElement();
|
|
var vitals = new VisualElement();
|
|
var instruments = new CombatInstruments(field, vitals, game);
|
|
Assert.DoesNotThrow(instruments.Refresh);
|
|
Assert.AreEqual(GameLocalization.Text("ui.combat.empty"), field.Q<Label>(className: "combat-primary").text);
|
|
Assert.AreEqual(GameLocalization.Text("ui.combat.empty"), field.Q<Label>(className: "combat-secondary").text);
|
|
Assert.AreEqual(GameLocalization.Text("ui.combat.cargo", 3, 2), vitals.Q<Label>(className: "combat-cargo").text);
|
|
|
|
Assert.IsTrue(store.InstallItem(primaryUid, Hold.Cargo, parts => true));
|
|
pilot.ApplyLoadout();
|
|
instruments.Refresh();
|
|
Assert.AreSame(primary, pilot.Primary);
|
|
Assert.AreEqual(primary.magazineSize.ToString(), field.Q<Label>(className: "combat-rounds").text);
|
|
Assert.IsTrue(store.Finish(true));
|
|
Assert.AreEqual(secondaryUid, store.Data.warehouse[0].uid);
|
|
Assert.AreEqual(1, store.Data.warehouse.Count);
|
|
}
|
|
finally
|
|
{
|
|
GameLocalization.SelectLocale(locale, false);
|
|
settings?.Dispose();
|
|
UnityEngine.Object.DestroyImmediate(host);
|
|
if (input) UnityEngine.Object.DestroyImmediate(input);
|
|
if (frame) UnityEngine.Object.DestroyImmediate(frame);
|
|
if (Directory.Exists(folder)) Directory.Delete(folder, true);
|
|
}
|
|
}
|
|
}
|
|
}
|