Beispiel #1
0
  public void testSendAndReceiveText() {
    connection.SendText("Hello");
    assertEquals(5, connection.BytesAvailableToReceive());
    assertEquals("Hello", connection.ReceiveText(5));

    // Test DelimiterByte with default value of 0
    connection.SendText("World");
    connection.Send1ByteNumber("0x0"); // send delimiter
    assertEquals(6, connection.BytesAvailableToReceive());
    assertEquals("World", connection.ReceiveText(-1));

    // Test DelimiterByte with value of 10 (line feed)
    connection.DelimiterByte(10);
    connection.SendText("Hello World");
    connection.Send1ByteNumber("13"); // send carriage return
    connection.Send1ByteNumber("10"); // send line feed
    assertEquals(13, connection.BytesAvailableToReceive());
    assertEquals("Hello World\r", connection.ReceiveText(-1));

    assertEquals(0, recordedErrorNumber);

    byte[] bytes = outputStream.toByteArray();
    assertEquals(24, bytes.length);
    int i = 0;
    assertEquals((byte) 72, bytes[i++]); // H
    assertEquals((byte) 101, bytes[i++]); // e
    assertEquals((byte) 108, bytes[i++]); // l
    assertEquals((byte) 108, bytes[i++]); // l
    assertEquals((byte) 111, bytes[i++]); // o
    assertEquals((byte) 87, bytes[i++]); // W
    assertEquals((byte) 111, bytes[i++]); // o
    assertEquals((byte) 114, bytes[i++]); // r
    assertEquals((byte) 108, bytes[i++]); // l
    assertEquals((byte) 100, bytes[i++]); // d
    assertEquals((byte) 0, bytes[i++]); // delimiter 0
    assertEquals((byte) 72, bytes[i++]); // H
    assertEquals((byte) 101, bytes[i++]); // e
    assertEquals((byte) 108, bytes[i++]); // l
    assertEquals((byte) 108, bytes[i++]); // l
    assertEquals((byte) 111, bytes[i++]); // o
    assertEquals((byte) 32, bytes[i++]); // space
    assertEquals((byte) 87, bytes[i++]); // W
    assertEquals((byte) 111, bytes[i++]); // o
    assertEquals((byte) 114, bytes[i++]); // r
    assertEquals((byte) 108, bytes[i++]); // l
    assertEquals((byte) 100, bytes[i++]); // d
    assertEquals((byte) 13, bytes[i++]); // carriage return
    assertEquals((byte) 10, bytes[i++]); // line feed
  }
Beispiel #2
0
  public void testSendandReceive1ByteNumber() {
    connection.Send1ByteNumber("0");
    assertEquals(0, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("1");
    assertEquals(1, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("-1");
    assertEquals(-1, connection.ReceiveSigned1ByteNumber());
    connection.Send1ByteNumber("127");
    assertEquals(127, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("-128");
    assertEquals(-128, connection.ReceiveSigned1ByteNumber());
    connection.Send1ByteNumber("255");
    assertEquals(255, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("0x0");
    assertEquals(0x0, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("0x1");
    assertEquals(0x1, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("0xFF");
    assertEquals(0xFF, connection.ReceiveUnsigned1ByteNumber());
    connection.Send1ByteNumber("0xab");
    assertEquals(0xab, connection.ReceiveUnsigned1ByteNumber());

    assertEquals(0, recordedErrorNumber);

    connection.Send1ByteNumber("abc");
    assertEquals(ErrorMessages.ERROR_BLUETOOTH_COULD_NOT_DECODE, recordedErrorNumber);

    recordedErrorNumber = 0;
    connection.Send1ByteNumber("256");
    assertEquals(ErrorMessages.ERROR_BLUETOOTH_COULD_NOT_FIT_NUMBER_IN_BYTE, recordedErrorNumber);

    byte[] bytes = outputStream.toByteArray();
    assertEquals(10, bytes.length);
    int i = 0;
    assertEquals((byte) 0x00, bytes[i++]); // 0
    assertEquals((byte) 0x01, bytes[i++]); // 1
    assertEquals((byte) 0xFF, bytes[i++]); // -1
    assertEquals((byte) 0x7F, bytes[i++]); // 127
    assertEquals((byte) 0x80, bytes[i++]); // -128
    assertEquals((byte) 0xFF, bytes[i++]); // 255
    assertEquals((byte) 0x00, bytes[i++]); // 0x00
    assertEquals((byte) 0x01, bytes[i++]); // 0x01
    assertEquals((byte) 0xFF, bytes[i++]); // 0xFF
    assertEquals((byte) 0xAB, bytes[i++]); // 0xab
  }