Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit 3a1fb1c

Browse files
author
Jamie Brynes
authored
Move test to editmode (#1362)
1 parent f69d120 commit 3a1fb1c

File tree

5 files changed

+68
-74
lines changed

5 files changed

+68
-74
lines changed

test-project/Assets/EditmodeTests/Improbable.Gdk.EditModeTests.asmdef

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"UnityEngine.TestRunner",
1212
"UnityEditor.TestRunner",
1313
"Improbable.Gdk.TestBases",
14-
"Improbable.Gdk.TestUtils"
14+
"Improbable.Gdk.TestUtils",
15+
"Improbable.Gdk.GameObjectCreation"
1516
],
1617
"includePlatforms": [
1718
"Editor"
@@ -27,5 +28,6 @@
2728
"defineConstraints": [
2829
"UNITY_INCLUDE_TESTS"
2930
],
30-
"versionDefines": []
31-
}
31+
"versionDefines": [],
32+
"noEngineReferences": false
33+
}

test-project/Assets/PlaymodeTests/Subscriptions/SubscriptionAggregateCouplingTests.cs renamed to test-project/Assets/EditmodeTests/Subscriptions/SubscriptionAggregateCouplingTests.cs

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,36 @@
55
using Improbable.Gdk.Core;
66
using Improbable.Gdk.GameObjectCreation;
77
using Improbable.Gdk.Subscriptions;
8+
using Improbable.Gdk.TestUtils;
89
using NUnit.Framework;
910
using UnityEngine;
10-
using Object = UnityEngine.Object;
1111

