Chromium Code Reviews| Index: components/test_runner/mock_web_midi_accessor.cc |
| diff --git a/components/test_runner/mock_web_midi_accessor.cc b/components/test_runner/mock_web_midi_accessor.cc |
| index e6f95de5ddeffd8cfbcbeaa14a832a0bd5537175..d3a98de34568a510f451b5dc0e1bff02afd376ea 100644 |
| --- a/components/test_runner/mock_web_midi_accessor.cc |
| +++ b/components/test_runner/mock_web_midi_accessor.cc |
| @@ -7,6 +7,7 @@ |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/macros.h" |
| +#include "base/strings/stringprintf.h" |
| #include "components/test_runner/test_interfaces.h" |
| #include "components/test_runner/test_runner.h" |
| #include "components/test_runner/web_test_delegate.h" |
| @@ -19,36 +20,102 @@ using midi::mojom::Result; |
| namespace test_runner { |
| +namespace { |
| + |
| +constexpr unsigned char kSysexHeader[] = {0xf0, 0x00, 0x02, 0x0d, 0x7f}; |
| +constexpr unsigned char kSysexFooter = 0xf7; |
| +constexpr size_t kSysexMinimumLength = |
| + arraysize(kSysexHeader) + sizeof(kSysexFooter) + 1; |
| + |
| +bool isSysexForTesting(const unsigned char* data, size_t length) { |
| + // It should have five bytes header, one byte footer, and at least one byte |
| + // payload. |
| + if (length < kSysexMinimumLength) |
| + return false; |
| + if (memcmp(data, kSysexHeader, arraysize(kSysexHeader))) |
| + return false; |
| + return data[length - 1] == kSysexFooter; |
| +} |
| + |
| +} // namespace |
| + |
| MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client, |
| TestInterfaces* interfaces) |
| - : client_(client), interfaces_(interfaces), weak_factory_(this) {} |
| + : client_(client), |
| + interfaces_(interfaces), |
| + next_input_port_index_(0), |
| + next_output_port_index_(0), |
| + weak_factory_(this) {} |
| MockWebMIDIAccessor::~MockWebMIDIAccessor() { |
| } |
| void MockWebMIDIAccessor::startSession() { |
| // Add a mock input and output port. |
| - client_->didAddInputPort("MockInputID", "MockInputManufacturer", |
| - "MockInputName", "MockInputVersion", |
| - PortState::CONNECTED); |
| - client_->didAddOutputPort("MockOutputID", "MockOutputManufacturer", |
| - "MockOutputName", "MockOutputVersion", |
| - PortState::CONNECTED); |
| + addInputPort(PortState::CONNECTED); |
| + addOutputPort(PortState::CONNECTED); |
| interfaces_->GetDelegate()->PostTask(base::Bind( |
| - &MockWebMIDIAccessor::ReportStartedSession, weak_factory_.GetWeakPtr(), |
| + &MockWebMIDIAccessor::reportStartedSession, weak_factory_.GetWeakPtr(), |
| interfaces_->GetTestRunner()->midiAccessorResult())); |
| } |
| -void MockWebMIDIAccessor::ReportStartedSession(Result result) { |
| - client_->didStartSession(result); |
| -} |
| - |
| void MockWebMIDIAccessor::sendMIDIData(unsigned port_index, |
| const unsigned char* data, |
| size_t length, |
| double timestamp) { |
| - // Emulate a loopback device for testing. |
| - client_->didReceiveMIDIData(port_index, data, length, timestamp); |
| + // Emulate a loopback device for testing. Make sure if an input port that has |
| + // the same index exists. |
| + if (port_index < next_input_port_index_) |
|
Takashi Toyoshima
2016/11/14 09:12:30
fixed
|
| + client_->didReceiveMIDIData(port_index, data, length, timestamp); |
| + |
| + // Handle special sysex messages for testing. |
| + // A special sequence is [0xf0, 0x00, 0x02, 0x0d, 0x7f, <function>, 0xf7]. |
| + // <function> should be one of following sequences. |
| + // - [0x00, 0x00]: Add an input port as connected. |
| + // - [0x00, 0x01]: Add an output port as connected. |
| + // - [0x00, 0x02]: Add an input port as opened. |
| + // - [0x00, 0x03]: Add an output port as opened. |
| + if (!isSysexForTesting(data, length)) |
| + return; |
| + size_t offset = arraysize(kSysexHeader); |
| + if (data[offset++] != 0) |
| + return; |
| + switch (data[offset]) { |
| + case 0: |
| + addInputPort(PortState::CONNECTED); |
| + break; |
| + case 1: |
| + addOutputPort(PortState::CONNECTED); |
| + break; |
| + case 2: |
| + addInputPort(PortState::OPENED); |
| + break; |
| + case 3: |
| + addOutputPort(PortState::OPENED); |
| + break; |
| + default: |
| + break; |
| + } |
| +} |
| + |
| +void MockWebMIDIAccessor::addInputPort(PortState state) { |
| + std::string id = |
| + base::StringPrintf("MockInputID-%d", next_input_port_index_++); |
| + client_->didAddInputPort(blink::WebString::fromUTF8(id), |
| + "MockInputManufacturer", "MockInputName", |
| + "MockInputVersion", state); |
| +} |
| + |
| +void MockWebMIDIAccessor::addOutputPort(PortState state) { |
| + std::string id = |
| + base::StringPrintf("MockOutputID-%d", next_output_port_index_++); |
| + client_->didAddOutputPort(blink::WebString::fromUTF8(id), |
| + "MockOutputManufacturer", "MockOutputName", |
| + "MockOutputVersion", state); |
| +} |
| + |
| +void MockWebMIDIAccessor::reportStartedSession(Result result) { |
| + client_->didStartSession(result); |
| } |
| } // namespace test_runner |