public void GivenCancel_WhenAdd_ThenDialogShown_AndPrinterNotAdded()
        {
            // Arrange
            var model = new PrinterConfigurationModel();
            var sut   = new PrintersConfigurationViewModel(
                _mockServiceProvider.Object,
                _mockPrinterConfigurationManagerService.Object);

            _mockPrinterConfigurationView.Setup(x => x.ShowDialog())
            .Returns(false);

            _mockServiceProvider.Setup(x => x.GetService(typeof(IPrinterConfigurationView)))
            .Returns(_mockPrinterConfigurationView.Object);

            _mockPrinterConfigurationView.SetupGet <PrinterConfigurationViewModel>(x => x.ViewModel)
            .Returns(new PrinterConfigurationViewModel()
            {
                Model = model
            });

            // Act
            sut.Add.Execute().Subscribe();

            // Assert
            _mockPrinterConfigurationManagerService.Verify(x => x.Add(
                                                               It.Is <PrinterConfigurationModel>(x => x == model)), Times.Never);
        }
Exemple #2
0
        public void GivenConnected_AndString_WhenFakeReceiveData_ThenCallbackInvoked()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            var expectedDataReceived = "Hello World!";
            var actualDataReceived   = string.Empty;
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
                actualDataReceived = data;
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port = (TestableSerialPort)sut.GetSerialPort(portRef);

            // Act
            port.FakeReceiveData(expectedDataReceived);

            // Assert
            Assert.Equal(expectedDataReceived, actualDataReceived);
        }
        public void GivenConnectedPrinterControllerService_AndString_AndEncoding_WhenWrite_ThenByteArrayWrittenToPrinter_AndPrinterCommandReturned()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var mockPrinterPacketParser   = new Mock <IPrinterPacketParser>();
            var testableSerialPortAdapter = new TestableSerialPortAdapter();
            var sut = new MarlinPrinterControllerService(
                testableSerialPortAdapter,
                mockPrinterPacketParser.Object);
            var expectedDataString = "Hello World!";
            var expectedDataBytes  = Encoding.ASCII.GetBytes(expectedDataString);
            var expectedOffset     = 0;
            var expectedCount      = expectedDataString.Length;

            sut.Connect(printer);

            // Act
            var result = sut.Write(
                expectedDataString,
                Encoding.ASCII);

            // Assert
            Assert.Equal(expectedDataBytes, result.Data);
            Assert.Equal(expectedOffset, result.Offset);
            Assert.Equal(expectedCount, result.Count);
            Assert.False(result.Acknowledged);
            Assert.Single(testableSerialPortAdapter.WrittenBinaryData);
            Assert.Equal(expectedDataBytes, testableSerialPortAdapter.WrittenBinaryData[0].Data);
            Assert.Equal(expectedOffset, testableSerialPortAdapter.WrittenBinaryData[0].Offset);
            Assert.Equal(expectedCount, testableSerialPortAdapter.WrittenBinaryData[0].Count);
        }
        public void GivenConnectedPrinterControllerService_WhenDisconnect_ThenSerialPortAdapterDisonnectCalled_AndPrinterUnSet_AndSerialPortAdapterRefUnSet()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var mockSerialPortAdapter   = new Mock <ISerialPortAdapter>();
            var mockPrinterPacketParser = new Mock <IPrinterPacketParser>();

            mockSerialPortAdapter.Setup(x => x.Connect(
                                            It.IsAny <PrinterConfigurationModel>(),
                                            It.IsAny <Action <SerialPortAdapterRef, string> >()))
            .Returns(new SerialPortAdapterRef(printer, null));

            var sut = new MarlinPrinterControllerService(
                mockSerialPortAdapter.Object,
                mockPrinterPacketParser.Object);

            sut.Connect(printer);

            // Act
            sut.Disconnect();

            // Assert
            mockSerialPortAdapter.Verify(x => x.Disconnect(
                                             It.IsAny <SerialPortAdapterRef>()), Times.Once);
            Assert.Null(sut.Printer);
            Assert.Null(sut.SerialPortAdapterRef);
        }
        public void GivenConnectedPrinterControllerService_AndPrevCommandWaitingAck_AndCommandStackCleared_AndString_AndEncoding_WhenWrite_ThenCommandSent()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var mockPrinterPacketParser   = new Mock <IPrinterPacketParser>();
            var testableSerialPortAdapter = new TestableSerialPortAdapter();
            var sut = new MarlinPrinterControllerService(
                testableSerialPortAdapter,
                mockPrinterPacketParser.Object);
            var expectedDataString = "Hello World!";

            sut.Connect(printer);
            sut.Write(
                expectedDataString,
                Encoding.ASCII);
            sut.ClearCommandStack();

            // Act
            var result = sut.Write(
                "This will get sent as the command stack has been cleared!",
                Encoding.ASCII);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(2, testableSerialPortAdapter.WrittenBinaryData.Count);
        }
        public void GivenPrinterConfigurationModel_AndConfigurationCorrect_WhenConnect_ThenSerialPortAdapterConnectCalled_AndPrinterSet_AndSerialPortAdapterRefSet()
        {
            // Arrange
            var printer = new PrinterConfigurationModel()
            {
                Name     = "Some wizzy printer",
                Port     = "com9",
                BaudRate = 1001
            };
            var mockSerialPortAdapter   = new Mock <ISerialPortAdapter>();
            var mockPrinterPacketParser = new Mock <IPrinterPacketParser>();

            mockSerialPortAdapter.Setup(x => x.Connect(
                                            It.IsAny <PrinterConfigurationModel>(),
                                            It.IsAny <Action <SerialPortAdapterRef, string> >()))
            .Returns(new SerialPortAdapterRef(printer, null));

            var sut = new MarlinPrinterControllerService(
                mockSerialPortAdapter.Object,
                mockPrinterPacketParser.Object);

            // Act
            sut.Connect(printer);

            // Assert
            mockSerialPortAdapter.Verify(x => x.Connect(
                                             It.Is <PrinterConfigurationModel>(x => x == printer),
                                             It.IsAny <Action <SerialPortAdapterRef, string> >()), Times.Once);
            Assert.Equal(printer, sut.Printer);
            Assert.Equal(printer, sut.SerialPortAdapterRef.Config);
        }
