using System; using System.Collections.Generic; using System.IO; using NUnit.Framework; using UnityEditor.AddressableAssets.Build; using UnityEditor.AddressableAssets.Build.DataBuilders; using UnityEditor.AddressableAssets.Settings; using UnityEngine; using UnityEngine.AddressableAssets; using UnityEngine.AddressableAssets.Initialization; namespace UnityEditor.AddressableAssets.Tests { public class BuildScriptPackedPlayTests : AddressableAssetTestBase { protected override void OnInit() { //Player data must be built before PackPlaymode can be used. var input = new AddressablesDataBuilderInput(Settings); ScriptableObject.CreateInstance().BuildData(input); } [Test] public void PackedPlayModeScript_CannotBuildPlayerContent() { var buildScript = ScriptableObject.CreateInstance(); Assert.IsFalse(buildScript.CanBuildData()); Assert.IsTrue(buildScript.CanBuildData()); Assert.IsTrue(buildScript.CanBuildData()); } [Test] public void PackedPlayModeScript_AppendsBuildLog_ForNonStandaloneBuildTarget() { //Setup BuildScriptPackedPlayMode buildScript = ScriptableObject.CreateInstance(); AddressablesDataBuilderInput input = new AddressablesDataBuilderInput(Settings); input.SetAllValues(Settings, BuildTargetGroup.Android, BuildTarget.Android, ""); ScriptableObject.CreateInstance().BuildData(input); //Test buildScript.BuildData(input); var buildLogPath = Addressables.BuildPath + "/buildLogs.json"; var logs = JsonUtility.FromJson(File.ReadAllText(buildLogPath)); //Cleanup (done early in case of test failure) File.Delete(buildLogPath); //Assert Assert.AreEqual(1, logs.RuntimeBuildLogs.Count); Assert.AreEqual(LogType.Warning, logs.RuntimeBuildLogs[0].Type); Assert.AreEqual($"Asset bundles built with build target {input.Target} may not be compatible with running in the Editor.", logs.RuntimeBuildLogs[0].Message); } [Test] public void PackedPlayModeScript_AppendsBuildLog_ForInvalidBuildTarget() { //Setup BuildScriptPackedPlayMode buildScript = ScriptableObject.CreateInstance(); AddressablesDataBuilderInput input = new AddressablesDataBuilderInput(Settings); var settingsPath = Addressables.BuildPath + "/settings.json"; var rtd = JsonUtility.FromJson(File.ReadAllText(settingsPath)); var buildLogPath = Addressables.BuildPath + "/buildLogs.json"; string storedBuildTarget = rtd.BuildTarget; string invalidTarget = rtd.BuildTarget = "NotAValidBuildTarget"; File.WriteAllText(settingsPath, JsonUtility.ToJson(rtd)); //Test buildScript.BuildData(input); var logs = JsonUtility.FromJson(File.ReadAllText(buildLogPath)); //Cleanup (done early in case of test failure) File.Delete(buildLogPath); rtd.BuildTarget = storedBuildTarget; File.WriteAllText(settingsPath, JsonUtility.ToJson(rtd)); //Assert Assert.AreEqual(1, logs.RuntimeBuildLogs.Count); Assert.AreEqual(LogType.Warning, logs.RuntimeBuildLogs[0].Type); Assert.AreEqual($"Unable to parse build target from initialization data: '{invalidTarget}'.", logs.RuntimeBuildLogs[0].Message); } } }