<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="resources/common.js"></script>
    <script src="resources/manual.js"></script>
  </head>
  <body>
    <p>
      These tests require a connected serial device configured to act as a
      "loopback" device, with the transmit and receive pins wired together.
    </p>
    <script>
      manual_serial_test(async (t, port) => {
        await port.open({baudRate: 115200, bufferSize: 1024});

        let reader = port.readable.getReader();
        let readPromise = (async () => {
          // A single zero byte will be read before the break is detected.
          const {value, done} = await reader.read();
          compareArrays(value, new Uint8Array([0]));
          assert_false(done);

          try {
            const {value, done} = await reader.read();
            assert_unreached(`Expected break, got ${value.byteLength} bytes`);
          } catch (e) {
            assert_equals(e.constructor, DOMException);
            assert_equals(e.name, 'BreakError');
          }
        })();

        await port.setSignals({break: true});
        await readPromise;
        await port.setSignals({break: false});

        const writer = port.writable.getWriter();
        // |data| is small enough to be completely transmitted.
        let data = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
        await writer.write(data);
        writer.releaseLock();

        reader = port.readable.getReader();
        const buffer = await readWithLength(reader, data.byteLength);;
        compareArrays(buffer, data);
        reader.releaseLock();

        await port.close();
      }, 'Break is detected.');
    </script>
  </body>
</html>
