Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests about prefab active/inactive state #122

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Component InstantiatePrefab(IObjectResolver resolver)
{
var parent = destination.GetParent();
var wasActive = destination.Prefab.gameObject.activeSelf;
if (destination.Prefab.gameObject.activeSelf)
if (wasActive)
{
destination.Prefab.gameObject.SetActive(false);
}
Expand All @@ -124,9 +124,13 @@ Component InstantiatePrefab(IObjectResolver resolver)
? UnityEngine.Object.Instantiate(destination.Prefab, parent)
: UnityEngine.Object.Instantiate(destination.Prefab);

destination.Prefab.gameObject.SetActive(wasActive);
injector.Inject(component, resolver, Parameters);
component.gameObject.SetActive(true);

if (wasActive)
{
destination.Prefab.gameObject.SetActive(true);
component.gameObject.SetActive(true);
}
return component;
}

Expand Down
7 changes: 3 additions & 4 deletions VContainer/Assets/VContainer/Runtime/Unity/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,19 @@ public LifetimeScope CreateChild(Action<IContainerBuilder> installation)
public LifetimeScope CreateChildFromPrefab(LifetimeScope prefab, IInstaller installer = null)
{
var wasActive = prefab.gameObject.activeSelf;
if (prefab.gameObject.activeSelf && prefab.autoRun)
if (wasActive)
{
prefab.gameObject.SetActive(false);
}

var child = Instantiate(prefab, transform, false);
prefab.gameObject.SetActive(wasActive);
if (installer != null)
{
child.extraInstallers.Add(installer);
}
child.parentReference.Object = this;
if (!child.gameObject.activeSelf)
if (wasActive)
{
prefab.gameObject.SetActive(true);
child.gameObject.SetActive(true);
}
return child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ public class ServiceA
public sealed class SampleMonoBehaviour : MonoBehaviour, IComponent
{
public ServiceA ServiceA;
public ServiceA ServiceAInAwake;
public bool StartCalled;
public int UpdateCalls;

void Start() => StartCalled = true;
void Update() => UpdateCalls += 1;

void Awake()
{
ServiceAInAwake = ServiceA;
}

[Inject]
public void Construct(ServiceA serviceA)
{
Expand Down
46 changes: 46 additions & 0 deletions VContainer/Assets/VContainer/Tests/Unity/LifetimeScopeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,52 @@ public IEnumerator CreateChild()
Assert.That(parentLifetimeScope.transform.childCount, Is.Zero);
}

[Test]
public void CreateChildFromPrefab()
{
var parent = LifetimeScope.Create(builder =>
{
builder.RegisterEntryPoint<SampleEntryPoint>(Lifetime.Singleton).AsSelf();
});

var childPrefab = new GameObject("Child").AddComponent<SampleChildLifetimeScope>();

var child = parent.CreateChildFromPrefab(childPrefab);

var parentResolved = parent.Container.Resolve<SampleEntryPoint>();
var childResolved = child.Container.Resolve<SampleEntryPoint>();
Assert.That(parentResolved, Is.InstanceOf<SampleEntryPoint>());
Assert.That(childResolved, Is.InstanceOf<SampleEntryPoint>());
Assert.That(childResolved, Is.EqualTo(parentResolved));
Assert.That(child.parentReference.Object, Is.EqualTo(parent));
Assert.That(childPrefab.gameObject.activeSelf, Is.True);
}

[Test]
public void CreateChildFromPrefabWithInActive()
{
var parent = LifetimeScope.Create(builder =>
{
builder.RegisterEntryPoint<SampleEntryPoint>(Lifetime.Singleton).AsSelf();
});

var childPrefab = new GameObject("Child").AddComponent<SampleChildLifetimeScope>();
childPrefab.gameObject.SetActive(false);

var child = parent.CreateChildFromPrefab(childPrefab);
Assert.That(childPrefab.gameObject.activeSelf, Is.False);
Assert.That(child.gameObject.activeSelf, Is.False);

child.Build();

var parentResolved = parent.Container.Resolve<SampleEntryPoint>();
var childResolved = child.Container.Resolve<SampleEntryPoint>();
Assert.That(parentResolved, Is.InstanceOf<SampleEntryPoint>());
Assert.That(childResolved, Is.InstanceOf<SampleEntryPoint>());
Assert.That(childResolved, Is.EqualTo(parentResolved));
Assert.That(child.parentReference.Object, Is.EqualTo(parent));
}

[Test]
public void CreateScopeWithSingleton()
{
Expand Down
116 changes: 77 additions & 39 deletions VContainer/Assets/VContainer/Tests/Unity/UnityContainerBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,50 +181,88 @@ public void RegisterComponentOnNewGameObjectUnderTransform()
public void RegisterComponentInNewPrefab()
{
var prefab = new GameObject("Sample").AddComponent<SampleMonoBehaviour>();
{
var go1 = new GameObject("Parent");
var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Transient)
.UnderTransform(go1.transform);

var container = builder.Build();
var resolved1 = container.Resolve<SampleMonoBehaviour>();
var resolved2 = container.Resolve<SampleMonoBehaviour>();
Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved1, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved2, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved1.transform.parent, Is.EqualTo(go1.transform));
Assert.That(resolved2.transform.parent, Is.EqualTo(go1.transform));
Assert.That(resolved1, Is.Not.EqualTo(resolved2));
}
var go1 = new GameObject("Parent");
var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Transient)
.UnderTransform(go1.transform);

{
var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped);
var container = builder.Build();
var resolved1 = container.Resolve<SampleMonoBehaviour>();
var resolved2 = container.Resolve<SampleMonoBehaviour>();
Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved1, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved2, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved1.transform.parent, Is.EqualTo(go1.transform));
Assert.That(resolved2.transform.parent, Is.EqualTo(go1.transform));
Assert.That(resolved1, Is.Not.EqualTo(resolved2));
Assert.That(prefab.gameObject.activeSelf, Is.True);
}