Exemple #7
0
 public SerialPortAdapterRef(
     PrinterConfigurationModel config,
     Action <SerialPortAdapterRef, string> dataReceivedCalllback)
 {
     Config = config;
     DataReceivedCallback = dataReceivedCalllback;
 }
        public PrinterConfigurationViewModel()
        {
            Model             = new PrinterConfigurationModel();
            ValidationContext = new ValidationContext();

            this.ValidationRule(
                viewModel => viewModel.Model.Name,
                value => !string.IsNullOrWhiteSpace(value),
                "You must specify a valid printer name.");

            this.ValidationRule(
                viewModel => viewModel.Model.Port,
                value => !string.IsNullOrWhiteSpace(value),
                "You must specify a valid serial port.");

            this.ValidationRule(
                viewModel => viewModel.Model.BaudRate,
                value => value > 0,
                "You must specify a valid serial port.");

            this.ValidationRule(
                viewModel => viewModel.Model.BedXSize,
                value => value > 0,
                "You must specify a valid X bed size.");

            this.ValidationRule(
                viewModel => viewModel.Model.BedYSize,
                value => value > 0,
                "You must specify a valid Y bed size.");

            this.ValidationRule(
                viewModel => viewModel.Model.BedZSize,
                value => value > 0,
                "You must specify a valid Z bed size.");

            this.ValidationRule(
                viewModel => viewModel.Model.PrintableAreaMarginBack,
                value => value >= 0,
                "You must specify a valid printable area back margin.");

            this.ValidationRule(
                viewModel => viewModel.Model.PrintableAreaMarginFront,
                value => value >= 0,
                "You must specify a valid printable area front margin.");

            this.ValidationRule(
                viewModel => viewModel.Model.PrintableAreaMarginLeft,
                value => value >= 0,
                "You must specify a valid printable area left margin.");

            this.ValidationRule(
                viewModel => viewModel.Model.PrintableAreaMarginRight,
                value => value >= 0,
                "You must specify a valid printable area right margin.");

            Save   = ReactiveCommand.Create(OnSave, this.IsValid());
            Cancel = ReactiveCommand.Create(OnCancel);
        }
