Exemplo n.º 1
0
        public void Should_Not_Have_Any_Handlers_By_Default()
        {
            var mockSerialProvider = new Mock <ISerialProvider>();

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
                Assert.IsEmpty(moqFirmataEmptyBase.Handlers);
        }
Exemplo n.º 2
0
        public void Should_Send_Messages_Using_The_SerialProvider()
        {
            var mockSerialProvider            = new Mock <ISerialProvider>();
            Func <IEnumerable <byte>, bool> f = b =>
            {
                var c = b.ToArray();
                return(c[0] == MessageConstants.ANALOG_MESSAGE && c[1] == 0x01 && c[2] == 0x02);
            };

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                // Add the creator for the message
                moqFirmataEmptyBase.Creators.Add(typeof(AnalogMessage), new AnalogOutputMessageCreator());
                // Send a new message
                moqFirmataEmptyBase.SendMessage(new AnalogMessage()
                {
                    Pin = 0x01, Value = 0x02
                });
            }

            // Verify that the base class called the serialprovider's send method
            mockSerialProvider.Verify(x => x.Send(
                                          It.Is <IEnumerable <byte> >(b =>
                                                                      b.First() ==
                                                                      (MessageConstants.ANALOG_MESSAGE | 0x01) &&
                                                                      b.Skip(1).First() == 0x02 &&
                                                                      b.Skip(2).First() == 0x00)));
        }
Exemplo n.º 3
0
        public void Should_Send_Bytes_To_Multiple_Handlers_Successfully()
        {
            var mockSerialProvider = new Mock <ISerialProvider>();

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                moqFirmataEmptyBase.Handlers.Add(new AnalogMappingMessageHandler((moqFirmataEmptyBase.Broker)));
                moqFirmataEmptyBase.Handlers.Add(new SysexFirmwareMessageHandler((moqFirmataEmptyBase.Broker)));
                mockSerialProvider.Raise(m => m.DataReceived += null,
                                         new DataReceivedEventArgs(new [] { MessageConstants.SYSEX_START }));

                // Wait for the parser thread to catch up
                Thread.Sleep(100);

                // See if the handler was successfully added to the appropriate ones
                Assert.AreEqual(2, moqFirmataEmptyBase.CurrentHandlers.Count);

                mockSerialProvider.Raise(m => m.DataReceived += null,
                                         new DataReceivedEventArgs(new [] { SysexCommands.ANALOG_MAPPING_RESPONSE }));

                // Wait for the parser thread to catch up
                Thread.Sleep(50);

                Assert.AreSame(moqFirmataEmptyBase.Handlers[0], moqFirmataEmptyBase.CurrentHandlers[0]);
                Assert.AreEqual(1, moqFirmataEmptyBase.CurrentHandlers.Count);
            }
        }
Exemplo n.º 4
0
        public void Should_Call_Dispose_On_The_Serial_Provider()
        {
            var mockSerialProvider = new Mock <ISerialProvider>();

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
            }
            mockSerialProvider.Verify(x => x.Dispose(), Times.Once());
        }
Exemplo n.º 5
0
        public void Should_Throw_Error_If_There_Is_Not_Suitable_MessageCreator()
        {
            var mockSerialProvider = new Mock <ISerialProvider>();

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                Assert.Throws <FirmataException>(() => moqFirmataEmptyBase.SendMessage(new AnalogMessage()));
            }
        }
Exemplo n.º 6
0
        public void Should_Call_Dispose_On_The_Serial_Provider()
        {
            var mockSerialProvider = new Mock<ISerialProvider>();
            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {

            }
            mockSerialProvider.Verify(x => x.Dispose(), Times.Once());
        }
Exemplo n.º 7
0
        public void No_Appropriate_Handler_After_Successful_Message()
        {
            var mockSerialProvider = new Mock<ISerialProvider>();
            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                moqFirmataEmptyBase.Handlers.Add(new AnalogMessageHandler(moqFirmataEmptyBase.Broker));
                mockSerialProvider.Raise(m => m.DataReceived += null, new DataReceivedEventArgs(new byte[] { MessageConstants.ANALOG_MESSAGE, 0x00,0x00 }));
                // Wait for the parser thread to catch up
                Thread.Sleep(100);

                // See if the handler was successfully added to the appropriate ones
                Assert.AreEqual(0, moqFirmataEmptyBase.CurrentHandlers.Count);
            }
        }
Exemplo n.º 8
0
        public void No_Appropriate_Handler_After_Successful_Message()
        {
            var mockSerialProvider = new Mock <ISerialProvider>();

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                moqFirmataEmptyBase.Handlers.Add(new AnalogMessageHandler(moqFirmataEmptyBase.Broker));
                mockSerialProvider.Raise(m => m.DataReceived += null, new DataReceivedEventArgs(new byte[] { MessageConstants.ANALOG_MESSAGE, 0x00, 0x00 }));
                // Wait for the parser thread to catch up
                Thread.Sleep(100);

                // See if the handler was successfully added to the appropriate ones
                Assert.AreEqual(0, moqFirmataEmptyBase.CurrentHandlers.Count);
            }
        }
