PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/HartAnalyzer/HartAnalyzer.UnitTest/_Shell/_RibbonViewModel/ConnectionCommand.cs

#
C# | 89 lines | 73 code | 16 blank | 0 comment | 0 complexity | de51289958d10032c197a5b5902c27c7 MD5 | raw file
  1. using Communication.Hart;
  2. using FluentAssertions;
  3. using HartAnalyzer.Services;
  4. using HartAnalyzer.Shell;
  5. using NUnit.Framework;
  6. namespace HartAnalyzer.UnitTest._Shell._RibbonViewModel
  7. {
  8. [TestFixture]
  9. public class ConnectionCommand
  10. {
  11. [Test]
  12. public async void ShouldNotShowMessageIfOpenedResultIsReceived()
  13. {
  14. var messageBoxService = new TestMessageBoxService();
  15. var hartCommunicationService = new TestHartCommunicationService();
  16. hartCommunicationService.OpenAsyncResponders.Enqueue(() => OpenResult.Opened);
  17. var viewModel = new RibbonViewModel(new ApplicationServices{ HartCommunicationService = hartCommunicationService}, new CommonServices{ MessageBoxService = messageBoxService });
  18. await viewModel.ConnectionCommand.Execute(null);
  19. messageBoxService.ShowInformationRequests.Count.Should().Be(0);
  20. }
  21. [TestCase(OpenResult.ComPortIsOpenAlreadyOpen)]
  22. [TestCase(OpenResult.ComPortNotExisting)]
  23. [TestCase(OpenResult.UnknownComPortError)]
  24. public async void ShouldShowMessageIfNotOpenedIsReceived(OpenResult openResult)
  25. {
  26. var messageBoxService = new TestMessageBoxService();
  27. var hartCommunicationService = new TestHartCommunicationService();
  28. hartCommunicationService.OpenAsyncResponders.Enqueue(() => openResult);
  29. var viewModel = new RibbonViewModel(new ApplicationServices { HartCommunicationService = hartCommunicationService }, new CommonServices { MessageBoxService = messageBoxService });
  30. await viewModel.ConnectionCommand.Execute(null);
  31. messageBoxService.ShowInformationRequests.Count.Should().Be(1);
  32. messageBoxService.ShowInformationRequests[0].Message.Should().NotBeNullOrEmpty();
  33. }
  34. [Test]
  35. public async void ShouldCallOpenIfStateIsClosed()
  36. {
  37. var messageBoxService = new TestMessageBoxService();
  38. var hartCommunicationService = new TestHartCommunicationService { PortState = Services.PortState.Closed };
  39. hartCommunicationService.OpenAsyncResponders.Enqueue(() => OpenResult.Opened);
  40. var viewModel = new RibbonViewModel(new ApplicationServices { HartCommunicationService = hartCommunicationService }, new CommonServices { MessageBoxService = messageBoxService });
  41. await viewModel.ConnectionCommand.Execute(null);
  42. hartCommunicationService.OpenAsyncResponders.Count.Should().Be(0);
  43. }
  44. [Test]
  45. public async void ShouldCallCloseIfStateIsOpened()
  46. {
  47. var messageBoxService = new TestMessageBoxService();
  48. var hartCommunicationService = new TestHartCommunicationService { PortState = Services.PortState.Opened };
  49. hartCommunicationService.CloseAsyncResponders.Enqueue(() => CloseResult.Closed);
  50. var viewModel = new RibbonViewModel(new ApplicationServices { HartCommunicationService = hartCommunicationService }, new CommonServices { MessageBoxService = messageBoxService });
  51. await viewModel.ConnectionCommand.Execute(null);
  52. hartCommunicationService.OpenAsyncResponders.Count.Should().Be(0);
  53. }
  54. [TestCase(Services.PortState.Opened)]
  55. [TestCase(Services.PortState.Closed)]
  56. public void ShouldCanExecuteIfStateIsNotChanging(Services.PortState portState)
  57. {
  58. var messageBoxService = new TestMessageBoxService();
  59. var hartCommunicationService = new TestHartCommunicationService { PortState = portState };
  60. var viewModel = new RibbonViewModel(new ApplicationServices { HartCommunicationService = hartCommunicationService }, new CommonServices { MessageBoxService = messageBoxService });
  61. viewModel.ConnectionCommand.CanExecute(null).Should().BeTrue();
  62. }
  63. [TestCase(Services.PortState.Opening)]
  64. [TestCase(Services.PortState.Closing)]
  65. public void ShouldCannotExecuteIfStateIsChanging(Services.PortState portState)
  66. {
  67. var messageBoxService = new TestMessageBoxService();
  68. var hartCommunicationService = new TestHartCommunicationService { PortState = portState };
  69. var viewModel = new RibbonViewModel(new ApplicationServices { HartCommunicationService = hartCommunicationService }, new CommonServices { MessageBoxService = messageBoxService });
  70. viewModel.ConnectionCommand.CanExecute(null).Should().BeFalse();
  71. }
  72. }
  73. }