Exemple #9
0
 public void Connect(PrinterConfigurationModel printer)
 {
     if (SerialPortAdapterRef == null)
     {
         SerialPortAdapterRef = _serialPortAdapter.Connect(
             printer,
             new Action <SerialPortAdapterRef, string>(DataReceivedCallback));
         Printer = printer;
     }
 }
 public SerialPortAdapterRef Connect(
     PrinterConfigurationModel config,
     Action <SerialPortAdapterRef, string> dataReceivedCallback)
 {
     WrittenBinaryData    = new List <SerialPortAdapterWriteBinaryData>();
     Config               = config;
     Callback             = dataReceivedCallback;
     SerialPortAdapterRef = new SerialPortAdapterRef(
         config,
         dataReceivedCallback);
     return(SerialPortAdapterRef);
 }
        public void GivenConnectedPrinterControllerService_AndCommandAcked_AndString_AndEncoding_WhenWrite_ThenCommandSent()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var mockPrinterPacketParser   = new Mock <IPrinterPacketParser>();
            var testableSerialPortAdapter = new TestableSerialPortAdapter();
            var sut = new MarlinPrinterControllerService(
                testableSerialPortAdapter,
                mockPrinterPacketParser.Object);
            var expectedDataString = "Hello World!";
            var expectedDataBytes  = Encoding.ASCII.GetBytes(expectedDataString);
            var expectedCount      = expectedDataString.Length;

            sut.Connect(printer);
            sut.Write(
                expectedDataString,
                Encoding.ASCII);

            mockPrinterPacketParser.Setup(x => x.ReceiveData(
                                              It.Is <string>(x => x == "ok\n")))
            .Callback(() =>
            {
                mockPrinterPacketParser.Raise(
                    x => x.PacketComplete += null,
                    new PrinterPacketParserPacketCompleteEventArgs()
                {
                    Packet = new PrinterPacket()
                    {
                        IsAck = true
                    }
                });
            });

            sut.SerialPortAdapterRef.DataReceivedCallback(
                sut.SerialPortAdapterRef,
                "ok\n");

            // Act
            var result = sut.Write(
                "This will get sent as the previous command was acknowledged!",
                Encoding.ASCII);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(2, testableSerialPortAdapter.WrittenBinaryData.Count);
        }
Exemple #12
0
        public SerialPortAdapterRef Connect(
            PrinterConfigurationModel config,
            Action <SerialPortAdapterRef, string> dataReceivedCallback)
        {
            var portRef = new SerialPortAdapterRef(
                config,
                dataReceivedCallback);

            var serialPort = (SerialPortType)Activator.CreateInstance(typeof(SerialPortType),
                                                                      config.Port,
                                                                      config.BaudRate);

            serialPort.DataReceived += SerialPort_DataReceived;
            serialPort.Open();

            _portsByRef.TryAdd(portRef, serialPort);
            _refsByPort.TryAdd(serialPort, portRef);
            return(portRef);
        }
Exemple #13
0
        public void GivenConnectedPortRef_AndBytes_AndOffset_AndCount_WhenWrite_ThenBytesWrittenToPort_AndTrueReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port           = (TestableSerialPort)sut.GetSerialPort(portRef);
            var expectedData   = Encoding.ASCII.GetBytes("Hello World");
            int expectedOffset = 0;
            int expectedCount  = expectedData.Length;
            var actualData     = default(byte[]);
            var actualOffset   = 0;
            var actualCount    = 0;

            port.WriteCallback = delegate(byte[] data, int offset, int count)
            {
                actualData   = data;
                actualOffset = offset;
                actualCount  = count;
            };

            // Act
            var result = sut.Write(
                portRef,
                expectedData,
                expectedOffset,
                expectedCount);

            // Assert
            Assert.True(result);
            Assert.Equal(expectedData, actualData);
            Assert.Equal(expectedOffset, actualOffset);
            Assert.Equal(expectedCount, actualCount);
        }