var container = builder.Build();
var resolved1 = container.Resolve<SampleMonoBehaviour>();
var resolved2 = container.Resolve<SampleMonoBehaviour>();
Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved1, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved2, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved1.transform.parent, Is.Null);
Assert.That(resolved2.transform.parent, Is.Null);
Assert.That(resolved1, Is.EqualTo(resolved2));
}
[Test]
public void RegisterComponentInNewPrefabScoped()
{
var prefab = new GameObject("Sample").AddComponent<SampleMonoBehaviour>();

{
var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped);
var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Scoped);

var container = builder.Build();
Assert.That(container.Resolve<SampleMonoBehaviour>(), Is.InstanceOf<SampleMonoBehaviour>());
}
var container = builder.Build();
var resolved1 = container.Resolve<SampleMonoBehaviour>();
var resolved2 = container.Resolve<SampleMonoBehaviour>();
Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved2.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved1, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved2, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved1.transform.parent, Is.Null);
Assert.That(resolved2.transform.parent, Is.Null);
Assert.That(resolved1, Is.EqualTo(resolved2));
Assert.That(prefab.gameObject.activeSelf, Is.True);
}

[Test]
public void RegisterComponentInNewPrefabWithAwake()
{
var prefab = new GameObject("Sample").AddComponent<SampleMonoBehaviour>();

var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Singleton);

var container = builder.Build();
var resolved1 = container.Resolve<SampleMonoBehaviour>();
Assert.That(resolved1.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved1, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved1.transform.parent, Is.Null);
Assert.That(prefab.gameObject.activeSelf, Is.True);
Assert.That(resolved1.gameObject.activeSelf, Is.True);
Assert.That(resolved1.ServiceA, Is.InstanceOf<ServiceA>());
Assert.That(resolved1.ServiceAInAwake, Is.InstanceOf<ServiceA>());
}

[Test]
public void RegisterComponentInNewPrefabWithInActive()
{
var prefab = new GameObject("Sample").AddComponent<SampleMonoBehaviour>();
prefab.gameObject.SetActive(false);

var builder = new ContainerBuilder();
builder.Register<ServiceA>(Lifetime.Transient);
builder.RegisterComponentInNewPrefab(prefab, Lifetime.Singleton);

var container = builder.Build();
var resolved = container.Resolve<SampleMonoBehaviour>();
Assert.That(resolved.gameObject.name, Is.EqualTo("Sample(Clone)"));
Assert.That(resolved, Is.InstanceOf<SampleMonoBehaviour>());
Assert.That(resolved.transform.parent, Is.Null);

Assert.That(prefab.gameObject.activeSelf, Is.False);
Assert.That(resolved.gameObject.activeSelf, Is.False);
Assert.That(resolved.ServiceA, Is.InstanceOf<ServiceA>());
Assert.That(resolved.ServiceAInAwake, Is.Null);
}

[Test]
Expand Down