sharee.bike-App/SharedBusinessLogic.Tests/ViewModel/Bikes/Bike/CopriLock/RequestHandler/TestFeedbackPending.cs

205 lines
6.8 KiB
C#
Raw Normal View History

2023-02-22 14:03:35 +01:00
using System;
2022-08-30 15:42:25 +02:00
using NSubstitute;
using NUnit.Framework;
2024-04-09 12:53:23 +02:00
using ShareeBike;
using ShareeBike.Model.Bikes.BikeInfoNS.CopriLock;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS.BatteryNS;
using ShareeBike.Model.Connector;
using ShareeBike.Model.Device;
using ShareeBike.Model.User;
using ShareeBike.View;
using ShareeBike.ViewModel;
using ShareeBike.ViewModel.Bikes;
using ShareeBike.ViewModel.Bikes.Bike.CopriLock.RequestHandler;
namespace SharedBusinessLogic.Tests.ViewModel.Bikes.Bike.CopriLock.RequestHandler
2022-08-30 15:42:25 +02:00
{
2022-09-06 16:08:19 +02:00
[TestFixture]
public class TestFeedbackPending
{
/// <summary>
/// Use case: Give feedback but submission fails.
/// Final state: Feedback is pending.
/// </summary>
[Test]
public void TestGiveFeedbackDoSubmitFeedbackFailsException()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
var handler = new FeedbackPending(
bike,
() => true, // isConnectedDelegate
2024-04-09 12:53:23 +02:00
(isConnected) => connector,
2022-09-06 16:08:19 +02:00
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
connector.Command.DoSubmitFeedback(
2024-04-09 12:53:23 +02:00
Arg.Any<ShareeBike.Model.Connector.IUserFeedback>(),
2022-09-06 16:08:19 +02:00
Arg.Any<Uri>()).Returns(x => throw new Exception("Context info", new Exception("Tst")));
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2023-09-22 11:38:42 +02:00
viewService.DisplayUserFeedbackPopup(Arg.Any<IBatteryMutable>());
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Submitting feedback...";
2024-04-09 12:53:23 +02:00
connector.Command.DoSubmitFeedback(Arg.Any<ShareeBike.Model.Connector.IUserFeedback>(), Arg.Any<Uri>()); // Booking must be performed
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2022-09-06 16:08:19 +02:00
viewService.DisplayAlert(
"Submitting feedback failed!",
2023-08-31 12:20:06 +02:00
"A stable Internet connection is required. Connect to WIFI or to mobile network and activate mobile data. Try again.",
2022-09-06 16:08:19 +02:00
"OK");
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // polling must be restarted again
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2022-09-06 16:08:19 +02:00
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("FeedbackPending"));
Assert.That(
subsequent.IsButtonVisible,
Is.False);
Assert.That(
subsequent.LockitButtonText,
2023-04-05 15:02:10 +02:00
Is.EqualTo("Give feedback"));
2022-09-06 16:08:19 +02:00
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Give feedback.
/// Final state: Feedback is pending.
/// </summary>
[Test]
public void TestGiveFeedback()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
var handler = new FeedbackPending(
bike,
() => true, // isConnectedDelegate
2024-04-09 12:53:23 +02:00
(isConnected) => connector,
2022-09-06 16:08:19 +02:00
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2023-09-22 11:38:42 +02:00
viewService.DisplayUserFeedbackPopup(Arg.Any<IBatteryMutable>());
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Submitting feedback...";
2024-04-09 12:53:23 +02:00
connector.Command.DoSubmitFeedback(Arg.Any<ShareeBike.Model.Connector.IUserFeedback>(), Arg.Any<Uri>()); // Booking must be performed
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // polling must be restarted again
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2022-09-06 16:08:19 +02:00
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("FeedbackPending"));
Assert.That(
subsequent.IsButtonVisible,
Is.False);
Assert.That(
subsequent.LockitButtonText,
2023-04-05 15:02:10 +02:00
Is.EqualTo("Give feedback"));
2022-09-06 16:08:19 +02:00
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
/// <summary>
/// Use case: Give feedback.
/// Final state: Feedback is pending.
/// </summary>
[Test]
public void TestGiveFeedbackAndQuerry()
{
var bike = Substitute.For<IBikeInfoMutable>();
var connector = Substitute.For<IConnector>();
var command = Substitute.For<ICommand>();
var pollingManager = Substitute.For<IPollingUpdateTaskManager>();
var viewService = Substitute.For<IViewService>();
var bikesViewModel = Substitute.For<IBikesViewModel>();
var activeUser = Substitute.For<IUser>();
var handler = new FeedbackPending(
bike,
() => true, // isConnectedDelegate
2024-04-09 12:53:23 +02:00
(isConnected) => connector,
2022-09-06 16:08:19 +02:00
() => pollingManager,
Substitute.For<ISmartDevice>(),
viewService,
bikesViewModel,
activeUser);
bike.BookingFinishedModel.MiniSurvey.Questions.Count.Returns(1);
var subsequent = handler.HandleRequestOption2().Result;
2023-04-19 12:14:14 +02:00
// Verify behavior
2022-09-06 16:08:19 +02:00
Received.InOrder(() =>
{
bikesViewModel.Received(1).IsIdle = false; // GUI must be locked
bikesViewModel.ActionText = "One moment please...";
2023-08-31 12:20:06 +02:00
pollingManager.StopAsync(); // Polling must be stopped before any COPR and lock service action
2023-09-22 11:38:42 +02:00
viewService.DisplayUserFeedbackPopup(Arg.Any<IBatteryMutable>());
2022-09-06 16:08:19 +02:00
bikesViewModel.ActionText = "Submitting feedback...";
2024-04-09 12:53:23 +02:00
connector.Command.DoSubmitFeedback(Arg.Any<ShareeBike.Model.Connector.IUserFeedback>(), Arg.Any<Uri>()); // Booking must be performed
2022-09-06 16:08:19 +02:00
viewService.PushModalAsync(ViewTypes.MiniSurvey);
bikesViewModel.ActionText = "Updating...";
2023-08-31 12:20:06 +02:00
pollingManager.StartAsync(); // polling must be restarted again
2023-02-22 14:03:35 +01:00
bikesViewModel.ActionText = string.Empty;
2022-09-06 16:08:19 +02:00
bikesViewModel.Received(1).IsIdle = true; // GUI must be unlocked
});
// Verify state after action
Assert.That(
subsequent.ButtonText,
Is.EqualTo("FeedbackPending"));
Assert.That(
subsequent.IsButtonVisible,
Is.False);
Assert.That(
subsequent.LockitButtonText,
2023-04-05 15:02:10 +02:00
Is.EqualTo("Give feedback"));
2022-09-06 16:08:19 +02:00
Assert.That(
subsequent.IsLockitButtonVisible,
Is.True);
}
}
2022-08-30 15:42:25 +02:00
}