Exemple #14
0
        public void GivenUnknownPortRef_WhenDisconnect_ThenFalseReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut = new SerialPortAdapter <TestableSerialPort>();

            // Act
            var result = sut.Disconnect(new SerialPortAdapterRef(null, null));

            // Assert
            Assert.False(result);
        }
        public void GivenInstance_AndSelectedPrinter_WhenRemove_ThenPrinterRemoved_AndPrinterDeselected()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var sut     = new PrintersConfigurationViewModel(
                null,
                _mockPrinterConfigurationManagerService.Object)
            {
                SelectedPrinter = printer
            };

            // Act
            sut.Remove.Execute().Subscribe();

            // Assert
            _mockPrinterConfigurationManagerService.Verify(x => x.Remove(
                                                               It.Is <PrinterConfigurationModel>(x => x == printer)), Times.Once);
            Assert.Null(sut.SelectedPrinter);
        }
        public void GivenConnectedPrinterControllerService_WhenReceivedData_ThenEventRaisedWithReceivedData()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var testableSerialPortAdapter = new TestableSerialPortAdapter();
            var mockPrinterPacketParser   = new Mock <IPrinterPacketParser>();
            var sut = new MarlinPrinterControllerService(
                testableSerialPortAdapter,
                mockPrinterPacketParser.Object);
            var receivedData = string.Empty;
            var expecetdData = "Hello World!";

            sut.Connect(printer);

            mockPrinterPacketParser.Setup(x => x.ReceiveData(
                                              It.IsAny <string>())).Callback((string data) =>
            {
                var eventArgs = new PrinterPacketParserPacketCompleteEventArgs()
                {
                    Packet = new PrinterPacket()
                    {
                        RawData = data
                    }
                };
                mockPrinterPacketParser.Raise(
                    x => x.PacketComplete += null,
                    eventArgs);
            });

            // Act
            sut.ReceivedData += new EventHandler <PrinterControllerReceivedDataEventArgs>(delegate(object s, PrinterControllerReceivedDataEventArgs ev)
            {
                receivedData = ev.Packet.RawData;
            });
            testableSerialPortAdapter.FakeReceiveData(
                testableSerialPortAdapter.SerialPortAdapterRef,
                expecetdData);

            // Assert
            Assert.Equal(expecetdData, receivedData);
        }
        public void GivenPrinterConfigurationModel_WhenAdd_ThenPrinterAddedToConfig()
        {
            // Arrange
            var mockFileIOService = new Mock <IFileIOService>();
            var config            = new PrinterConfigurationModel()
            {
                Name     = Guid.NewGuid().ToString(),
                Port     = "com6",
                BaudRate = 100,
                BedXSize = 101,
                BedYSize = 102,
                BedZSize = 103,
                PrintableAreaMarginBack  = 104,
                PrintableAreaMarginFront = 105,
                PrintableAreaMarginLeft  = 106,
                PrintableAreaMarginRight = 107
            };

            mockFileIOService.Setup(x => x.Exists(
                                        It.IsAny <string>()))
            .Returns(true);

            mockFileIOService.Setup(x => x.ReadAllText(
                                        It.IsAny <string>()))
            .Returns("{}");

            var sut = new PrinterConfigurationManagerService(
                new WindowsStoragePathService(),
                mockFileIOService.Object);

            sut.Load();

            // Act
            sut.Add(config);

            // Assert
            Assert.Single(sut.Config.Printers);
            Assert.Equal(config, sut.Config.Printers.First());
        }
Exemple #18
0
        public void GivenConfig_AndCallback_WhenConnect_ThenPortCreated_AndPortOpened_AndRefReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut = new SerialPortAdapter <TestableSerialPort>();

            // Act
            var result = sut.Connect(
                config,
                callback);

            // Assert
            Assert.Equal(config, result.Config);
            Assert.Equal(callback, result.DataReceivedCallback);
            Assert.True(sut.GetSerialPort(result).IsOpen);
        }
Exemple #19
0
        public void GivenConnected_AndPortRef_WhenGetSerialPort_ThenCorrectSerialPortReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);

            // Act
            var port = sut.GetSerialPort(portRef);

            // Assert
            Assert.Equal(config.Port, port.PortName);
            Assert.Equal(config.BaudRate, port.BaudRate);
        }
Exemple #20
0
        public void GivenConnected_WhenDispose_ThenAllPortsDisposed()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port = (TestableSerialPort)sut.GetSerialPort(portRef);

            // Act
            sut.Dispose();

            // Assert
            Assert.Empty(sut.PortRefs);
            Assert.True(port.Disposed);
        }
Exemple #21
0
        public void GivenConnectedPortRef_WhenDisconnect_ThenPortClosed_AndTrueReturned()
        {
            // Arrange
            var config = new PrinterConfigurationModel()
            {
                Port     = "com9",
                BaudRate = 1001
            };
            Action <SerialPortAdapterRef, string> callback = delegate(SerialPortAdapterRef portRef, string data)
            {
            };
            var sut     = new SerialPortAdapter <TestableSerialPort>();
            var portRef = sut.Connect(
                config,
                callback);
            var port = sut.GetSerialPort(portRef);

            // Act
            var result = sut.Disconnect(portRef);

            // Assert
            Assert.True(result);
            Assert.False(port.IsOpen);
        }
        public void GivenConnectedPrinterControllerService_AndPrevCommandWaitingAck_AndString_AndEncoding_WhenWrite_ThenNullReturned()
        {
            // Arrange
            var printer = new PrinterConfigurationModel();
            var mockPrinterPacketParser   = new Mock <IPrinterPacketParser>();
            var testableSerialPortAdapter = new TestableSerialPortAdapter();
            var sut = new MarlinPrinterControllerService(
                testableSerialPortAdapter,
                mockPrinterPacketParser.Object);
            var expectedDataString = "Hello World!";

            sut.Connect(printer);
            sut.Write(
                expectedDataString,
                Encoding.ASCII);

            // Act
            var result = sut.Write(
                "This won't get sent!",
                Encoding.ASCII);

            // Assert
            Assert.Null(result);
        }
 public void Remove(PrinterConfigurationModel printerConfigurationModel)
 {
     Config.Printers.Remove(printerConfigurationModel);
 }
 public void Add(PrinterConfigurationModel printerConfigurationModel)
 {
     Config.Printers.Add(printerConfigurationModel);
 }