Skip to content

Commit

Permalink
add fixed game time
Browse files Browse the repository at this point in the history
It is a must to provide a fixed game time in MockWindowSystem.RunOneLoop() in order to make tests more deterministic.
  • Loading branch information
MarcosCobena committed Jul 18, 2023
1 parent 30881e8 commit f7cdc6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
26 changes: 12 additions & 14 deletions src/Evergine.Mocks/MockWindowsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Evergine.Framework;
using Evergine.Framework.Graphics;
using Evergine.Framework.Services;
using System.Diagnostics;

namespace Evergine.Mocks
{
Expand All @@ -12,6 +11,8 @@ public class MockWindowsSystem : WindowsSystem

private MockWindow? mockWindow;

private static TimeSpan? nextRenderCallbackElapsedTime;

private MockWindowsSystem()
{
}
Expand All @@ -37,14 +38,9 @@ public override Window CreateWindow(string title, uint width, uint height, bool
return this.mockWindow;
}

protected override void CreateLoopThread(Action loadAction, Action renderCallback)
{
loadAction();
this.renderCallback = renderCallback;
}

public void RunOneLoop()
public void RunOneLoop(TimeSpan elapsedTime)
{
nextRenderCallbackElapsedTime = elapsedTime;
this.renderCallback?.Invoke();
}

Expand All @@ -71,7 +67,6 @@ public static MockWindowsSystem Create(Application application, Scene scene)
var firstDisplay = new Display(window, swapChain);
graphicsPresenter.AddDisplay("DefaultDisplay", firstDisplay);
application.Container.RegisterInstance(graphicsContext);
var clockTimer = Stopwatch.StartNew();
instance.Run(
() =>
{
Expand All @@ -82,14 +77,17 @@ public static MockWindowsSystem Create(Application application, Scene scene)
},
() =>
{
var gameTime = clockTimer.Elapsed;
clockTimer.Restart();
application.UpdateFrame(gameTime);
application.DrawFrame(gameTime);
application.UpdateFrame(nextRenderCallbackElapsedTime!.Value);
application.DrawFrame(nextRenderCallbackElapsedTime.Value);
});

return instance;
}

protected override void CreateLoopThread(Action loadAction, Action renderCallback)
{
loadAction();
this.renderCallback = renderCallback;
}
}
}
25 changes: 14 additions & 11 deletions src/Sample/Sample.Tests/MyBehaviorShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Evergine.Framework;
using Evergine.Framework.Graphics;
using Evergine.Mocks;
using System;
using Xunit;

namespace Sample.Tests
Expand All @@ -13,6 +14,8 @@ public class MyBehaviorShould

private readonly MockWindowsSystem windowsSystem;

private readonly TimeSpan fixedElapsedTime = TimeSpan.FromSeconds(1d / 60);

public MyBehaviorShould()
{
this.component = new MyBehavior();
Expand All @@ -32,7 +35,7 @@ public void KeepMyBooleanPropertyFalseOnStart()
// Arrange

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);

// Assert
Assert.False(this.component.MyBooleanProperty);
Expand All @@ -44,8 +47,8 @@ public void ChangeMyBooleanPropertyToTrueAfterTwoLoops()
// Arrange

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);

// Assert
Assert.True(this.component.MyBooleanProperty);
Expand All @@ -57,8 +60,8 @@ public void KeepEntityAtOriginWithoutPressingUpKey()
// Arrange

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);

// Assert
var transform3D = this.component.Owner.FindComponent<Transform3D>();
Expand All @@ -71,9 +74,9 @@ public void MoveEntityAfterPressingUpKey()
// Arrange

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);
this.windowsSystem.KeyboardDispatcher?.Press(Keys.Up);
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);

// Assert
var transform3D = this.component.Owner.FindComponent<Transform3D>();
Expand All @@ -86,8 +89,8 @@ public void KeepEntityAtOriginWithoutMovingMouseWithLeftButtonPressed()
// Arrange

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);

// Assert
var transform3D = this.component.Owner.FindComponent<Transform3D>();
Expand All @@ -101,11 +104,11 @@ public void MoveEntityAfterMovingMouseWithLeftButtonPressed()
var mouseDispatcher = this.windowsSystem.MouseDispatcher!;

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);
mouseDispatcher.Enter(0, 0);
mouseDispatcher.Press(MouseButtons.Left);
mouseDispatcher.Move(100, 0);
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(this.fixedElapsedTime);

// Assert
var transform3D = this.component.Owner.FindComponent<Transform3D>();
Expand Down
3 changes: 2 additions & 1 deletion src/Sample/Sample.Tests/MyComponentShould.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Evergine.Framework;
using Evergine.Framework.Graphics;
using Evergine.Mocks;
using System;
using Xunit;

namespace Sample.Tests
Expand Down Expand Up @@ -41,7 +42,7 @@ public void ChangeMyBooleanPropertyToTrueOnStart()
// Arrange

// Act
this.windowsSystem.RunOneLoop();
this.windowsSystem.RunOneLoop(TimeSpan.FromSeconds(1d / 60));

// Assert
Assert.True(this.component.MyBooleanProperty);
Expand Down

0 comments on commit f7cdc6f

Please sign in to comment.