using System.Threading.Tasks;
using NSubstitute;
using NUnit.Framework;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
using ShareeBike.Model.Connector;
using ShareeBike.Model.State;
using ShareeBike.Services.BluetoothLock;
using ShareeBike.View;
using ShareeBike.ViewModel;
using ShareeBike.ViewModel.Bikes;
using ShareeBike.ViewModel.Bikes.Bike;
using ShareeBike.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler;
using AuthCommand = ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.AuthCommand;
using ConnectCommand = ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.ConnectAndGetStateCommand;
namespace SharedBusinessLogic.Tests.ViewModel.Bikes.Bike.BluetoothLock
{
[TestFixture]
public class TestConnectLockActionViewModel
{
///
/// Use case: Connect lock.
/// Final state: Occupied open
///
[Test]
public async Task TestConnectLock()
{
var bike = Substitute.For();
var connector = Substitute.For();
var command = Substitute.For();
var locks = Substitute.For();
var pollingManager = Substitute.For();
var viewService = Substitute.For();
var bikesViewModel = Substitute.For();
var listener = Substitute.For();
var connectLockActionViewModel = new ConnectLockActionViewModel(
bike,
() => pollingManager,
viewService,
bikesViewModel);
bike.Id.Returns("0");
bike.ConnectAsync(Arg.Any()).Returns(x =>
{
// Add calls to ReportStep which IBikeInfoMutable implementation would do.
connectLockActionViewModel.ReportStep(AuthCommand.Step.Authenticate);
connectLockActionViewModel.ReportStep(ConnectCommand.Step.ConnectLock);
connectLockActionViewModel.ReportStep(ConnectCommand.Step.GetLockingState);
return Task.CompletedTask;
});
bike.State.Value.Returns(InUseStateEnum.Booked); // Request handler factory queries state to create appropriate request handler object.
bike.LockInfo.State.Returns(LockingState.Open); // Request handler factory queries lock state to create appropriate request handler object.
await connectLockActionViewModel.ConnectLockAsync();
// Verify behavior
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
//Auth
bikesViewModel.ActionText = "Calculate authentication keys...";
//Connect lock & Get State
bikesViewModel.ActionText = "Connecting lock...";
bikesViewModel.ActionText = "One moment please...";
pollingManager.StartAsync(); // polling must be restarted again
bikesViewModel.ActionText = string.Empty;
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
return;
});
}
}
}