12-
namespace Improbable.Gdk.PlaymodeTests.Subscriptions
12+
namespace Improbable.Gdk.EditmodeTests.Subscriptions
1313
{
14-
public class SubscriptionAggregateCouplingTests
14+
public class SubscriptionAggregateCouplingTests : MockBase
1515
{
16-
private MockConnectionHandler connectionHandler;
17-
private WorkerInWorld workerInWorld;
18-
private EntityGameObjectLinker linker;
19-
20-
private SpatialOSReceiveSystem receiveSystem;
21-
private RequireLifecycleSystem requireLifecycleSystem;
22-
23-
private const string WorkerType = "TestWorkerType";
24-
25-
[SetUp]
26-
public void Setup()
16+
protected override MockWorld.Options GetOptions()
2717
{
28-
var logDispatcher = new LoggingDispatcher();
29-
30-
var connectionBuilder = new MockConnectionHandlerBuilder();
31-
connectionHandler = connectionBuilder.ConnectionHandler;
32-
workerInWorld = WorkerInWorld
33-
.CreateWorkerInWorldAsync(connectionBuilder, WorkerType, logDispatcher, Vector3.zero)
34-
.Result;
35-
receiveSystem = workerInWorld.World.GetExistingSystem<SpatialOSReceiveSystem>();
36-
requireLifecycleSystem = workerInWorld.World.GetExistingSystem<RequireLifecycleSystem>();
37-
38-
var goInitSystem = workerInWorld.World
39-
.CreateSystem<GameObjectInitializationSystem>(
40-
new GameObjectCreatorFromMetadata(WorkerType, Vector3.zero, logDispatcher), null);
41-
linker = goInitSystem.Linker;
42-
}
18+
var opts = base.GetOptions();
4319

44-
[TearDown]
45-
public void TearDown()
46-
{
47-
workerInWorld.Dispose();
20+
// Required for the LinkedGameObjectMap subscription to be fulfilled.
21+
opts.AdditionalSystems = (world) =>
22+
{
23+
world.AddSystem(
24+
new GameObjectInitializationSystem(
25+
new GameObjectCreatorFromMetadata(opts.WorkerType, Vector3.zero, null), null));
26+
};
27+
28+
return opts;
4829
}
4930

5031
[Test]
5132
public void All_non_generated_subscription_managers_have_a_test()
5233
{
5334
// Find all non-generated subscription manager implementations.
54-
var subscriptionManagersTypes = ReflectionUtility.GetNonAbstractTypesWithBlacklist(typeof(SubscriptionManagerBase), new[] { "Improbable.Gdk.Generated" });
35+
var subscriptionManagersTypes =
36+
ReflectionUtility.GetNonAbstractTypesWithBlacklist(typeof(SubscriptionManagerBase),
37+
new[] { "Improbable.Gdk.Generated" });
5538

5639
// Get the subscription payload type.
5740
var subscriptionManagerContainedTypes = subscriptionManagersTypes
@@ -157,45 +140,49 @@ public void DifferentAggregateSubscriptions_should_not_couple_together(Type requ
157140
// were cached incorrectly would cause the subscriptions to become coupled.
158141
private void DifferentAggregateSubscriptions_should_not_couple_together_impl<T>() where T : MonoBehaviour
159142
{
160-
var gameObject1 = CreateAndLinkGameObject<T>(1);
161-
var gameObject2 = CreateAndLinkGameObject<T>(2);
162-
163-
RemoveAndUnlinkGameObject(1, gameObject1);
164-
RemoveAndUnlinkGameObject(2, gameObject2);
165-
166-
var gameObject3 = CreateAndLinkGameObject<T>(3);
167-
var gameObject4 = CreateAndLinkGameObject<T>(4);
168-
169-
RemoveAndUnlinkGameObject(3, gameObject3);
170-
171-
Assert.IsTrue(gameObject4.GetComponent<T>().enabled, "gameObject4.GetComponent<T>().enabled");
143+
World
144+
.Step(world =>
145+
{
146+
world.Connection.CreateEntity(1, GetEntityTemplate());
147+
world.Connection.CreateEntity(2, GetEntityTemplate());
148+
})
149+
.Step(world =>
150+
{
151+
world.CreateGameObject<T>(1);
152+
world.CreateGameObject<T>(2);
153+
})
154+
.Step(world =>
155+
{
156+
world.Connection.RemoveComponent(1, Position.ComponentId);
157+
world.Connection.RemoveEntity(1);
158+
world.Connection.RemoveComponent(2, Position.ComponentId);
159+
world.Connection.RemoveEntity(2);
160+
})
161+
.Step(world =>
162+
{
163+
world.Connection.CreateEntity(3, GetEntityTemplate());
164+
world.Connection.CreateEntity(4, GetEntityTemplate());
165+
})
166+
.Step(world =>
167+
{
168+
world.CreateGameObject<T>(3);
169+
var (_, behaviour) = world.CreateGameObject<T>(4);
170+
return behaviour;
171+
}).Step(world =>
172+
{
173+
world.Connection.RemoveComponent(3, Position.ComponentId);
174+
world.Connection.RemoveEntity(3);
175+
}).Step((world, behaviour) =>
176+
{
177+
Assert.IsTrue(behaviour.enabled);
178+
});
172179
}
173180

174-
private GameObject CreateAndLinkGameObject<T>(long entityId) where T : MonoBehaviour
181+
private EntityTemplate GetEntityTemplate()
175182
{
176183
var template = new EntityTemplate();
177184
template.AddComponent(new Position.Snapshot(), "worker");
178-
connectionHandler.CreateEntity(entityId, template);
179-
receiveSystem.Update();
180-
181-
var gameObject = new GameObject("TestGameObject");
182-
gameObject.AddComponent<T>();
183-
gameObject.GetComponent<T>().enabled = false;
184-
185-
linker.LinkGameObjectToSpatialOSEntity(new EntityId(entityId), gameObject);
186-
requireLifecycleSystem.Update();
187-
188-
return gameObject;
189-
}
190-
191-
private void RemoveAndUnlinkGameObject(long entityId, GameObject gameObject)
192-
{
193-
connectionHandler.RemoveComponent(entityId, Position.ComponentId);
194-
connectionHandler.RemoveEntity(entityId);
195-
receiveSystem.Update();
196-
requireLifecycleSystem.Update();
197-
198-
Object.DestroyImmediate(gameObject);
185+
return template;
199186
}
200187
}
201188
}

test-project/Assets/PlaymodeTests/Subscriptions.meta

Lines changed: 0 additions & 3 deletions
This file was deleted.

workers/unity/Packages/io.improbable.gdk.testutils/MockWorld.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public MockWorldWithContext<U> Step<U>(Func<MockWorld, T, U> frame)
115115
return new MockWorldWithContext<U>(world, newContext);
116116
}
117117

118+
public MockWorldWithContext<U> Step<U>(Func<MockWorld, U> frame)
119+
{
120+
var newContext = frame(world);
121+
world.Update();
122+
123+
return new MockWorldWithContext<U>(world, newContext);
124+
}
125+
118126
public MockWorldWithContext<T> Step(Action<MockWorld> frame)
119127
{
120128
frame(world);

0 commit comments

Comments
 (0)