ThreeKingdomsSlingshot/Assets/Tests/PlayMode/BootFlowTests.cs

237 lines
11 KiB
C#

using System.Collections;
using NUnit.Framework;
using TKS.OutGame;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace TKS.Tests
{
/// <summary>
/// 게임 시작 흐름(PD 2026-11-13 「스플래쉬 → 리소스 다운로드가 필요하면 다운로드 화면 → 다 받으면 페이드 → 타이틀」).
/// ① 받을 것 없음 = 그 자리에서 바로 로비(흐름 객체·로딩 화면 없음 = 기존 시작과 같음) ② 받을 것 있음 = 로딩 화면 Download 세트 · 진행률이 오름
/// → 다 받은 뒤 페이드 아웃(로딩 화면 위 캔버스) → 로딩 화면 닫고 타이틀 훅 → 페이드 인 → 페이드 객체 정리 ③ 타이틀 훅이 없으면 페이드 사이에 로비
/// ④ 표 download 줄 공급자 = 표 값대로(C45).
/// </summary>
public class BootFlowTests
{
Camera cam;
// 타이틀 화면이 시작 때 등록한 훅(TitleScreen · RuntimeInitializeOnLoadMethod) — 이 테스트가 바꿨다가 끝나면 되돌림(뒤 테스트 영향 없음)
System.Func<Camera, System.Action, bool> savedTitle;
[SetUp]
public void SetUp()
{
LoadingTable.Reload();
PlayerPrefs.DeleteKey(BootFlow.PreviewKey);
savedTitle = BootFlow.TitleProvider;
BootFlow.OverrideDownloader = null;
BootFlow.TitleProvider = null;
cam = new GameObject("TestCam").AddComponent<Camera>();
cam.orthographic = true;
}
[TearDown]
public void TearDown()
{
BootFlow.OverrideDownloader = null;
BootFlow.TitleProvider = savedTitle;
LoadingScreen.HideCurrent();
if (BootFlow.Current != null) Object.Destroy(BootFlow.Current.gameObject);
foreach (var f in Object.FindObjectsByType<ScreenFader>(FindObjectsSortMode.None)) Object.Destroy(f.gameObject);
if (cam != null) Object.Destroy(cam.gameObject);
}
static IEnumerator Until(System.Func<bool> cond, float timeout, string what)
{
float t0 = Time.realtimeSinceStartup;
while (!cond())
{
if (Time.realtimeSinceStartup - t0 > timeout) Assert.Fail("시간 초과: " + what);
yield return null;
}
}
[Test]
public void NoDownload_GoesStraightToLobby()
{
BootFlow.OverrideDownloader = new NoDownload();
int lobby = 0;
var bf = BootFlow.Begin(cam, () => lobby++);
Assert.IsNull(bf, "받을 것 없음 = 흐름 객체 없음");
Assert.AreEqual(1, lobby, "그 자리에서 로비(기존 시작과 같음)");
Assert.IsNull(LoadingScreen.Current, "다운로드 화면 안 뜸");
}
[UnityTest]
public IEnumerator Download_ThenFade_ThenTitle()
{
BootFlow.OverrideDownloader = new SimulatedDownload(4f, 8f, 0.1f);
int title = 0, lobby = 0;
BootFlow.TitleProvider = (c, toLobby) =>
{
title++;
return true;
};
var bf = BootFlow.Begin(cam, () => lobby++);
Assert.IsNotNull(bf);
yield return Until(() => bf.State == BootFlow.Step.Download, 3f, "받기 단계");
var ls = LoadingScreen.Current;
Assert.IsNotNull(ls, "받기 = 로딩 화면");
Assert.AreEqual(LoadingMode.Download, ls.Mode);
Assert.AreEqual("download", ls.Set);
Assert.AreEqual(bf.Downloader.TotalBytes, ls.TotalBytes, "전체 용량 글자");
Assert.AreEqual(0, title + lobby, "받는 중 = 타이틀·로비 없음");
float p0 = ls.Shown;
yield return new WaitForSecondsRealtime(0.25f);
Assert.Greater(ls.Shown, p0, "진행률이 오름");
yield return Until(() => bf.State == BootFlow.Step.FadeOut, 5f, "페이드 아웃");
Assert.IsNotNull(bf.Fader);
Assert.IsNotNull(LoadingScreen.Current, "페이드 아웃 동안 로딩 화면 유지");
Assert.GreaterOrEqual(ls.Shown, 0.999f, "100 % 를 보인 뒤 페이드");
var fadeRow = LoadingTable.Load(BootFlow.TablePath).Find(r => r.kind == "option" && r.id == "fade");
var canvasRow = LoadingTable.First("option", "canvas");
Assert.AreEqual(fadeRow.size, bf.Fader.Canvas.sortingOrder, "페이드 정렬 순서 = 표");
Assert.Greater(bf.Fader.Canvas.sortingOrder, (int)canvasRow.a, "페이드 = 로딩 화면 위");
Assert.AreEqual(0, title + lobby);
yield return Until(() => bf == null || bf.State >= BootFlow.Step.Switch, 3f, "화면 바꿈");
Assert.IsNull(LoadingScreen.Current, "검정 뒤에서 로딩 화면 닫음");
Assert.AreEqual(1, title, "타이틀 훅 1번");
Assert.AreEqual(0, lobby, "타이틀이 떴으면 로비는 시작 눌림 때");
yield return Until(() => bf == null, 3f, "흐름 끝");
yield return null;
Assert.AreEqual(0, Object.FindObjectsByType<ScreenFader>(FindObjectsSortMode.None).Length, "페이드 객체 정리");
Assert.IsNull(BootFlow.Current);
}
[UnityTest]
public IEnumerator Download_NoTitle_LobbyBehindFade()
{
BootFlow.OverrideDownloader = new SimulatedDownload(2f, 8f, 0f);
int lobby = 0;
float alphaAtLobby = -1f;
BootFlow bf = null;
bf = BootFlow.Begin(cam, () =>
{
lobby++;
if (bf != null && bf.Fader != null) alphaAtLobby = bf.Fader.Alpha;
});
Assert.IsNotNull(bf);
yield return Until(() => bf == null, 8f, "흐름 끝");
Assert.AreEqual(1, lobby, "타이틀 훅 없음 = 로비 1번");
Assert.AreEqual(1f, alphaAtLobby, 0.001f, "로비는 검정으로 덮인 뒤 바뀜");
}
[Test]
public void Table_Provider_FollowsTable()
{
var r = LoadingTable.Load(BootFlow.TablePath).Find(x => x.kind == "option" && x.id == "download");
Assert.IsNotNull(r, "표 download 줄");
var dl = BootFlow.FromTable();
string k = r.text.ToLowerInvariant();
if (k == "simulate") Assert.IsInstanceOf<SimulatedDownload>(dl);
else if (k == "cdn") Assert.IsInstanceOf<CdnDownloader>(dl);
else Assert.IsInstanceOf<NoDownload>(dl);
}
// ---------- CDN 확인·받기(PD 2026-11-16 「CDN 파일이 존재할 경우 CDN 리소스 체크 후 타이틀」) ----------
static string TempDir(string name)
{
string d = System.IO.Path.Combine(Application.temporaryCachePath, "tks_cdn_test", name + "_" + System.Guid.NewGuid().ToString("N").Substring(0, 8));
System.IO.Directory.CreateDirectory(d);
return d;
}
static string FileUrl(string dir)
{
return new System.Uri(dir).AbsoluteUri;
}
[Test]
public void Cdn_NoAddress_NothingToCheck()
{
var dl = new CdnDownloader("", "cdn_manifest.json", 5f, TempDir("local"));
dl.Begin();
Assert.IsTrue(dl.Checked && !dl.Needed && dl.Finished, "CDN 주소 없음 = 받을 것 없음 → 바로 타이틀");
Assert.IsFalse(dl.Configured);
}
[Test]
public void Cdn_NoManifest_SkipsToTitle()
{
var dl = new CdnDownloader(FileUrl(TempDir("empty_cdn")), "cdn_manifest.json", 5f, TempDir("local"));
dl.Begin();
Assert.IsTrue(dl.Checked && !dl.Needed, "CDN 에 목록 파일 없음 = CDN 파일 없음 → 받지 않고 타이틀");
Assert.IsFalse(dl.ManifestFound);
}
[UnityTest]
public IEnumerator Cdn_FilesExist_CheckThenDownloadThenSkip()
{
string src = TempDir("cdn"), local = TempDir("local");
System.IO.Directory.CreateDirectory(System.IO.Path.Combine(src, "sub"));
System.IO.File.WriteAllBytes(System.IO.Path.Combine(src, "a.bin"), new byte[1000]);
System.IO.File.WriteAllBytes(System.IO.Path.Combine(src, "sub", "b.bin"), new byte[2048]);
System.IO.File.WriteAllText(System.IO.Path.Combine(src, "cdn_manifest.json"),
"{\"version\":\"t1\",\"files\":[{\"path\":\"a.bin\",\"size\":1000},{\"path\":\"sub/b.bin\",\"size\":2048}]}");
var dl = new CdnDownloader(FileUrl(src), "cdn_manifest.json", 5f, local);
dl.Begin();
yield return Until(() =>
{
dl.Tick(0.016f);
return dl.Checked;
}, 5f, "CDN 확인");
Assert.IsTrue(dl.ManifestFound, "목록 파일 읽음");
Assert.IsTrue(dl.Needed, "로컬에 없음 = 받을 것 있음");
Assert.AreEqual(3048L, dl.TotalBytes, "받을 용량 = 목록 크기 합");
yield return Until(() =>
{
dl.Tick(0.016f);
return dl.Finished;
}, 5f, "CDN 받기");
Assert.IsNull(dl.Error);
Assert.AreEqual(1000L, new System.IO.FileInfo(System.IO.Path.Combine(local, "a.bin")).Length);
Assert.AreEqual(2048L, new System.IO.FileInfo(System.IO.Path.Combine(local, "sub", "b.bin")).Length);
var again = new CdnDownloader(FileUrl(src), "cdn_manifest.json", 5f, local);
again.Begin();
yield return Until(() =>
{
again.Tick(0.016f);
return again.Checked;
}, 5f, "다시 확인");
Assert.IsFalse(again.Needed, "다 받아 둔 뒤 = 받을 것 없음(크기 같으면 건너뜀)");
}
// ---------- 씬 분리(PD 2026-11-16 「타이틀 화면은 별도의 씬 · 스플래쉬로 회사 CI 먼저」) ----------
[UnityTest]
public IEnumerator Scenes_Splash_Title_Battle()
{
WorldProgress.Reset();
SaveStore.UseMemory(true);
BootFlow.TitleProvider = savedTitle;
SceneManager.LoadScene(BootScenes.Splash);
yield return Until(() => SplashBoot.Current != null, 3f, "Splash 씬");
var ciRow = BootScenes.SplashRow;
Assert.IsNotNull(ciRow, "표 splash 줄");
Assert.IsNotNull(SplashBoot.Current.Ci.sprite, "회사 CI 그림 = 표 s_sprite");
yield return Until(() => SplashBoot.Current == null || SplashBoot.Current.CiPeak >= 0.999f, 5f, "회사 CI 노출");
yield return Until(() => SceneManager.GetActiveScene().name == BootScenes.Title && TitleScreen.Current != null, 10f, "Title 씬 = 타이틀");
Assert.IsNull(OutGameRoot.Instance, "Title 씬 = 타이틀만(로비 없음)");
yield return Until(() => SceneSwitch.Current == null, 5f, "페이드 인 끝");
var ts = TitleScreen.Current;
ts.SkipIntro();
yield return Until(() => ts == null || ts.PressReady, 3f, "Press Any Key");
ts.Press();
yield return Until(() => SceneManager.GetActiveScene().name == BootScenes.Game && SceneSwitch.Current == null, 10f, "누름 → Battle 씬");
yield return null;
Assert.IsNotNull(OutGameRoot.Instance, "Battle 씬 = 본영 로비");
Assert.IsNull(TitleScreen.Current, "타이틀 닫힘");
}
}
}