| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/test_runner/mock_web_midi_accessor.h" | 5 #include "components/test_runner/mock_web_midi_accessor.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/stringprintf.h" |
| 10 #include "components/test_runner/test_interfaces.h" | 11 #include "components/test_runner/test_interfaces.h" |
| 11 #include "components/test_runner/test_runner.h" | 12 #include "components/test_runner/test_runner.h" |
| 12 #include "components/test_runner/web_test_delegate.h" | 13 #include "components/test_runner/web_test_delegate.h" |
| 13 #include "components/test_runner/web_test_runner.h" | 14 #include "components/test_runner/web_test_runner.h" |
| 14 #include "third_party/WebKit/public/platform/WebString.h" | 15 #include "third_party/WebKit/public/platform/WebString.h" |
| 15 #include "third_party/WebKit/public/platform/modules/webmidi/WebMIDIAccessorClie
nt.h" | 16 #include "third_party/WebKit/public/platform/modules/webmidi/WebMIDIAccessorClie
nt.h" |
| 16 | 17 |
| 17 using midi::mojom::PortState; | 18 using midi::mojom::PortState; |
| 18 using midi::mojom::Result; | 19 using midi::mojom::Result; |
| 19 | 20 |
| 20 namespace test_runner { | 21 namespace test_runner { |
| 21 | 22 |
| 23 namespace { |
| 24 |
| 25 constexpr unsigned char kSysexHeader[] = {0xf0, 0x00, 0x02, 0x0d, 0x7f}; |
| 26 constexpr unsigned char kSysexFooter = 0xf7; |
| 27 constexpr size_t kSysexMinimumLength = |
| 28 arraysize(kSysexHeader) + sizeof(kSysexFooter) + 1; |
| 29 |
| 30 bool isSysexForTesting(const unsigned char* data, size_t length) { |
| 31 // It should have five bytes header, one byte footer, and at least one byte |
| 32 // payload. |
| 33 if (length < kSysexMinimumLength) |
| 34 return false; |
| 35 if (memcmp(data, kSysexHeader, arraysize(kSysexHeader))) |
| 36 return false; |
| 37 return data[length - 1] == kSysexFooter; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 22 MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client, | 42 MockWebMIDIAccessor::MockWebMIDIAccessor(blink::WebMIDIAccessorClient* client, |
| 23 TestInterfaces* interfaces) | 43 TestInterfaces* interfaces) |
| 24 : client_(client), interfaces_(interfaces), weak_factory_(this) {} | 44 : client_(client), |
| 45 interfaces_(interfaces), |
| 46 next_input_port_index_(0), |
| 47 next_output_port_index_(0), |
| 48 weak_factory_(this) {} |
| 25 | 49 |
| 26 MockWebMIDIAccessor::~MockWebMIDIAccessor() { | 50 MockWebMIDIAccessor::~MockWebMIDIAccessor() { |
| 27 } | 51 } |
| 28 | 52 |
| 29 void MockWebMIDIAccessor::startSession() { | 53 void MockWebMIDIAccessor::startSession() { |
| 30 // Add a mock input and output port. | 54 // Add a mock input and output port. |
| 31 client_->didAddInputPort("MockInputID", "MockInputManufacturer", | 55 addInputPort(PortState::CONNECTED); |
| 32 "MockInputName", "MockInputVersion", | 56 addOutputPort(PortState::CONNECTED); |
| 33 PortState::CONNECTED); | |
| 34 client_->didAddOutputPort("MockOutputID", "MockOutputManufacturer", | |
| 35 "MockOutputName", "MockOutputVersion", | |
| 36 PortState::CONNECTED); | |
| 37 interfaces_->GetDelegate()->PostTask(base::Bind( | 57 interfaces_->GetDelegate()->PostTask(base::Bind( |
| 38 &MockWebMIDIAccessor::ReportStartedSession, weak_factory_.GetWeakPtr(), | 58 &MockWebMIDIAccessor::reportStartedSession, weak_factory_.GetWeakPtr(), |
| 39 interfaces_->GetTestRunner()->midiAccessorResult())); | 59 interfaces_->GetTestRunner()->midiAccessorResult())); |
| 40 } | 60 } |
| 41 | 61 |
| 42 void MockWebMIDIAccessor::ReportStartedSession(Result result) { | |
| 43 client_->didStartSession(result); | |
| 44 } | |
| 45 | |
| 46 void MockWebMIDIAccessor::sendMIDIData(unsigned port_index, | 62 void MockWebMIDIAccessor::sendMIDIData(unsigned port_index, |
| 47 const unsigned char* data, | 63 const unsigned char* data, |
| 48 size_t length, | 64 size_t length, |
| 49 double timestamp) { | 65 double timestamp) { |
| 50 // Emulate a loopback device for testing. | 66 // Emulate a loopback device for testing. |
| 51 client_->didReceiveMIDIData(port_index, data, length, timestamp); | 67 if (port_index < next_output_port_index_) |
| 68 client_->didReceiveMIDIData(port_index, data, length, timestamp); |
| 69 |
| 70 // Handle special sysex messages for testing. |
| 71 // A special sequence is [0xf0, 0x00, 0x02, 0x0d, 0x7f, <function>, 0xf7]. |
| 72 // <function> should be one of following sequences. |
| 73 // - [0x00, 0x00]: Add an input port as connected. |
| 74 // - [0x00, 0x01]: Add an output port as connected. |
| 75 // - [0x00, 0x02]: Add an input port as opened. |
| 76 // - [0x00, 0x03]: Add an output port as opened. |
| 77 if (!isSysexForTesting(data, length)) |
| 78 return; |
| 79 size_t offset = arraysize(kSysexHeader); |
| 80 if (data[offset++] != 0) |
| 81 return; |
| 82 switch (data[offset]) { |
| 83 case 0: |
| 84 addInputPort(PortState::CONNECTED); |
| 85 break; |
| 86 case 1: |
| 87 addOutputPort(PortState::CONNECTED); |
| 88 break; |
| 89 case 2: |
| 90 addInputPort(PortState::OPENED); |
| 91 break; |
| 92 case 3: |
| 93 addOutputPort(PortState::OPENED); |
| 94 break; |
| 95 default: |
| 96 break; |
| 97 } |
| 98 } |
| 99 |
| 100 void MockWebMIDIAccessor::addInputPort(PortState state) { |
| 101 std::string id = |
| 102 base::StringPrintf("MockInputID-%d", next_input_port_index_++); |
| 103 client_->didAddInputPort(blink::WebString::fromUTF8(id), |
| 104 "MockInputManufacturer", "MockInputName", |
| 105 "MockInputVersion", state); |
| 106 } |
| 107 |
| 108 void MockWebMIDIAccessor::addOutputPort(PortState state) { |
| 109 std::string id = |
| 110 base::StringPrintf("MockOutputID-%d", next_output_port_index_++); |
| 111 client_->didAddOutputPort(blink::WebString::fromUTF8(id), |
| 112 "MockOutputManufacturer", "MockOutputName", |
| 113 "MockOutputVersion", state); |
| 114 } |
| 115 |
| 116 void MockWebMIDIAccessor::reportStartedSession(Result result) { |
| 117 client_->didStartSession(result); |
| 52 } | 118 } |
| 53 | 119 |
| 54 } // namespace test_runner | 120 } // namespace test_runner |
| OLD | NEW |