Exemplo n.º 9
0
        public void Should_Reset_Handlers_When_They_Are_Discarded_From_The_AppropriateHandlers_List()
        {
            var mockSerialProvider = new Mock<ISerialProvider>();
            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                var ana = new AnalogMappingMessageHandler(moqFirmataEmptyBase.Broker);
                var firm = new SysexFirmwareMessageHandler(moqFirmataEmptyBase.Broker);
                moqFirmataEmptyBase.Handlers.Add(ana);
                moqFirmataEmptyBase.Handlers.Add(firm);
                mockSerialProvider.Raise(m => m.DataReceived += null,
                    new DataReceivedEventArgs(new[] { MessageConstants.SYSEX_START, SysexCommands.ANALOG_MAPPING_RESPONSE }));

                // Wait for the parser thread to catch up
                Thread.Sleep(100);

                // Assert that the firmware handler has been reset and can handle a sysex start message
                // if it wasn't reset it would require a SysexCommands.QUERY_FIRMWARE byte
                Assert.IsTrue(firm.CanHandle(MessageConstants.SYSEX_START));
            }
        }
Exemplo n.º 10
0
        public void Should_Reset_Handlers_When_They_Are_Discarded_From_The_AppropriateHandlers_List()
        {
            var mockSerialProvider = new Mock <ISerialProvider>();

            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                var ana  = new AnalogMappingMessageHandler(moqFirmataEmptyBase.Broker);
                var firm = new SysexFirmwareMessageHandler(moqFirmataEmptyBase.Broker);
                moqFirmataEmptyBase.Handlers.Add(ana);
                moqFirmataEmptyBase.Handlers.Add(firm);
                mockSerialProvider.Raise(m => m.DataReceived += null,
                                         new DataReceivedEventArgs(new[] { MessageConstants.SYSEX_START, SysexCommands.ANALOG_MAPPING_RESPONSE }));

                // Wait for the parser thread to catch up
                Thread.Sleep(100);

                // Assert that the firmware handler has been reset and can handle a sysex start message
                // if it wasn't reset it would require a SysexCommands.QUERY_FIRMWARE byte
                Assert.IsTrue(firm.CanHandle(MessageConstants.SYSEX_START));
            }
        }
Exemplo n.º 11
0
 public void Should_Not_Have_Any_Handlers_By_Default()
 {
     var mockSerialProvider = new Mock<ISerialProvider>();
     using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
         Assert.IsEmpty(moqFirmataEmptyBase.Handlers);
 }
Exemplo n.º 12
0
 public void Should_Throw_Error_If_There_Is_Not_Suitable_MessageCreator()
 {
     var mockSerialProvider = new Mock<ISerialProvider>();
     using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
     {
         Assert.Throws<FirmataException>(() => moqFirmataEmptyBase.SendMessage(new AnalogMessage()));
     }
 }
Exemplo n.º 13
0
        public void Should_Send_Messages_Using_The_SerialProvider()
        {
            var mockSerialProvider = new Mock<ISerialProvider>();
            Func<IEnumerable<byte>, bool> f = b =>
                                                  {
                                                      var c = b.ToArray();
                                                      return c[0] == MessageConstants.ANALOG_MESSAGE && c[1] == 0x01 && c[2] == 0x02;
                                                  };
            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                // Add the creator for the message
                moqFirmataEmptyBase.Creators.Add(typeof(AnalogMessage),new AnalogOutputMessageCreator());
                // Send a new message
                moqFirmataEmptyBase.SendMessage(new AnalogMessage() { Pin = 0x01, Value = 0x02 });
            }

            // Verify that the base class called the serialprovider's send method
            mockSerialProvider.Verify(x => x.Send(
                It.Is<IEnumerable<byte>>(b =>
                    b.First() ==
                        (MessageConstants.ANALOG_MESSAGE | 0x01) &&
                        b.Skip(1).First() == 0x02 &&
                        b.Skip(2).First() == 0x00)));
        }
Exemplo n.º 14
0
        public void Should_Send_Bytes_To_Multiple_Handlers_Successfully()
        {
            var mockSerialProvider = new Mock<ISerialProvider>();
            using (var moqFirmataEmptyBase = new FirmataEmptyBaseStub(mockSerialProvider.Object))
            {
                moqFirmataEmptyBase.Handlers.Add(new AnalogMappingMessageHandler((moqFirmataEmptyBase.Broker)));
                moqFirmataEmptyBase.Handlers.Add(new SysexFirmwareMessageHandler((moqFirmataEmptyBase.Broker)));
                mockSerialProvider.Raise(m => m.DataReceived += null,
                    new DataReceivedEventArgs(new [] { MessageConstants.SYSEX_START }));

                // Wait for the parser thread to catch up
                Thread.Sleep(100);

                // See if the handler was successfully added to the appropriate ones
                Assert.AreEqual(2,moqFirmataEmptyBase.CurrentHandlers.Count);

                mockSerialProvider.Raise(m => m.DataReceived += null,
                    new DataReceivedEventArgs(new [] { SysexCommands.ANALOG_MAPPING_RESPONSE }));

                // Wait for the parser thread to catch up
                Thread.Sleep(50);

                Assert.AreSame(moqFirmataEmptyBase.Handlers[0],moqFirmataEmptyBase.CurrentHandlers[0]);
                Assert.AreEqual(1,moqFirmataEmptyBase.CurrentHandlers.Count);

            }
        }